Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eth: BlockRef #12251

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions op-service/eth/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

@geoknee geoknee Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest describing BlockRef as the "the common properties of L1 and L2 blocks", to avoid any ambiguity where we might expect it to "work" as an L2 block in all cases.

We could also consider embedding BlockRef into L2BlockRef, which could reduce duplication across a lot of the methods. But, this may result in some yak shaving (we would need to update hundreds of cases where we construct a literal L2BlockRef).

type BlockRef = L1BlockRef

func (id L2BlockRef) ID() BlockID {
return BlockID{
Hash: id.Hash,
Expand Down
2 changes: 1 addition & 1 deletion op-supervisor/supervisor/backend/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (db *ChainsDB) AddLog(

func (db *ChainsDB) SealBlock(
chain types.ChainID,
block eth.L2BlockRef) error {
block eth.BlockRef) error {
logDB, ok := db.logDBs[chain]
if !ok {
return fmt.Errorf("%w: %v", ErrUnknownChain, chain)
Expand Down
24 changes: 12 additions & 12 deletions op-supervisor/supervisor/backend/safety/safety.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

type SafetyIndex interface {
// Updaters for the latest local safety status of each chain
UpdateLocalUnsafe(chainID types.ChainID, ref eth.L2BlockRef) error
UpdateLocalSafe(chainID types.ChainID, at eth.L1BlockRef, ref eth.L2BlockRef) error
UpdateFinalizeL1(ref eth.L1BlockRef) error
UpdateLocalUnsafe(chainID types.ChainID, ref eth.BlockRef) error
UpdateLocalSafe(chainID types.ChainID, at eth.BlockRef, ref eth.BlockRef) error
UpdateFinalizeL1(ref eth.BlockRef) error

// Getters for the latest safety status of each chain
UnsafeL2(chainID types.ChainID) (heads.HeadPointer, error)
Expand All @@ -42,10 +42,10 @@ type safetyIndex struct {
finalized map[types.ChainID]eth.BlockID

// 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.
finalizedL1 eth.L1BlockRef
finalizedL1 eth.BlockRef
}

func NewSafetyIndex(log log.Logger, chains ChainsDBClient) *safetyIndex {
Expand All @@ -55,12 +55,12 @@ func NewSafetyIndex(log log.Logger, chains ChainsDBClient) *safetyIndex {
unsafe: make(map[types.ChainID]*View),
safe: make(map[types.ChainID]*View),
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.
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]
if !ok {
iter, err := r.chains.IteratorStartingAt(chainID, ref.Number, 0)
Expand All @@ -76,11 +76,11 @@ func (r *safetyIndex) UpdateLocalUnsafe(chainID types.ChainID, ref eth.L2BlockRe
LastSealedTimestamp: ref.Time,
LogsSince: 0,
},
localDerivedFrom: eth.L1BlockRef{},
localDerivedFrom: eth.BlockRef{},
validWithinView: r.ValidWithinUnsafeView,
}
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)
}
local, _ := r.unsafe[chainID].Local()
Expand All @@ -102,7 +102,7 @@ func (r *safetyIndex) advanceCrossUnsafe() {

// UpdateLocalSafe updates the local-safe view for the given chain, and advances the cross-safe status.
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]
if !ok {
iter, err := r.chains.IteratorStartingAt(chainID, ref.Number, 0)
Expand All @@ -129,7 +129,7 @@ func (r *safetyIndex) UpdateLocalSafe(
// register what this L2 block is derived from
m, ok := r.derivedFrom[chainID]
if !ok {
m = make(map[common.Hash]eth.L1BlockRef)
m = make(map[common.Hash]eth.BlockRef)
r.derivedFrom[chainID] = m
}
m[ref.Hash] = at
Expand All @@ -152,7 +152,7 @@ func (r *safetyIndex) advanceCrossSafe() {
}

// 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 {
return fmt.Errorf("ignoring old L1 finality signal of %s, already have %s", ref, r.finalizedL1)
}
Expand Down
4 changes: 2 additions & 2 deletions op-supervisor/supervisor/backend/safety/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type View struct {
iter logs.Iterator

localView heads.HeadPointer
localDerivedFrom eth.L1BlockRef
localDerivedFrom eth.BlockRef

validWithinView func(l1View uint64, execMsg *types.ExecutingMessage) error
}
Expand All @@ -31,7 +31,7 @@ func (vi *View) Local() (heads.HeadPointer, error) {
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{
LastSealedBlockHash: ref.Hash,
LastSealedBlockNum: ref.Number,
Expand Down
10 changes: 5 additions & 5 deletions op-supervisor/supervisor/backend/source/chain_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ type Source 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 {
Rewind(chain types.ChainID, headBlockNum uint64) error
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)
}

Expand Down Expand Up @@ -131,7 +131,7 @@ func (s *ChainProcessor) worker() {
func (s *ChainProcessor) update(nextNum uint64) error {
ctx, cancel := context.WithTimeout(s.ctx, time.Second*10)
nextL1, err := s.client.L1BlockRefByNumber(ctx, nextNum)
next := eth.L2BlockRef{
next := eth.BlockRef{
Hash: nextL1.Hash,
ParentHash: nextL1.ParentHash,
Number: nextL1.Number,
Expand Down Expand Up @@ -166,7 +166,7 @@ func (s *ChainProcessor) update(nextNum uint64) error {
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
s.lastHead.Store(head.Number)
// signal that we have something to process
Expand Down
6 changes: 3 additions & 3 deletions op-supervisor/supervisor/backend/source/log_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
)

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
}

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
}

Expand All @@ -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
// 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 _, l := range rcpt.Logs {
// log hash represents the hash of *this* log as a potentially initiating message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var logProcessorChainID = types.ChainIDFromUInt64(4)

func TestLogProcessor(t *testing.T) {
ctx := context.Background()
block1 := eth.L2BlockRef{
block1 := eth.BlockRef{
ParentHash: common.Hash{0x42},
Number: 100,
Hash: common.Hash{0x11},
Expand Down Expand Up @@ -205,7 +205,7 @@ type stubLogStorage struct {
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 {
return fmt.Errorf("chain id mismatch, expected %v but got %v", logProcessorChainID, chainID)
}
Expand Down