Commit 9ea1b53b authored by Andreas Bigger's avatar Andreas Bigger

fix: separation of concerns

parent 776fea22
...@@ -125,27 +125,13 @@ func (n *OpNode) initL1(ctx context.Context, cfg *Config) error { ...@@ -125,27 +125,13 @@ func (n *OpNode) initL1(ctx context.Context, cfg *Config) error {
} }
// Validate the L1 Client Chain ID // Validate the L1 Client Chain ID
id, err := n.l1Source.L1ChainID(ctx) if err := cfg.Rollup.CheckL1ChainID(ctx, n.l1Source); err != nil {
if err != nil { n.log.Error("failed to verify L1 RPC chain id", "err", err)
n.log.Warn("failed to verify L1 RPC chain id", "err", err)
} else {
if cfg.Rollup.L1ChainID != id {
n.log.Warn("incorrect L1 RPC chain id", "expected", cfg.Rollup.L1ChainID, "actual", id)
} else {
n.log.Info("L1 RPC chain id verified", "id", id)
}
} }
// Validate the Rollup L1 Genesis Blockhash // Validate the Rollup L1 Genesis Blockhash
l1GenesisBlockRef, err := n.l1Source.L1BlockRefByNumber(ctx, cfg.Rollup.Genesis.L1.Number) if err := cfg.Rollup.CheckL1GenesisBlockHash(ctx, n.l1Source); err != nil {
if err != nil { n.log.Error("failed to verify L1 genesis block hash", "err", err)
n.log.Warn("failed to validate L1 genesis block", "err", err)
} else {
if l1GenesisBlockRef.Hash != cfg.Rollup.Genesis.L1.Hash {
n.log.Warn("incorrect L1 genesis block", "expected", cfg.Rollup.Genesis.L1.Hash, "actual", l1GenesisBlockRef.Hash)
} else {
n.log.Info("L1 genesis block verified", "hash", l1GenesisBlockRef.Hash)
}
} }
// Keep subscribed to the L1 heads, which keeps the L1 maintainer pointing to the best headers to sync // Keep subscribed to the L1 heads, which keeps the L1 maintainer pointing to the best headers to sync
...@@ -214,27 +200,13 @@ func (n *OpNode) initL2(ctx context.Context, cfg *Config, snapshotLog log.Logger ...@@ -214,27 +200,13 @@ func (n *OpNode) initL2(ctx context.Context, cfg *Config, snapshotLog log.Logger
} }
// Validate the L2 Client Chain ID // Validate the L2 Client Chain ID
id, err := n.l2Source.L2ChainID(ctx) if err := cfg.Rollup.CheckL2ChainID(ctx, n.l2Source); err != nil {
if err != nil { n.log.Error("failed to verify L2 RPC chain id", "err", err)
n.log.Warn("failed to verify L2 RPC chain id", "err", err)
} else {
if cfg.Rollup.L2ChainID != id {
n.log.Warn("incorrect L2 RPC chain id", "expected", cfg.Rollup.L2ChainID, "actual", id)
} else {
n.log.Info("L2 RPC chain id verified", "id", id)
}
} }
// Validate the Rollup L2 Genesis Blockhash // Validate the Rollup L2 Genesis Blockhash
l2GenesisBlockRef, err := n.l2Source.L2BlockRefByNumber(ctx, cfg.Rollup.Genesis.L2.Number) if err := cfg.Rollup.CheckL2GenesisBlockHash(ctx, n.l2Source); err != nil {
if err != nil { n.log.Error("failed to verify L2 genesis block hash", "err", err)
n.log.Warn("failed to validate L2 genesis block", "err", err)
} else {
if l2GenesisBlockRef.Hash != cfg.Rollup.Genesis.L2.Hash {
n.log.Warn("incorrect L2 genesis block", "expected", cfg.Rollup.Genesis.L2.Hash, "actual", l2GenesisBlockRef.Hash)
} else {
n.log.Info("L2 genesis block verified", "hash", l2GenesisBlockRef.Hash)
}
} }
n.l2Driver = driver.NewDriver(&cfg.Driver, &cfg.Rollup, n.l2Source, n.l1Source, n, n.log, snapshotLog, n.metrics) n.l2Driver = driver.NewDriver(&cfg.Driver, &cfg.Rollup, n.l2Source, n.l1Source, n, n.log, snapshotLog, n.metrics)
......
package rollup package rollup
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
...@@ -55,6 +56,64 @@ type Config struct { ...@@ -55,6 +56,64 @@ type Config struct {
L1SystemConfigAddress common.Address `json:"l1_system_config_address"` L1SystemConfigAddress common.Address `json:"l1_system_config_address"`
} }
type L1Client interface {
L1ChainID(context.Context) (*big.Int, error)
L1BlockRefByNumber(context.Context, uint64) (eth.L1BlockRef, error)
}
// CheckL1ChainID checks that the configured L1 chain ID matches the client's chain ID.
func (cfg *Config) CheckL1ChainID(ctx context.Context, client L1Client) error {
id, err := client.L1ChainID(ctx)
if err != nil {
return err
}
if cfg.L1ChainID != id {
return fmt.Errorf("incorrect L1 RPC chain id %d, expected %d", cfg.L1ChainID, id)
}
return nil
}
// CheckL1GenesisBlockHash checks that the configured L1 genesis block hash is valid for the given client.
func (cfg *Config) CheckL1GenesisBlockHash(ctx context.Context, client L1Client) error {
l1GenesisBlockRef, err := client.L1BlockRefByNumber(ctx, cfg.Genesis.L1.Number)
if err != nil {
return err
}
if l1GenesisBlockRef.Hash != cfg.Genesis.L1.Hash {
return fmt.Errorf("incorrect L1 genesis block hash %d, expected %d", cfg.Genesis.L1.Hash, l1GenesisBlockRef.Hash)
}
return nil
}
type L2Client interface {
L2ChainID(context.Context) (*big.Int, error)
L2BlockRefByNumber(context.Context, uint64) (eth.L2BlockRef, error)
}
// CheckL2ChainID checks that the configured L2 chain ID matches the client's chain ID.
func (cfg *Config) CheckL2ChainID(ctx context.Context, client L2Client) error {
id, err := client.L2ChainID(ctx)
if err != nil {
return err
}
if cfg.L2ChainID != id {
return fmt.Errorf("incorrect L2 RPC chain id %d, expected %d", cfg.L2ChainID, id)
}
return nil
}
// CheckL2GenesisBlockHash checks that the configured L2 genesis block hash is valid for the given client.
func (cfg *Config) CheckL2GenesisBlockHash(ctx context.Context, client L2Client) error {
l2GenesisBlockRef, err := client.L2BlockRefByNumber(ctx, cfg.Genesis.L2.Number)
if err != nil {
return err
}
if l2GenesisBlockRef.Hash != cfg.Genesis.L2.Hash {
return fmt.Errorf("incorrect L2 genesis block hash %d, expected %d", cfg.Genesis.L2.Hash, l2GenesisBlockRef.Hash)
}
return nil
}
// Check verifies that the given configuration makes sense // Check verifies that the given configuration makes sense
func (cfg *Config) Check() error { func (cfg *Config) Check() error {
if cfg.BlockTime == 0 { if cfg.BlockTime == 0 {
......
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