From 4c11a6247beb43ec0d80b1741dfb829384a15d56 Mon Sep 17 00:00:00 2001 From: quantumagi Date: Mon, 4 Jul 2022 17:37:00 +1000 Subject: [PATCH] Move CachedCoinView initialization from PowConsensusRuleEngine --- .../CoinViews/CachedCoinView.cs | 24 +++++++++++++++++++ .../CoinViews/CoinView.cs | 7 ++++++ .../CoinViews/InMemoryCoinView.cs | 6 +++++ .../Rules/PowConsensusRuleEngine.cs | 22 +---------------- .../MemPoolCoinView.cs | 5 ++++ .../Consensus/TestInMemoryCoinView.cs | 6 +++++ 6 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/Stratis.Bitcoin.Features.Consensus/CoinViews/CachedCoinView.cs b/src/Stratis.Bitcoin.Features.Consensus/CoinViews/CachedCoinView.cs index 890cd8d2f2..be7b38ecd5 100644 --- a/src/Stratis.Bitcoin.Features.Consensus/CoinViews/CachedCoinView.cs +++ b/src/Stratis.Bitcoin.Features.Consensus/CoinViews/CachedCoinView.cs @@ -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) diff --git a/src/Stratis.Bitcoin.Features.Consensus/CoinViews/CoinView.cs b/src/Stratis.Bitcoin.Features.Consensus/CoinViews/CoinView.cs index f2b6469361..e0ac6e45a7 100644 --- a/src/Stratis.Bitcoin.Features.Consensus/CoinViews/CoinView.cs +++ b/src/Stratis.Bitcoin.Features.Consensus/CoinViews/CoinView.cs @@ -10,6 +10,13 @@ namespace Stratis.Bitcoin.Features.Consensus.CoinViews /// public interface ICoinView { + /// + /// Initializes the coin view. + /// + /// The chain tip. + /// The chain indexer. + void Initialize(ChainedHeader chainTip, ChainIndexer chainIndexer); + /// /// Retrieves the block hash of the current tip of the coinview. /// diff --git a/src/Stratis.Bitcoin.Features.Consensus/CoinViews/InMemoryCoinView.cs b/src/Stratis.Bitcoin.Features.Consensus/CoinViews/InMemoryCoinView.cs index 2f2bd59f15..11073a96da 100644 --- a/src/Stratis.Bitcoin.Features.Consensus/CoinViews/InMemoryCoinView.cs +++ b/src/Stratis.Bitcoin.Features.Consensus/CoinViews/InMemoryCoinView.cs @@ -32,6 +32,12 @@ public InMemoryCoinView(HashHeightPair tipHash) this.tipHash = tipHash; } + /// + public void Initialize(ChainedHeader chainTip, ChainIndexer chainIndexer) + { + throw new NotImplementedException(); + } + /// public HashHeightPair GetTipHash() { diff --git a/src/Stratis.Bitcoin.Features.Consensus/Rules/PowConsensusRuleEngine.cs b/src/Stratis.Bitcoin.Features.Consensus/Rules/PowConsensusRuleEngine.cs index 97ade69440..60dd9519b7 100644 --- a/src/Stratis.Bitcoin.Features.Consensus/Rules/PowConsensusRuleEngine.cs +++ b/src/Stratis.Bitcoin.Features.Consensus/Rules/PowConsensusRuleEngine.cs @@ -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 FullValidationAsync(ChainedHeader header, Block block) diff --git a/src/Stratis.Bitcoin.Features.MemoryPool/MemPoolCoinView.cs b/src/Stratis.Bitcoin.Features.MemoryPool/MemPoolCoinView.cs index 91f98a282a..286478d185 100644 --- a/src/Stratis.Bitcoin.Features.MemoryPool/MemPoolCoinView.cs +++ b/src/Stratis.Bitcoin.Features.MemoryPool/MemPoolCoinView.cs @@ -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(); + } + /// /// Gets the unspent transaction output set. /// diff --git a/src/Stratis.Bitcoin.Tests/Consensus/TestInMemoryCoinView.cs b/src/Stratis.Bitcoin.Tests/Consensus/TestInMemoryCoinView.cs index 3ebdd093a1..35fd7898fa 100644 --- a/src/Stratis.Bitcoin.Tests/Consensus/TestInMemoryCoinView.cs +++ b/src/Stratis.Bitcoin.Tests/Consensus/TestInMemoryCoinView.cs @@ -33,6 +33,12 @@ public TestInMemoryCoinView(HashHeightPair tipHash) this.tipHash = tipHash; } + /// + public void Initialize(ChainedHeader chainTip, ChainIndexer chainIndexer) + { + throw new NotImplementedException(); + } + /// public HashHeightPair GetTipHash() {