Exemple #1
0
        public async Task ChecBlockFutureTimestamp_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.ChainedBlock = new ChainedBlock(context.BlockValidationContext.Block.Header, context.BlockValidationContext.Block.Header.GetHash(NetworkOptions.TemporaryOptions), context.ConsensusTip);
            context.SetBestBlock(DateTimeProvider.Default.GetTimeOffset());

            // increment the bits.
            context.NextWorkRequired = context.BlockValidationContext.ChainedBlock.GetNextWorkRequired(Network.RegTest.Consensus);
            context.BlockValidationContext.Block.Header.BlockTime = context.Time.AddHours(3);

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

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

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

            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.BlockTime = context.ConsensusTip.Header.BlockTime.AddSeconds(-1);

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

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