Skip to content

Commit a5d688f

Browse files
authored
If finality is ahead of consensus don't fail the indexer. (#103)
* If finality is ahead of consensus dont fail the indexer, load rewind data as normal * Add comments to explain finality
1 parent 144f47e commit a5d688f

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/Blockcore/Consensus/FinalizedBlockInfoRepository.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ namespace Blockcore.Consensus
1414
/// Finalized block height is the height of the last block that can't be reorged.
1515
/// Blocks with height greater than finalized height can be reorged.
1616
/// <para>Finalized block height value is always <c>0</c> for blockchains without max reorg property.</para>
17+
///
18+
/// The protection finality is to not allow rewinding the node bellow a certain height, whileh MaxReorgProtection does the same
19+
/// MaxReorg does not protect from chain with different work. MaxReorg determins the chain by its height
20+
/// while rewind uses work as indicator (the chain with most work is the winner),
21+
/// this can cause the scenario where a chain with lower height but more work triggeres a reorg and consensus tip ends up in a lower height.
22+
/// Now a second rewind can heppen that will go below MaxReorg of the initial hiegher (but lower work) tip,
23+
/// for that we need finality, even if such a reorg happens finality will prevent the second reorg that will vioulate MAxReorgProtection.
24+
///
25+
/// A - - - - - - - - - - - - - - - - - [current tip]
26+
/// B | L - - - - - - - [more work then chain A]
27+
/// C L - - - - - [more work then chain B but violate max reorg]
1728
/// </remarks>
1829
public interface IFinalizedBlockInfoRepository : IDisposable
1930
{
@@ -168,4 +179,4 @@ public void Dispose()
168179
this.finalizedBlockInfoPersistingTask.GetAwaiter().GetResult();
169180
}
170181
}
171-
}
182+
}

src/Features/Blockcore.Features.Consensus/ProvenBlockHeaders/RewindDataIndexCache.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ public void Initialize(int tipHeight, ICoinView coinView)
6161

6262
int heightToSyncTo = tipHeight > this.numberOfBlocksToKeep ? tipHeight - this.numberOfBlocksToKeep : 1;
6363

64-
if (tipHeight < finalBlock.Height)
65-
throw new ConsensusException($"Violation of finality on height { tipHeight } for RewindDataIndex.");
66-
67-
if (heightToSyncTo < finalBlock.Height)
68-
heightToSyncTo = finalBlock.Height;
64+
if (tipHeight > finalBlock.Height)
65+
{
66+
if (heightToSyncTo < finalBlock.Height)
67+
heightToSyncTo = finalBlock.Height;
6968

70-
if (heightToSyncTo < this.lastCheckpoint)
71-
heightToSyncTo = this.lastCheckpoint;
69+
if (heightToSyncTo < this.lastCheckpoint)
70+
heightToSyncTo = this.lastCheckpoint;
71+
}
7272

7373
for (int rewindHeight = tipHeight; rewindHeight >= heightToSyncTo; rewindHeight--)
7474
{

0 commit comments

Comments
 (0)