Skip to content

Commit

Permalink
fix bug (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdiallam authored Jul 31, 2024
1 parent ce0c9af commit 87cd75f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 19 deletions.
7 changes: 2 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ func main() {

func SupersimMain(ctx *cli.Context, closeApp context.CancelCauseFunc) (cliapp.Lifecycle, error) {
log := oplog.NewLogger(oplog.AppOut(ctx), oplog.ReadCLIConfig(ctx))
ok, minAnvilErr := isMinAnvilInstalled(log)

ok, minAnvilErr := isMinAnvilInstalled()
if !ok {
return nil, fmt.Errorf("anvil version timestamp of %s or higher is required, please use foundryup to update to the latest version.", minAnvilTimestamp)
}

if minAnvilErr != nil {
return nil, fmt.Errorf("error determining installed anvil version: %w.", minAnvilErr)
}
Expand All @@ -90,7 +88,7 @@ func SupersimMain(ctx *cli.Context, closeApp context.CancelCauseFunc) (cliapp.Li
return s, nil
}

func isMinAnvilInstalled(log log.Logger) (bool, error) {
func isMinAnvilInstalled() (bool, error) {
cmd := exec.Command("anvil", "--version")
var out bytes.Buffer
cmd.Stdout = &out
Expand All @@ -104,7 +102,6 @@ func isMinAnvilInstalled(log log.Logger) (bool, error) {
// anvil does not use semver until 1.0.0 is released so using timestamp to determine version.
timestampRegex := regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z`)
timestamp := timestampRegex.FindString(output)

if timestamp == "" {
return false, fmt.Errorf("failed to parse anvil timestamp from anvil --version")
}
Expand Down
11 changes: 6 additions & 5 deletions config/superchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
registry "github.com/ethereum-optimism/superchain-registry/superchain"
)

var OpChainToId map[string]uint64 = map[string]uint64{}

func init() {
for id, chainCfg := range registry.OPChains {
OpChainToId[chainCfg.Chain] = id
func OPChainByName(superchain *registry.Superchain, name string) *registry.ChainConfig {
for _, id := range superchain.ChainIDs {
if registry.OPChains[id].Chain == name {
return registry.OPChains[id]
}
}
return nil
}

func superchainNetworks() []string {
Expand Down
2 changes: 1 addition & 1 deletion genesis/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func newL2GenesisDeployment(l2ChainID uint64, l1DeploymentAddressesJSON []byte,

return &L2GenesisDeployment{
ChainID: l2ChainID,
L1DeploymentAddresses: &l1DeploymentAddresses,
GenesisJSON: l2GenesisJSON,
L1DeploymentAddresses: &l1DeploymentAddresses,
}
}
1 change: 0 additions & 1 deletion opsimulator/opsimulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func (opSim *OpSimulator) Stop(ctx context.Context) error {
}

opSim.bgTasksCancel()

return opSim.httpServer.Stop(ctx)
}

Expand Down
6 changes: 5 additions & 1 deletion orchestrator/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func NetworkConfigFromForkCLIConfig(forkConfig *config.ForkCLIConfig) (config.Ne

// L2s
for _, chain := range forkConfig.Chains {
chainCfg := registry.OPChains[config.OpChainToId[chain]]
chainCfg := config.OPChainByName(superchain, chain)
if chainCfg == nil {
return networkConfig, fmt.Errorf("unrecoginized chain %s. superchain %s", chain, superchain.Superchain)
}

l2ForkHeight, err := latestL2HeightFromL1Header(chainCfg, l1Header)
if err != nil {
return networkConfig, fmt.Errorf("failed to find right l2 height: %w", err)
Expand Down
23 changes: 18 additions & 5 deletions orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
_ "embed"
"fmt"
"sort"
"strings"
"sync"

Expand Down Expand Up @@ -51,6 +52,7 @@ func (o *Orchestrator) Start(ctx context.Context) error {
if err := o.l1Anvil.Start(ctx); err != nil {
return fmt.Errorf("anvil instance %s failed to start: %w", o.l1Anvil.Name(), err)
}

for _, anvil := range o.l2Anvils {
if err := anvil.Start(ctx); err != nil {
return fmt.Errorf("anvil instance %s failed to start: %w", anvil.Name(), err)
Expand All @@ -72,10 +74,6 @@ func (o *Orchestrator) Start(ctx context.Context) error {

func (o *Orchestrator) Stop(ctx context.Context) error {
o.log.Info("stopping orchestrator")
if err := o.l1Anvil.Stop(); err != nil {
return fmt.Errorf("anvil instance %s failed to stop: %w", o.l1Anvil.Name(), err)
}

for _, opSim := range o.l2OpSims {
if err := opSim.Stop(ctx); err != nil {
return fmt.Errorf("op simulator chain.id=%d failed to stop: %w", opSim.ChainID(), err)
Expand All @@ -89,11 +87,18 @@ func (o *Orchestrator) Stop(ctx context.Context) error {
o.log.Debug("stopped anvil", "chain.id", anvil.ChainID())
}

if err := o.l1Anvil.Stop(); err != nil {
return fmt.Errorf("anvil instance %s failed to stop: %w", o.l1Anvil.Name(), err)
}

o.log.Debug("stopped orchestrator")
return nil
}

func (o *Orchestrator) Stopped() bool {
if stopped := o.l1Anvil.Stopped(); stopped {
return stopped
}
for _, anvil := range o.l2Anvils {
if stopped := anvil.Stopped(); !stopped {
return stopped
Expand Down Expand Up @@ -161,7 +166,15 @@ func (o *Orchestrator) ConfigAsString() string {

if len(o.l2OpSims) > 0 {
fmt.Fprintf(&b, "L2:\n")
for _, opSim := range o.l2OpSims {

opSims := make([]*opsimulator.OpSimulator, 0, len(o.l2OpSims))
for _, chain := range o.l2OpSims {
opSims = append(opSims, chain)
}

// 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())
}
}
Expand Down
2 changes: 1 addition & 1 deletion orchestrator/orchestrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ func TestStartup(t *testing.T) {

require.Equal(t, chains[1].ChainID(), uint64(30))
require.NotNil(t, chains[1].Config().L2Config)
require.Equal(t, chains[0].Config().L2Config.L1ChainID, uint64(1))
require.Equal(t, chains[1].Config().L2Config.L1ChainID, uint64(1))
}

0 comments on commit 87cd75f

Please sign in to comment.