Timestamp/globalIndex ordering consistency #266
+141
−0
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.
Investigation and Root Cause Analysis for Issue #259
Fixes #259
🎯 Problem Statement
Issue #259 reports inconsistent indexing where transactions from 2023 appear before transactions from 2024 when ordering by
timestampandglobalIndexproperties, despite the chronological ordering being incorrect. The issue occurs "at some point" (intermittently) at the indexer/database level.🔍 Root Cause: Chain Reorganization Bug
After thorough investigation, I identified the root cause:
The bug occurs during blockchain reorganizations (reorgs). When a fork block becomes the main chain:
Initial State:
globalIndex1000-1010globalIndex1011-1020Reorg Occurs: Block B becomes the new main chain
Current Behavior (BUGGY):
ChainIndexer.updateChainStatus()→repos.txs.updateChainStatusByHeaderId()UPDATE node_transactions SET main_chain = true WHERE header_id = blockBmain_chainflag is updated, globalIndex values are NOT recalculatedConsequence:
globalIndex1011-1020 (from original insertion order)globalIndex1000-1010 (as the new main chain)ORDER BY timestamp≠ORDER BY globalIndexEvidence in codebase:
ChainIndexer.scala:226-233-updateChainStatus()only updatesmainChainflagTransactionQuerySet.scala:178-182- SQL only updatesmain_chain, notglobal_index✅ This PR: Foundation for the Fix
This PR provides the essential first step toward fixing issue #259:
Added: Comprehensive test suite (
TimestampGlobalIndexConsistencySpec.scala) that validates:sortBy(timestamp)must equalsortBy(globalIndex)The fix requires:
recalculateGlobalIndexAtHeight()method to recalculate globalIndex during reorgsChainIndexer.updateChainStatus()to call recalculationThis test suite provides:
📊 Technical Details
Current correct behavior (when no reorg occurs):