Commit d8616f54 authored by Adrian Sutton's avatar Adrian Sutton

op-program: Pass a ChainConfig to the Config struct rather than a file path to load from

parent a01d02c3
This diff is collapsed.
package config
import (
"encoding/json"
"errors"
"fmt"
"os"
opnode "github.com/ethereum-optimism/optimism/op-node"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/sources"
"github.com/ethereum-optimism/optimism/op-program/host/flags"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/params"
"github.com/urfave/cli"
)
......@@ -23,15 +28,23 @@ var (
type Config struct {
Rollup *rollup.Config
// DataDir is the directory to read/write pre-image data from/to.
//If not set, an in-memory key-value store is used and fetching data must be enabled
DataDir string
L2URL string
L2GenesisPath string
// L1Head is the block has of the L1 chain head block
L1Head common.Hash
L2Head common.Hash
L2Claim common.Hash
L1URL string
L1TrustRPC bool
L1RPCKind sources.RPCProviderKind
// L2Head is the agreed L2 block to start derivation from
L2Head common.Hash
L2URL string
// L2Claim is the claimed L2 output root to verify
L2Claim common.Hash
// L2ChainConfig is the op-geth chain config for the L2 execution engine
L2ChainConfig *params.ChainConfig
}
func (c *Config) Check() error {
......@@ -50,7 +63,7 @@ func (c *Config) Check() error {
if c.L2Claim == (common.Hash{}) {
return ErrInvalidL2Claim
}
if c.L2GenesisPath == "" {
if c.L2ChainConfig == nil {
return ErrMissingL2Genesis
}
if (c.L1URL != "") != (c.L2URL != "") {
......@@ -67,10 +80,10 @@ func (c *Config) FetchingEnabled() bool {
}
// NewConfig creates a Config with all optional values set to the CLI default value
func NewConfig(rollupCfg *rollup.Config, l2GenesisPath string, l1Head common.Hash, l2Head common.Hash, l2Claim common.Hash) *Config {
func NewConfig(rollupCfg *rollup.Config, l2Genesis *params.ChainConfig, l1Head common.Hash, l2Head common.Hash, l2Claim common.Hash) *Config {
return &Config{
Rollup: rollupCfg,
L2GenesisPath: l2GenesisPath,
L2ChainConfig: l2Genesis,
L1Head: l1Head,
L2Head: l2Head,
L2Claim: l2Claim,
......@@ -98,11 +111,16 @@ func NewConfigFromCLI(ctx *cli.Context) (*Config, error) {
if l1Head == (common.Hash{}) {
return nil, ErrInvalidL1Head
}
l2GenesisPath := ctx.GlobalString(flags.L2GenesisPath.Name)
l2ChainConfig, err := loadChainConfigFromGenesis(l2GenesisPath)
if err != nil {
return nil, fmt.Errorf("invalid genesis: %w", err)
}
return &Config{
Rollup: rollupCfg,
DataDir: ctx.GlobalString(flags.DataDir.Name),
L2URL: ctx.GlobalString(flags.L2NodeAddr.Name),
L2GenesisPath: ctx.GlobalString(flags.L2GenesisPath.Name),
L2ChainConfig: l2ChainConfig,
L2Head: l2Head,
L2Claim: l2Claim,
L1Head: l1Head,
......@@ -111,3 +129,16 @@ func NewConfigFromCLI(ctx *cli.Context) (*Config, error) {
L1RPCKind: sources.RPCProviderKind(ctx.GlobalString(flags.L1RPCProviderKind.Name)),
}, nil
}
func loadChainConfigFromGenesis(path string) (*params.ChainConfig, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read l2 genesis file: %w", err)
}
var genesis core.Genesis
err = json.Unmarshal(data, &genesis)
if err != nil {
return nil, fmt.Errorf("parse l2 genesis file: %w", err)
}
return genesis.Config, nil
}
......@@ -6,11 +6,12 @@ import (
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"
)
var validRollupConfig = &chaincfg.Goerli
var validL2GenesisPath = "genesis.json"
var validL2Genesis = params.GoerliChainConfig
var validL1Head = common.Hash{0xaa}
var validL2Head = common.Hash{0xbb}
var validL2Claim = common.Hash{0xcc}
......@@ -60,7 +61,7 @@ func TestL2ClaimRequired(t *testing.T) {
func TestL2GenesisRequired(t *testing.T) {
config := validConfig()
config.L2GenesisPath = ""
config.L2ChainConfig = nil
err := config.Check()
require.ErrorIs(t, err, ErrMissingL2Genesis)
}
......@@ -132,7 +133,7 @@ func TestRequireDataDirInNonFetchingMode(t *testing.T) {
}
func validConfig() *Config {
cfg := NewConfig(validRollupConfig, validL2GenesisPath, validL1Head, validL2Head, validL2Claim)
cfg := NewConfig(validRollupConfig, validL2Genesis, validL1Head, validL2Head, validL2Claim)
cfg.DataDir = "/tmp/configTest"
return cfg
}
......@@ -2,25 +2,17 @@ package l2
import (
"context"
"encoding/json"
"fmt"
"os"
cll2 "github.com/ethereum-optimism/optimism/op-program/client/l2"
"github.com/ethereum-optimism/optimism/op-program/host/config"
"github.com/ethereum-optimism/optimism/op-program/preimage"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
)
func NewEngine(logger log.Logger, pre preimage.Oracle, hint preimage.Hinter, cfg *config.Config) (*cll2.OracleEngine, error) {
oracle := cll2.NewCachingOracle(cll2.NewPreimageOracle(pre, hint))
genesis, err := loadL2Genesis(cfg)
if err != nil {
return nil, err
}
engineBackend, err := cll2.NewOracleBackedL2Chain(logger, oracle, genesis, cfg.L2Head)
engineBackend, err := cll2.NewOracleBackedL2Chain(logger, oracle, cfg.L2ChainConfig, cfg.L2Head)
if err != nil {
return nil, fmt.Errorf("create l2 chain: %w", err)
}
......@@ -34,16 +26,3 @@ func NewFetchingOracle(ctx context.Context, logger log.Logger, cfg *config.Confi
}
return oracle, nil
}
func loadL2Genesis(cfg *config.Config) (*params.ChainConfig, error) {
data, err := os.ReadFile(cfg.L2GenesisPath)
if err != nil {
return nil, fmt.Errorf("read l2 genesis file: %w", err)
}
var genesis core.Genesis
err = json.Unmarshal(data, &genesis)
if err != nil {
return nil, fmt.Errorf("parse l2 genesis file: %w", err)
}
return genesis.Config, nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment