Exemplo n.º 1
0
        public void InIBDIfTipIsOlderThanMaxAge()
        {
            BlockHeader blockHeader = this.network.Consensus.ConsensusFactory.CreateBlockHeader();

            // Enough work to get us past the chain work check.
            blockHeader.Bits = new Target(new uint256(uint.MaxValue));

            // Block has a time sufficiently in the past that it can't be the tip.
            blockHeader.Time = ((uint)DateTimeOffset.Now.ToUnixTimeSeconds()) - (uint)this.network.MaxTipAge - 1;

            this.chainState.ConsensusTip = new ChainedHeader(blockHeader, uint256.Zero, this.checkpoints.GetLastCheckpointHeight() + 1);
            var blockDownloadState = new InitialBlockDownloadState(this.chainState, this.network, this.consensusSettings, this.checkpoints, this.loggerFactory.Object, DateTimeProvider.Default);

            Assert.True(blockDownloadState.IsInitialBlockDownload());
        }
        /// <summary>
        /// Creates the consensus manager used by <see cref="PosCoinViewRuleFailsAsync"/>.
        /// </summary>
        /// <param name="unspentOutputs">The dictionary used to mock up the <see cref="ICoinView"/>.</param>
        /// <returns>The constructed consensus manager.</returns>
        private async Task <ConsensusManager> CreateConsensusManagerAsync(Dictionary <OutPoint, UnspentOutput> unspentOutputs)
        {
            this.consensusSettings = new ConsensusSettings(Configuration.NodeSettings.Default(this.network));
            var initialBlockDownloadState = new InitialBlockDownloadState(this.chainState.Object, this.network, this.consensusSettings, new Checkpoints(), DateTimeProvider.Default);
            var signals       = new Signals.Signals(this.loggerFactory.Object, null);
            var asyncProvider = new AsyncProvider(this.loggerFactory.Object, signals);

            var consensusRulesContainer = new ConsensusRulesContainer();

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

            // Register POS consensus rules.
            // new FullNodeBuilderConsensusExtension.PosConsensusRulesRegistration().RegisterRules(this.network.Consensus);
            var consensusRuleEngine = new PosConsensusRuleEngine(this.network, this.loggerFactory.Object, DateTimeProvider.Default,
                                                                 this.ChainIndexer, this.nodeDeployments, this.consensusSettings, this.checkpoints.Object, this.coinView.Object, this.stakeChain.Object,
                                                                 this.stakeValidator.Object, this.chainState.Object, new InvalidBlockHashStore(this.dateTimeProvider.Object), new Mock <INodeStats>().Object, this.rewindDataIndexStore.Object, this.asyncProvider, consensusRulesContainer)
                                      .SetupRulesEngineParent();

            var headerValidator    = new HeaderValidator(consensusRuleEngine, this.loggerFactory.Object);
            var integrityValidator = new IntegrityValidator(consensusRuleEngine, this.loggerFactory.Object);
            var partialValidator   = new PartialValidator(asyncProvider, consensusRuleEngine, this.loggerFactory.Object);
            var fullValidator      = new FullValidator(consensusRuleEngine, this.loggerFactory.Object);

            // Create the chained header tree.
            var chainedHeaderTree = new ChainedHeaderTree(this.network, this.loggerFactory.Object, headerValidator, this.checkpoints.Object,
                                                          this.chainState.Object, new Mock <IFinalizedBlockInfoRepository>().Object, this.consensusSettings, new InvalidBlockHashStore(new DateTimeProvider()), new ChainWorkComparer());

            // Create consensus manager.
            var consensus = new ConsensusManager(chainedHeaderTree, this.network, this.loggerFactory.Object, this.chainState.Object, integrityValidator,
                                                 partialValidator, fullValidator, consensusRuleEngine, new Mock <IFinalizedBlockInfoRepository>().Object, signals,
                                                 new Mock <IPeerBanning>().Object, initialBlockDownloadState, this.ChainIndexer, new Mock <IBlockPuller>().Object, new Mock <IBlockStore>().Object,
                                                 new Mock <IConnectionManager>().Object, new Mock <INodeStats>().Object, new Mock <INodeLifetime>().Object, this.consensusSettings, this.dateTimeProvider.Object);

            // Mock the coinviews "FetchCoinsAsync" method. We will use the "unspentOutputs" dictionary to track spendable outputs.
            this.coinView.Setup(d => d.FetchCoins(It.IsAny <OutPoint[]>()))
            .Returns((OutPoint[] txIds) =>
            {
                var result = new FetchCoinsResponse();

                for (int i = 0; i < txIds.Length; i++)
                {
                    unspentOutputs.TryGetValue(txIds[i], out UnspentOutput unspent);
                    result.UnspentOutputs.Add(txIds[i], unspent);
                }

                return(result);
            });

            // Mock the coinviews "GetTipHashAsync" method.
            this.coinView.Setup(d => d.GetTipHash()).Returns(() =>
            {
                return(new HashHeightPair(this.ChainIndexer.Tip));
            });

            // Since we are mocking the stake validator ensure that GetNextTargetRequired returns something sensible. Otherwise we get the "bad-diffbits" error.
            this.stakeValidator.Setup(s => s.GetNextTargetRequired(It.IsAny <IStakeChain>(), It.IsAny <ChainedHeader>(), It.IsAny <IConsensus>(), It.IsAny <bool>()))
            .Returns(this.network.Consensus.PowLimit);

            // Skip validation of signature in the proven header
            this.stakeValidator.Setup(s => s.VerifySignature(It.IsAny <UnspentOutput>(), It.IsAny <Transaction>(), 0, It.IsAny <ScriptVerify>()))
            .Returns(true);

            // Skip validation of stake kernel
            this.stakeValidator.Setup(s => s.CheckStakeKernelHash(It.IsAny <PosRuleContext>(), It.IsAny <uint>(), It.IsAny <uint256>(), It.IsAny <UnspentOutput>(), It.IsAny <OutPoint>(), It.IsAny <uint>()))
            .Returns(true);

            // Since we are mocking the stakechain ensure that the Get returns a BlockStake. Otherwise this results in "previous stake is not found".
            this.stakeChain.Setup(d => d.Get(It.IsAny <uint256>())).Returns(new BlockStake()
            {
                Flags           = BlockFlag.BLOCK_PROOF_OF_STAKE,
                StakeModifierV2 = 0,
                StakeTime       = (this.ChainIndexer.Tip.Header.Time + 60) & ~PosConsensusOptions.StakeTimestampMask
            });

            // Since we are mocking the chainState ensure that the BlockStoreTip returns a usable value.
            this.chainState.Setup(d => d.BlockStoreTip).Returns(this.ChainIndexer.Tip);

            // Since we are mocking the chainState ensure that the ConsensusTip returns a usable value.
            this.chainState.Setup(d => d.ConsensusTip).Returns(this.ChainIndexer.Tip);

            // Initialize the consensus manager.
            await consensus.InitializeAsync(this.ChainIndexer.Tip);

            return(consensus);
        }
Exemplo n.º 3
0
        public void NotInIBDIfChainStateIsNull()
        {
            var blockDownloadState = new InitialBlockDownloadState(null, this.network, this.nodeSettings, this.checkpoints);

            Assert.False(blockDownloadState.IsInitialBlockDownload());
        }
Exemplo n.º 4
0
        public void NotInIBDIfChainStateIsNull()
        {
            var blockDownloadState = new InitialBlockDownloadState(null, this.network, this.consensusSettings, this.checkpoints, this.loggerFactory.Object, DateTimeProvider.Default);

            Assert.False(blockDownloadState.IsInitialBlockDownload());
        }