Skip to content

Commit

Permalink
BCFR-888 LP support chains that have not reached finality yet (#14366)
Browse files Browse the repository at this point in the history
* LP support chains that have not reached finality yet

* changelog

* fix test

* ensure Test_ContractTransmitter_TransmitWithoutSignatures do not run in parallel to prevent db deadlock
  • Loading branch information
dhaidashenko authored Sep 23, 2024
1 parent 5acca37 commit 27d5cbf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-goats-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

LogPoller polls logs even if chain have not reached finality #internal
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func Test_ContractTransmitter_TransmitWithoutSignatures(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc := tc
t.Parallel()
testTransmitter(t, tc.pluginType, tc.withSigs, tc.expectedSigsEnabled, tc.report)
})
}
Expand Down
21 changes: 11 additions & 10 deletions core/chains/evm/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,18 +598,11 @@ func (lp *logPoller) run() {
}
// Otherwise this is the first poll _ever_ on a new chain.
// Only safe thing to do is to start at the first finalized block.
latestBlock, latestFinalizedBlockNumber, err := lp.latestBlocks(ctx)
_, latestFinalizedBlockNumber, err := lp.latestBlocks(ctx)
if err != nil {
lp.lggr.Warnw("Unable to get latest for first poll", "err", err)
continue
}
// Do not support polling chains which don't even have finality depth worth of blocks.
// Could conceivably support this but not worth the effort.
// Need last finalized block number to be higher than 0
if latestFinalizedBlockNumber <= 0 {
lp.lggr.Warnw("Insufficient number of blocks on chain, waiting for finality depth", "err", err, "latest", latestBlock.Number)
continue
}
// Starting at the first finalized block. We do not backfill the first finalized block.
start = latestFinalizedBlockNumber
} else {
Expand Down Expand Up @@ -1023,8 +1016,16 @@ func (lp *logPoller) latestBlocks(ctx context.Context) (*evmtypes.Head, int64, e
return nil, 0, fmt.Errorf("failed to get latest and latest finalized block from HeadTracker: %w", err)
}

lp.lggr.Debugw("Latest blocks read from chain", "latest", latest.Number, "finalized", finalized.BlockNumber())
return latest, finalized.BlockNumber(), nil
finalizedBN := finalized.BlockNumber()
// This is a dirty trick that allows LogPoller to function properly in tests where chain needs significant time to
// reach finality depth. An alternative to this one-liner is a database migration that drops restriction
// LogPollerBlock.FinalizedBlockNumber > 0 (which we actually want to keep to spot cases when FinalizedBlockNumber was simply not populated)
// and refactoring of queries that assume that restriction still holds.
if finalizedBN == 0 {
finalizedBN = 1
}
lp.lggr.Debugw("Latest blocks read from chain", "latest", latest.Number, "finalized", finalizedBN)
return latest, finalizedBN, nil
}

// Find the first place where our chain and their chain have the same block,
Expand Down
2 changes: 1 addition & 1 deletion core/chains/evm/logpoller/log_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,7 @@ func Test_PollAndSavePersistsFinalityInBlocks(t *testing.T) {
name: "setting last finalized block number to 0 if finality is too deep",
useFinalityTag: false,
finalityDepth: 20,
expectedFinalizedBlock: 0,
expectedFinalizedBlock: 1,
},
{
name: "using finality from chain",
Expand Down

0 comments on commit 27d5cbf

Please sign in to comment.