Commit 40a70bda authored by Axel Kingsley's avatar Axel Kingsley Committed by GitHub

Eth: BlockRef (#12251)

parent a12738b7
...@@ -85,6 +85,10 @@ func (id L1BlockRef) ParentID() BlockID { ...@@ -85,6 +85,10 @@ func (id L1BlockRef) ParentID() BlockID {
} }
} }
// BlockRef is a Block Ref indepdendent of L1 or L2
// Because L1BlockRefs are strict subsets of L2BlockRefs, BlockRef is a direct alias of L1BlockRef
type BlockRef = L1BlockRef
func (id L2BlockRef) ID() BlockID { func (id L2BlockRef) ID() BlockID {
return BlockID{ return BlockID{
Hash: id.Hash, Hash: id.Hash,
......
...@@ -167,7 +167,7 @@ func (db *ChainsDB) AddLog( ...@@ -167,7 +167,7 @@ func (db *ChainsDB) AddLog(
func (db *ChainsDB) SealBlock( func (db *ChainsDB) SealBlock(
chain types.ChainID, chain types.ChainID,
block eth.L2BlockRef) error { block eth.BlockRef) error {
logDB, ok := db.logDBs[chain] logDB, ok := db.logDBs[chain]
if !ok { if !ok {
return fmt.Errorf("%w: %v", ErrUnknownChain, chain) return fmt.Errorf("%w: %v", ErrUnknownChain, chain)
......
...@@ -14,9 +14,9 @@ import ( ...@@ -14,9 +14,9 @@ import (
type SafetyIndex interface { type SafetyIndex interface {
// Updaters for the latest local safety status of each chain // Updaters for the latest local safety status of each chain
UpdateLocalUnsafe(chainID types.ChainID, ref eth.L2BlockRef) error UpdateLocalUnsafe(chainID types.ChainID, ref eth.BlockRef) error
UpdateLocalSafe(chainID types.ChainID, at eth.L1BlockRef, ref eth.L2BlockRef) error UpdateLocalSafe(chainID types.ChainID, at eth.BlockRef, ref eth.BlockRef) error
UpdateFinalizeL1(ref eth.L1BlockRef) error UpdateFinalizeL1(ref eth.BlockRef) error
// Getters for the latest safety status of each chain // Getters for the latest safety status of each chain
UnsafeL2(chainID types.ChainID) (heads.HeadPointer, error) UnsafeL2(chainID types.ChainID) (heads.HeadPointer, error)
...@@ -42,10 +42,10 @@ type safetyIndex struct { ...@@ -42,10 +42,10 @@ type safetyIndex struct {
finalized map[types.ChainID]eth.BlockID finalized map[types.ChainID]eth.BlockID
// remember what each non-finalized L2 block is derived from // remember what each non-finalized L2 block is derived from
derivedFrom map[types.ChainID]map[common.Hash]eth.L1BlockRef derivedFrom map[types.ChainID]map[common.Hash]eth.BlockRef
// the last received L1 finality signal. // the last received L1 finality signal.
finalizedL1 eth.L1BlockRef finalizedL1 eth.BlockRef
} }
func NewSafetyIndex(log log.Logger, chains ChainsDBClient) *safetyIndex { func NewSafetyIndex(log log.Logger, chains ChainsDBClient) *safetyIndex {
...@@ -55,12 +55,12 @@ func NewSafetyIndex(log log.Logger, chains ChainsDBClient) *safetyIndex { ...@@ -55,12 +55,12 @@ func NewSafetyIndex(log log.Logger, chains ChainsDBClient) *safetyIndex {
unsafe: make(map[types.ChainID]*View), unsafe: make(map[types.ChainID]*View),
safe: make(map[types.ChainID]*View), safe: make(map[types.ChainID]*View),
finalized: make(map[types.ChainID]eth.BlockID), finalized: make(map[types.ChainID]eth.BlockID),
derivedFrom: make(map[types.ChainID]map[common.Hash]eth.L1BlockRef), derivedFrom: make(map[types.ChainID]map[common.Hash]eth.BlockRef),
} }
} }
// UpdateLocalUnsafe updates the local-unsafe view for the given chain, and advances the cross-unsafe status. // UpdateLocalUnsafe updates the local-unsafe view for the given chain, and advances the cross-unsafe status.
func (r *safetyIndex) UpdateLocalUnsafe(chainID types.ChainID, ref eth.L2BlockRef) error { func (r *safetyIndex) UpdateLocalUnsafe(chainID types.ChainID, ref eth.BlockRef) error {
view, ok := r.safe[chainID] view, ok := r.safe[chainID]
if !ok { if !ok {
iter, err := r.chains.IteratorStartingAt(chainID, ref.Number, 0) iter, err := r.chains.IteratorStartingAt(chainID, ref.Number, 0)
...@@ -76,11 +76,11 @@ func (r *safetyIndex) UpdateLocalUnsafe(chainID types.ChainID, ref eth.L2BlockRe ...@@ -76,11 +76,11 @@ func (r *safetyIndex) UpdateLocalUnsafe(chainID types.ChainID, ref eth.L2BlockRe
LastSealedTimestamp: ref.Time, LastSealedTimestamp: ref.Time,
LogsSince: 0, LogsSince: 0,
}, },
localDerivedFrom: eth.L1BlockRef{}, localDerivedFrom: eth.BlockRef{},
validWithinView: r.ValidWithinUnsafeView, validWithinView: r.ValidWithinUnsafeView,
} }
r.unsafe[chainID] = view r.unsafe[chainID] = view
} else if err := view.UpdateLocal(eth.L1BlockRef{}, ref); err != nil { } else if err := view.UpdateLocal(eth.BlockRef{}, ref); err != nil {
return fmt.Errorf("failed to update local-unsafe: %w", err) return fmt.Errorf("failed to update local-unsafe: %w", err)
} }
local, _ := r.unsafe[chainID].Local() local, _ := r.unsafe[chainID].Local()
...@@ -102,7 +102,7 @@ func (r *safetyIndex) advanceCrossUnsafe() { ...@@ -102,7 +102,7 @@ func (r *safetyIndex) advanceCrossUnsafe() {
// UpdateLocalSafe updates the local-safe view for the given chain, and advances the cross-safe status. // UpdateLocalSafe updates the local-safe view for the given chain, and advances the cross-safe status.
func (r *safetyIndex) UpdateLocalSafe( func (r *safetyIndex) UpdateLocalSafe(
chainID types.ChainID, at eth.L1BlockRef, ref eth.L2BlockRef) error { chainID types.ChainID, at eth.BlockRef, ref eth.BlockRef) error {
view, ok := r.safe[chainID] view, ok := r.safe[chainID]
if !ok { if !ok {
iter, err := r.chains.IteratorStartingAt(chainID, ref.Number, 0) iter, err := r.chains.IteratorStartingAt(chainID, ref.Number, 0)
...@@ -129,7 +129,7 @@ func (r *safetyIndex) UpdateLocalSafe( ...@@ -129,7 +129,7 @@ func (r *safetyIndex) UpdateLocalSafe(
// register what this L2 block is derived from // register what this L2 block is derived from
m, ok := r.derivedFrom[chainID] m, ok := r.derivedFrom[chainID]
if !ok { if !ok {
m = make(map[common.Hash]eth.L1BlockRef) m = make(map[common.Hash]eth.BlockRef)
r.derivedFrom[chainID] = m r.derivedFrom[chainID] = m
} }
m[ref.Hash] = at m[ref.Hash] = at
...@@ -152,7 +152,7 @@ func (r *safetyIndex) advanceCrossSafe() { ...@@ -152,7 +152,7 @@ func (r *safetyIndex) advanceCrossSafe() {
} }
// UpdateFinalizeL1 updates the finalized L1 block, and advances the finalized safety status. // UpdateFinalizeL1 updates the finalized L1 block, and advances the finalized safety status.
func (r *safetyIndex) UpdateFinalizeL1(ref eth.L1BlockRef) error { func (r *safetyIndex) UpdateFinalizeL1(ref eth.BlockRef) error {
if ref.Number <= r.finalizedL1.Number { if ref.Number <= r.finalizedL1.Number {
return fmt.Errorf("ignoring old L1 finality signal of %s, already have %s", ref, r.finalizedL1) return fmt.Errorf("ignoring old L1 finality signal of %s, already have %s", ref, r.finalizedL1)
} }
......
...@@ -15,7 +15,7 @@ type View struct { ...@@ -15,7 +15,7 @@ type View struct {
iter logs.Iterator iter logs.Iterator
localView heads.HeadPointer localView heads.HeadPointer
localDerivedFrom eth.L1BlockRef localDerivedFrom eth.BlockRef
validWithinView func(l1View uint64, execMsg *types.ExecutingMessage) error validWithinView func(l1View uint64, execMsg *types.ExecutingMessage) error
} }
...@@ -31,7 +31,7 @@ func (vi *View) Local() (heads.HeadPointer, error) { ...@@ -31,7 +31,7 @@ func (vi *View) Local() (heads.HeadPointer, error) {
return vi.localView, nil return vi.localView, nil
} }
func (vi *View) UpdateLocal(at eth.L1BlockRef, ref eth.L2BlockRef) error { func (vi *View) UpdateLocal(at eth.BlockRef, ref eth.BlockRef) error {
vi.localView = heads.HeadPointer{ vi.localView = heads.HeadPointer{
LastSealedBlockHash: ref.Hash, LastSealedBlockHash: ref.Hash,
LastSealedBlockNum: ref.Number, LastSealedBlockNum: ref.Number,
......
...@@ -21,7 +21,7 @@ type Source interface { ...@@ -21,7 +21,7 @@ type Source interface {
} }
type LogProcessor interface { type LogProcessor interface {
ProcessLogs(ctx context.Context, block eth.L2BlockRef, receipts gethtypes.Receipts) error ProcessLogs(ctx context.Context, block eth.BlockRef, receipts gethtypes.Receipts) error
} }
type DatabaseRewinder interface { type DatabaseRewinder interface {
...@@ -29,9 +29,9 @@ type DatabaseRewinder interface { ...@@ -29,9 +29,9 @@ type DatabaseRewinder interface {
LatestBlockNum(chain types.ChainID) (num uint64, ok bool) LatestBlockNum(chain types.ChainID) (num uint64, ok bool)
} }
type BlockProcessorFn func(ctx context.Context, block eth.L1BlockRef) error type BlockProcessorFn func(ctx context.Context, block eth.BlockRef) error
func (fn BlockProcessorFn) ProcessBlock(ctx context.Context, block eth.L1BlockRef) error { func (fn BlockProcessorFn) ProcessBlock(ctx context.Context, block eth.BlockRef) error {
return fn(ctx, block) return fn(ctx, block)
} }
...@@ -131,7 +131,7 @@ func (s *ChainProcessor) worker() { ...@@ -131,7 +131,7 @@ func (s *ChainProcessor) worker() {
func (s *ChainProcessor) update(nextNum uint64) error { func (s *ChainProcessor) update(nextNum uint64) error {
ctx, cancel := context.WithTimeout(s.ctx, time.Second*10) ctx, cancel := context.WithTimeout(s.ctx, time.Second*10)
nextL1, err := s.client.L1BlockRefByNumber(ctx, nextNum) nextL1, err := s.client.L1BlockRefByNumber(ctx, nextNum)
next := eth.L2BlockRef{ next := eth.BlockRef{
Hash: nextL1.Hash, Hash: nextL1.Hash,
ParentHash: nextL1.ParentHash, ParentHash: nextL1.ParentHash,
Number: nextL1.Number, Number: nextL1.Number,
...@@ -166,7 +166,7 @@ func (s *ChainProcessor) update(nextNum uint64) error { ...@@ -166,7 +166,7 @@ func (s *ChainProcessor) update(nextNum uint64) error {
return nil return nil
} }
func (s *ChainProcessor) OnNewHead(ctx context.Context, head eth.L1BlockRef) error { func (s *ChainProcessor) OnNewHead(ctx context.Context, head eth.BlockRef) error {
// update the latest target // update the latest target
s.lastHead.Store(head.Number) s.lastHead.Store(head.Number)
// signal that we have something to process // signal that we have something to process
......
...@@ -15,12 +15,12 @@ import ( ...@@ -15,12 +15,12 @@ import (
) )
type LogStorage interface { type LogStorage interface {
SealBlock(chain types.ChainID, block eth.L2BlockRef) error SealBlock(chain types.ChainID, block eth.BlockRef) error
AddLog(chain types.ChainID, logHash common.Hash, parentBlock eth.BlockID, logIdx uint32, execMsg *types.ExecutingMessage) error AddLog(chain types.ChainID, logHash common.Hash, parentBlock eth.BlockID, logIdx uint32, execMsg *types.ExecutingMessage) error
} }
type ChainsDBClientForLogProcessor interface { type ChainsDBClientForLogProcessor interface {
SealBlock(chain types.ChainID, block eth.L2BlockRef) error SealBlock(chain types.ChainID, block eth.BlockRef) error
AddLog(chain types.ChainID, logHash common.Hash, parentBlock eth.BlockID, logIdx uint32, execMsg *types.ExecutingMessage) error AddLog(chain types.ChainID, logHash common.Hash, parentBlock eth.BlockID, logIdx uint32, execMsg *types.ExecutingMessage) error
} }
...@@ -44,7 +44,7 @@ func newLogProcessor(chain types.ChainID, logStore LogStorage) *logProcessor { ...@@ -44,7 +44,7 @@ func newLogProcessor(chain types.ChainID, logStore LogStorage) *logProcessor {
// ProcessLogs processes logs from a block and stores them in the log storage // ProcessLogs processes logs from a block and stores them in the log storage
// for any logs that are related to executing messages, they are decoded and stored // for any logs that are related to executing messages, they are decoded and stored
func (p *logProcessor) ProcessLogs(_ context.Context, block eth.L2BlockRef, rcpts ethTypes.Receipts) error { func (p *logProcessor) ProcessLogs(_ context.Context, block eth.BlockRef, rcpts ethTypes.Receipts) error {
for _, rcpt := range rcpts { for _, rcpt := range rcpts {
for _, l := range rcpt.Logs { for _, l := range rcpt.Logs {
// log hash represents the hash of *this* log as a potentially initiating message // log hash represents the hash of *this* log as a potentially initiating message
......
...@@ -17,7 +17,7 @@ var logProcessorChainID = types.ChainIDFromUInt64(4) ...@@ -17,7 +17,7 @@ var logProcessorChainID = types.ChainIDFromUInt64(4)
func TestLogProcessor(t *testing.T) { func TestLogProcessor(t *testing.T) {
ctx := context.Background() ctx := context.Background()
block1 := eth.L2BlockRef{ block1 := eth.BlockRef{
ParentHash: common.Hash{0x42}, ParentHash: common.Hash{0x42},
Number: 100, Number: 100,
Hash: common.Hash{0x11}, Hash: common.Hash{0x11},
...@@ -205,7 +205,7 @@ type stubLogStorage struct { ...@@ -205,7 +205,7 @@ type stubLogStorage struct {
seals []storedSeal seals []storedSeal
} }
func (s *stubLogStorage) SealBlock(chainID types.ChainID, block eth.L2BlockRef) error { func (s *stubLogStorage) SealBlock(chainID types.ChainID, block eth.BlockRef) error {
if logProcessorChainID != chainID { if logProcessorChainID != chainID {
return fmt.Errorf("chain id mismatch, expected %v but got %v", logProcessorChainID, chainID) return fmt.Errorf("chain id mismatch, expected %v but got %v", logProcessorChainID, chainID)
} }
......
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