Skip to content

Fix reorg threshold state with start block - #470

Merged
DZakh merged 4 commits into
mainfrom
dz/reorg-detection-with-strat-block
Mar 12, 2025
Merged

Fix reorg threshold state with start block#470
DZakh merged 4 commits into
mainfrom
dz/reorg-detection-with-strat-block

Conversation

@DZakh

@DZakh DZakh commented Mar 11, 2025

Copy link
Copy Markdown
Member

It caused Reorg threshold reached happening earlier than it should have been.

Comment on lines +2497 to +2498
it(
"Returns false when we just started the indexer and it has currentBlockHeight=0, while start block is more than 0 + reorg threshold",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This test was returning true

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Was the first test passing the assertion?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes

@DZakh
DZakh requested a review from JonoPrest March 11, 2025 14:22

@JonoPrest JonoPrest left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Great catch thanks @DZakh 🙏🏼

item.earliestEvent->FetchState.queueItemIsInReorgThreshold(
~heighestBlockBelowThreshold=ChainMap.get(
fetchStatesMap,
a.chain,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

a.chain instead of item.chain is another bug I've found

} else {
false
let queueItemIsInReorgThreshold = (queueItem: queueItem, ~currentBlockHeight, ~heighestBlockBelowThreshold) => {
if currentBlockHeight === 0 {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Compared to the first fix I use currentBlockHeight now instead of heighestBlockBelowThreshold. This way chains with height less than reorg threshold will work correctly - relevant for our tests

Comment on lines +1075 to +1081
dispatchAction(SetIsInReorgThreshold(true))
let batch =
state.chainManager->ChainManager.createBatch(
~maxBatchSize=state.maxBatchSize,
~onlyBelowReorgThreshold=false,
)
await handleBatch(batch)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is not really needed for the indexer execution, but it makes lifecycle more predictable and fixes Rollback_test.res. Without the change rollback in the test happens before we processed events in the reorg threshold.

Comment on lines -323 to -324
GlobalState.NextQuery(CheckAllChains),
ProcessEventBatch,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is a change caused by handleBatch, since we now handle reaching the reorg threshold inside of ProcessEventBatch itself without redispatching it in the case when there are no items to process before threshold.

Comment on lines +5 to +17
// Keep only the first chain
let config = Config.make(
~shouldRollbackOnReorg=false,
~shouldSaveFullHistory=false,
~isUnorderedMultichainMode=false,
~chains=config.chainMap
->ChainMap.entries
->Array.keepMap(((chain, config)) =>
chain == RollbackMultichain_test.Mock.Chain1.chain ? Some(config) : None
),
~enableRawEvents=false,
~entities=config.entities->Obj.magic,
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This one and a few other tests randomly broke after the fix. It is caused by them having shouldRollbackOnReorg enabled. The chain we are testing gets some height and considered to be in the reorg threshold, while other chains stay at block 0 and have in reorg threshold as false. To fix this I remove other chains from the tests which we don't use.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ok cool 👍🏼

@DZakh
DZakh requested a review from JonoPrest March 12, 2025 12:03
if isInReorgThreshold {
Logging.info("Reorg threshold reached")
}
({...state, chainManager: {...state.chainManager, isInReorgThreshold}}, [ProcessEventBatch])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is it sure that we never get into a deadlock where ProcessEventBatch doesn't get dispatched again?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep

@JonoPrest JonoPrest left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks so much @DZakh and good catch on the on the other bug!

@DZakh
DZakh merged commit 205bdd0 into main Mar 12, 2025
@DZakh
DZakh deleted the dz/reorg-detection-with-strat-block branch March 12, 2025 12:33
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.

2 participants