Skip to content

Commit

Permalink
Update finality depth check headtracker (#13089)
Browse files Browse the repository at this point in the history
* Update finality depth check headtracker

Signed-off-by: Silas Lenihan <sjl@lenihan.net>

* added check for nil prevLatestFinalized

* added changeset

* updated changeset

* cleaned up nil protection in LatestFinalizedHead

* Added error tuple to LatestFinalizedHead

* Added error tuple to LatestFinalizedHead

* removed error from LatestFinalizedHead

---------

Signed-off-by: Silas Lenihan <sjl@lenihan.net>
  • Loading branch information
silaslenihan committed May 20, 2024
1 parent dccbda7 commit b00ad69
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-onions-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": minor
---

#internal Switched finality check in HeadTracker to use the underlying finality type
5 changes: 3 additions & 2 deletions common/headtracker/head_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,9 @@ 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 {
prevLatestFinalized := prevHead.LatestFinalizedHead()

if prevLatestFinalized != nil && head.BlockNumber() <= prevLatestFinalized.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.

8 changes: 6 additions & 2 deletions core/chains/evm/types/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,15 @@ func (h *Head) ChainHashes() []common.Hash {
}

func (h *Head) LatestFinalizedHead() commontypes.Head[common.Hash] {
for h != nil && !h.IsFinalized {
for h != nil {
if h.IsFinalized {
return h
}

h = h.Parent
}

return h
return nil
}

func (h *Head) ChainID() *big.Int {
Expand Down
2 changes: 1 addition & 1 deletion core/internal/cltest/cltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ func AssertEthTxAttemptCountStays(t testing.TB, txStore txmgr.TestEvmTxStore, wa
return txaIds
}

// Head given the value convert it into an Head
// Head given the value convert it into a Head
func Head(val interface{}) *evmtypes.Head {
var h evmtypes.Head
time := uint64(0)
Expand Down

0 comments on commit b00ad69

Please sign in to comment.