Example #1
0
        public async Task BlockReceived_NotNextBlock_ValidationFailAsync()
        {
            TestRulesContext testContext = TestRulesContextFactory.CreateAsync(Network.RegTest);
            var blockHeaderRule          = testContext.CreateRule <BlockHeaderRule>();

            var context = new PowRuleContext(new ValidationContext(), Network.RegTest.Consensus, testContext.Chain.Tip, testContext.DateTimeProvider.GetTimeOffset());

            context.ValidationContext.Block = Network.RegTest.Consensus.ConsensusFactory.CreateBlock();
            context.ValidationContext.Block.Header.HashPrevBlock = uint256.Zero;
            ConsensusErrorException error = await Assert.ThrowsAsync <ConsensusErrorException>(async() => await blockHeaderRule.RunAsync(context));

            Assert.Equal(ConsensusErrors.InvalidPrevTip, error.ConsensusError);
        }
Example #2
0
        public async Task BlockReceived_IsNextBlock_ValidationSucessAsync()
        {
            TestRulesContext testContext = TestRulesContextFactory.CreateAsync(this.network);
            var blockHeaderRule          = testContext.CreateRule <SetActivationDeploymentsPartialValidationRule>();

            var context = new PowRuleContext(new ValidationContext(), testContext.DateTimeProvider.GetTimeOffset());

            context.ValidationContext.BlockToValidate = KnownNetworks.RegTest.Consensus.ConsensusFactory.CreateBlock();
            context.ValidationContext.BlockToValidate.Header.HashPrevBlock = testContext.ChainIndexer.Tip.HashBlock;
            context.ValidationContext.ChainedHeaderToValidate = new ChainedHeader(context.ValidationContext.BlockToValidate.Header, context.ValidationContext.BlockToValidate.Header.GetHash(), 0);

            await blockHeaderRule.RunAsync(context);

            Assert.NotNull(context.ValidationContext.ChainedHeaderToValidate);
            Assert.NotNull(context.Flags);
        }
Example #3
0
        public async Task ChecBlockFutureTimestamp_ValidationFailAsync()
        {
            TestRulesContext testContext = TestRulesContextFactory.CreateAsync(Network.RegTest);
            var rule = testContext.CreateRule<HeaderTimeChecksRule>();

            RuleContext context = new PowRuleContext(new ValidationContext(), Network.RegTest.Consensus, testContext.Chain.Tip, testContext.DateTimeProvider.GetTimeOffset());
            context.ValidationContext.Block = TestRulesContextFactory.MineBlock(Network.RegTest, testContext.Chain);
            context.ValidationContext.ChainedHeader = new ChainedHeader(context.ValidationContext.Block.Header, context.ValidationContext.Block.Header.GetHash(), context.ConsensusTip);
            context.Time = DateTimeProvider.Default.GetTimeOffset();

            // increment the bits.
            context.ValidationContext.Block.Header.BlockTime = context.Time.AddHours(3);

            ConsensusErrorException error = await Assert.ThrowsAsync<ConsensusErrorException>(async () => await rule.RunAsync(context));
            Assert.Equal(ConsensusErrors.TimeTooNew, error.ConsensusError);
        }
