Skip to content

Commit

Permalink
op-program: Avoid parsing op-sepolia and op-mainnet rollup and chain …
Browse files Browse the repository at this point in the history
…configs in init() methods (ethereum-optimism#12307)

This was causing op-program to spend cycles parsing the config JSON files to set constants that weren't actually used.
  • Loading branch information
ajsutton authored Oct 8, 2024
1 parent 8cb56d9 commit fb7f306
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 52 deletions.
19 changes: 9 additions & 10 deletions op-node/chaincfg/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ import (
"github.com/ethereum-optimism/optimism/op-node/rollup"
)

var Mainnet, Sepolia *rollup.Config
// OPSepolia loads the op-sepolia rollup config. This is intended for tests that need an arbitrary, valid rollup config.
func OPSepolia() *rollup.Config {
return mustLoadRollupConfig("op-sepolia")
}

func init() {
mustCfg := func(name string) *rollup.Config {
cfg, err := GetRollupConfig(name)
if err != nil {
panic(fmt.Errorf("failed to load rollup config %q: %w", name, err))
}
return cfg
func mustLoadRollupConfig(name string) *rollup.Config {
cfg, err := GetRollupConfig(name)
if err != nil {
panic(fmt.Errorf("failed to load rollup config %q: %w", name, err))
}
Mainnet = mustCfg("op-mainnet")
Sepolia = mustCfg("op-sepolia")
return cfg
}

var L2ChainIDToNetworkDisplayName = func() map[string]string {
Expand Down
6 changes: 3 additions & 3 deletions op-node/p2p/peer_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ func (testSuite *PeerParamsTestSuite) TestNewPeerScoreThresholds() {

// TestGetPeerScoreParams validates the peer score parameters.
func (testSuite *PeerParamsTestSuite) TestGetPeerScoreParams_None() {
params, err := GetScoringParams("none", chaincfg.Sepolia)
params, err := GetScoringParams("none", chaincfg.OPSepolia())
testSuite.NoError(err)
testSuite.Nil(params)
}

// TestLightPeerScoreParams validates the light peer score params.
func (testSuite *PeerParamsTestSuite) TestGetPeerScoreParams_Light() {
cfg := chaincfg.Sepolia
cfg := chaincfg.OPSepolia()
cfg.BlockTime = 1
slot := time.Duration(cfg.BlockTime) * time.Second
epoch := 6 * slot
Expand Down Expand Up @@ -98,7 +98,7 @@ func (testSuite *PeerParamsTestSuite) TestGetPeerScoreParams_Light() {

// TestParamsZeroBlockTime validates peer score params use default slot for 0 block time.
func (testSuite *PeerParamsTestSuite) TestParamsZeroBlockTime() {
cfg := chaincfg.Sepolia
cfg := chaincfg.OPSepolia()
cfg.BlockTime = 0
slot := 2 * time.Second
params, err := GetScoringParams("light", cfg)
Expand Down
33 changes: 16 additions & 17 deletions op-program/chainconfig/chaincfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,14 @@ package chainconfig
import (
"fmt"

"github.com/ethereum-optimism/optimism/op-node/chaincfg"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum/go-ethereum/params"
)

var OPSepoliaChainConfig, OPMainnetChainConfig *params.ChainConfig

func init() {
mustLoadConfig := func(chainID uint64) *params.ChainConfig {
cfg, err := params.LoadOPStackChainConfig(chainID)
if err != nil {
panic(err)
}
return cfg
}
OPSepoliaChainConfig = mustLoadConfig(11155420)
OPMainnetChainConfig = mustLoadConfig(10)
}

var L2ChainConfigsByChainID = map[uint64]*params.ChainConfig{
11155420: OPSepoliaChainConfig,
10: OPMainnetChainConfig,
// OPSepoliaChainConfig loads the op-sepolia chain config. This is intended for tests that need an arbitrary, valid chain config.
func OPSepoliaChainConfig() *params.ChainConfig {
return mustLoadChainConfig("op-sepolia")
}

func RollupConfigByChainID(chainID uint64) (*rollup.Config, error) {
Expand All @@ -37,3 +24,15 @@ func RollupConfigByChainID(chainID uint64) (*rollup.Config, error) {
func ChainConfigByChainID(chainID uint64) (*params.ChainConfig, error) {
return params.LoadOPStackChainConfig(chainID)
}

func mustLoadChainConfig(name string) *params.ChainConfig {
chainCfg := chaincfg.ChainByName(name)
if chainCfg == nil {
panic(fmt.Errorf("unknown chain config %q", name))
}
cfg, err := ChainConfigByChainID(chainCfg.ChainID)
if err != nil {
panic(fmt.Errorf("failed to load rollup config: %q: %w", name, err))
}
return cfg
}
11 changes: 6 additions & 5 deletions op-program/client/boot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (
)

func TestBootstrapClient(t *testing.T) {
rollupCfg := chaincfg.OPSepolia()
bootInfo := &BootInfo{
L1Head: common.HexToHash("0x1111"),
L2OutputRoot: common.HexToHash("0x2222"),
L2Claim: common.HexToHash("0x3333"),
L2ClaimBlockNumber: 1,
L2ChainID: chaincfg.Sepolia.L2ChainID.Uint64(),
L2ChainConfig: chainconfig.OPSepoliaChainConfig,
RollupConfig: chaincfg.Sepolia,
L2ChainID: rollupCfg.L2ChainID.Uint64(),
L2ChainConfig: chainconfig.OPSepoliaChainConfig(),
RollupConfig: rollupCfg,
}
mockOracle := &mockBoostrapOracle{bootInfo, false}
readBootInfo := NewBootstrapClient(mockOracle).BootInfo()
Expand All @@ -35,8 +36,8 @@ func TestBootstrapClient_CustomChain(t *testing.T) {
L2Claim: common.HexToHash("0x3333"),
L2ClaimBlockNumber: 1,
L2ChainID: CustomChainIDIndicator,
L2ChainConfig: chainconfig.OPSepoliaChainConfig,
RollupConfig: chaincfg.Sepolia,
L2ChainConfig: chainconfig.OPSepoliaChainConfig(),
RollupConfig: chaincfg.OPSepolia(),
}
mockOracle := &mockBoostrapOracle{bootInfo, true}
readBootInfo := NewBootstrapClient(mockOracle).BootInfo()
Expand Down
4 changes: 2 additions & 2 deletions op-program/client/l2/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ func createOracleEngine(t *testing.T) (*OracleEngine, *stubEngineBackend) {
}
engine := OracleEngine{
backend: backend,
rollupCfg: chaincfg.Sepolia,
rollupCfg: chaincfg.OPSepolia(),
}
return &engine, backend
}

func createL2Block(t *testing.T, number int) *types.Block {
tx, err := derive.L1InfoDeposit(chaincfg.Sepolia, eth.SystemConfig{}, uint64(1), eth.HeaderBlockInfo(&types.Header{
tx, err := derive.L1InfoDeposit(chaincfg.OPSepolia(), eth.SystemConfig{}, uint64(1), eth.HeaderBlockInfo(&types.Header{
Number: big.NewInt(32),
BaseFee: big.NewInt(7),
}), 0)
Expand Down
10 changes: 5 additions & 5 deletions op-program/host/cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestDefaultCLIOptionsMatchDefaultConfig(t *testing.T) {
require.NoError(t, err)
defaultCfg := config.NewConfig(
rollupCfg,
chainconfig.OPSepoliaChainConfig,
chainconfig.OPSepoliaChainConfig(),
common.HexToHash(l1HeadValue),
common.HexToHash(l2HeadValue),
common.HexToHash(l2OutputRoot),
Expand All @@ -100,7 +100,7 @@ func TestNetwork(t *testing.T) {
genesisFile := writeValidGenesis(t)

cfg := configForArgs(t, addRequiredArgsExcept("--network", "--rollup.config", configFile, "--l2.genesis", genesisFile))
require.Equal(t, *chaincfg.Sepolia, *cfg.Rollup)
require.Equal(t, *chaincfg.OPSepolia(), *cfg.Rollup)
})

for _, name := range chaincfg.AvailableNetworks() {
Expand Down Expand Up @@ -154,9 +154,9 @@ func TestL2Genesis(t *testing.T) {
require.Equal(t, l2GenesisConfig, cfg.L2ChainConfig)
})

t.Run("NotRequiredForGoerli", func(t *testing.T) {
t.Run("NotRequiredForSepolia", func(t *testing.T) {
cfg := configForArgs(t, replaceRequiredArg("--network", "sepolia"))
require.Equal(t, chainconfig.OPSepoliaChainConfig, cfg.L2ChainConfig)
require.Equal(t, chainconfig.OPSepoliaChainConfig(), cfg.L2ChainConfig)
})
}

Expand Down Expand Up @@ -388,7 +388,7 @@ func writeValidGenesis(t *testing.T) string {

func writeValidRollupConfig(t *testing.T) string {
dir := t.TempDir()
j, err := json.Marshal(chaincfg.Sepolia)
j, err := json.Marshal(chaincfg.OPSepolia())
require.NoError(t, err)
cfgFile := dir + "/rollup.json"
require.NoError(t, os.WriteFile(cfgFile, j, 0666))
Expand Down
4 changes: 2 additions & 2 deletions op-program/host/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
)

var (
validRollupConfig = chaincfg.Sepolia
validL2Genesis = chainconfig.OPSepoliaChainConfig
validRollupConfig = chaincfg.OPSepolia()
validL2Genesis = chainconfig.OPSepoliaChainConfig()
validL1Head = common.Hash{0xaa}
validL2Head = common.Hash{0xbb}
validL2Claim = common.Hash{0xcc}
Expand Down
2 changes: 1 addition & 1 deletion op-program/host/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestServerMode(t *testing.T) {

l1Head := common.Hash{0x11}
l2OutputRoot := common.Hash{0x33}
cfg := config.NewConfig(chaincfg.Sepolia, chainconfig.OPSepoliaChainConfig, l1Head, common.Hash{0x22}, l2OutputRoot, common.Hash{0x44}, 1000)
cfg := config.NewConfig(chaincfg.OPSepolia(), chainconfig.OPSepoliaChainConfig(), l1Head, common.Hash{0x22}, l2OutputRoot, common.Hash{0x44}, 1000)
cfg.DataDir = dir
cfg.ServerMode = true

Expand Down
2 changes: 1 addition & 1 deletion op-program/host/kvstore/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func TestLocalPreimageSource(t *testing.T) {
cfg := &config.Config{
Rollup: chaincfg.Sepolia,
Rollup: chaincfg.OPSepolia(),
L1Head: common.HexToHash("0x1111"),
L2OutputRoot: common.HexToHash("0x2222"),
L2Claim: common.HexToHash("0x3333"),
Expand Down
5 changes: 3 additions & 2 deletions op-program/verify/mainnet/cmd/mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"fmt"
"os"

"github.com/ethereum-optimism/optimism/op-program/chainconfig"
"github.com/ethereum-optimism/optimism/op-program/verify"
"github.com/ethereum/go-ethereum/common"
)

const opMainnetChainID = 10

func main() {
var l1RpcUrl string
var l1RpcKind string
Expand Down Expand Up @@ -44,7 +45,7 @@ func main() {
os.Exit(2)
}

runner, err := verify.NewRunner(l1RpcUrl, l1RpcKind, l1BeaconUrl, l2RpcUrl, dataDir, "op-mainnet", chainconfig.OPMainnetChainConfig)
runner, err := verify.NewRunner(l1RpcUrl, l1RpcKind, l1BeaconUrl, l2RpcUrl, dataDir, "op-mainnet", opMainnetChainID)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to create runner: %v\n", err.Error())
os.Exit(1)
Expand Down
5 changes: 3 additions & 2 deletions op-program/verify/sepolia/cmd/sepolia.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"fmt"
"os"

"github.com/ethereum-optimism/optimism/op-program/chainconfig"
"github.com/ethereum-optimism/optimism/op-program/verify"
"github.com/ethereum/go-ethereum/common"
)

const opSepoliaChainID = 11155420

func main() {
var l1RpcUrl string
var l1RpcKind string
Expand Down Expand Up @@ -44,7 +45,7 @@ func main() {
os.Exit(2)
}

runner, err := verify.NewRunner(l1RpcUrl, l1RpcKind, l1BeaconUrl, l2RpcUrl, dataDir, "op-sepolia", chainconfig.OPSepoliaChainConfig)
runner, err := verify.NewRunner(l1RpcUrl, l1RpcKind, l1BeaconUrl, l2RpcUrl, dataDir, "op-sepolia", opSepoliaChainID)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to create runner: %v\n", err.Error())
os.Exit(1)
Expand Down
10 changes: 8 additions & 2 deletions op-program/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-program/chainconfig"
"github.com/ethereum-optimism/optimism/op-program/host"
"github.com/ethereum-optimism/optimism/op-program/host/config"
"github.com/ethereum-optimism/optimism/op-service/client"
Expand Down Expand Up @@ -43,7 +44,7 @@ type Runner struct {
rollupCfg *rollup.Config
}

func NewRunner(l1RpcUrl string, l1RpcKind string, l1BeaconUrl string, l2RpcUrl string, dataDir string, network string, chainCfg *params.ChainConfig) (*Runner, error) {
func NewRunner(l1RpcUrl string, l1RpcKind string, l1BeaconUrl string, l2RpcUrl string, dataDir string, network string, chainID uint64) (*Runner, error) {
ctx := context.Background()
logCfg := oplog.DefaultCLIConfig()
logCfg.Level = log.LevelDebug
Expand All @@ -55,11 +56,16 @@ func NewRunner(l1RpcUrl string, l1RpcKind string, l1BeaconUrl string, l2RpcUrl s
return nil, fmt.Errorf("dial L2 client: %w", err)
}

rollupCfg, err := rollup.LoadOPStackRollupConfig(chainCfg.ChainID.Uint64())
rollupCfg, err := rollup.LoadOPStackRollupConfig(chainID)
if err != nil {
return nil, fmt.Errorf("failed to load rollup config: %w", err)
}

chainCfg, err := chainconfig.ChainConfigByChainID(chainID)
if err != nil {
return nil, fmt.Errorf("failed to load chain config: %w", err)
}

l2ClientCfg := sources.L2ClientDefaultConfig(rollupCfg, false)
l2RPC := client.NewBaseRPCClient(l2RawRpc)
l2Client, err := sources.NewL2Client(l2RPC, setupLog, nil, l2ClientCfg)
Expand Down

0 comments on commit fb7f306

Please sign in to comment.