From df208bcf8fc14fcdf4de859c50d099d0986b7b6d Mon Sep 17 00:00:00 2001 From: James Kim Date: Tue, 25 Jun 2024 14:37:18 -0400 Subject: [PATCH] feat: spin up multiple chains --- cmd/main.go | 2 +- supersim.go | 59 +++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 76d0f570..5dda1c92 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -52,5 +52,5 @@ func SupersimMain(ctx *cli.Context, closeApp context.CancelCauseFunc) (cliapp.Li } // use config and setup supersim - return supersim.NewSupersim(log), nil + return supersim.NewSupersim(log, &supersim.DefaultConfig), nil } diff --git a/supersim.go b/supersim.go index 34156460..ebbb9b8d 100644 --- a/supersim.go +++ b/supersim.go @@ -1,6 +1,8 @@ package supersim import ( + "fmt" + "context" "github.com/ethereum-optimism/supersim/anvil" @@ -8,27 +10,68 @@ import ( "github.com/ethereum/go-ethereum/log" ) + + +type Config struct { + l1Chain anvil.Config + l2Chains []anvil.Config +} + +var DefaultConfig = Config{ + l1Chain: anvil.Config{ChainId: 1, Port: 8545}, + l2Chains: []anvil.Config{{ChainId: 10, Port: 9545}, {ChainId: 30, Port: 9555}}, +} + type Supersim struct { log log.Logger - anvil *anvil.Anvil + l1Chain *anvil.Anvil + l2Chains map[uint64]*anvil.Anvil + } -func NewSupersim(log log.Logger) *Supersim { - anvil := anvil.New(log, &anvil.Config{ChainId: 10, Port: 9545}) - return &Supersim{log, anvil} +func NewSupersim(log log.Logger, config *Config) *Supersim { + l1Chain := anvil.New(log, &config.l1Chain) + + l2Chains := make(map[uint64]*anvil.Anvil) + for _, l2Chain := range config.l2Chains { + l2Chains[l2Chain.ChainId] = anvil.New(log, &l2Chain) + } + + return &Supersim{log, l1Chain, l2Chains} } func (s *Supersim) Start(ctx context.Context) error { s.log.Info("starting supersim") - return s.anvil.Start(ctx) + + if err := s.l1Chain.Start(ctx); err != nil { + return fmt.Errorf("l1 chain failed to start: %w", err) + } + + for _, l2Chain := range s.l2Chains { + if err := l2Chain.Start(ctx); err!= nil { + return fmt.Errorf("l2 chain failed to start: %w", err) + } + } + + return nil } func (s *Supersim) Stop(_ context.Context) error { s.log.Info("stopping supersim") - return s.anvil.Stop() -} + + for _, l2Chain := range s.l2Chains { + if err := l2Chain.Stop(); err!= nil { + return fmt.Errorf("l2 chain failed to stop: %w", err) + } + } + + if err := s.l1Chain.Stop(); err != nil { + return fmt.Errorf("l1 chain failed to stop: %w", err) + } + + return nil} func (s *Supersim) Stopped() bool { - return s.anvil.Stopped() + return s.l1Chain.Stopped() }