-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
CCIP-1078 Support for finality tags in LogPoller #10762
Conversation
I see that you haven't updated any README files. Would it make sense to do so? |
c52292c
to
19545b8
Compare
5bf960e
to
0aa4094
Compare
38cf585
to
1d71390
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall logic, looks nice. I added a few comments about the code. Let's address some of them before merging.
df4b28d
to
4363421
Compare
4363421
to
36d562b
Compare
b50059b
to
36d01f5
Compare
// Current is where the mismatch starts. | ||
// Check its parent to see if its the same as ours saved. | ||
parent, err := lp.ec.HeadByHash(ctx, current.ParentHash) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// If finalityTag is not used, we need to compute lastFinalizedBlockNumber based on the configured finalityDepth and the parent block | ||
if !lp.useFinalityTag { | ||
lastFinalizedBlockNumber = parent.Number - lp.finalityDepth |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be:
lastFinalizedBlockNumber = current.Number - lp.finalityDepth
and then below we want:
for parent.Number >= lastFinalizedBlockNumber - 1 {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least, that's what would keep present logic the same... although it doesn't seem consistent with this comment:
// If the parent block number becomes < the first finalized block our reorg is too deep.
which would indicate to me either the comment was wrong or the current logic is 1 block safer than we need?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, what we are trying to do here is to find the first block (after the last finalized block) that matches our state in the database. Worst case scenario, we should return the last finalized block, so the loop should terminate when parent.Number < lastFinalizedBlockNumber
, which matches current implementation
I think this should be:
lastFinalizedBlockNumber = current.Number - lp.finalityDepth
and then below we want:
for parent.Number >= lastFinalizedBlockNumber - 1 {
I think that parent.Number - lp.finalityDepth
is actually current.Number - lp.finalityDepth - 1
(because parent is one block behind), so it kind of matches current logic, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although, I'm not sure why we did compute max reorg depth based on the parent
block, not the current
in the previous implementation
for parent.Number >= (reorgStart - lp.finalityDepth) // reorgStart = parent.Number
Was that accidental?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was that accidental
yeah looks to me like its one block extra of safety for no reason
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed extra block, it required increasing finality depth in tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, makes sense now!
4bb0671
to
4f172a0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think all the outstanding issues have been resolved, LGTM!
06e62a5
to
7de8bd4
Compare
@@ -497,21 +498,20 @@ 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. | |||
latest, err := lp.ec.HeadByNumber(lp.ctx, nil) | |||
latestBlock, lastFinalizedBlockNumber, err := lp.latestBlocks(lp.ctx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: prefer latestFinalizedBlockNumber to lastFinalizedBlockNumber, more clear and aligned with latestBlock
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about the database field? Do you have any suggestions? Should we keep it last_finalized_block_number
or change to latest
, or maybe only finalized_block_number
? This is the last time we can easily change it ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I like just finalized_block_number
@@ -558,22 +564,16 @@ func (lp *logPoller) BackupPollAndSaveLogs(ctx context.Context, backupPollerBloc | |||
} | |||
return | |||
} | |||
|
|||
finalityDepth := latestBlock.Number - lastFinalizedBlockNumber |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is incorrect. Say we crash with finality depth 10, then reboot and latest finality depth is 5. Ignoring backup delay, we'll set backup poll to 5 when it should be 10. So we'll miss backup polling 5 blocks. Simple solution : use lastProcessed.LastFinalizedBlock number instead. I think whole thing can be simplified:
lp.backupPollerNextBlock = mathutil.Min(lastProcessed.LastFinalizedBlock + 1, lastProcessed.Number - backupPollerBlockDelay)
Can move the latest query back to where it was that way as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, good catch!
It looks like the existing logic is equivalent to mathutil.Min(lastProcessed.LastFinalizedBlock - 1, lastProcessed.Number - backupPollerBlockDelay)
though. Was it off by 2 blocks? Instead of starting at the block before last finalized, now we'd be using the block after the last finalized.
I think that sounds right, just wanted to confirm that this is an intentional fix of another case where the production code was "more safe than necessary", rather than keeping functionality the same and just refactoring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, you are right @connorwstein. Btw, we should have more tests that verify edge cases like these, I think the problem is also present in the current develop
, as we subtract finalityDepth
from the current tip of the blockchain
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right @reductionista. Let's keep logic as is with starting from finalized - 1. The benefit is just consistency: everywhere we backfill, we backfill up to finalized - 1 (we need the finalized block itself to be able to reorg check on finalized+1).
@@ -735,7 +735,7 @@ func (lp *logPoller) getCurrentBlockMaybeHandleReorg(ctx context.Context, curren | |||
// There can be another reorg while we're finding the LCA. | |||
// That is ok, since we'll detect it on the next iteration. | |||
// Since we go currentBlock by currentBlock for unfinalized logs, the mismatch starts at currentBlockNumber - 1. | |||
blockAfterLCA, err2 := lp.findBlockAfterLCA(ctx, currentBlock) | |||
blockAfterLCA, err2 := lp.findBlockAfterLCA(ctx, currentBlock, expectedParent.LastFinalizedBlockNumber) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧠
31b399f
to
51467fc
Compare
7dc5cf7
to
3f6373b
Compare
3f6373b
to
a2cd824
Compare
SonarQube Quality Gate |
evm_chain_id, | ||
CASE | ||
WHEN evm_chain_id = 43113 then 1 -- Avax Fuji | ||
WHEN evm_chain_id = 43114 then 1 -- Avax Mainnet |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be worth including https://github.com/smartcontractkit/ccip/blob/ccip-develop/core/chains/evm/config/toml/defaults/Avalanche_ANZ_testnet.toml#L2 as well, though its just a testnet so not a big deal.
|
||
b.ResetTimer() | ||
|
||
// 1. Measure time of migration 200 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
202?
{ | ||
name: "chain finality in use", | ||
finalityDepth: 0, | ||
finalityTag: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥇
require.Equal(t, uint64(10), b.Time()) | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
th := SetupTH(t, tt.finalityTag, tt.finalityDepth, 3, 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: could pull this into its own function to reduce nesting, ditto for poll and save logs test
@@ -122,21 +127,22 @@ type logPoller struct { | |||
// How fast that can be done depends largely on network speed and DB, but even for the fastest | |||
// support chain, polygon, which has 2s block times, we need RPCs roughly with <= 500ms latency | |||
func NewLogPoller(orm ORM, ec Client, lggr logger.Logger, pollPeriod time.Duration, | |||
finalityDepth int64, backfillBatchSize int64, rpcBatchSize int64, keepBlocksDepth int64) *logPoller { | |||
useFinalityTag bool, finalityDepth int64, backfillBatchSize int64, rpcBatchSize int64, keepFinalizedBlocksDepth int64) *logPoller { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should comment somewhere that useFinalityTag=true overrides the finalityDepth parameter
LogPoller now accepts
cfg.EVM().FinalityTagEnabled()
and, when enabled, can also fetch information about finalized block duringPollAndSave
step. This allows us to use chain finality instead of the opinionated value of finality depth without introducing many changes to the LogPoller API and logic.A high-level overview of the changes introduced:
LogPollerBlock
has an additional attribute representing the last number of the finalized block whenLogPollerBlock
was persisted. This gives us the ability to ask about finalized logs using the same nested query we did so far (with slight modification)confs
can now return finalized logs. A dedicatedConfirmation
type was introduced for this caseAdditionally:
LogsUntilBlockHashDataWordGreaterThan
is removed because it's no longer needed (this was used on the CCIP end to get finalized logs based on the last finalized block hash)