Example #4
0
        public async Task BlockReceived_IsNextBlock_ValidationSucessAsync()
        {
            TestRulesContext testContext = TestRulesContextFactory.CreateAsync(Network.RegTest);
            var blockHeaderRule          = testContext.CreateRule <BlockHeaderRule>();

            var context = new PowRuleContext(new ValidationContext(), Network.RegTest.Consensus, testContext.Chain.Tip, testContext.DateTimeProvider.GetTimeOffset());

            context.ValidationContext.Block = Network.RegTest.Consensus.ConsensusFactory.CreateBlock();
            context.ValidationContext.Block.Header.HashPrevBlock = testContext.Chain.Tip.HashBlock;

            await blockHeaderRule.RunAsync(context);

            Assert.NotNull(context.ValidationContext.ChainedHeader);
            Assert.NotNull(context.ConsensusTip);
            Assert.NotNull(context.Flags);
        }
        public void ChecBlockFutureTimestamp_ValidationFail()
        {
            TestRulesContext testContext = TestRulesContextFactory.CreateAsync(this.network);
            var rule = testContext.CreateRule <HeaderTimeChecksRule>();

            RuleContext context = new PowRuleContext(new ValidationContext(), testContext.DateTimeProvider.GetTimeOffset());

            context.ValidationContext.BlockToValidate         = TestRulesContextFactory.MineBlock(KnownNetworks.RegTest, testContext.ChainIndexer);
            context.ValidationContext.ChainedHeaderToValidate = new ChainedHeader(context.ValidationContext.BlockToValidate.Header, context.ValidationContext.BlockToValidate.Header.GetHash(), testContext.ChainIndexer.Tip);
            context.Time = DateTimeProvider.Default.GetTimeOffset();

            // increment the bits.
            context.ValidationContext.BlockToValidate.Header.BlockTime = context.Time.AddHours(3);

            ConsensusErrorException error = Assert.Throws <ConsensusErrorException>(() => rule.Run(context));

            Assert.Equal(ConsensusErrors.TimeTooNew, error.ConsensusError);
        }
        public async Task CheckHeaderBits_ValidationFailAsync()
        {
            TestRulesContext testContext = TestRulesContextFactory.CreateAsync(Network.RegTest);
            var rule = testContext.CreateRule <BlockHeaderPowContextualRule>();

            RuleContext context = new PowRuleContext(new ValidationContext(), Network.RegTest.Consensus, testContext.Chain.Tip, testContext.DateTimeProvider.GetTimeOffset());

            context.ValidationContext.Block         = TestRulesContextFactory.MineBlock(Network.RegTest, testContext.Chain);
            context.ValidationContext.ChainedHeader = new ChainedHeader(context.ValidationContext.Block.Header, context.ValidationContext.Block.Header.GetHash(), context.ConsensusTip);
            context.Time = DateTimeProvider.Default.GetTimeOffset();

            // increment the bits.
            context.NextWorkRequired = context.ValidationContext.ChainedHeader.GetNextWorkRequired(Network.RegTest.Consensus);
            context.ValidationContext.Block.Header.Bits += 1;

            ConsensusErrorException error = await Assert.ThrowsAsync <ConsensusErrorException>(async() => await rule.RunAsync(context));

            Assert.Equal(ConsensusErrors.BadDiffBits, error.ConsensusError);
        }
Example #7
0
        public async Task ChecBlockPreviousTimestamp_ValidationFailAsync()
        {
            TestRulesContext             testContext = TestRulesContextFactory.CreateAsync(Network.RegTest);
            BlockHeaderPowContextualRule rule        = testContext.CreateRule <BlockHeaderPowContextualRule>();

            RuleContext context = new RuleContext(new BlockValidationContext(), Network.RegTest.Consensus, testContext.Chain.Tip);

            context.BlockValidationContext.Block         = TestRulesContextFactory.MineBlock(Network.RegTest, testContext.Chain);
            context.BlockValidationContext.ChainedHeader = new ChainedHeader(context.BlockValidationContext.Block.Header, context.BlockValidationContext.Block.Header.GetHash(), context.ConsensusTip);
            context.SetBestBlock(DateTimeProvider.Default.GetTimeOffset());

            // increment the bits.
            context.NextWorkRequired = context.BlockValidationContext.ChainedHeader.GetNextWorkRequired(Network.RegTest.Consensus);
            context.BlockValidationContext.Block.Header.BlockTime = context.BestBlock.Header.BlockTime.AddSeconds(-1);

            var error = await Assert.ThrowsAsync <ConsensusErrorException>(async() => await rule.RunAsync(context));

            Assert.Equal(ConsensusErrors.TimeTooOld, error.ConsensusError);
        }