Skip to content
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

Merged
merged 3 commits into from
Oct 16, 2023

Conversation

mateusz-sekara
Copy link
Collaborator

@mateusz-sekara mateusz-sekara commented Sep 22, 2023

LogPoller now accepts cfg.EVM().FinalityTagEnabled() and, when enabled, can also fetch information about finalized block during PollAndSave 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:

  • every LogPollerBlock has an additional attribute representing the last number of the finalized block when LogPollerBlock was persisted. This gives us the ability to ask about finalized logs using the same nested query we did so far (with slight modification)
  • every LogPoller function accepting confs can now return finalized logs. A dedicated Confirmation type was introduced for this case

Additionally:

  • 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)

@github-actions
Copy link
Contributor

I see that you haven't updated any README files. Would it make sense to do so?

@mateusz-sekara mateusz-sekara force-pushed the lp/finality-support branch 2 times, most recently from c52292c to 19545b8 Compare September 25, 2023 12:45
@mateusz-sekara mateusz-sekara force-pushed the lp/finality-support branch 4 times, most recently from 5bf960e to 0aa4094 Compare September 26, 2023 10:01
@mateusz-sekara mateusz-sekara marked this pull request as ready for review September 26, 2023 10:08
core/chains/evm/logpoller/log_poller.go Outdated Show resolved Hide resolved
core/chains/evm/logpoller/log_poller.go Outdated Show resolved Hide resolved
core/chains/evm/logpoller/log_poller.go Outdated Show resolved Hide resolved
core/chains/evm/logpoller/log_poller.go Outdated Show resolved Hide resolved
core/chains/evm/logpoller/log_poller.go Outdated Show resolved Hide resolved
core/chains/evm/logpoller/log_poller.go Outdated Show resolved Hide resolved
core/chains/evm/logpoller/log_poller.go Outdated Show resolved Hide resolved
Copy link
Contributor

@dimkouv dimkouv left a 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.

core/chains/evm/chain.go Show resolved Hide resolved
core/chains/evm/client/simulated_backend_client.go Outdated Show resolved Hide resolved
core/chains/evm/client/simulated_backend_client.go Outdated Show resolved Hide resolved
core/chains/evm/logpoller/log_poller.go Outdated Show resolved Hide resolved
core/chains/evm/logpoller/log_poller.go Outdated Show resolved Hide resolved
dimkouv
dimkouv previously approved these changes Sep 28, 2023
// 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
Copy link
Contributor

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 {

Copy link
Contributor

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?

Copy link
Collaborator Author

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?

Copy link
Collaborator Author

@mateusz-sekara mateusz-sekara Oct 9, 2023

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?

Copy link
Contributor

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

Copy link
Collaborator Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, makes sense now!

reductionista
reductionista previously approved these changes Oct 9, 2023
Copy link
Contributor

@reductionista reductionista left a 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!

connorwstein
connorwstein previously approved these changes Oct 10, 2023
@@ -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)
Copy link
Contributor

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

Copy link
Collaborator Author

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 ;)

Copy link
Contributor

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

core/chains/evm/logpoller/log_poller.go Outdated Show resolved Hide resolved
@@ -558,22 +564,16 @@ func (lp *logPoller) BackupPollAndSaveLogs(ctx context.Context, backupPollerBloc
}
return
}

finalityDepth := latestBlock.Number - lastFinalizedBlockNumber
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Collaborator Author

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

Copy link
Contributor

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧠

@mateusz-sekara mateusz-sekara force-pushed the lp/finality-support branch 3 times, most recently from 7dc5cf7 to 3f6373b Compare October 12, 2023 15:53
@cl-sonarqube-production
Copy link

evm_chain_id,
CASE
WHEN evm_chain_id = 43113 then 1 -- Avax Fuji
WHEN evm_chain_id = 43114 then 1 -- Avax Mainnet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


b.ResetTimer()

// 1. Measure time of migration 200
Copy link
Contributor

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,
Copy link
Contributor

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)
Copy link
Contributor

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 {
Copy link
Contributor

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

@mateusz-sekara mateusz-sekara added this pull request to the merge queue Oct 16, 2023
Merged via the queue into develop with commit 4519370 Oct 16, 2023
82 checks passed
@mateusz-sekara mateusz-sekara deleted the lp/finality-support branch October 16, 2023 14:07
@mateusz-sekara mateusz-sekara changed the title Support for finality tags in LogPoller CCIP-1078 Support for finality tags in LogPoller Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants