Example #1
0
        public CoinviewTests()
        {
            this.network          = new StraxMain();
            this.dataFolder       = TestBase.CreateDataFolder(this);
            this.dateTimeProvider = new DateTimeProvider();
            this.loggerFactory    = new ExtendedLoggerFactory();
            this.nodeStats        = new NodeStats(this.dateTimeProvider, this.loggerFactory);

            this.coindb = new DBreezeCoindb(this.network, this.dataFolder, this.dateTimeProvider, this.loggerFactory, this.nodeStats, new DBreezeSerializer(this.network.Consensus.ConsensusFactory));
            this.coindb.Initialize();

            this.chainIndexer    = new ChainIndexer(this.network);
            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.cachedCoinView = new CachedCoinView(this.network, new Checkpoints(), this.coindb, this.dateTimeProvider, this.loggerFactory, this.nodeStats, new ConsensusSettings(new NodeSettings(this.network)), this.stakeChainStore, this.rewindDataIndexCache);

            this.rewindDataIndexCache.Initialize(this.chainIndexer.Height, this.cachedCoinView);

            this.random = new Random();

            ChainedHeader newTip = ChainedHeadersHelper.CreateConsecutiveHeaders(1000, this.chainIndexer.Tip, true, null, this.network).Last();

            this.chainIndexer.SetTip(newTip);
        }
        public CoinviewTests()
        {
            this.network          = new StratisMain();
            this.dataFolder       = TestBase.CreateDataFolder(this);
            this.dateTimeProvider = new DateTimeProvider();
            this.loggerFactory    = new ExtendedLoggerFactory();
            this.nodeStats        = new NodeStats(this.dateTimeProvider);

            this.dbreezeCoinview = new DBreezeCoinView(this.network, this.dataFolder, this.dateTimeProvider, this.loggerFactory, this.nodeStats, new DBreezeSerializer(this.network));
            this.dbreezeCoinview.InitializeAsync().GetAwaiter().GetResult();

            this.concurrentChain = new ConcurrentChain(this.network);
            this.stakeChainStore = new StakeChainStore(this.network, this.concurrentChain, this.dbreezeCoinview, this.loggerFactory);
            this.stakeChainStore.LoadAsync().GetAwaiter().GetResult();

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

            this.cachedCoinView = new CachedCoinView(this.dbreezeCoinview, this.dateTimeProvider, this.loggerFactory, this.nodeStats, this.stakeChainStore, this.rewindDataIndexCache);

            this.rewindDataIndexCache.InitializeAsync(this.concurrentChain.Height, this.cachedCoinView);

            this.random = new Random();

            ChainedHeader newTip = ChainedHeadersHelper.CreateConsecutiveHeaders(1000, this.concurrentChain.Tip, true, null, this.network).Last();

            this.concurrentChain.SetTip(newTip);
        }
Example #3
0
        public CachedCoinView(Network network, ICheckpoints checkpoints, ICoindb coindb, IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, INodeStats nodeStats, ConsensusSettings consensusSettings, StakeChainStore stakeChainStore = null, IRewindDataIndexCache rewindDataIndexCache = null)
        {
            Guard.NotNull(coindb, nameof(CachedCoinView.coindb));

            this.coindb               = coindb;
            this.logger               = loggerFactory.CreateLogger(this.GetType().FullName);
            this.network              = network;
            this.checkpoints          = checkpoints;
            this.dateTimeProvider     = dateTimeProvider;
            this.consensusSettings    = consensusSettings;
            this.stakeChainStore      = stakeChainStore;
            this.rewindDataIndexCache = rewindDataIndexCache;
            this.lockobj              = new object();
            this.cachedUtxoItems      = new Dictionary <OutPoint, CacheItem>();
            this.performanceCounter   = new CachePerformanceCounter(this.dateTimeProvider);
            this.lastCacheFlushTime   = this.dateTimeProvider.GetUtcNow();
            this.cachedRewindData     = new Dictionary <int, RewindData>();
            this.random               = new Random();

            this.lastCheckpointHeight = this.checkpoints.GetLastCheckpointHeight();

            this.MaxCacheSizeBytes             = consensusSettings.MaxCoindbCacheInMB * 1024 * 1024;
            this.CacheFlushTimeIntervalSeconds = consensusSettings.CoindbIbdFlushMin * 60;

            if (nodeStats.DisplayBenchStats)
            {
                nodeStats.RegisterStats(this.AddBenchStats, StatsType.Benchmark, this.GetType().Name, 300);
            }
        }
