Skip to content
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

Fix RewindDataIndexCache finality error #1131

Draft
wants to merge 1 commit into
base: release/1.6.0.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public CoinviewTests()
this.stakeChainStore = new StakeChainStore(this.network, this.chainIndexer, (IStakedb)this.coindb, this.loggerFactory);
this.stakeChainStore.Load();

this.rewindDataIndexCache = new RewindDataIndexCache(this.dateTimeProvider, this.network, new FinalizedBlockInfoRepository(new HashHeightPair()), new Checkpoints());
this.rewindDataIndexCache = new RewindDataIndexCache(this.dateTimeProvider, this.network);

this.cachedCoinView = new CachedCoinView(this.network, this.coindb, this.dateTimeProvider, this.loggerFactory, this.nodeStats, new ConsensusSettings(new NodeSettings(this.network)), this.chainIndexer, this.stakeChainStore, this.rewindDataIndexCache);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void RewindDataIndex_InitialiseCache_BelowMaxReorg()
var finalizedBlockInfoRepositoryMock = new Mock<IFinalizedBlockInfoRepository>();
finalizedBlockInfoRepositoryMock.Setup(s => s.GetFinalizedBlockInfo()).Returns(new HashHeightPair());

var rewindDataIndexCache = new RewindDataIndexCache(dateTimeProviderMock.Object, this.Network, finalizedBlockInfoRepositoryMock.Object, new Checkpoints());
var rewindDataIndexCache = new RewindDataIndexCache(dateTimeProviderMock.Object, this.Network);

rewindDataIndexCache.Initialize(5, coinViewMock.Object);

Expand All @@ -54,7 +54,7 @@ public void RewindDataIndex_InitialiseCache()
var finalizedBlockInfoRepositoryMock = new Mock<IFinalizedBlockInfoRepository>();
finalizedBlockInfoRepositoryMock.Setup(s => s.GetFinalizedBlockInfo()).Returns(new HashHeightPair());

var rewindDataIndexCache = new RewindDataIndexCache(dateTimeProviderMock.Object, this.Network, finalizedBlockInfoRepositoryMock.Object, new Checkpoints());
var rewindDataIndexCache = new RewindDataIndexCache(dateTimeProviderMock.Object, this.Network);

rewindDataIndexCache.Initialize(20, coinViewMock.Object);

Expand All @@ -74,7 +74,7 @@ public void RewindDataIndex_Save()
var finalizedBlockInfoRepositoryMock = new Mock<IFinalizedBlockInfoRepository>();
finalizedBlockInfoRepositoryMock.Setup(s => s.GetFinalizedBlockInfo()).Returns(new HashHeightPair());

var rewindDataIndexCache = new RewindDataIndexCache(dateTimeProviderMock.Object, this.Network, finalizedBlockInfoRepositoryMock.Object, new Checkpoints());
var rewindDataIndexCache = new RewindDataIndexCache(dateTimeProviderMock.Object, this.Network);

rewindDataIndexCache.Initialize(20, coinViewMock.Object);

Expand All @@ -95,7 +95,7 @@ public void RewindDataIndex_Flush()
var finalizedBlockInfoRepositoryMock = new Mock<IFinalizedBlockInfoRepository>();
finalizedBlockInfoRepositoryMock.Setup(s => s.GetFinalizedBlockInfo()).Returns(new HashHeightPair());

var rewindDataIndexCache = new RewindDataIndexCache(dateTimeProviderMock.Object, this.Network, finalizedBlockInfoRepositoryMock.Object, new Checkpoints());
var rewindDataIndexCache = new RewindDataIndexCache(dateTimeProviderMock.Object, this.Network);

rewindDataIndexCache.Initialize(20, coinViewMock.Object);

Expand All @@ -116,7 +116,7 @@ public void RewindDataIndex_Remove()
var finalizedBlockInfoRepositoryMock = new Mock<IFinalizedBlockInfoRepository>();
finalizedBlockInfoRepositoryMock.Setup(s => s.GetFinalizedBlockInfo()).Returns(new HashHeightPair());

var rewindDataIndexCache = new RewindDataIndexCache(dateTimeProviderMock.Object, this.Network, finalizedBlockInfoRepositoryMock.Object, new Checkpoints());
var rewindDataIndexCache = new RewindDataIndexCache(dateTimeProviderMock.Object, this.Network);

