-
Notifications
You must be signed in to change notification settings - Fork 13
(Requires Audit) PLEX-2473 LogPoller Fix Reorg Handing on Replay (Part 1) #355
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
Open
dhaidashenko
wants to merge
3
commits into
develop
Choose a base branch
from
feature/PLEX-2473-fix-reorg-handing-on-replay
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -76,7 +76,7 @@ type LogPoller interface { | |||||||
|
|
||||||||
| type LogPollerTest interface { | ||||||||
| LogPoller | ||||||||
| PollAndSaveLogs(ctx context.Context, currentBlockNumber int64) | ||||||||
| PollAndSaveLogs(ctx context.Context, currentBlockNumber int64, isReplay bool) | ||||||||
| BackupPollAndSaveLogs(ctx context.Context) error | ||||||||
| Filter(from, to *big.Int, bh *common.Hash) ethereum.FilterQuery | ||||||||
| GetReplayFromBlock(ctx context.Context, requested int64) (int64, error) | ||||||||
|
|
@@ -662,7 +662,7 @@ func (lp *logPoller) run() { | |||||||
| } else { | ||||||||
| start = lastProcessed.BlockNumber + 1 | ||||||||
| } | ||||||||
| lp.PollAndSaveLogs(ctx, start) | ||||||||
| lp.PollAndSaveLogs(ctx, start, false) | ||||||||
| case <-backupLogPollTicker.C: | ||||||||
| if lp.backupPollerBlockDelay == 0 { | ||||||||
| continue // backup poller is disabled | ||||||||
|
|
@@ -772,7 +772,7 @@ func (lp *logPoller) handleReplayRequest(ctx context.Context, fromBlockReq int64 | |||||||
| if err == nil { | ||||||||
| // Serially process replay requests. | ||||||||
| lp.lggr.Infow("Executing replay", "fromBlock", fromBlock, "requested", fromBlockReq) | ||||||||
| lp.PollAndSaveLogs(ctx, fromBlock) | ||||||||
| lp.PollAndSaveLogs(ctx, fromBlock, true) | ||||||||
| lp.lggr.Infow("Executing replay finished", "fromBlock", fromBlock, "requested", fromBlockReq) | ||||||||
| } | ||||||||
| } else { | ||||||||
|
|
@@ -954,6 +954,25 @@ func (lp *logPoller) backfill(ctx context.Context, start, end int64) error { | |||||||
| return nil | ||||||||
| } | ||||||||
|
|
||||||||
| func (lp *logPoller) headerByNumber(ctx context.Context, blockNumber int64) (*evmtypes.Head, error) { | ||||||||
| header, err := lp.latencyMonitor.HeadByNumber(ctx, big.NewInt(blockNumber)) | ||||||||
| if err != nil { | ||||||||
| lp.lggr.Warnw("Unable to get currentBlock", "err", err, "blockNumber", blockNumber) | ||||||||
| return nil, fmt.Errorf("unable to get current block header for block number %d: %w", blockNumber, err) | ||||||||
| } | ||||||||
| // Additional sanity checks, don't necessarily trust the RPC. | ||||||||
| if header == nil { | ||||||||
| lp.lggr.Errorw("Unexpected nil block from RPC", "blockNumber", blockNumber) | ||||||||
| return nil, fmt.Errorf("got nil block for %d", blockNumber) | ||||||||
| } | ||||||||
| if header.Number != blockNumber { | ||||||||
| lp.lggr.Warnw("Unable to get currentBlock, rpc returned incorrect block", "blockNumber", blockNumber, "got", header.Number) | ||||||||
| return nil, fmt.Errorf("block mismatch have %d want %d", header.Number, blockNumber) | ||||||||
| } | ||||||||
|
|
||||||||
| return header, nil | ||||||||
| } | ||||||||
|
|
||||||||
| // getCurrentBlockMaybeHandleReorg accepts a block number | ||||||||
| // and will return that block if its parent points to our last saved block. | ||||||||
| // One can optionally pass the block header if it has already been queried to avoid an extra RPC call. | ||||||||
|
|
@@ -962,23 +981,12 @@ func (lp *logPoller) backfill(ctx context.Context, start, end int64) error { | |||||||
| // 1. Find the LCA by following parent hashes. | ||||||||
| // 2. Delete all logs and blocks after the LCA | ||||||||
| // 3. Return the LCA+1, i.e. our new current (unprocessed) block. | ||||||||
| func (lp *logPoller) getCurrentBlockMaybeHandleReorg(ctx context.Context, currentBlockNumber int64, currentBlock *evmtypes.Head) (head *evmtypes.Head, err error) { | ||||||||
| func (lp *logPoller) getCurrentBlockMaybeHandleReorg(ctx context.Context, currentBlockNumber int64, currentBlock *evmtypes.Head, isReplay bool) (head *evmtypes.Head, err error) { | ||||||||
| var err1 error | ||||||||
| if currentBlock == nil { | ||||||||
| // If we don't have the current block already, lets get it. | ||||||||
| currentBlock, err1 = lp.latencyMonitor.HeadByNumber(ctx, big.NewInt(currentBlockNumber)) | ||||||||
| currentBlock, err1 = lp.headerByNumber(ctx, currentBlockNumber) | ||||||||
| if err1 != nil { | ||||||||
| lp.lggr.Warnw("Unable to get currentBlock", "err", err1, "currentBlockNumber", currentBlockNumber) | ||||||||
| return nil, err1 | ||||||||
| } | ||||||||
| // Additional sanity checks, don't necessarily trust the RPC. | ||||||||
| if currentBlock == nil { | ||||||||
| lp.lggr.Errorw("Unexpected nil block from RPC", "currentBlockNumber", currentBlockNumber) | ||||||||
| return nil, pkgerrors.Errorf("Got nil block for %d", currentBlockNumber) | ||||||||
| } | ||||||||
| if currentBlock.Number != currentBlockNumber { | ||||||||
| lp.lggr.Warnw("Unable to get currentBlock, rpc returned incorrect block", "currentBlockNumber", currentBlockNumber, "got", currentBlock.Number) | ||||||||
| return nil, pkgerrors.Errorf("Block mismatch have %d want %d", currentBlock.Number, currentBlockNumber) | ||||||||
| return nil, fmt.Errorf("unable to get current block header for block number %d: %w", currentBlockNumber, err1) | ||||||||
| } | ||||||||
| } | ||||||||
| // Does this currentBlock point to the same parent that we have saved? | ||||||||
|
|
@@ -997,39 +1005,98 @@ func (lp *logPoller) getCurrentBlockMaybeHandleReorg(ctx context.Context, curren | |||||||
| } | ||||||||
| // Check for reorg. | ||||||||
| if currentBlock.ParentHash != expectedParent.BlockHash { | ||||||||
| // 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, expectedParent.FinalizedBlockNumber) | ||||||||
| if err2 != nil { | ||||||||
| return nil, fmt.Errorf("unable to find LCA after reorg: %w", err2) | ||||||||
| return lp.handleReorg(ctx, currentBlock) | ||||||||
| } | ||||||||
|
|
||||||||
| if !isReplay { | ||||||||
| // During normal polling DB does not have any blocks after currentBlockNumber, so no reorg is possible. We can skip extra checks and just return currentBlock. | ||||||||
| return currentBlock, nil | ||||||||
| } | ||||||||
|
|
||||||||
| // Ensure that if DB contains current block it matches the current block from RPC. | ||||||||
| currentBlockDB, err := lp.orm.SelectBlockByNumber(ctx, currentBlockNumber) | ||||||||
| if err != nil && !pkgerrors.Is(err, sql.ErrNoRows) { | ||||||||
| return nil, fmt.Errorf("failed to get current block from DB %d: %w", currentBlockNumber, err) | ||||||||
| } | ||||||||
|
|
||||||||
| if currentBlockDB != nil && currentBlock.Hash != currentBlockDB.BlockHash { | ||||||||
| return lp.handleReorg(ctx, currentBlock) | ||||||||
| } | ||||||||
|
|
||||||||
| // No reorg for current block, but during replay it's possible that current block is older than the latest block, let's check it too to avoid false positives on finality violation. | ||||||||
| latestBlockDB, err1 := lp.orm.SelectLatestBlock(ctx) | ||||||||
| if err1 != nil { | ||||||||
| if pkgerrors.Is(err1, sql.ErrNoRows) { | ||||||||
| lp.lggr.Criticalw("Unexpected state. Expected at least one block to be present in the db when checking for reorg during replay, but got no rows", "currentBlockNumber", currentBlockNumber, "err", err1) | ||||||||
| } | ||||||||
| return nil, pkgerrors.Wrap(err1, "unable to get latest block") | ||||||||
| } | ||||||||
|
|
||||||||
| lp.lggr.Infow("Reorg detected", "blockAfterLCA", blockAfterLCA.Number, "currentBlockNumber", currentBlockNumber) | ||||||||
| // We truncate all the blocks and logs after the LCA. | ||||||||
| // We could preserve the logs for forensics, since its possible | ||||||||
| // that applications see them and take action upon it, however that | ||||||||
| // results in significantly slower reads since we must then compute | ||||||||
| // the canonical set per read. Typically, if an application took action on a log | ||||||||
| // it would be saved elsewhere e.g. evm.txes, so it seems better to just support the fast reads. | ||||||||
| // Its also nicely analogous to reading from the chain itself. | ||||||||
| err2 = lp.orm.DeleteLogsAndBlocksAfter(ctx, blockAfterLCA.Number) | ||||||||
| if err2 != nil { | ||||||||
| // If we error on db commit, we can't know if the tx went through or not. | ||||||||
| // We return an error here which will cause us to restart polling from lastBlockSaved + 1 | ||||||||
| return nil, err2 | ||||||||
| if currentBlock.BlockNumber() >= latestBlockDB.BlockNumber { | ||||||||
| // currentBlock is newest, nothing more to check | ||||||||
| return currentBlock, nil | ||||||||
| } | ||||||||
|
|
||||||||
| latestBlockRPC, err := lp.headerByNumber(ctx, latestBlockDB.BlockNumber) | ||||||||
| if err != nil { | ||||||||
| return nil, fmt.Errorf("unable to get latest block header for block number %d: %w", latestBlockDB.BlockNumber, err) | ||||||||
| } | ||||||||
|
|
||||||||
| if latestBlockRPC.Hash != latestBlockDB.BlockHash { | ||||||||
| // Reorg detected, handle it | ||||||||
| blockAfterLCA, err := lp.handleReorg(ctx, latestBlockRPC) | ||||||||
| if err != nil { | ||||||||
| return nil, fmt.Errorf("failed to handle reorg: %w", err) | ||||||||
| } | ||||||||
|
|
||||||||
| if blockAfterLCA.Number < currentBlock.BlockNumber() { | ||||||||
| return blockAfterLCA, nil | ||||||||
| } | ||||||||
| return blockAfterLCA, nil | ||||||||
| } | ||||||||
| // No reorg, return current block. | ||||||||
|
|
||||||||
| return currentBlock, nil | ||||||||
| } | ||||||||
|
|
||||||||
| func (lp *logPoller) handleReorg(ctx context.Context, currentBlock *evmtypes.Head) (*evmtypes.Head, error) { | ||||||||
| // during replay currentBlock may be older than the latest block, thus it's possible to miss finality violation, | ||||||||
| // if we use its view on latest finalized block. To be safe, we get the latest block from the db. | ||||||||
| latestBlock, err := lp.orm.SelectLatestBlock(ctx) | ||||||||
| if err != nil { | ||||||||
| if pkgerrors.Is(err, sql.ErrNoRows) { | ||||||||
| lp.lggr.Criticalw("Unexpected state. Expected at least one block to be present in the db when handling reorg, but got no rows", "currentBlockNumber", currentBlock.Number, "err", err) | ||||||||
| } | ||||||||
| return nil, pkgerrors.Wrap(err, "failed to get latest finalized block from db") | ||||||||
| } | ||||||||
| // 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, latestBlock.FinalizedBlockNumber) | ||||||||
| if err2 != nil { | ||||||||
| return nil, fmt.Errorf("unable to find LCA after reorg: %w", err2) | ||||||||
| } | ||||||||
|
|
||||||||
| lp.lggr.Infow("Reorg detected", "blockAfterLCA", blockAfterLCA.Number, "currentBlockNumber", currentBlock.Number) | ||||||||
| // We truncate all the blocks and logs after the LCA. | ||||||||
| // We could preserve the logs for forensics, since its possible | ||||||||
| // that applications see them and take action upon it, however that | ||||||||
| // results in significantly slower reads since we must then compute | ||||||||
| // the canonical set per read. Typically, if an application took action on a log | ||||||||
| // it would be saved elsewhere e.g. evm.txes, so it seems better to just support the fast reads. | ||||||||
| // Its also nicely analogous to reading from the chain itself. | ||||||||
| err2 = lp.orm.DeleteLogsAndBlocksAfter(ctx, blockAfterLCA.Number) | ||||||||
| if err2 != nil { | ||||||||
| // If we error on db commit, we can't know if the tx went through or not. | ||||||||
| // We return an error here which will cause us to restart polling from lastBlockSaved + 1 | ||||||||
| return nil, err2 | ||||||||
| } | ||||||||
| return blockAfterLCA, nil | ||||||||
| } | ||||||||
|
|
||||||||
| // PollAndSaveLogs On startup/crash current is the first block after the last processed block. | ||||||||
| // currentBlockNumber is the block from where new logs are to be polled & saved. Under normal | ||||||||
| // conditions this would be equal to lastProcessed.BlockNumber + 1. | ||||||||
|
||||||||
| // conditions this would be equal to lastProcessed.BlockNumber + 1. | |
| // conditions this would be equal to lastProcessed.BlockNumber + 1. The isReplay flag should be | |
| // true when PollAndSaveLogs is called as part of a replay operation and false during normal polling. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Misleading error message: The error message says "failed to get latest finalized block from db" but the function actually calls orm.SelectLatestBlock (line 1063), not SelectLatestFinalizedBlock. The error message should be updated to "failed to get latest block from db" for accuracy.