Example #4
0
 /// <summary>
 ///     Initializes instance of the object based on memory based coinview.
 /// </summary>
 /// <param name="inner">Underlying coinview with memory based storage.</param>
 /// <param name="dateTimeProvider">Provider of time functions.</param>
 /// <param name="loggerFactory">Factory to be used to create logger for the puller.</param>
 /// <param name="nodeStats">The node stats.</param>
 /// <param name="stakeChainStore">Storage of POS block information.</param>
 /// <param name="rewindDataIndexCache">Rewind data index store.</param>
 /// <remarks>
 ///     This is used for testing the coinview.
 ///     It allows a coin view that only has in-memory entries.
 /// </remarks>
 public CachedCoinView(InMemoryCoinView inner, IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory,
                       INodeStats nodeStats, StakeChainStore stakeChainStore = null,
                       IRewindDataIndexCache rewindDataIndexCache            = null) :
     this(dateTimeProvider, loggerFactory, nodeStats, stakeChainStore, rewindDataIndexCache)
 {
     Guard.NotNull(inner, nameof(inner));
     this.inner = inner;
 }
Example #5
0
 /// <summary>
 /// Initializes instance of the object based.
 /// </summary>
 /// <param name="loggerFactory">Factory to be used to create logger for the puller.</param>
 /// <param name="stakeChainStore">Storage of POS block information.</param>
 private CachedCoinView(ILoggerFactory loggerFactory, StakeChainStore stakeChainStore = null)
 {
     this.logger             = loggerFactory.CreateLogger(this.GetType().FullName);
     this.stakeChainStore    = stakeChainStore;
     this.MaxItems           = CacheMaxItemsDefault;
     this.lockobj            = new ReaderWriterLock();
     this.unspents           = new Dictionary <uint256, CacheItem>();
     this.PerformanceCounter = new CachePerformanceCounter();
 }
Example #6
0
 /// <summary>
 /// Initializes instance of the object based.
 /// </summary>
 /// <param name="dateTimeProvider">Provider of time functions.</param>
 /// <param name="loggerFactory">Factory to be used to create logger for the puller.</param>
 /// <param name="stakeChainStore">Storage of POS block information.</param>
 private CachedCoinView(IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, StakeChainStore stakeChainStore = null)
 {
     this.logger             = loggerFactory.CreateLogger(this.GetType().FullName);
     this.dateTimeProvider   = dateTimeProvider;
     this.stakeChainStore    = stakeChainStore;
     this.MaxItems           = CacheMaxItemsDefault;
     this.lockobj            = new AsyncLock();
     this.unspents           = new Dictionary <uint256, CacheItem>();
     this.PerformanceCounter = new CachePerformanceCounter(this.dateTimeProvider);
     this.lastCacheFlushTime = this.dateTimeProvider.GetUtcNow();
 }
Example #7
0
        /// <summary>
        /// This is used for testing the coin view
        /// it allows a coinview that only has in memory entries
        /// </summary>
        public CachedCoinView(InMemoryCoinView inner, StakeChainStore stakeChainStore = null)
        {
            Guard.NotNull(inner, nameof(inner));

            this.inner              = inner;
            this.stakeChainStore    = stakeChainStore;
            this.MaxItems           = 100000;
            this.lockobj            = new Stratis.Bitcoin.Utilities.ReaderWriterLock();
            this.unspents           = new Dictionary <uint256, CacheItem>();
            this.PerformanceCounter = new CachePerformanceCounter();
        }
        /// <summary>
        /// Initializes instance of the object based.
        /// </summary>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="loggerFactory">Factory to be used to create logger for the puller.</param>
        /// <param name="stakeChainStore">Storage of POS block information.</param>
        private CachedCoinView(IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, INodeStats nodeStats, StakeChainStore stakeChainStore = null)
        {
            this.logger               = loggerFactory.CreateLogger(this.GetType().FullName);
            this.dateTimeProvider     = dateTimeProvider;
            this.stakeChainStore      = stakeChainStore;
            this.MaxItems             = CacheMaxItemsDefault;
            this.lockobj              = new AsyncLock();
            this.cachedUtxoItems      = new Dictionary <uint256, CacheItem>();
            this.performanceCounter   = new CachePerformanceCounter(this.dateTimeProvider);
            this.lastCacheFlushTime   = this.dateTimeProvider.GetUtcNow();
            this.cachedRewindDataList = new List <RewindData>();

            nodeStats.RegisterStats(this.AddBenchStats, StatsType.Benchmark, 300);
        }
