public void CreateNewBlock_WithScript_ValidatesTemplateUsingRuleContext() { var newOptions = new ConsensusOptions(); this.ExecuteWithConsensusOptions(newOptions, () => { ConcurrentChain chain = GenerateChainWithHeight(5, this.testNet, this.key); this.SetupRulesEngine(chain); this.dateTimeProvider.Setup(d => d.GetAdjustedTimeAsUnixTimestamp()) .Returns(new DateTime(2017, 1, 7, 0, 0, 1, DateTimeKind.Utc).ToUnixTimestamp()); this.consensusLoop.Setup(c => c.Tip) .Returns(chain.GetBlock(5)); Transaction transaction = CreateTransaction(this.testNet, this.key, 5, new Money(400 * 1000 * 1000), new Key(), new uint256(124124)); var txFee = new Money(1000); SetupTxMempool(chain, this.testNet.Consensus.Options as ConsensusOptions, txFee, transaction); ValidationContext validationContext = null; var powRuleContext = new PowRuleContext(new ValidationContext(), this.testNet.Consensus, chain.Tip, this.dateTimeProvider.Object.GetTimeOffset()); this.consensusRules .Setup(s => s.CreateRuleContext(It.IsAny <ValidationContext>(), It.IsAny <ChainedHeader>())).Callback <ValidationContext, ChainedHeader>((r, s) => validationContext = r) .Returns(powRuleContext); var blockDefinition = new PowBlockDefinition(this.consensusLoop.Object, this.dateTimeProvider.Object, this.LoggerFactory.Object, this.txMempool.Object, new MempoolSchedulerLock(), this.minerSettings.Object, this.testNet, this.consensusRules.Object); BlockTemplate blockTemplate = blockDefinition.Build(chain.Tip, this.key.ScriptPubKey); Assert.NotNull(this.callbackRuleContext); Assert.True(this.callbackRuleContext.MinedBlock); Assert.Equal(blockTemplate.Block.GetHash(), validationContext.Block.GetHash()); Assert.Equal(chain.GetBlock(5).HashBlock, powRuleContext.ConsensusTip.HashBlock); this.consensusLoop.Verify(); }); }
public void TestBlockValidity_UsesRuleContextToValidateBlock() { var newOptions = new PowConsensusOptions() { MaxBlockWeight = 1500 }; this.ExecuteWithConsensusOptions(newOptions, () => { ConcurrentChain chain = GenerateChainWithHeight(5, this.network, new Key()); this.consensusLoop.Setup(c => c.Tip).Returns(chain.GetBlock(5)); ValidationContext validationContext = null; var powRuleContext = new PowRuleContext(new ValidationContext(), this.network.Consensus, chain.Tip, this.dateTimeProvider.Object.GetTimeOffset()); this.consensusRules .Setup(s => s.CreateRuleContext(It.IsAny <ValidationContext>(), It.IsAny <ChainedHeader>())).Callback <ValidationContext, ChainedHeader>((r, s) => validationContext = r) .Returns(powRuleContext); var powBlockAssembler = new PowTestBlockDefinition(this.consensusLoop.Object, this.dateTimeProvider.Object, this.LoggerFactory.Object, this.txMempool.Object, new MempoolSchedulerLock(), this.network, this.consensusRules.Object); Block block = powBlockAssembler.TestBlockValidity(); Assert.NotNull(this.callbackRuleContext); Assert.True(this.callbackRuleContext.MinedBlock); Assert.Equal(block.GetHash(), validationContext.Block.GetHash()); Assert.Equal(chain.GetBlock(5).HashBlock, powRuleContext.ConsensusTip.HashBlock); Assert.Equal(1500, this.callbackRuleContext.Consensus.Option <PowConsensusOptions>().MaxBlockWeight); this.consensusLoop.Verify(); }); }
public async Task BlockReceived_NotNextBlock_ValidationFailAsync() { var testContext = TestRulesContextFactory.CreateAsync(Network.RegTest); BlockHeaderRule blockHeaderRule = testContext.CreateRule <BlockHeaderRule>(); var context = new PowRuleContext(new ValidationContext(), Network.RegTest.Consensus, testContext.Chain.Tip); context.ValidationContext.Block = Network.RegTest.Consensus.ConsensusFactory.CreateBlock(); context.ValidationContext.Block.Header.HashPrevBlock = uint256.Zero; var error = await Assert.ThrowsAsync <ConsensusErrorException>(async() => await blockHeaderRule.RunAsync(context)); Assert.Equal(ConsensusErrors.InvalidPrevTip, error.ConsensusError); }
public async Task SmartContractFormatRule_MultipleOutputs_SuccessAsync() { TestRulesContext testContext = TestRulesContextFactory.CreateAsync(this.network); ContractTransactionPartialValidationRule rule = testContext.CreateContractValidationRule(); var context = new PowRuleContext(new ValidationContext(), testContext.DateTimeProvider.GetTimeOffset()) { UnspentOutputSet = GetMockOutputSet() }; context.ValidationContext.BlockToValidate = testContext.Network.Consensus.ConsensusFactory.CreateBlock(); var gasPriceSatoshis = 20; var gasLimit = 4000000; var gasBudgetSatoshis = gasPriceSatoshis * gasLimit; var relayFeeSatoshis = 10000; var change = 200000; var totalSuppliedSatoshis = gasBudgetSatoshis + relayFeeSatoshis; var contractTxData = new ContractTxData(1, (ulong)gasPriceSatoshis, (Stratis.SmartContracts.RuntimeObserver.Gas)gasLimit, 0, "TestMethod"); var serialized = this.callDataSerializer.Serialize(contractTxData); Transaction funding = new Transaction { Outputs = { new TxOut(totalSuppliedSatoshis + change, new Script()) } }; var transactionBuilder = new TransactionBuilder(testContext.Network); transactionBuilder.AddCoins(funding); transactionBuilder.SendFees(totalSuppliedSatoshis); transactionBuilder.Send(new Script(serialized), 0); // Add a change output to the transaction transactionBuilder.SetChange(new Script()); Transaction transaction = transactionBuilder.BuildTransaction(false); context.ValidationContext.BlockToValidate.Transactions = new List <Transaction> { transaction }; await rule.RunAsync(context); }
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); }
public async Task BlockReceived_IsNextBlock_ValidationSucessAsync() { var testContext = TestRulesContextFactory.CreateAsync(Network.RegTest); BlockHeaderRule blockHeaderRule = testContext.CreateRule <BlockHeaderRule>(); var context = new PowRuleContext(new ValidationContext(), Network.RegTest.Consensus, testContext.Chain.Tip); 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 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); }
public async Task BlockReceived_IsNextBlock_ValidationSucessAsync() { TestRulesContext testContext = TestRulesContextFactory.CreateAsync(Network.RegTest); var blockHeaderRule = testContext.CreateRule <SetActivationDeploymentsRule>(); 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; context.ValidationContext.ChainedHeader = new ChainedHeader(context.ValidationContext.Block.Header, context.ValidationContext.Block.Header.GetHash(), 0); await blockHeaderRule.RunAsync(context); Assert.NotNull(context.ValidationContext.ChainedHeader); Assert.NotNull(context.ConsensusTip); Assert.NotNull(context.Flags); }
public async Task SmartContractFormatRule_FailureAsync() { TestRulesContext testContext = TestRulesContextFactory.CreateAsync(this.network); ContractTransactionPartialValidationRule rule = testContext.CreateContractValidationRule(); var context = new PowRuleContext(new ValidationContext(), testContext.DateTimeProvider.GetTimeOffset()); context.ValidationContext.BlockToValidate = testContext.Network.Consensus.ConsensusFactory.CreateBlock(); var gasPriceSatoshis = 20; var gasLimit = 4000000; var gasBudgetSatoshis = gasPriceSatoshis * gasLimit; var relayFeeSatoshis = 10000; var totalSuppliedSatoshis = gasBudgetSatoshis + relayFeeSatoshis; var higherGasLimit = gasLimit + 10000; var contractTxData = new ContractTxData(1, (ulong)gasPriceSatoshis, (Stratis.SmartContracts.RuntimeObserver.Gas)higherGasLimit, 0, "TestMethod"); var serialized = this.callDataSerializer.Serialize(contractTxData); Transaction funding = new Transaction { Outputs = { new TxOut(totalSuppliedSatoshis, new Script()) } }; var transactionBuilder = new TransactionBuilder(testContext.Network); transactionBuilder.AddCoins(funding); transactionBuilder.SendFees(relayFeeSatoshis); transactionBuilder.Send(new Script(serialized), gasBudgetSatoshis); Transaction transaction = transactionBuilder.BuildTransaction(false); context.ValidationContext.BlockToValidate.Transactions = new List <Transaction> { new Transaction(), // Include an empty transaction to ensure we're checking multiple. transaction }; await Assert.ThrowsAsync <ConsensusErrorException>(async() => await rule.RunAsync(context)); }
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 void SmartContractFormatRule_FailureAsync() { TestRulesContext testContext = TestRulesContextFactory.CreateAsync(this.network); SmartContractFormatRule rule = testContext.CreateRule <SmartContractFormatRule>(); var context = new PowRuleContext(new ValidationContext(), testContext.DateTimeProvider.GetTimeOffset()); context.ValidationContext.BlockToValidate = testContext.Network.Consensus.ConsensusFactory.CreateBlock(); var gasPriceSatoshis = 20; var gasLimit = 4000000; var gasBudgetSatoshis = gasPriceSatoshis * gasLimit; var relayFeeSatoshis = 10000; var totalSuppliedSatoshis = gasBudgetSatoshis + relayFeeSatoshis; var higherGasLimit = gasLimit + 10000; var carrier = SmartContractCarrier.CallContract(1, 0, "TestMethod", (ulong)gasPriceSatoshis, (Gas)higherGasLimit); var serialized = carrier.Serialize(); Transaction funding = new Transaction { Outputs = { new TxOut(totalSuppliedSatoshis, new Script()) } }; var transactionBuilder = new TransactionBuilder(testContext.Network); transactionBuilder.AddCoins(funding); transactionBuilder.SendFees(relayFeeSatoshis); transactionBuilder.Send(new Script(serialized), gasBudgetSatoshis); Transaction transaction = transactionBuilder.BuildTransaction(false); context.ValidationContext.BlockToValidate.Transactions = new List <Transaction> { transaction }; Task <ConsensusErrorException> error = Assert.ThrowsAsync <ConsensusErrorException>(async() => await rule.RunAsync(context)); }
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); }
public async Task SmartContractFormatRule_SuccessAsync() { TestRulesContext testContext = TestRulesContextFactory.CreateAsync(this.network); SmartContractFormatRule rule = testContext.CreateRule <SmartContractFormatRule>(); var context = new PowRuleContext(new ValidationContext(), testContext.DateTimeProvider.GetTimeOffset()); context.UnspentOutputSet = GetMockOutputSet(); context.ValidationContext.BlockToValidate = testContext.Network.Consensus.ConsensusFactory.CreateBlock(); var gasPriceSatoshis = 20; var gasLimit = 4_000_000; var gasBudgetSatoshis = gasPriceSatoshis * gasLimit; var relayFeeSatoshis = 10000; var totalSuppliedSatoshis = gasBudgetSatoshis + relayFeeSatoshis; var carrier = SmartContractCarrier.CallContract(1, 0, "TestMethod", (ulong)gasPriceSatoshis, (Gas)gasLimit); var serialized = carrier.Serialize(); Transaction funding = new Transaction { Outputs = { new TxOut(totalSuppliedSatoshis, new Script()) } }; var transactionBuilder = new TransactionBuilder(testContext.Network); transactionBuilder.AddCoins(funding); transactionBuilder.SendFees(relayFeeSatoshis + gasBudgetSatoshis); transactionBuilder.Send(new Script(serialized), 0); Transaction transaction = transactionBuilder.BuildTransaction(false); context.ValidationContext.BlockToValidate.Transactions = new List <Transaction> { transaction }; await rule.RunAsync(context); }