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: op-program --network flag accepts number as chainid #12503

Merged
merged 3 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 op-program/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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
67 changes: 50 additions & 17 deletions op-program/host/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"fmt"
"os"
"slices"
"strconv"

"github.com/ethereum-optimism/optimism/op-node/chaincfg"
"github.com/ethereum-optimism/optimism/op-program/chainconfig"
"github.com/ethereum-optimism/optimism/op-program/host/types"

opnode "github.com/ethereum-optimism/optimism/op-node"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-program/host/flags"
"github.com/ethereum-optimism/optimism/op-service/sources"
Expand Down Expand Up @@ -146,10 +147,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 @@ -171,27 +169,46 @@ 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("flag %s is required for network %s", flags.L2GenesisPath.Name, networkName)
}
chainID = 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)
}
cfg, err := params.LoadOPStackChainConfig(ch.ChainID)
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)
}

dbFormat := types.DataFormat(ctx.String(flags.DataFormat.Name))
if !slices.Contains(types.SupportedDataFormats, dbFormat) {
return nil, fmt.Errorf("invalid %w: %v", ErrInvalidDataFormat, dbFormat)
Expand Down Expand Up @@ -229,3 +246,19 @@ 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
dec := json.NewDecoder(file)
dec.DisallowUnknownFields()
if err := dec.Decode(&rollupConfig); err != nil {
return nil, fmt.Errorf("failed to decode rollup config: %w", err)
}
return &rollupConfig, nil
}
ajsutton marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions op-program/host/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,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
2 changes: 1 addition & 1 deletion op-program/verify/devnet/cmd/devnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
}

// Apply the custom configs by running op-program in the same process
runner, err := verify.NewRunner(l1RpcUrl, l1RpcKind, l1BeaconUrl, l2RpcUrl, dataDir, "", 901, true)
runner, err := verify.NewRunner(l1RpcUrl, l1RpcKind, l1BeaconUrl, l2RpcUrl, dataDir, "901", 901, false)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to create runner: %v\n", err.Error())
os.Exit(1)
Expand Down