Example #9
0
        /// <summary>
        /// Initializes instance of the object based.
        /// </summary>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="loggerFactory">Factory to be used to create logger for the puller.</param>
        /// <param name="nodeStats">The node stats.</param>
        /// <param name="stakeChainStore">Storage of POS block information.</param>
        /// <param name="rewindDataIndexCache">Rewind data index store.</param>
        private CachedCoinView(IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, INodeStats nodeStats, StakeChainStore stakeChainStore = null, IRewindDataIndexCache rewindDataIndexCache = null)
        {
            this.logger                = loggerFactory.CreateLogger("Impleum.Bitcoin.FullNode");
            this.dateTimeProvider      = dateTimeProvider;
            this.stakeChainStore       = stakeChainStore;
            this.rewindDataIndexCache  = rewindDataIndexCache;
            this.MaxItems              = CacheMaxItemsDefault;
            this.lockobj               = new object();
            this.cachedUtxoItems       = new Dictionary <uint256, CacheItem>();
            this.performanceCounter    = new CachePerformanceCounter(this.dateTimeProvider);
            this.lastCacheFlushTime    = this.dateTimeProvider.GetUtcNow();
            this.cachedRewindDataIndex = new SortedDictionary <int, RewindData>();
            this.random                = new Random();

            nodeStats.RegisterStats(this.AddBenchStats, StatsType.Benchmark, 300);
        }
 public CachedCoinView(Network network, ICheckpoints checkpoint, InMemoryCoinView inner, IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, INodeStats nodeStats, ConsensusSettings consensusSettings, StakeChainStore stakeChainStore = null, IRewindDataIndexCache rewindDataIndexCache = null) :
     this(network, checkpoint, dateTimeProvider, loggerFactory, nodeStats, consensusSettings, stakeChainStore, rewindDataIndexCache)
 {
     Guard.NotNull(inner, nameof(inner));
     this.inner = inner;
 }
Example #11
0
 /// <summary>
 /// Initializes instance of the object based on memory based coinview.
 /// </summary>
 /// <param name="inner">Underlying coinview with memory based storage.</param>
 /// <param name="dateTimeProvider">Provider of time functions.</param>
 /// <param name="loggerFactory">Factory to be used to create logger for the puller.</param>
 /// <param name="stakeChainStore">Storage of POS block information.</param>
 /// <remarks>
 /// This is used for testing the coinview.
 /// It allows a coin view that only has in-memory entries.
 /// </remarks>
 public CachedCoinView(InMemoryCoinView inner, IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, StakeChainStore stakeChainStore = null) :
     this(dateTimeProvider, loggerFactory, stakeChainStore)
 {
     Guard.NotNull(inner, nameof(inner));
     this.inner = inner;
 }
