Skip to content

Commit

Permalink
Update finality depth check headtracker
Browse files Browse the repository at this point in the history
Signed-off-by: Silas Lenihan <[email protected]>
  • Loading branch information
silaslenihan committed May 2, 2024
1 parent 3f065cf commit fc9c081
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions common/headtracker/head_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ func (ht *headTracker[HTH, S, ID, BLOCK_HASH]) handleNewHead(ctx context.Context
}
} else {
ht.log.Debugw("Got out of order head", "blockNum", head.BlockNumber(), "head", head.BlockHash(), "prevHead", prevHead.BlockNumber())
prevUnFinalizedHead := prevHead.BlockNumber() - int64(ht.config.FinalityDepth())
if head.BlockNumber() < prevUnFinalizedHead {
fmt.Println("PREVHEAD:", prevHead, head.BlockNumber())
if head.BlockNumber() <= prevHead.LatestFinalizedHead().BlockNumber() {
promOldHead.WithLabelValues(ht.chainID.String()).Inc()
ht.log.Criticalf("Got very old block with number %d (highest seen was %d). This is a problem and either means a very deep re-org occurred, one of the RPC nodes has gotten far out of sync, or the chain went backwards in block numbers. This node may not function correctly without manual intervention.", head.BlockNumber(), prevHead.BlockNumber())
ht.SvcErrBuffer.Append(errors.New("got very old block"))
Expand Down
3 changes: 3 additions & 0 deletions common/types/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ type Head[BLOCK_HASH Hashable] interface {
BlockDifficulty() *big.Int
// IsValid returns true if the head is valid.
IsValid() bool

// Returns the latest finalized based on finality tag or depth
LatestFinalizedHead() Head[BLOCK_HASH]
}
20 changes: 20 additions & 0 deletions common/types/mocks/head.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/chains/evm/headtracker/head_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestHeadTracker_New(t *testing.T) {
orm := headtracker.NewORM(cltest.FixtureChainID, db)
assert.Nil(t, orm.IdempotentInsertHead(testutils.Context(t), cltest.Head(1)))
last := cltest.Head(16)
last.IsFinalized = true
assert.Nil(t, orm.IdempotentInsertHead(testutils.Context(t), last))
assert.Nil(t, orm.IdempotentInsertHead(testutils.Context(t), cltest.Head(10)))

Expand Down
1 change: 1 addition & 0 deletions core/chains/evm/types/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (h *Head) ChainHashes() []common.Hash {

func (h *Head) LatestFinalizedHead() commontypes.Head[common.Hash] {
for h != nil && !h.IsFinalized {
fmt.Println(h.BlockNumber(), h.IsFinalized)
h = h.Parent
}

Expand Down

0 comments on commit fc9c081

Please sign in to comment.