rewindDataIndexCache.Initialize(20, coinViewMock.Object);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace Stratis.Bitcoin.Features.Consensus.ProvenBlockHeaders
public class RewindDataIndexCache : IRewindDataIndexCache
{
private readonly Network network;
private readonly IFinalizedBlockInfoRepository finalizedBlockInfoRepository;
private readonly ICheckpoints checkpoints;

/// <summary>
/// Internal cache for rewind data index. Key is a TxId + N (N is an index of output in a transaction)
Expand All @@ -26,22 +24,18 @@ public class RewindDataIndexCache : IRewindDataIndexCache
/// The number of items stored in cache is the sum of inputs used in every transaction in each of those blocks.
/// </summary>
private int numberOfBlocksToKeep;
private int lastCheckpoint;

/// <summary>
/// Performance counter to measure performance of the save and get operations.
/// </summary>
private readonly BackendPerformanceCounter performanceCounter;

public RewindDataIndexCache(IDateTimeProvider dateTimeProvider, Network network, IFinalizedBlockInfoRepository finalizedBlockInfoRepository, ICheckpoints checkpoints)
public RewindDataIndexCache(IDateTimeProvider dateTimeProvider, Network network)
{
Guard.NotNull(dateTimeProvider, nameof(dateTimeProvider));

this.network = network;
this.finalizedBlockInfoRepository = finalizedBlockInfoRepository;
this.checkpoints = checkpoints;
this.items = new ConcurrentDictionary<OutPoint, int>();
this.lastCheckpoint = this.checkpoints.GetLastCheckpointHeight();

this.performanceCounter = new BackendPerformanceCounter(dateTimeProvider);
}
Expand All @@ -51,24 +45,10 @@ public void Initialize(int tipHeight, ICoinView coinView)
{
this.items.Clear();

if (this.lastCheckpoint > tipHeight)
return;

HashHeightPair finalBlock = this.finalizedBlockInfoRepository.GetFinalizedBlockInfo();

this.numberOfBlocksToKeep = (int)this.network.Consensus.MaxReorgLength;

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

if (tipHeight < finalBlock.Height)
throw new ConsensusException($"Violation of finality on height { tipHeight } for RewindDataIndex.");

if (heightToSyncTo < finalBlock.Height)
heightToSyncTo = finalBlock.Height;

if (heightToSyncTo < this.lastCheckpoint)
heightToSyncTo = this.lastCheckpoint;

for (int rewindHeight = tipHeight; rewindHeight >= heightToSyncTo; rewindHeight--)
{
RewindData rewindData = coinView.GetRewindData(rewindHeight);
Expand Down Expand Up @@ -104,9 +84,6 @@ private void AddRewindData(int rewindHeight, RewindData rewindData)
/// <inheritdoc />
public void Remove(int tipHeight, ICoinView coinView)
{
if (this.lastCheckpoint > tipHeight)
return;

this.SaveAndEvict(tipHeight, null);

int bottomHeight = tipHeight > this.numberOfBlocksToKeep ? tipHeight - this.numberOfBlocksToKeep : 1;
Expand All @@ -118,9 +95,6 @@ public void Remove(int tipHeight, ICoinView coinView)
/// <inheritdoc />
public void SaveAndEvict(int tipHeight, Dictionary<OutPoint, int> indexData)
{
if (this.lastCheckpoint > tipHeight)
return;

if (indexData != null)
{
using (new StopwatchDisposable(o => this.performanceCounter.AddInsertTime(o)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static async Task<ITestChainContext> CreatePosAsync(Network network, Scri
var stakeChain = new StakeChainStore(network, chain, null, loggerFactory);
ConsensusRuleEngine consensusRules = new PosConsensusRuleEngine(network, loggerFactory, dateTimeProvider, chain, deployments, consensusSettings, new Checkpoints(),
inMemoryCoinView, stakeChain, new StakeValidator(network, stakeChain, chain, inMemoryCoinView, loggerFactory), chainState, new InvalidBlockHashStore(dateTimeProvider),
new NodeStats(dateTimeProvider, NodeSettings.Default(network), new Mock<IVersionProvider>().Object), new RewindDataIndexCache(dateTimeProvider, network, finalizedBlockInfoRepository, new Checkpoints()), asyncProvider, consensusRulesContainer).SetupRulesEngineParent();
new NodeStats(dateTimeProvider, NodeSettings.Default(network), new Mock<IVersionProvider>().Object), new RewindDataIndexCache(dateTimeProvider, network), asyncProvider, consensusRulesContainer).SetupRulesEngineParent();

IConsensusManager consensus = ConsensusManagerHelper.CreateConsensusManager(network, dataDir, chainState, chainIndexer: chain, consensusRules: consensusRules, inMemoryCoinView: inMemoryCoinView);

Expand Down