Example #12
0
        public static async Task <ITestChainContext> CreatePosAsync(Network network, Script scriptPubKey, string dataDir, bool requireStandard = true)
        {
            var nodeSettings = new NodeSettings(network, args: new string[] { $"-datadir={dataDir}" });

            ILoggerFactory    loggerFactory    = nodeSettings.LoggerFactory;
            IDateTimeProvider dateTimeProvider = DateTimeProvider.Default;

            network.Consensus.Options = new ConsensusOptions();

            var consensusRulesContainer = new ConsensusRulesContainer();

            foreach (var ruleType in network.Consensus.ConsensusRules.HeaderValidationRules)
            {
                // Don't check PoW of a header in this test.
                if (ruleType == typeof(CheckDifficultyPowRule))
                {
                    continue;
                }

                consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule);
            }
            foreach (Type ruleType in network.Consensus.ConsensusRules.FullValidationRules)
            {
                consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule);
            }
            foreach (Type ruleType in network.Consensus.ConsensusRules.PartialValidationRules)
            {
                consensusRulesContainer.PartialValidationRules.Add(Activator.CreateInstance(ruleType) as PartialValidationConsensusRule);
            }

            var consensusSettings = new ConsensusSettings(nodeSettings);
            var chain             = new ChainIndexer(network);
            var inMemoryCoinView  = new InMemoryCoinView(new HashHeightPair(chain.Tip));

            var chainState  = new ChainState();
            var deployments = new NodeDeployments(network, chain);

            var asyncProvider = new AsyncProvider(loggerFactory, new Mock <ISignals>().Object, new NodeLifetime());

            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, loggerFactory), new RewindDataIndexCache(dateTimeProvider, network, new FinalizedBlockInfoRepository(new HashHeightPair()), new Checkpoints()), asyncProvider, consensusRulesContainer).SetupRulesEngineParent();

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

            var genesis = new ChainedHeader(network.GetGenesis().Header, network.GenesisHash, 0);

            chainState.BlockStoreTip = genesis;
            await consensus.InitializeAsync(genesis).ConfigureAwait(false);

            var mempoolSettings = new MempoolSettings(nodeSettings)
            {
                RequireStandard = requireStandard
            };
            var blockPolicyEstimator = new BlockPolicyEstimator(mempoolSettings, loggerFactory, nodeSettings);
            var mempool     = new TxMempool(dateTimeProvider, blockPolicyEstimator, loggerFactory, nodeSettings);
            var mempoolLock = new MempoolSchedulerLock();

            // The mempool rule constructors aren't parameterless, so we have to manually inject the dependencies for every rule
            var mempoolRules = new List <MempoolRule>
            {
                new CheckConflictsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CheckCoinViewMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CreateMempoolEntryMempoolRule(network, mempool, mempoolSettings, chain, consensusRules, loggerFactory),
                new CheckSigOpsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CheckFeeMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CheckRateLimitMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CheckAncestorsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CheckReplacementMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CheckAllInputsMempoolRule(network, mempool, mempoolSettings, chain, consensusRules, deployments, loggerFactory),
                new CheckTxOutDustRule(network, mempool, mempoolSettings, chain, loggerFactory),
            };

            // We also have to check that the manually instantiated rules match the ones in the network, or the test isn't valid
            for (int i = 0; i < network.Consensus.MempoolRules.Count; i++)
            {
                if (network.Consensus.MempoolRules[i] != mempoolRules[i].GetType())
                {
                    throw new Exception("Mempool rule type mismatch");
                }
            }

            var mempoolValidator = new MempoolValidator(mempool, mempoolLock, dateTimeProvider, mempoolSettings, chain, inMemoryCoinView, loggerFactory, nodeSettings, consensusRules, mempoolRules, deployments);

            var            blocks = new List <Block>();
            var            srcTxs = new List <Transaction>();
            DateTimeOffset now    = DateTimeOffset.UtcNow;

            for (int i = 0; i < 50; i++)
            {
                uint  nonce = 0;
                Block block = network.CreateBlock();
                block.Header.HashPrevBlock = chain.Tip.HashBlock;
                block.Header.Bits          = block.Header.GetWorkRequired(network, chain.Tip);
                block.Header.UpdateTime(now, network, chain.Tip);

                Transaction coinbase = network.CreateTransaction();
                coinbase.AddInput(TxIn.CreateCoinbase(chain.Height + 1));
                coinbase.AddOutput(new TxOut(network.Consensus.ProofOfWorkReward, scriptPubKey));
                block.AddTransaction(coinbase);
                block.UpdateMerkleRoot();
                while (!block.CheckProofOfWork())
                {
                    block.Header.Nonce = ++nonce;
                }
                block.Header.PrecomputeHash();
                blocks.Add(block);
                chain.SetTip(block.Header);
                srcTxs.Add(block.Transactions[0]);

                inMemoryCoinView.SaveChanges(new List <UnspentOutput>()
                {
                    new UnspentOutput(new OutPoint(block.Transactions[0], 0), new Coins((uint)(i + 1), block.Transactions[0].Outputs.First(), block.Transactions[0].IsCoinBase))
                }, new HashHeightPair(chain.Tip.Previous), new HashHeightPair(chain.Tip));
            }

            return(new TestChainContext {
                MempoolValidator = mempoolValidator, MempoolSettings = mempoolSettings, ChainIndexer = chain, SrcTxs = srcTxs
            });
        }
