Skip to content

Commit

Permalink
chore: supersim changes from demo (#134)
Browse files Browse the repository at this point in the history
* supersim.demo.changes

* undo comment
  • Loading branch information
hamdiallam authored Sep 9, 2024
1 parent 25dea51 commit 19841ff
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 43 deletions.
6 changes: 0 additions & 6 deletions anvil/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,6 @@ func (a *Anvil) Config() *config.ChainConfig {
return a.cfg
}

func (a *Anvil) String() string {
var b strings.Builder
fmt.Fprintf(&b, "Name: %s Chain ID: %d RPC: %s LogPath: %s", a.Name(), a.ChainID(), a.Endpoint(), a.LogPath())
return b.String()
}

func (a *Anvil) EthClient() *ethclient.Client {
return a.ethClient
}
Expand Down
3 changes: 1 addition & 2 deletions config/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ type Chain interface {
// Properties
Endpoint() string
LogPath() string
String() string
Config() *ChainConfig
EthClient() *ethclient.Client

Expand All @@ -87,7 +86,7 @@ type Chain interface {
func GetDefaultNetworkConfig(startingTimestamp uint64) NetworkConfig {
return NetworkConfig{
L1Config: ChainConfig{
Name: "L1",
Name: "Local",
ChainID: genesis.GeneratedGenesisDeployment.L1.ChainID,
SecretsConfig: DefaultSecretsConfig,
GenesisJSON: genesis.GeneratedGenesisDeployment.L1.GenesisJSON,
Expand Down
5 changes: 4 additions & 1 deletion opsimulator/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ type jsonRpcMessage struct {
}

func readJsonMessages(body io.Reader) ([]*jsonRpcMessage, bool, error) {
dec := json.NewDecoder(body)
dec.UseNumber()

var rawmsg json.RawMessage
if err := json.NewDecoder(body).Decode(&rawmsg); err != nil {
if err := dec.Decode(&rawmsg); err != nil {
return nil, false, err
}

Expand Down
53 changes: 29 additions & 24 deletions opsimulator/opsimulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,19 @@ func (opSim *OpSimulator) startBackgroundTasks() {
return fmt.Errorf("failed to subscribe to deposit tx: %w", err)
}

chainId := opSim.Config().ChainID

for {
select {
case dep := <-depositTxCh:
depTx := types.NewTx(dep)
opSim.log.Debug("received deposit tx", "hash", depTx.Hash().String())
opSim.log.Debug("observed deposit event on L1", "hash", depTx.Hash().String())

clnt := opSim.Chain.EthClient()
if err := clnt.SendTransaction(opSim.bgTasksCtx, depTx); err != nil {
opSim.log.Error("failed to submit deposit tx: %w", err)
opSim.log.Error("failed to submit deposit tx to chain: %w", "chain.id", chainId, "err", err)
}

opSim.log.Debug("submitted deposit tx", "hash", depTx.Hash().String())
opSim.log.Debug("submitted deposit tx to chain", "chain.id", chainId, "hash", depTx.Hash().String())

case <-opSim.bgTasksCtx.Done():
sub.Unsubscribe()
Expand Down Expand Up @@ -290,18 +291,30 @@ func (opSim *OpSimulator) handler(ctx context.Context) http.HandlerFunc {
}
}

var encdata []byte
if isBatchRequest {
if err := json.NewEncoder(w).Encode(batchRes); err != nil {
opSim.log.Error("failed to write batch response", "err", err)
http.Error(w, "Failed to write batch response", http.StatusInternalServerError)
return
}
encdata, err = json.Marshal(batchRes)
} else {
if err := json.NewEncoder(w).Encode(batchRes[0]); err != nil {
opSim.log.Error("failed to write response", "err", err)
http.Error(w, "Failed to write batch response", http.StatusInternalServerError)
return
}
encdata, err = json.Marshal(batchRes[0])
}

// encoding error
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// write to response
w.Header().Set("Content-Length", strconv.Itoa(len(encdata)))
w.Header().Set("Transfer-Encoding", "identity")
if _, err := w.Write(encdata); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// trigger a flush if applicable
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
}
}
Expand Down Expand Up @@ -417,8 +430,8 @@ func (opSim *OpSimulator) checkInteropInvariants(ctx context.Context, tx *types.

// Overridden such that the port field can appropiately be set
func (opSim *OpSimulator) Config() *config.ChainConfig {
// we dereference the config so that a copy is made.
// - NOTE: This is only okay since the field were modifying are non-pointer fields
// we dereference the config so that a copy is made. This is only okay since
// the field were modifying is a non-pointer fields
cfg := *opSim.Chain.Config()
cfg.Port = opSim.port
return &cfg
Expand All @@ -429,14 +442,6 @@ func (opSim *OpSimulator) Endpoint() string {
return fmt.Sprintf("http://%s:%d", host, opSim.port)
}

// Overridden such that the right endpoint is used
func (opSim *OpSimulator) String() string {
var b strings.Builder
cfg := opSim.Config()
fmt.Fprintf(&b, "Name: %s Chain ID: %d RPC: %s LogPath: %s", cfg.Name, cfg.ChainID, opSim.Endpoint(), opSim.LogPath())
return b.String()
}

func corsHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
Expand Down
17 changes: 10 additions & 7 deletions orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,11 @@ func (o *Orchestrator) Endpoint(chainId uint64) string {

func (o *Orchestrator) ConfigAsString() string {
var b strings.Builder

if o.l1Chain != nil {
fmt.Fprintf(&b, "L1:\n")
fmt.Fprintf(&b, " %s\n", o.l1Chain.String())
}
l1Cfg := o.l1Chain.Config()
fmt.Fprintf(&b, "L1: Name: %s ChainID: %d RPC: %s LogPath: %s\n", l1Cfg.Name, l1Cfg.ChainID, o.l1Chain.Endpoint(), o.l1Chain.LogPath())

if len(o.l2OpSims) > 0 {
fmt.Fprintf(&b, "L2:\n")
fmt.Fprintf(&b, "\nL2s: Predeploy Contracts Spec ( %s )\n", "https://specs.optimism.io/protocol/predeploys.html")

opSims := make([]*opsimulator.OpSimulator, 0, len(o.l2OpSims))
for _, chain := range o.l2OpSims {
Expand All @@ -219,7 +216,13 @@ func (o *Orchestrator) ConfigAsString() string {
// sort by port number (retain ordering of chain flags)
sort.Slice(opSims, func(i, j int) bool { return opSims[i].Config().Port < opSims[j].Config().Port })
for _, opSim := range opSims {
fmt.Fprintf(&b, " %s\n", opSim.String())
cfg := opSim.Config()
fmt.Fprintf(&b, "\n")
fmt.Fprintf(&b, " * Name: %s ChainID: %d RPC: %s LogPath: %s\n", cfg.Name, cfg.ChainID, opSim.Endpoint(), opSim.LogPath())
fmt.Fprintf(&b, " L1 Contracts:\n")
fmt.Fprintf(&b, " - OptimismPortal: %s\n", cfg.L2Config.L1Addresses.OptimismPortalProxy)
fmt.Fprintf(&b, " - L1CrossDomainMessenger: %s\n", cfg.L2Config.L1Addresses.L1CrossDomainMessengerProxy)
fmt.Fprintf(&b, " - L1StandardBridge: %s\n", cfg.L2Config.L1Addresses.L1StandardBridgeProxy)
}
}

Expand Down
7 changes: 4 additions & 3 deletions supersim.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ func (s *Supersim) Stopped() bool {

func (s *Supersim) ConfigAsString() string {
var b strings.Builder
fmt.Fprint(&b, config.DefaultSecretsConfigAsString())
fmt.Fprintln(&b, config.DefaultSecretsConfigAsString())

fmt.Fprintf(&b, "\nOrchestrator Config:\n")
fmt.Fprint(&b, s.Orchestrator.ConfigAsString())
fmt.Fprintln(&b, "Orchestrator Config")
fmt.Fprintln(&b, "-----------------------")
fmt.Fprintln(&b, s.Orchestrator.ConfigAsString())

return b.String()
}

0 comments on commit 19841ff

Please sign in to comment.