Commit 73f16c19 authored by inphi's avatar inphi

op-program: Use hardcoded chain config when available

Fixes a bug where the host incorrectly informs the client that a
custom chain config is in use. The client then attempts to read the
chain config pre-image data. However, this pre-image data will not be
available in on-chain contexts.
parent d2ce890a
......@@ -64,6 +64,9 @@ type Config struct {
// ServerMode indicates that the program should run in pre-image server mode and wait for requests.
// No client program is run.
ServerMode bool
// IsCustomChainConfig indicates that the program uses a custom chain configuration
IsCustomChainConfig bool
}
func (c *Config) Check() error {
......@@ -117,6 +120,8 @@ func NewConfig(
l2Claim common.Hash,
l2ClaimBlockNum uint64,
) *Config {
_, err := params.LoadOPStackChainConfig(l2Genesis.ChainID.Uint64())
isCustomConfig := err != nil
return &Config{
Rollup: rollupCfg,
L2ChainConfig: l2Genesis,
......@@ -126,6 +131,7 @@ func NewConfig(
L2Claim: l2Claim,
L2ClaimBlockNumber: l2ClaimBlockNum,
L1RPCKind: sources.RPCKindBasic,
IsCustomChainConfig: isCustomConfig,
}
}
......@@ -156,6 +162,7 @@ func NewConfigFromCLI(log log.Logger, ctx *cli.Context) (*Config, error) {
}
l2GenesisPath := ctx.String(flags.L2GenesisPath.Name)
var l2ChainConfig *params.ChainConfig
var isCustomConfig bool
if l2GenesisPath == "" {
networkName := ctx.String(flags.Network.Name)
ch := chaincfg.ChainByName(networkName)
......@@ -169,6 +176,7 @@ func NewConfigFromCLI(log log.Logger, ctx *cli.Context) (*Config, error) {
l2ChainConfig = cfg
} else {
l2ChainConfig, err = loadChainConfigFromGenesis(l2GenesisPath)
isCustomConfig = true
}
if err != nil {
return nil, fmt.Errorf("invalid genesis: %w", err)
......@@ -188,6 +196,7 @@ func NewConfigFromCLI(log log.Logger, ctx *cli.Context) (*Config, error) {
L1RPCKind: sources.RPCProviderKind(ctx.String(flags.L1RPCProviderKind.Name)),
ExecCmd: ctx.String(flags.Exec.Name),
ServerMode: ctx.Bool(flags.Server.Name),
IsCustomChainConfig: isCustomConfig,
}, nil
}
......
package config
import (
"math/big"
"testing"
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-program/chainconfig"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"
......@@ -12,7 +14,7 @@ import (
var (
validRollupConfig = chaincfg.Goerli
validL2Genesis = params.GoerliChainConfig
validL2Genesis = chainconfig.OPGoerliChainConfig
validL1Head = common.Hash{0xaa}
validL2Head = common.Hash{0xbb}
validL2Claim = common.Hash{0xcc}
......@@ -158,6 +160,19 @@ func TestRejectExecAndServerMode(t *testing.T) {
require.ErrorIs(t, err, ErrNoExecInServerMode)
}
func TestIsCustomChainConfig(t *testing.T) {
t.Run("nonCustom", func(t *testing.T) {
cfg := validConfig()
require.Equal(t, cfg.IsCustomChainConfig, false)
})
t.Run("custom", func(t *testing.T) {
customChainConfig := &params.ChainConfig{ChainID: big.NewInt(0x1212121212)}
cfg := NewConfig(validRollupConfig, customChainConfig, validL1Head, validL2Head, validL2OutputRoot, validL2Claim, validL2ClaimBlockNum)
require.Equal(t, cfg.IsCustomChainConfig, true)
})
}
func validConfig() *Config {
cfg := NewConfig(validRollupConfig, validL2Genesis, validL1Head, validL2Head, validL2OutputRoot, validL2Claim, validL2ClaimBlockNum)
cfg.DataDir = "/tmp/configTest"
......
......@@ -38,7 +38,15 @@ func (s *LocalPreimageSource) Get(key common.Hash) ([]byte, error) {
case l2ClaimBlockNumberKey:
return binary.BigEndian.AppendUint64(nil, s.config.L2ClaimBlockNumber), nil
case l2ChainIDKey:
return binary.BigEndian.AppendUint64(nil, client.CustomChainIDIndicator), nil
// The CustomChainIDIndicator informs the client to rely on the L2ChainConfigKey to
// read the chain config. Otherwise, it'll attempt to read a non-existent hardcoded chain config
var chainID uint64
if s.config.IsCustomChainConfig {
chainID = client.CustomChainIDIndicator
} else {
chainID = s.config.L2ChainConfig.ChainID.Uint64()
}
return binary.BigEndian.AppendUint64(nil, chainID), nil
case l2ChainConfigKey:
return json.Marshal(s.config.L2ChainConfig)
case rollupKey:
......
......@@ -7,7 +7,6 @@ import (
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
preimage "github.com/ethereum-optimism/optimism/op-preimage"
"github.com/ethereum-optimism/optimism/op-program/client"
"github.com/ethereum-optimism/optimism/op-program/host/config"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
......@@ -33,7 +32,7 @@ func TestLocalPreimageSource(t *testing.T) {
{"L2OutputRoot", l2OutputRootKey, cfg.L2OutputRoot.Bytes()},
{"L2Claim", l2ClaimKey, cfg.L2Claim.Bytes()},
{"L2ClaimBlockNumber", l2ClaimBlockNumberKey, binary.BigEndian.AppendUint64(nil, cfg.L2ClaimBlockNumber)},
{"L2ChainID", l2ChainIDKey, binary.BigEndian.AppendUint64(nil, client.CustomChainIDIndicator)},
{"L2ChainID", l2ChainIDKey, binary.BigEndian.AppendUint64(nil, cfg.L2ChainConfig.ChainID.Uint64())},
{"Rollup", rollupKey, asJson(t, cfg.Rollup)},
{"ChainConfig", l2ChainConfigKey, asJson(t, cfg.L2ChainConfig)},
{"Unknown", preimage.LocalIndexKey(1000).PreimageKey(), 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