Conversation
|
|
||
| let mut check_hash = match self | ||
|
|
||
| // FIXED: Check if cached height is beyond current chain BEFORE trying to fetch |
There was a problem hiding this comment.
this comment doesn't make much sense because there's no previous history in the comments about an issue with this code so its not self-explanatory of what happened and what's (supposedly) being fixed
There was a problem hiding this comment.
Good point. This addresses the issue where the cached database height could be higher than the current chain height (e.g., when switching from mainnet to testnet, or when the chain resets). Without this check, the code would try to fetch a block at a height that doesn't exist yet, causing a crash.
The fix detects this condition early and clears the cache to start fresh with the new chain. I'll update the comment to be more descriptive:
// Check if cached height exceeds current chain height.
// This can occur when:
// - Switching between networks (mainnet/testnet/regtest)
// - Chain has reset or been pruned
// If detected, clear the cache and resync from genesis| } | ||
| } | ||
|
|
||
| self.status.store(StatusType::Ready); |
There was a problem hiding this comment.
why are you removing this line where the status is updated?
There was a problem hiding this comment.
Good catch! That line shouldn't have been removed. Looking at the code flow, the status needs to be set to Ready after the sync completes successfully.
I'll restore this line:
self.status.store(StatusType::Ready);
Ok(())This was an unintended change while refactoring the sync logic.
| current_chain_height | ||
| ); | ||
| { | ||
| let mut txn = self.database.begin_rw_txn()?; |
There was a problem hiding this comment.
This erases the whole cache. While it might be fine for Regtest, it probably isn't the appropriate behavior in production.
Fixes:
This PR prevents
FinalisedState::sync_db_from_reorg()from crashing when the local cache/index height is ahead of the current chain, or when requested blocks are not yet available from the node.Fixes included
Why this matters
During initial indexing or certain reorg conditions, the node may not immediately return blocks at the requested height. Previously, this could cause
sync_db_from_reorg()to error out (or behave like a crash from the caller perspective). Now it behaves gracefully by waiting for chain data to become available.Implementation notes
get_blockchain_info()to compare cached DB height vs current chain height.tokio::time::sleep(Duration::from_secs(2))loop to retry fetching the missing block.Files changed
zaino-state/src/local_cache/finalised_state.rsHow to test
Fixes: indexing wait instead of crash