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

Move CachedCoinView initialization from PowConsensusRuleEngine #1014

Merged
merged 1 commit into from
Jul 4, 2022
Merged
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
24 changes: 24 additions & 0 deletions src/Stratis.Bitcoin.Features.Consensus/CoinViews/CachedCoinView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,30 @@ public CachedCoinView(Network network, ICheckpoints checkpoints, ICoindb coindb,
nodeStats.RegisterStats(this.AddBenchStats, StatsType.Benchmark, this.GetType().Name, 300);
}

public void Initialize(ChainedHeader chainTip, ChainIndexer chainIndexer)
{
this.coindb.Initialize(chainTip);

HashHeightPair coinViewTip = this.coindb.GetTipHash();

while (true)
{
ChainedHeader pendingTip = chainTip.FindAncestorOrSelf(coinViewTip.Hash);

if (pendingTip != null)
break;

if ((coinViewTip.Height % 100) == 0)
this.logger.LogInformation("Rewinding coin view from '{0}' to {1}.", coinViewTip, chainTip);

// If the block store was initialized behind the coin view's tip, rewind it to on or before it's tip.
// The node will complete loading before connecting to peers so the chain will never know that a reorg happened.
coinViewTip = this.coindb.Rewind(new HashHeightPair(chainTip));
}

this.logger.LogInformation("Coin view initialized at '{0}'.", this.coindb.GetTipHash());
}

public HashHeightPair GetTipHash()
{
if (this.blockHash == null)
Expand Down
7 changes: 7 additions & 0 deletions src/Stratis.Bitcoin.Features.Consensus/CoinViews/CoinView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ namespace Stratis.Bitcoin.Features.Consensus.CoinViews
/// </summary>
public interface ICoinView
{
/// <summary>
/// Initializes the coin view.
/// </summary>
/// <param name="chainTip">The chain tip.</param>
/// <param name="chainIndexer">The chain indexer.</param>
void Initialize(ChainedHeader chainTip, ChainIndexer chainIndexer);

/// <summary>
/// Retrieves the block hash of the current tip of the coinview.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public InMemoryCoinView(HashHeightPair tipHash)
this.tipHash = tipHash;
}

/// <inheritdoc />
public void Initialize(ChainedHeader chainTip, ChainIndexer chainIndexer)
{
throw new NotImplementedException();
}

/// <inheritdoc />
public HashHeightPair GetTipHash()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,7 @@ public override void Initialize(ChainedHeader chainTip)
{
base.Initialize(chainTip);

var coinDatabase = ((CachedCoinView)this.UtxoSet).ICoindb;
coinDatabase.Initialize(chainTip);

HashHeightPair coinViewTip = coinDatabase.GetTipHash();

while (true)
{
ChainedHeader pendingTip = chainTip.FindAncestorOrSelf(coinViewTip.Hash);

if (pendingTip != null)
break;

if ((coinViewTip.Height % 100) == 0)
this.logger.LogInformation("Rewinding coin view from '{0}' to {1}.", coinViewTip, chainTip);

// If the block store was initialized behind the coin view's tip, rewind it to on or before it's tip.
// The node will complete loading before connecting to peers so the chain will never know that a reorg happened.
coinViewTip = coinDatabase.Rewind(new HashHeightPair(chainTip));
}

this.logger.LogInformation("Coin view initialized at '{0}'.", coinDatabase.GetTipHash());
this.UtxoSet.Initialize(chainTip, this.ChainIndexer);
}

public override async Task<ValidationContext> FullValidationAsync(ChainedHeader header, Block block)
Expand Down
5 changes: 5 additions & 0 deletions src/Stratis.Bitcoin.Features.MemoryPool/MemPoolCoinView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public MempoolCoinView(Network network, ICoinView inner, ITxMempool memPool, Sch
this.Set = new UnspentOutputSet();
}

public void Initialize(ChainedHeader chainTip, ChainIndexer chainIndexer)
{
throw new NotImplementedException();
}

/// <summary>
/// Gets the unspent transaction output set.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Stratis.Bitcoin.Tests/Consensus/TestInMemoryCoinView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public TestInMemoryCoinView(HashHeightPair tipHash)
this.tipHash = tipHash;
}

/// <inheritdoc />
public void Initialize(ChainedHeader chainTip, ChainIndexer chainIndexer)
{
throw new NotImplementedException();
}

/// <inheritdoc />
public HashHeightPair GetTipHash()
{
Expand Down