Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: spin up multiple chains #24

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
59 changes: 51 additions & 8 deletions supersim.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,77 @@
package supersim

import (
"fmt"

"context"

"github.com/ethereum-optimism/supersim/anvil"

"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()
}