-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
197 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/log" | ||
) | ||
|
||
func Logger(w io.Writer, lvl log.Lvl) log.Logger { | ||
h := log.StreamHandler(w, log.LogfmtFormat()) | ||
h = log.SyncHandler(h) | ||
h = log.LvlFilterHandler(lvl, h) | ||
l := log.New() | ||
l.SetHandler(h) | ||
return l | ||
} | ||
|
||
// LoggingWriter is a simple util to wrap a logger, | ||
// and expose an io Writer interface, | ||
// for the program running within the VM to write to. | ||
type LoggingWriter struct { | ||
Name string | ||
Log log.Logger | ||
} | ||
|
||
func logAsText(b string) bool { | ||
for _, c := range b { | ||
if (c < 0x20 || c >= 0x7F) && (c != '\n' && c != '\t') { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func (lw *LoggingWriter) Write(b []byte) (int, error) { | ||
t := string(b) | ||
if logAsText(t) { | ||
lw.Log.Info("", "text", t) | ||
} else { | ||
lw.Log.Info("", "data", hexutil.Bytes(b)) | ||
} | ||
return len(b), nil | ||
} | ||
|
||
// HexU32 to lazy-format integer attributes for logging | ||
type HexU32 uint32 | ||
|
||
func (v HexU32) String() string { | ||
return fmt.Sprintf("%08x", uint32(v)) | ||
} | ||
|
||
func (v HexU32) MarshalText() ([]byte, error) { | ||
return []byte(v.String()), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"time" | ||
|
||
preimage "github.com/ethereum-optimism/optimism/op-preimage" | ||
) | ||
|
||
type rawHint string | ||
|
||
func (rh rawHint) Hint() string { | ||
return string(rh) | ||
} | ||
|
||
type rawKey [32]byte | ||
|
||
func (rk rawKey) PreimageKey() [32]byte { | ||
return rk | ||
} | ||
|
||
type ProcessPreimageOracle struct { | ||
pCl *preimage.OracleClient | ||
hCl *preimage.HintWriter | ||
cmd *exec.Cmd | ||
waitErr chan error | ||
cancelIO context.CancelCauseFunc | ||
} | ||
|
||
const clientPollTimeout = time.Second * 15 | ||
|
||
func NewProcessPreimageOracle(name string, args []string) (*ProcessPreimageOracle, error) { | ||
if name == "" { | ||
return &ProcessPreimageOracle{}, nil | ||
} | ||
|
||
pClientRW, pOracleRW, err := preimage.CreateBidirectionalChannel() | ||
if err != nil { | ||
return nil, err | ||
} | ||
hClientRW, hOracleRW, err := preimage.CreateBidirectionalChannel() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cmd := exec.Command(name, args...) // nosemgrep | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
cmd.ExtraFiles = []*os.File{ | ||
hOracleRW.Reader(), | ||
hOracleRW.Writer(), | ||
pOracleRW.Reader(), | ||
pOracleRW.Writer(), | ||
} | ||
|
||
// Note that the client file descriptors are not closed when the pre-image server exits. | ||
// So we use the FilePoller to ensure that we don't get stuck in a blocking read/write. | ||
ctx, cancelIO := context.WithCancelCause(context.Background()) | ||
preimageClientIO := preimage.NewFilePoller(ctx, pClientRW, clientPollTimeout) | ||
hostClientIO := preimage.NewFilePoller(ctx, hClientRW, clientPollTimeout) | ||
out := &ProcessPreimageOracle{ | ||
pCl: preimage.NewOracleClient(preimageClientIO), | ||
hCl: preimage.NewHintWriter(hostClientIO), | ||
cmd: cmd, | ||
waitErr: make(chan error), | ||
cancelIO: cancelIO, | ||
} | ||
return out, nil | ||
} | ||
|
||
func (p *ProcessPreimageOracle) Hint(v []byte) { | ||
if p.hCl == nil { // no hint processor | ||
return | ||
} | ||
p.hCl.Hint(rawHint(v)) | ||
} | ||
|
||
func (p *ProcessPreimageOracle) GetPreimage(k [32]byte) []byte { | ||
if p.pCl == nil { | ||
panic("no pre-image retriever available") | ||
} | ||
return p.pCl.Get(rawKey(k)) | ||
} | ||
|
||
func (p *ProcessPreimageOracle) GetCmd() *exec.Cmd { | ||
return p.cmd | ||
} | ||
|
||
func (p *ProcessPreimageOracle) Start() error { | ||
if p.cmd == nil { | ||
return nil | ||
} | ||
err := p.cmd.Start() | ||
go p.wait() | ||
return err | ||
} | ||
|
||
func (p *ProcessPreimageOracle) Close() error { | ||
if p.cmd == nil { | ||
return nil | ||
} | ||
// Give the pre-image server time to exit cleanly before killing it. | ||
time.Sleep(time.Second * 1) | ||
_ = p.cmd.Process.Signal(os.Interrupt) | ||
return <-p.waitErr | ||
} | ||
|
||
func (p *ProcessPreimageOracle) wait() { | ||
err := p.cmd.Wait() | ||
var waitErr error | ||
if err, ok := err.(*exec.ExitError); !ok || !err.Success() { | ||
waitErr = err | ||
} | ||
p.cancelIO(fmt.Errorf("%w: pre-image server has exited", waitErr)) | ||
p.waitErr <- waitErr | ||
close(p.waitErr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters