Skip to content

Commit

Permalink
op-program: Only allow requests for rollup config and l2 chain config…
Browse files Browse the repository at this point in the history
… for custom chains (ethereum-optimism#12420)
  • Loading branch information
ajsutton authored Oct 11, 2024
1 parent d470c77 commit 18732db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions op-program/host/kvstore/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ func (s *LocalPreimageSource) Get(key common.Hash) ([]byte, error) {
}
return binary.BigEndian.AppendUint64(nil, chainID), nil
case l2ChainConfigKey:
if !s.config.IsCustomChainConfig {
return nil, ErrNotFound
}
return json.Marshal(s.config.L2ChainConfig)
case rollupKey:
if !s.config.IsCustomChainConfig {
return nil, ErrNotFound
}
return json.Marshal(s.config.Rollup)
default:
return nil, ErrNotFound
Expand Down
23 changes: 21 additions & 2 deletions op-program/host/kvstore/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestLocalPreimageSource(t *testing.T) {
{"L2Claim", l2ClaimKey, cfg.L2Claim.Bytes()},
{"L2ClaimBlockNumber", l2ClaimBlockNumberKey, binary.BigEndian.AppendUint64(nil, cfg.L2ClaimBlockNumber)},
{"L2ChainID", l2ChainIDKey, binary.BigEndian.AppendUint64(nil, cfg.L2ChainConfig.ChainID.Uint64())},
{"Rollup", rollupKey, asJson(t, cfg.Rollup)},
{"ChainConfig", l2ChainConfigKey, asJson(t, cfg.L2ChainConfig)},
{"Rollup", rollupKey, nil}, // Only available for custom chain configs
{"ChainConfig", l2ChainConfigKey, nil}, // Only available for custom chain configs
{"Unknown", preimage.LocalIndexKey(1000).PreimageKey(), nil},
}
for _, test := range tests {
Expand All @@ -50,6 +50,25 @@ func TestLocalPreimageSource(t *testing.T) {
}
}

func TestGetCustomChainConfigPreimages(t *testing.T) {
cfg := &config.Config{
Rollup: chaincfg.OPSepolia(),
IsCustomChainConfig: true,
L1Head: common.HexToHash("0x1111"),
L2OutputRoot: common.HexToHash("0x2222"),
L2Claim: common.HexToHash("0x3333"),
L2ClaimBlockNumber: 1234,
L2ChainConfig: params.SepoliaChainConfig,
}
source := NewLocalPreimageSource(cfg)
actualRollup, err := source.Get(rollupKey)
require.NoError(t, err)
require.Equal(t, asJson(t, cfg.Rollup), actualRollup)
actualChainConfig, err := source.Get(l2ChainConfigKey)
require.NoError(t, err)
require.Equal(t, asJson(t, cfg.L2ChainConfig), actualChainConfig)
}

func asJson(t *testing.T, v any) []byte {
d, err := json.Marshal(v)
require.NoError(t, err)
Expand Down

0 comments on commit 18732db

Please sign in to comment.