Example #13
0
 /// <summary>
 /// Initializes instance of the object based on DBreeze based coinview.
 /// </summary>
 /// <param name="inner">Underlying coinview with database storage.</param>
 /// <param name="dateTimeProvider">Provider of time functions.</param>
 /// <param name="loggerFactory">Factory to be used to create logger for the puller.</param>
 /// <param name="stakeChainStore">Storage of POS block information.</param>
 public CachedCoinView(DBreezeCoinView inner, IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory, INodeStats nodeStats, StakeChainStore stakeChainStore = null) :
     this(dateTimeProvider, loggerFactory, nodeStats, stakeChainStore)
 {
     Guard.NotNull(inner, nameof(inner));
     this.inner = inner;
 }
        public static async Task <ITestChainContext> CreatePosAsync(Network network, Script scriptPubKey, string dataDir)
        {
            var nodeSettings = new NodeSettings(network, args: new string[] { $"-datadir={dataDir}" });

            ILoggerFactory    loggerFactory    = nodeSettings.LoggerFactory;
            IDateTimeProvider dateTimeProvider = DateTimeProvider.Default;

            network.Consensus.Options = new ConsensusOptions();

            var consensusRulesContainer = new ConsensusRulesContainer();

            foreach (var ruleType in network.Consensus.ConsensusRules.HeaderValidationRules)
            {
                // Dont check PoW of a header in this test.
                if (ruleType == typeof(CheckDifficultyPowRule))
                {
                    continue;
                }

                consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule);
            }
            foreach (var ruleType in network.Consensus.ConsensusRules.FullValidationRules)
            {
                consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule);
            }
            foreach (var ruleType in network.Consensus.ConsensusRules.PartialValidationRules)
            {
                consensusRulesContainer.PartialValidationRules.Add(Activator.CreateInstance(ruleType) as PartialValidationConsensusRule);
            }

            var consensusSettings = new ConsensusSettings(nodeSettings);
            var chain             = new ChainIndexer(network);
            var inMemoryCoinView  = new InMemoryCoinView(chain.Tip.HashBlock);

            var chainState  = new ChainState();
            var deployments = new NodeDeployments(network, chain);
            ConsensusRuleEngine consensusRules;

            var asyncProvider = new AsyncProvider(loggerFactory, new Mock <ISignals>().Object, new NodeLifetime());

            var stakeChain = new StakeChainStore(network, chain, null, loggerFactory);

            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, loggerFactory), new RewindDataIndexCache(dateTimeProvider, network), asyncProvider, consensusRulesContainer).SetupRulesEngineParent();

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

            var genesis = new ChainedHeader(network.GetGenesis().Header, network.GenesisHash, 0);

            chainState.BlockStoreTip = genesis;
            await consensus.InitializeAsync(genesis).ConfigureAwait(false);

            var blockPolicyEstimator = new BlockPolicyEstimator(new MempoolSettings(nodeSettings), loggerFactory, nodeSettings);
            var mempool     = new TxMempool(dateTimeProvider, blockPolicyEstimator, loggerFactory, nodeSettings);
            var mempoolLock = new MempoolSchedulerLock();

            var mempoolValidator = new MempoolValidator(mempool, mempoolLock, dateTimeProvider, new MempoolSettings(nodeSettings), chain, inMemoryCoinView, loggerFactory, nodeSettings, consensusRules);

            return(new TestChainContext {
                MempoolValidator = mempoolValidator
            });
        }
        public static async Task <ITestChainContext> CreatePosAsync(Network network, Script scriptPubKey, string dataDir)
        {
            var nodeSettings = new NodeSettings(network, args: new string[] { $"-datadir={dataDir}" });

            ILoggerFactory    loggerFactory    = nodeSettings.LoggerFactory;
            IDateTimeProvider dateTimeProvider = DateTimeProvider.Default;

            network.Consensus.Options = new ConsensusOptions();

            var consensusRulesContainer = new ConsensusRulesContainer();

            foreach (var ruleType in network.Consensus.ConsensusRules.HeaderValidationRules)
            {
                // Dont check PoW of a header in this test.
                if (ruleType == typeof(CheckDifficultyPowRule))
                {
                    continue;
                }

                consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule);
            }
            foreach (var ruleType in network.Consensus.ConsensusRules.FullValidationRules)
            {
                consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule);
            }
            foreach (var ruleType in network.Consensus.ConsensusRules.PartialValidationRules)
            {
                consensusRulesContainer.PartialValidationRules.Add(Activator.CreateInstance(ruleType) as PartialValidationConsensusRule);
            }

            var consensusSettings = new ConsensusSettings(nodeSettings);
            var chain             = new ChainIndexer(network);
            var inMemoryCoinView  = new InMemoryCoinView(chain.Tip.HashBlock);

            var chainState  = new ChainState();
            var deployments = new NodeDeployments(network, chain);
            ConsensusRuleEngine consensusRules;

            var asyncProvider = new AsyncProvider(loggerFactory, new Mock <ISignals>().Object, new NodeLifetime());

            var stakeChain = new StakeChainStore(network, chain, null, loggerFactory);

            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, loggerFactory), new RewindDataIndexCache(dateTimeProvider, network), asyncProvider, consensusRulesContainer).SetupRulesEngineParent();

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

            var genesis = new ChainedHeader(network.GetGenesis().Header, network.GenesisHash, 0);

            chainState.BlockStoreTip = genesis;
            await consensus.InitializeAsync(genesis).ConfigureAwait(false);

            var mempoolSettings      = new MempoolSettings(nodeSettings);
            var blockPolicyEstimator = new BlockPolicyEstimator(mempoolSettings, loggerFactory, nodeSettings);
            var mempool     = new TxMempool(dateTimeProvider, blockPolicyEstimator, loggerFactory, nodeSettings);
            var mempoolLock = new MempoolSchedulerLock();

            // The mempool rule constructors aren't parameterless, so we have to manually inject the dependencies for every rule
            var mempoolRules = new List <MempoolRule>
            {
                new CheckConflictsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CheckCoinViewMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CreateMempoolEntryMempoolRule(network, mempool, mempoolSettings, chain, consensusRules, loggerFactory),
                new CheckSigOpsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CheckFeeMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CheckRateLimitMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CheckAncestorsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CheckReplacementMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory),
                new CheckAllInputsMempoolRule(network, mempool, mempoolSettings, chain, consensusRules, loggerFactory),
                new CheckTxOutDustRule(network, mempool, mempoolSettings, chain, loggerFactory),
            };

            // We also have to check that the manually instantiated rules match the ones in the network, or the test isn't valid
            for (int i = 0; i < network.Consensus.MempoolRules.Count; i++)
            {
                if (network.Consensus.MempoolRules[i] != mempoolRules[i].GetType())
                {
                    throw new Exception("Mempool rule type mismatch");
                }
            }

            var nodeDeployments  = new NodeDeployments(network, chain);
            var mempoolValidator = new MempoolValidator(mempool, mempoolLock, dateTimeProvider, new MempoolSettings(nodeSettings), chain, inMemoryCoinView, loggerFactory, nodeSettings, consensusRules, mempoolRules, nodeDeployments);

            return(new TestChainContext {
                MempoolValidator = mempoolValidator
            });
        }
Example #16
0
 /// <summary>
 /// Initializes instance of the object based on DBreeze based coinview.
 /// </summary>
 /// <param name="inner">Underlaying coinview with database storage.</param>
 /// <param name="loggerFactory">Factory to be used to create logger for the puller.</param>
 /// <param name="stakeChainStore">Storage of POS block information.</param>
 public CachedCoinView(DBreezeCoinView inner, ILoggerFactory loggerFactory, StakeChainStore stakeChainStore = null) :
     this(loggerFactory, stakeChainStore)
 {
     Guard.NotNull(inner, nameof(inner));
     this.inner = inner;
 }