Skip to content

Commit

Permalink
feat: op-program --network flag accepts number as chainid (#12503)
Browse files Browse the repository at this point in the history
* feat: op-program --network flag accepts number as chainid

* put common parse code into rollup.Config struct

* address comments

---------

Co-authored-by: Qi Zhou <[email protected]>
  • Loading branch information
2 people authored and ajsutton committed Nov 27, 2024
1 parent 2187ab0 commit 138c5c7
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 47 deletions.
11 changes: 11 additions & 0 deletions op-node/rollup/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package rollup

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"time"

Expand Down Expand Up @@ -621,6 +623,15 @@ func (c *Config) LogDescription(log log.Logger, l2Chains map[string]string) {
)
}

func (c *Config) ParseRollupConfig(in io.Reader) error {
dec := json.NewDecoder(in)
dec.DisallowUnknownFields()
if err := dec.Decode(c); err != nil {
return fmt.Errorf("failed to decode rollup config: %w", err)
}
return nil
}

func fmtForkTimeOrUnset(v *uint64) string {
if v == nil {
return "(not configured)"
Expand Down
2 changes: 1 addition & 1 deletion op-program/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test:
verify-sepolia: op-program-host op-program-client
env GO111MODULE=on go run ./verify/sepolia/cmd/sepolia.go --l1 $$SEPOLIA_L1URL --l1.beacon $$SEPOLIA_BEACON_URL --l2 $$SEPOLIA_L2URL --datadir /tmp/test-sepolia

verify-devnet:
verify-devnet: op-program-host op-program-client
env GO111MODULE=on go run ./verify/devnet/cmd/devnet.go --l1 http://localhost:8545 --l1.beacon http://localhost:5052 --l2 http://localhost:9545 --datadir /tmp/test-devnet

capture-mainnet-genesis: op-program-host op-program-client
Expand Down
9 changes: 3 additions & 6 deletions op-program/chainconfig/chaincfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,10 @@ func rollupConfigByChainID(chainID uint64, customChainFS embed.FS) (*rollup.Conf
} else if err != nil {
return nil, fmt.Errorf("failed to get rollup config for chain ID %d: %w", chainID, err)
}
dec := json.NewDecoder(file)
dec.DisallowUnknownFields()
defer file.Close()

var customRollupConfig rollup.Config
if err := dec.Decode(&customRollupConfig); err != nil {
return nil, fmt.Errorf("failed to parse rollup config for chain ID %d: %w", chainID, err)
}
return &customRollupConfig, nil
return &customRollupConfig, customRollupConfig.ParseRollupConfig(file)
}

func ChainConfigByChainID(chainID uint64) (*params.ChainConfig, error) {
Expand Down
62 changes: 44 additions & 18 deletions op-program/host/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"errors"
"fmt"
"os"
"strconv"

"github.com/ethereum-optimism/optimism/op-node/chaincfg"

opnode "github.com/ethereum-optimism/optimism/op-node"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-program/chainconfig"
"github.com/ethereum-optimism/optimism/op-program/host/flags"
"github.com/ethereum-optimism/optimism/op-service/sources"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -137,10 +137,7 @@ func NewConfigFromCLI(log log.Logger, ctx *cli.Context) (*Config, error) {
if err := flags.CheckRequired(ctx); err != nil {
return nil, err
}
rollupCfg, err := opnode.NewRollupConfigFromCLI(log, ctx)
if err != nil {
return nil, err
}

l2Head := common.HexToHash(ctx.String(flags.L2Head.Name))
if l2Head == (common.Hash{}) {
return nil, ErrInvalidL2Head
Expand All @@ -162,27 +159,45 @@ func NewConfigFromCLI(log log.Logger, ctx *cli.Context) (*Config, error) {
if l1Head == (common.Hash{}) {
return nil, ErrInvalidL1Head
}
l2GenesisPath := ctx.String(flags.L2GenesisPath.Name)

var err error
var rollupCfg *rollup.Config
var l2ChainConfig *params.ChainConfig
var isCustomConfig bool
if l2GenesisPath == "" {
networkName := ctx.String(flags.Network.Name)
ch := chaincfg.ChainByName(networkName)
if ch == nil {
return nil, fmt.Errorf("flag %s is required for network %s", flags.L2GenesisPath.Name, networkName)
networkName := ctx.String(flags.Network.Name)
if networkName != "" {
var chainID uint64
if chainID, err = strconv.ParseUint(networkName, 10, 64); err != nil {
ch := chaincfg.ChainByName(networkName)
if ch == nil {
return nil, fmt.Errorf("invalid network: %q", networkName)
}
chainID = ch.ChainID
}
cfg, err := params.LoadOPStackChainConfig(ch.ChainID)

l2ChainConfig, err = chainconfig.ChainConfigByChainID(chainID)
if err != nil {
return nil, fmt.Errorf("failed to load chain config for chain %d: %w", chainID, err)
}
rollupCfg, err = chainconfig.RollupConfigByChainID(chainID)
if err != nil {
return nil, fmt.Errorf("failed to load chain config for chain %d: %w", ch.ChainID, err)
return nil, fmt.Errorf("failed to load rollup config for chain %d: %w", chainID, err)
}
l2ChainConfig = cfg
} else {
l2GenesisPath := ctx.String(flags.L2GenesisPath.Name)
l2ChainConfig, err = loadChainConfigFromGenesis(l2GenesisPath)
if err != nil {
return nil, fmt.Errorf("invalid genesis: %w", err)
}

rollupConfigPath := ctx.String(flags.RollupConfig.Name)
rollupCfg, err = loadRollupConfig(rollupConfigPath)
if err != nil {
return nil, fmt.Errorf("invalid rollup config: %w", err)
}

isCustomConfig = true
}
if err != nil {
return nil, fmt.Errorf("invalid genesis: %w", err)
}
return &Config{
Rollup: rollupCfg,
DataDir: ctx.String(flags.DataDir.Name),
Expand Down Expand Up @@ -215,3 +230,14 @@ func loadChainConfigFromGenesis(path string) (*params.ChainConfig, error) {
}
return genesis.Config, nil
}

func loadRollupConfig(rollupConfigPath string) (*rollup.Config, error) {
file, err := os.Open(rollupConfigPath)
if err != nil {
return nil, fmt.Errorf("failed to read rollup config: %w", err)
}
defer file.Close()

var rollupConfig rollup.Config
return &rollupConfig, rollupConfig.ParseRollupConfig(file)
}
3 changes: 3 additions & 0 deletions op-program/host/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ func CheckRequired(ctx *cli.Context) error {
if network == "" && ctx.String(L2GenesisPath.Name) == "" {
return fmt.Errorf("flag %s is required for custom networks", L2GenesisPath.Name)
}
if ctx.String(L2GenesisPath.Name) != "" && network != "" {
return fmt.Errorf("cannot specify both %s and %s", L2GenesisPath.Name, Network.Name)
}
for _, flag := range requiredFlags {
if !ctx.IsSet(flag.Names()[0]) {
return fmt.Errorf("flag %s is required", flag.Names()[0])
Expand Down
44 changes: 22 additions & 22 deletions op-program/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ import (
)

type Runner struct {
l1RpcUrl string
l1RpcKind string
l1BeaconUrl string
l2RpcUrl string
dataDir string
network string
chainCfg *params.ChainConfig
l2Client *sources.L2Client
logCfg oplog.CLIConfig
setupLog log.Logger
rollupCfg *rollup.Config
l1RpcUrl string
l1RpcKind string
l1BeaconUrl string
l2RpcUrl string
dataDir string
network string
chainCfg *params.ChainConfig
l2Client *sources.L2Client
logCfg oplog.CLIConfig
setupLog log.Logger
rollupCfg *rollup.Config
}

func NewRunner(l1RpcUrl string, l1RpcKind string, l1BeaconUrl string, l2RpcUrl string, dataDir string, network string, chainCfg *params.ChainConfig) (*Runner, error) {
Expand Down Expand Up @@ -66,17 +66,17 @@ func NewRunner(l1RpcUrl string, l1RpcKind string, l1BeaconUrl string, l2RpcUrl s
}

return &Runner{
l1RpcUrl: l1RpcUrl,
l1RpcKind: l1RpcKind,
l1BeaconUrl: l1BeaconUrl,
l2RpcUrl: l2RpcUrl,
dataDir: dataDir,
network: network,
chainCfg: chainCfg,
logCfg: logCfg,
setupLog: setupLog,
l2Client: l2Client,
rollupCfg: rollupCfg,
l1RpcUrl: l1RpcUrl,
l1RpcKind: l1RpcKind,
l1BeaconUrl: l1BeaconUrl,
l2RpcUrl: l2RpcUrl,
dataDir: dataDir,
network: network,
chainCfg: chainCfg,
logCfg: logCfg,
setupLog: setupLog,
l2Client: l2Client,
rollupCfg: rollupCfg,
}, nil
}

Expand Down

0 comments on commit 138c5c7

Please sign in to comment.