forked from ethereum-optimism/supersim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supersim.go
117 lines (93 loc) · 3.87 KB
/
supersim.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package supersim
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/ethereum-optimism/optimism/op-service/predeploys"
registry "github.com/ethereum-optimism/superchain-registry/superchain"
"github.com/ethereum-optimism/supersim/config"
"github.com/ethereum-optimism/supersim/orchestrator"
"github.com/ethereum/go-ethereum/log"
)
type Supersim struct {
log log.Logger
CLIConfig *config.CLIConfig
NetworkConfig *config.NetworkConfig
Orchestrator *orchestrator.Orchestrator
}
func NewSupersim(log log.Logger, envPrefix string, closeApp context.CancelCauseFunc, cliConfig *config.CLIConfig) (*Supersim, error) {
networkConfig := config.GetDefaultNetworkConfig(uint64(time.Now().Unix()), cliConfig.LogsDirectory)
// If Forking, override the network config with the generated fork config
if cliConfig.ForkConfig != nil {
superchain := registry.Superchains[cliConfig.ForkConfig.Network]
log.Info("generating fork configuration", "superchain", superchain.Superchain)
var err error
networkConfig, err = orchestrator.NetworkConfigFromForkCLIConfig(log, envPrefix, cliConfig)
if err != nil {
return nil, fmt.Errorf("failed to construct fork configuration: %w", err)
}
l1ForkHeightStr := "latest"
if cliConfig.ForkConfig.L1ForkHeight > 0 {
l1ForkHeightStr = fmt.Sprintf("%d", cliConfig.ForkConfig.L1ForkHeight)
}
log.Info("forked l1 chain config", "name", superchain.Superchain, "chain.id", networkConfig.L1Config.ChainID, "fork.height", l1ForkHeightStr)
for _, chainCfg := range networkConfig.L2Configs {
name := registry.OPChains[chainCfg.ChainID].Chain
log.Info("forked l2 chain config", "name", name, "chain.id", chainCfg.ChainID, "fork.height", chainCfg.ForkConfig.BlockNumber)
}
}
// Forward set ports. Setting `0` will work to allocate a random port
networkConfig.L1Config.Port = cliConfig.L1Port
networkConfig.L2StartingPort = cliConfig.L2StartingPort
// Forward interop config
networkConfig.InteropAutoRelay = cliConfig.InteropAutoRelay
networkConfig.InteropDelay = cliConfig.InteropDelay
o, err := orchestrator.NewOrchestrator(log, closeApp, &networkConfig, cliConfig.AdminPort)
if err != nil {
return nil, fmt.Errorf("failed to create orchestrator")
}
return &Supersim{log, cliConfig, &networkConfig, o}, nil
}
func (s *Supersim) Start(ctx context.Context) error {
s.log.Info("starting supersim")
if err := s.Orchestrator.Start(ctx); err != nil {
return fmt.Errorf("orchestrator failed to start: %w", err)
}
s.log.Info("supersim is ready")
s.log.Info(s.ConfigAsString())
return nil
}
func (s *Supersim) Stop(ctx context.Context) error {
var errs []error
s.log.Info("stopping supersim")
if err := s.Orchestrator.Stop(ctx); err != nil {
errs = append(errs, fmt.Errorf("orchestrator failed to stop: %w", err))
}
s.log.Info("stopped supersim")
return errors.Join(errs...)
}
// no-op dead code in the cliapp lifecycle
func (s *Supersim) Stopped() bool {
return false
}
func (s *Supersim) ConfigAsString() string {
var b strings.Builder
fmt.Fprintln(&b, config.DefaultSecretsConfigAsString())
fmt.Fprintln(&b, "Supersim Config")
fmt.Fprintln(&b, "-----------------------")
fmt.Fprintln(&b, s.Orchestrator.ConfigAsString())
// Vanilla mode or enabled in fork mode
if s.NetworkConfig.InteropEnabled {
fmt.Fprintln(&b, "(EXPERIMENTAL) Interop!")
fmt.Fprintln(&b, "-----------------------")
fmt.Fprintln(&b, "For more information see the explainer! ( https://docs.optimism.io/stack/protocol/interop/explainer )")
fmt.Fprintln(&b, "\nAdded Predeploy Contracts:")
fmt.Fprintf(&b, " - L2ToL2CrossDomainMessenger: %s\n", predeploys.L2toL2CrossDomainMessenger)
fmt.Fprintf(&b, " - CrossL2Inbox: %s\n", predeploys.CrossL2Inbox)
fmt.Fprintf(&b, " - SuperchainTokenBridge: %s\n", predeploys.SuperchainTokenBridge)
fmt.Fprintf(&b, " - SuperchainWETH: %s\n", predeploys.SuperchainWETH)
}
return b.String()
}