Skip to content

Commit

Permalink
Fix LatestFinalizedBlock=0 bug (#BCI-3618) (#13861)
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista authored Jul 17, 2024
1 parent 9aef8d5 commit b3c93a7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-rings-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": minor
---

#bugfix Set LatestFinalizedBlock for finalized blocks saved by logpoller
11 changes: 6 additions & 5 deletions core/chains/evm/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1276,11 +1276,12 @@ func (lp *logPoller) fillRemainingBlocksFromRPC(
logPollerBlocks := make(map[uint64]LogPollerBlock)
for _, head := range evmBlocks {
logPollerBlocks[uint64(head.Number)] = LogPollerBlock{
EvmChainId: head.EVMChainID,
BlockHash: head.Hash,
BlockNumber: head.Number,
BlockTimestamp: head.Timestamp,
CreatedAt: head.Timestamp,
EvmChainId: head.EVMChainID,
BlockHash: head.Hash,
BlockNumber: head.Number,
BlockTimestamp: head.Timestamp,
FinalizedBlockNumber: head.Number, // always finalized; only matters if this block is returned by LatestBlock()
CreatedAt: head.Timestamp,
}
}
return logPollerBlocks, nil
Expand Down
5 changes: 5 additions & 0 deletions core/chains/evm/logpoller/log_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,7 @@ func TestLogPoller_GetBlocks_Range(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, 1, len(blocks))
assert.Equal(t, 1, int(blocks[0].BlockNumber))
assert.Equal(t, 1, int(blocks[0].FinalizedBlockNumber))

// LP fails to return block 2 because it hasn't been finalized yet
blockNums = []uint64{2}
Expand All @@ -1360,6 +1361,7 @@ func TestLogPoller_GetBlocks_Range(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, 1, len(rpcBlocks))
assert.Equal(t, 2, int(rpcBlocks[0].BlockNumber))
assert.Equal(t, 2, int(rpcBlocks[0].FinalizedBlockNumber))

th.Client.Commit() // commit block #5 so that #3 becomes finalized

Expand All @@ -1374,6 +1376,7 @@ func TestLogPoller_GetBlocks_Range(t *testing.T) {
assert.Equal(t, 2, len(rpcBlocks2))
assert.Equal(t, 1, int(rpcBlocks2[0].BlockNumber))
assert.Equal(t, 3, int(rpcBlocks2[1].BlockNumber))
assert.Equal(t, 3, int(rpcBlocks2[1].FinalizedBlockNumber))

// after calling PollAndSaveLogs, block 3 (latest finalized block) is persisted in DB
th.LogPoller.PollAndSaveLogs(testutils.Context(t), 1)
Expand All @@ -1387,6 +1390,7 @@ func TestLogPoller_GetBlocks_Range(t *testing.T) {
assert.Equal(t, 1, len(lpBlocks))
assert.Equal(t, rpcBlocks[0].BlockNumber, lpBlocks[0].BlockNumber)
assert.Equal(t, rpcBlocks[0].BlockHash, lpBlocks[0].BlockHash)
assert.Equal(t, rpcBlocks[0].FinalizedBlockNumber, lpBlocks[0].FinalizedBlockNumber)

// getBlocksRange return multiple blocks
blockNums = []uint64{1, 2}
Expand All @@ -1396,6 +1400,7 @@ func TestLogPoller_GetBlocks_Range(t *testing.T) {
assert.NotEmpty(t, blocks[0].BlockHash)
assert.Equal(t, 2, int(blocks[1].BlockNumber))
assert.NotEmpty(t, blocks[1].BlockHash)
assert.Equal(t, 2, int(blocks[1].FinalizedBlockNumber))

// getBlocksRange return blocks in requested order
blockNums = []uint64{2, 1}
Expand Down

0 comments on commit b3c93a7

Please sign in to comment.