diff --git a/anvil/anvil.go b/anvil/anvil.go index 0eba53b5..aec8b47f 100644 --- a/anvil/anvil.go +++ b/anvil/anvil.go @@ -57,9 +57,6 @@ func (a *Anvil) Start(ctx context.Context) error { return errors.New("anvil already started") } - anvilLog := a.log.New("chain.id", a.cfg.ChainId) - anvilLog.Info("starting anvil") - tempFile, err := os.CreateTemp("", "genesis-*.json") if err != nil { return fmt.Errorf("error creating temporary genesis file: %w", err) @@ -79,6 +76,8 @@ func (a *Anvil) Start(ctx context.Context) error { "--init", tempFile.Name(), } + anvilLog := a.log.New("role", "anvil", "chain.id", a.cfg.ChainId) + anvilLog.Info("starting anvil", "args", args) a.cmd = exec.CommandContext(a.resourceCtx, "anvil", args...) go func() { <-ctx.Done() diff --git a/supersim.go b/supersim.go index f9c1e03d..bed26b2a 100644 --- a/supersim.go +++ b/supersim.go @@ -9,19 +9,19 @@ import ( "context" "github.com/ethereum-optimism/supersim/anvil" - op_simulator "github.com/ethereum-optimism/supersim/op-simulator" + opsim "github.com/ethereum-optimism/supersim/op-simulator" "github.com/ethereum/go-ethereum/log" ) type Config struct { - l1Chain Chain - l2Chains []Chain + l1Chain ChainConfig + l2Chains []ChainConfig } -type Chain struct { +type ChainConfig struct { anvilConfig anvil.Config - opSimConfig op_simulator.Config + opSimConfig opsim.Config } //go:embed genesis/genesis-l1.json @@ -31,18 +31,18 @@ var genesisL1JSON []byte var genesisL2JSON []byte var DefaultConfig = Config{ - l1Chain: Chain{ + l1Chain: ChainConfig{ anvilConfig: anvil.Config{ChainId: 1, Port: 8545, Genesis: genesisL1JSON}, - opSimConfig: op_simulator.Config{Port: 8546}, + opSimConfig: opsim.Config{Port: 8546}, }, - l2Chains: []Chain{ + l2Chains: []ChainConfig{ { anvilConfig: anvil.Config{ChainId: 10, Port: 9545, Genesis: genesisL2JSON}, - opSimConfig: op_simulator.Config{Port: 9546}, + opSimConfig: opsim.Config{Port: 9546}, }, { anvilConfig: anvil.Config{ChainId: 30, Port: 9555, Genesis: genesisL2JSON}, - opSimConfig: op_simulator.Config{Port: 9556}, + opSimConfig: opsim.Config{Port: 9556}, }, }, } @@ -50,25 +50,27 @@ var DefaultConfig = Config{ type Supersim struct { log log.Logger - l1Anvil *anvil.Anvil + l1Anvil *anvil.Anvil + l1OpSim *opsim.OpSimulator + l2Anvils map[uint64]*anvil.Anvil - l1OpSim *op_simulator.OpSimulator - l2OpSims map[uint64]*op_simulator.OpSimulator + l2OpSims map[uint64]*opsim.OpSimulator } func NewSupersim(log log.Logger, config *Config) *Supersim { l1Anvil := anvil.New(log, &config.l1Chain.anvilConfig) - l1OpSim := op_simulator.New(log, &config.l1Chain.opSimConfig, l1Anvil) + l1OpSim := opsim.New(log, &config.l1Chain.opSimConfig, l1Anvil) l2Anvils := make(map[uint64]*anvil.Anvil) - l2OpSims := make(map[uint64]*op_simulator.OpSimulator) - for _, l2ChainConfig := range config.l2Chains { + l2OpSims := make(map[uint64]*opsim.OpSimulator) + for i := range config.l2Chains { + l2ChainConfig := config.l2Chains[i] l2Anvil := anvil.New(log, &l2ChainConfig.anvilConfig) l2Anvils[l2ChainConfig.anvilConfig.ChainId] = l2Anvil - l2OpSims[l2ChainConfig.anvilConfig.ChainId] = op_simulator.New(log, &l2ChainConfig.opSimConfig, l2Anvil) + l2OpSims[l2ChainConfig.anvilConfig.ChainId] = opsim.New(log, &l2ChainConfig.opSimConfig, l2Anvil) } - return &Supersim{log, l1Anvil, l2Anvils, l1OpSim, l2OpSims} + return &Supersim{log, l1Anvil, l1OpSim, l2Anvils, l2OpSims} } func (s *Supersim) Start(ctx context.Context) error { @@ -77,17 +79,15 @@ func (s *Supersim) Start(ctx context.Context) error { if err := s.l1Anvil.Start(ctx); err != nil { return fmt.Errorf("l1 anvil failed to start: %w", err) } + if err := s.l1OpSim.Start(ctx); err != nil { + return fmt.Errorf("l1 op simulator failed to start: %w", err) + } for _, l2Anvil := range s.l2Anvils { if err := l2Anvil.Start(ctx); err != nil { return fmt.Errorf("l2 anvil failed to start: %w", err) } } - - if err := s.l1OpSim.Start(ctx); err != nil { - return fmt.Errorf("l1 op simulator failed to start: %w", err) - } - for _, l2OpSim := range s.l2OpSims { if err := l2OpSim.Start(ctx); err != nil { return fmt.Errorf("l2 op simulator failed to start: %w", err) @@ -109,32 +109,35 @@ func (s *Supersim) Start(ctx context.Context) error { func (s *Supersim) Stop(ctx context.Context) error { s.log.Info("stopping supersim") + for _, l2OpSim := range s.l2OpSims { + if err := l2OpSim.Stop(ctx); err != nil { + return fmt.Errorf("l2 op simulator failed to stop: %w", err) + } + s.log.Info("stopped op simulator", "chain.id", l2OpSim.ChainId()) + } for _, l2Anvil := range s.l2Anvils { if err := l2Anvil.Stop(); err != nil { return fmt.Errorf("l2 anvil failed to stop: %w", err) } } - if err := s.l1Anvil.Stop(); err != nil { - return fmt.Errorf("l1 anvil failed to stop: %w", err) - } - if err := s.l1OpSim.Stop(ctx); err != nil { return fmt.Errorf("l1 op simulator failed to stop: %w", err) } - s.log.Info("Stopped op simulator", "chain.id", s.l1OpSim.ChainId()) - - for _, l2OpSim := range s.l2OpSims { - if err := l2OpSim.Stop(ctx); err != nil { - return fmt.Errorf("l2 op simulator failed to stop: %w", err) - } - s.log.Info("Stopped op simulator", "chain.id", l2OpSim.ChainId()) + if err := s.l1Anvil.Stop(); err != nil { + return fmt.Errorf("l1 anvil failed to stop: %w", err) } + s.log.Info("stopped op simulator", "chain.id", s.l1OpSim.ChainId()) return nil } func (s *Supersim) Stopped() bool { + for _, l2OpSim := range s.l2OpSims { + if stopped := l2OpSim.Stopped(); !stopped { + return stopped + } + } for _, l2Anvil := range s.l2Anvils { if stopped := l2Anvil.Stopped(); !stopped { return stopped @@ -144,13 +147,6 @@ func (s *Supersim) Stopped() bool { if stopped := s.l1Anvil.Stopped(); !stopped { return stopped } - - for _, l2OpSim := range s.l2OpSims { - if stopped := l2OpSim.Stopped(); !stopped { - return stopped - } - } - if stopped := s.l1OpSim.Stopped(); !stopped { return stopped } diff --git a/supersim_test.go b/supersim_test.go index 3c29f879..2be05b72 100644 --- a/supersim_test.go +++ b/supersim_test.go @@ -8,6 +8,7 @@ import ( "github.com/ethereum-optimism/optimism/op-service/testlog" "github.com/stretchr/testify/require" + "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rpc" ) @@ -53,17 +54,26 @@ func createTestSuite(t *testing.T) *TestSuite { func TestStartup(t *testing.T) { testSuite := createTestSuite(t) + var chainId math.HexOrDecimal64 + // test that all chains can be queried - client, err := rpc.Dial(testSuite.Supersim.l1OpSim.Endpoint()) + l1Client, err := rpc.Dial(testSuite.Supersim.l1OpSim.Endpoint()) require.NoError(t, err) - require.NoError(t, client.CallContext(context.Background(), nil, "eth_chainId")) - client.Close() + require.NoError(t, l1Client.CallContext(context.Background(), &chainId, "eth_chainId")) + require.Equal(t, uint64(chainId), testSuite.Supersim.l1OpSim.ChainId()) + l1Client.Close() - for _, l2Chain := range testSuite.Supersim.l2OpSims { - client, err := rpc.Dial(l2Chain.Endpoint()) + for id, l2Chain := range testSuite.Supersim.l2OpSims { + require.Equal(t, id, l2Chain.ChainId()) + + l2Client, err := rpc.Dial(l2Chain.Endpoint()) require.NoError(t, err) - require.NoError(t, client.CallContext(context.Background(), nil, "eth_chainId")) - client.Close() + require.NoError(t, l2Client.CallContext(context.Background(), &chainId, "eth_chainId")) + + // Commented out due to a bug in foundry that sets the chain id to 1 whenever genesis.json file is supplied + //require.Equal(t, l2Chain.ChainId(), uint64(chainId)) + + l2Client.Close() } } @@ -78,12 +88,12 @@ func TestGenesisState(t *testing.T) { var code string require.NoError(t, client.CallContext(context.Background(), &code, "eth_getCode", crossL2InboxAddress, "latest")) - require.NotEqual(t, code, emptyCode, "CrossL2Inbox is not deployed") + require.NotEqual(t, emptyCode, code, "CrossL2Inbox is not deployed") require.NoError(t, client.CallContext(context.Background(), &code, "eth_getCode", l2toL2CrossDomainMessengerAddress, "latest")) - require.NotEqual(t, code, emptyCode, "L2ToL2CrosSDomainMessenger is not deployed") + require.NotEqual(t, emptyCode, code, "L2ToL2CrosSDomainMessenger is not deployed") require.NoError(t, client.CallContext(context.Background(), &code, "eth_getCode", l1BlockAddress, "latest")) - require.NotEqual(t, code, emptyCode, "L1Block is not deployed") + require.NotEqual(t, emptyCode, code, "L1Block is not deployed") } }