public async Task TxOutSmartContractExec_AllTransactions_ValidationFailAsync()
        {
            TestRulesContext           testContext = TestRulesContextFactory.CreateAsync(this.network);
            TxOutSmartContractExecRule rule        = testContext.CreateRule <TxOutSmartContractExecRule>();

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

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

            var transactions = new List <Transaction>
            {
                new Transaction()
                {
                    Outputs =
                    {
                        new TxOut(Money.Zero, new Script(new [] { (byte)ScOpcodeType.OP_CALLCONTRACT   })),
                        new TxOut(Money.Zero, new Script(new [] { (byte)ScOpcodeType.OP_CREATECONTRACT }))
                    }
                }
            };

            context.ValidationContext.BlockToValidate.Transactions = transactions;
            await Assert.ThrowsAsync <ConsensusErrorException>(async() => await rule.RunAsync(context));

            transactions = new List <Transaction>
            {
                new Transaction()
                {
                    Outputs =
                    {
                        new TxOut(Money.Zero, new Script(new [] { (byte)ScOpcodeType.OP_CALLCONTRACT })),
                        new TxOut(Money.Zero, new Script(new [] { (byte)ScOpcodeType.OP_CALLCONTRACT }))
                    }
                }
            };

            context.ValidationContext.BlockToValidate.Transactions = transactions;
            await Assert.ThrowsAsync <ConsensusErrorException>(async() => await rule.RunAsync(context));

            transactions = new List <Transaction>
            {
                new Transaction()
                {
                    Outputs =
                    {
                        new TxOut(Money.Zero, new Script(new [] { (byte)ScOpcodeType.OP_CREATECONTRACT })),
                        new TxOut(Money.Zero, new Script(new [] { (byte)ScOpcodeType.OP_CREATECONTRACT }))
                    }
                }
            };

            context.ValidationContext.BlockToValidate.Transactions = transactions;
            await Assert.ThrowsAsync <ConsensusErrorException>(async() => await rule.RunAsync(context));
        }
        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 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 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 void OpSpend_PreviousTransactionOther_FailureAsync()
        {
            TestRulesContext testContext = TestRulesContextFactory.CreateAsync(this.network);
            OpSpendRule      rule        = testContext.CreateRule <OpSpendRule>();

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

            context.ValidationContext.BlockToValidate = testContext.Network.Consensus.ConsensusFactory.CreateBlock();
            context.ValidationContext.BlockToValidate.Header.HashPrevBlock = testContext.Chain.Tip.HashBlock;
            context.ValidationContext.BlockToValidate.Transactions         = new List <Transaction>
            {
                new Transaction()
                {
                    Outputs =
                    {
                        new TxOut(Money.Zero, new Script(OpcodeType.OP_NOP))
                    }
                },
                new Transaction()
                {
                    Outputs =
                    {
                        new TxOut(Money.Zero, new Script(OpcodeType.OP_NOP))
                    }
                },
                new Transaction()
                {
                    Outputs =
                    {
                        new TxOut(Money.Zero, new Script(OpcodeType.OP_NOP))
                    }
                },
                new Transaction()
                {
                    Outputs =
                    {
                        new TxOut(new Money(1000), new Script(new [] { (byte)ScOpcodeType.OP_SPEND }))
                    }
                }
            };

            Task <ConsensusErrorException> error = Assert.ThrowsAsync <ConsensusErrorException>(async() => await rule.RunAsync(context));
        }
        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);
        }
        public async Task TxOutSmartContractExec_AllTransactions_ValidationSuccessAsync()
        {
            TestRulesContext           testContext = TestRulesContextFactory.CreateAsync(this.network);
            TxOutSmartContractExecRule rule        = testContext.CreateRule <TxOutSmartContractExecRule>();

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

            context.ValidationContext.Block = testContext.Network.Consensus.ConsensusFactory.CreateBlock();
            context.ValidationContext.Block.Header.HashPrevBlock = testContext.Chain.Tip.HashBlock;
            context.ValidationContext.Block.Transactions         = new List <Transaction>
            {
                new Transaction
                {
                    Outputs =
                    {
                        new TxOut(Money.Zero, new Script(new [] { (byte)ScOpcodeType.OP_CREATECONTRACT }))
                    }
                }
            };

            await rule.RunAsync(context);
        }
        public async Task OpSpend_PreviousTransactionNone_FailureAsync()
        {
            TestRulesContext testContext = TestRulesContextFactory.CreateAsync(this.network);
            OpSpendRule      rule        = testContext.CreateRule <OpSpendRule>();

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

            context.ValidationContext.BlockToValidate = testContext.Network.Consensus.ConsensusFactory.CreateBlock();
            context.ValidationContext.BlockToValidate.Header.HashPrevBlock = testContext.ChainIndexer.Tip.HashBlock;
            context.ValidationContext.BlockToValidate.Transactions         = new List <Transaction>
            {
                new Transaction()
                {
                    Inputs =
                    {
                        new TxIn(new Script(new [] { (byte)ScOpcodeType.OP_SPEND }))
                    }
                }
            };

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