Commit 05d376ee authored by Andreas Bigger's avatar Andreas Bigger

fix: more nits 🐛

parent ab90a101
...@@ -124,15 +124,7 @@ func (n *OpNode) initL1(ctx context.Context, cfg *Config) error { ...@@ -124,15 +124,7 @@ func (n *OpNode) initL1(ctx context.Context, cfg *Config) error {
return fmt.Errorf("failed to create L1 source: %w", err) return fmt.Errorf("failed to create L1 source: %w", err)
} }
// Validate the L1 Client Chain ID if err := cfg.Rollup.ValidateL1Config(ctx, n.l1Source); err != nil {
if err := cfg.Rollup.CheckL1ChainID(ctx, n.l1Source); err != nil {
n.log.Error("failed to verify L1 RPC chain id", "err", err)
return err
}
// Validate the Rollup L1 Genesis Blockhash
if err := cfg.Rollup.CheckL1GenesisBlockHash(ctx, n.l1Source); err != nil {
n.log.Error("failed to verify L1 genesis block hash", "err", err)
return err return err
} }
...@@ -201,15 +193,7 @@ func (n *OpNode) initL2(ctx context.Context, cfg *Config, snapshotLog log.Logger ...@@ -201,15 +193,7 @@ func (n *OpNode) initL2(ctx context.Context, cfg *Config, snapshotLog log.Logger
return fmt.Errorf("failed to create Engine client: %w", err) return fmt.Errorf("failed to create Engine client: %w", err)
} }
// Validate the L2 Client Chain ID if err := cfg.Rollup.ValidateL2Config(ctx, n.l2Source); err != nil {
if err := cfg.Rollup.CheckL2ChainID(ctx, n.l2Source); err != nil {
n.log.Error("failed to verify L2 RPC chain id", "err", err)
return err
}
// Validate the Rollup L2 Genesis Blockhash
if err := cfg.Rollup.CheckL2GenesisBlockHash(ctx, n.l2Source); err != nil {
n.log.Error("failed to verify L2 genesis block hash", "err", err)
return err return err
} }
......
...@@ -56,14 +56,44 @@ type Config struct { ...@@ -56,14 +56,44 @@ type Config struct {
L1SystemConfigAddress common.Address `json:"l1_system_config_address"` L1SystemConfigAddress common.Address `json:"l1_system_config_address"`
} }
// ValidateL1Config checks L1 config variables for errors.
func (cfg *Config) ValidateL1Config(ctx context.Context, client L1Client) error {
// Validate the L1 Client Chain ID
if err := cfg.CheckL1ChainID(ctx, client); err != nil {
return err
}
// Validate the Rollup L1 Genesis Blockhash
if err := cfg.CheckL1GenesisBlockHash(ctx, client); err != nil {
return err
}
return nil
}
// ValidateL2Config checks L2 config variables for errors.
func (cfg *Config) ValidateL2Config(ctx context.Context, client L2Client) error {
// Validate the L2 Client Chain ID
if err := cfg.CheckL2ChainID(ctx, client); err != nil {
return err
}
// Validate the Rollup L2 Genesis Blockhash
if err := cfg.CheckL2GenesisBlockHash(ctx, client); err != nil {
return err
}
return nil
}
type L1Client interface { type L1Client interface {
L1ChainID(context.Context) (*big.Int, error) ChainID(context.Context) (*big.Int, error)
L1BlockRefByNumber(context.Context, uint64) (eth.L1BlockRef, error) L1BlockRefByNumber(context.Context, uint64) (eth.L1BlockRef, error)
} }
// CheckL1ChainID checks that the configured L1 chain ID matches the client's chain ID. // CheckL1ChainID checks that the configured L1 chain ID matches the client's chain ID.
func (cfg *Config) CheckL1ChainID(ctx context.Context, client L1Client) error { func (cfg *Config) CheckL1ChainID(ctx context.Context, client L1Client) error {
id, err := client.L1ChainID(ctx) id, err := client.ChainID(ctx)
if err != nil { if err != nil {
return err return err
} }
...@@ -86,13 +116,13 @@ func (cfg *Config) CheckL1GenesisBlockHash(ctx context.Context, client L1Client) ...@@ -86,13 +116,13 @@ func (cfg *Config) CheckL1GenesisBlockHash(ctx context.Context, client L1Client)
} }
type L2Client interface { type L2Client interface {
L2ChainID(context.Context) (*big.Int, error) ChainID(context.Context) (*big.Int, error)
L2BlockRefByNumber(context.Context, uint64) (eth.L2BlockRef, error) L2BlockRefByNumber(context.Context, uint64) (eth.L2BlockRef, error)
} }
// CheckL2ChainID checks that the configured L2 chain ID matches the client's chain ID. // CheckL2ChainID checks that the configured L2 chain ID matches the client's chain ID.
func (cfg *Config) CheckL2ChainID(ctx context.Context, client L2Client) error { func (cfg *Config) CheckL2ChainID(ctx context.Context, client L2Client) error {
id, err := client.L2ChainID(ctx) id, err := client.ChainID(ctx)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -62,7 +62,7 @@ type mockL1Client struct { ...@@ -62,7 +62,7 @@ type mockL1Client struct {
Hash common.Hash Hash common.Hash
} }
func (m *mockL1Client) L1ChainID(context.Context) (*big.Int, error) { func (m *mockL1Client) ChainID(context.Context) (*big.Int, error) {
return m.chainID, nil return m.chainID, nil
} }
...@@ -73,6 +73,42 @@ func (m *mockL1Client) L1BlockRefByNumber(ctx context.Context, number uint64) (e ...@@ -73,6 +73,42 @@ func (m *mockL1Client) L1BlockRefByNumber(ctx context.Context, number uint64) (e
}, nil }, nil
} }
func TestValidateL1Config(t *testing.T) {
config := randConfig()
config.L1ChainID = big.NewInt(100)
config.Genesis.L1.Number = 100
config.Genesis.L1.Hash = [32]byte{0x01}
mockClient := mockL1Client{chainID: big.NewInt(100), Hash: common.Hash{0x01}}
err := config.ValidateL1Config(context.TODO(), &mockClient)
assert.NoError(t, err)
}
func TestValidateL1ConfigInvalidChainIdFails(t *testing.T) {
config := randConfig()
config.L1ChainID = big.NewInt(101)
config.Genesis.L1.Number = 100
config.Genesis.L1.Hash = [32]byte{0x01}
mockClient := mockL1Client{chainID: big.NewInt(100), Hash: common.Hash{0x01}}
err := config.ValidateL1Config(context.TODO(), &mockClient)
assert.Error(t, err)
config.L1ChainID = big.NewInt(99)
err = config.ValidateL1Config(context.TODO(), &mockClient)
assert.Error(t, err)
}
func TestValidateL1ConfigInvalidGenesisHashFails(t *testing.T) {
config := randConfig()
config.L1ChainID = big.NewInt(100)
config.Genesis.L1.Number = 100
config.Genesis.L1.Hash = [32]byte{0x00}
mockClient := mockL1Client{chainID: big.NewInt(100), Hash: common.Hash{0x01}}
err := config.ValidateL1Config(context.TODO(), &mockClient)
assert.Error(t, err)
config.Genesis.L1.Hash = [32]byte{0x02}
err = config.ValidateL1Config(context.TODO(), &mockClient)
assert.Error(t, err)
}
func TestCheckL1ChainID(t *testing.T) { func TestCheckL1ChainID(t *testing.T) {
config := randConfig() config := randConfig()
config.L1ChainID = big.NewInt(100) config.L1ChainID = big.NewInt(100)
...@@ -104,7 +140,7 @@ type mockL2Client struct { ...@@ -104,7 +140,7 @@ type mockL2Client struct {
Hash common.Hash Hash common.Hash
} }
func (m *mockL2Client) L2ChainID(context.Context) (*big.Int, error) { func (m *mockL2Client) ChainID(context.Context) (*big.Int, error) {
return m.chainID, nil return m.chainID, nil
} }
...@@ -115,6 +151,42 @@ func (m *mockL2Client) L2BlockRefByNumber(ctx context.Context, number uint64) (e ...@@ -115,6 +151,42 @@ func (m *mockL2Client) L2BlockRefByNumber(ctx context.Context, number uint64) (e
}, nil }, nil
} }
func TestValidateL2Config(t *testing.T) {
config := randConfig()
config.L2ChainID = big.NewInt(100)
config.Genesis.L2.Number = 100
config.Genesis.L2.Hash = [32]byte{0x01}
mockClient := mockL2Client{chainID: big.NewInt(100), Hash: common.Hash{0x01}}
err := config.ValidateL2Config(context.TODO(), &mockClient)
assert.NoError(t, err)
}
func TestValidateL2ConfigInvalidChainIdFails(t *testing.T) {
config := randConfig()
config.L2ChainID = big.NewInt(101)
config.Genesis.L2.Number = 100
config.Genesis.L2.Hash = [32]byte{0x01}
mockClient := mockL2Client{chainID: big.NewInt(100), Hash: common.Hash{0x01}}
err := config.ValidateL2Config(context.TODO(), &mockClient)
assert.Error(t, err)
config.L2ChainID = big.NewInt(99)
err = config.ValidateL2Config(context.TODO(), &mockClient)
assert.Error(t, err)
}
func TestValidateL2ConfigInvalidGenesisHashFails(t *testing.T) {
config := randConfig()
config.L2ChainID = big.NewInt(100)
config.Genesis.L2.Number = 100
config.Genesis.L2.Hash = [32]byte{0x00}
mockClient := mockL2Client{chainID: big.NewInt(100), Hash: common.Hash{0x01}}
err := config.ValidateL2Config(context.TODO(), &mockClient)
assert.Error(t, err)
config.Genesis.L2.Hash = [32]byte{0x02}
err = config.ValidateL2Config(context.TODO(), &mockClient)
assert.Error(t, err)
}
func TestCheckL2ChainID(t *testing.T) { func TestCheckL2ChainID(t *testing.T) {
config := randConfig() config := randConfig()
config.L2ChainID = big.NewInt(100) config.L2ChainID = big.NewInt(100)
......
...@@ -3,7 +3,6 @@ package sources ...@@ -3,7 +3,6 @@ package sources
import ( import (
"context" "context"
"fmt" "fmt"
"math/big"
"strings" "strings"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
...@@ -69,11 +68,6 @@ func NewL1Client(client client.RPC, log log.Logger, metrics caching.Metrics, con ...@@ -69,11 +68,6 @@ func NewL1Client(client client.RPC, log log.Logger, metrics caching.Metrics, con
}, nil }, nil
} }
// L1ChainID fetches the chain id for the RPC Client.
func (s *L1Client) L1ChainID(ctx context.Context) (*big.Int, error) {
return s.ChainID(ctx)
}
// L1BlockRefByLabel returns the [eth.L1BlockRef] for the given block label. // L1BlockRefByLabel returns the [eth.L1BlockRef] for the given block label.
// Notice, we cannot cache a block reference by label because labels are not guaranteed to be unique. // Notice, we cannot cache a block reference by label because labels are not guaranteed to be unique.
func (s *L1Client) L1BlockRefByLabel(ctx context.Context, label eth.BlockLabel) (eth.L1BlockRef, error) { func (s *L1Client) L1BlockRefByLabel(ctx context.Context, label eth.BlockLabel) (eth.L1BlockRef, error) {
......
...@@ -3,7 +3,6 @@ package sources ...@@ -3,7 +3,6 @@ package sources
import ( import (
"context" "context"
"fmt" "fmt"
"math/big"
"strings" "strings"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
...@@ -88,11 +87,6 @@ func NewL2Client(client client.RPC, log log.Logger, metrics caching.Metrics, con ...@@ -88,11 +87,6 @@ func NewL2Client(client client.RPC, log log.Logger, metrics caching.Metrics, con
}, nil }, nil
} }
// L2ChainID fetches the chain id for the RPC Client.
func (s *L2Client) L2ChainID(ctx context.Context) (*big.Int, error) {
return s.ChainID(ctx)
}
// L2BlockRefByLabel returns the L2 block reference for the given label. // L2BlockRefByLabel returns the L2 block reference for the given label.
// L2BlockRefByLabel returns the [eth.L2BlockRef] for the given block label. // L2BlockRefByLabel returns the [eth.L2BlockRef] for the given block label.
func (s *L2Client) L2BlockRefByLabel(ctx context.Context, label eth.BlockLabel) (eth.L2BlockRef, error) { func (s *L2Client) L2BlockRefByLabel(ctx context.Context, label eth.BlockLabel) (eth.L2BlockRef, error) {
......
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