Exemple #1
0
        public void Constructor_ExceptionTest()
        {
            var ut  = new MockUtxoDatabase();
            var mem = new MockMempool(null);
            var c   = new MockConsensus(MockHeight);

            Assert.Throws <ArgumentNullException>(() => new TransactionVerifier(true, null, mem, c));
            Assert.Throws <ArgumentNullException>(() => new TransactionVerifier(true, ut, null, c));
            Assert.Throws <ArgumentNullException>(() => new TransactionVerifier(true, ut, mem, null));
        }
Exemple #2
0
        public static IEnumerable <object[]> GetTxVerifyCases()
        {
            var c = new MockConsensus()
            {
                expHeight = MockHeight,
                bip65     = true,
                bip112    = true,
                strictDer = true,
                bip147    = true,
                bip16     = true,
                segWit    = true,
            };

            byte[] simpTxHash1 = new byte[32];
            simpTxHash1[1] = 1;
            byte[] simpTxHash2 = new byte[32];
            simpTxHash2[1] = 2;
            byte[] simpTxHash3 = new byte[32];
            simpTxHash3[1] = 3;

            var simpPubScr = new PubkeyScript();
            var simpSigScr = new SignatureScript(new byte[1] {
                (byte)OP._1
            });

            // *** Fee Tests ***

            yield return(new object[]
            {
                new MockUtxoDatabase(simpTxHash1, new MockUtxo()
                {
                    Amount = 0, Index = 0, PubScript = simpPubScr
                }),
                new MockMempool(null),
                c,
                new Transaction()
                {
                    TxInList = new TxIn[1]
                    {
                        new TxIn(simpTxHash1, 0, simpSigScr, uint.MaxValue)
                    },
                    TxOutList = new TxOut[1]
                    {
                        new TxOut(0, simpPubScr)
                    }
                },
                true,  // Verification success
                null,  // Error
                0,     // Added SigOpCount
                0,     // Added fee
                false, // AnySegWit
            });

            yield return(new object[]
            {
                new MockUtxoDatabase(simpTxHash1, new MockUtxo()
                {
                    Amount = 0, Index = 0, PubScript = simpPubScr
                }),
                new MockMempool(null),
                c,
                new Transaction()
                {
                    TxInList = new TxIn[1]
                    {
                        new TxIn(simpTxHash1, 0, simpSigScr, uint.MaxValue)
                    },
                    TxOutList = new TxOut[1]
                    {
                        new TxOut(1, simpPubScr)
                    }
                },
                false,                                       // Verification success
                "Transaction is spending more than it can.", // Error
                0,                                           // Added SigOpCount
                0,                                           // Added fee
                false,                                       // AnySegWit
            });

            yield return(new object[]
            {
                new MockUtxoDatabase(simpTxHash1, new MockUtxo()
                {
                    Amount = 123, Index = 0, PubScript = simpPubScr
                }),
                new MockMempool(null),
                c,
                new Transaction()
                {
                    TxInList = new TxIn[1]
                    {
                        new TxIn(simpTxHash1, 0, simpSigScr, uint.MaxValue)
                    },
                    TxOutList = new TxOut[1]
                    {
                        new TxOut(3, simpPubScr)
                    }
                },
                true,  // Verification success
                null,  // Error
                0,     // Added SigOpCount
                120,   // Added fee
                false, // AnySegWit
            });

            yield return(new object[]
            {
                // Make sure ulong is being used for calculation of fee
                new MockUtxoDatabase(simpTxHash1, new MockUtxo()
                {
                    Amount = Constants.TotalSupply, Index = 0, PubScript = simpPubScr
                }),
                new MockMempool(null),
                c,
                new Transaction()
                {
                    TxInList = new TxIn[1]
                    {
                        new TxIn(simpTxHash1, 0, simpSigScr, uint.MaxValue)
                    },
                    TxOutList = new TxOut[1]
                    {
                        new TxOut(1000, simpPubScr)
                    }
                },
                true,                           // Verification success
                null,                           // Error
                0,                              // Added SigOpCount
                Constants.TotalSupply - 1000UL, // Added fee
                false,                          // AnySegWit
            });

            yield return(new object[]
            {
                new MockUtxoDatabase(new byte[][] { simpTxHash1, simpTxHash2, simpTxHash3 },
                                     new IUtxo[]
                {
                    new MockUtxo()
                    {
                        Amount = 13, Index = 3, PubScript = simpPubScr
                    },
                    new MockUtxo()
                    {
                        Amount = 57, Index = 7, PubScript = simpPubScr
                    },
                    new MockUtxo()
                    {
                        Amount = 73, Index = 5, PubScript = simpPubScr
                    },
                }),
                new MockMempool(null),
                c,
                new Transaction()
                {
                    TxInList = new TxIn[3]
                    {
                        new TxIn(simpTxHash1, 3, simpSigScr, uint.MaxValue),
                        new TxIn(simpTxHash2, 7, simpSigScr, uint.MaxValue),
                        new TxIn(simpTxHash3, 5, simpSigScr, uint.MaxValue),
                    },
                    TxOutList = new TxOut[3]
                    {
                        new TxOut(140, simpPubScr),
                        new TxOut(3, simpPubScr),
                        new TxOut(1, simpPubScr),
                    }
                },
                false,                                       // Verification success
                "Transaction is spending more than it can.", // Error
                0,                                           // Added SigOpCount
                0,                                           // Added fee
                false,                                       // AnySegWit
            });

            yield return(new object[]
            {
                new MockUtxoDatabase(new byte[][] { simpTxHash1, simpTxHash2, simpTxHash3 },
                                     new IUtxo[]
                {
                    new MockUtxo()
                    {
                        Amount = 13, Index = 3, PubScript = simpPubScr
                    },
                    new MockUtxo()
                    {
                        Amount = 57, Index = 7, PubScript = simpPubScr
                    },
                    new MockUtxo()
                    {
                        Amount = 73, Index = 5, PubScript = simpPubScr
                    },
                }),
                new MockMempool(null),
                c,
                new Transaction()
                {
                    TxInList = new TxIn[3]
                    {
                        new TxIn(simpTxHash1, 3, simpSigScr, uint.MaxValue),
                        new TxIn(simpTxHash2, 7, simpSigScr, uint.MaxValue),
                        new TxIn(simpTxHash3, 5, simpSigScr, uint.MaxValue),
                    },
                    TxOutList = new TxOut[3]
                    {
                        new TxOut(12, simpPubScr),
                        new TxOut(6, simpPubScr),
                        new TxOut(50, simpPubScr),
                    }
                },
                true,  // Verification success
                null,  // Error
                0,     // Added SigOpCount
                75,    // Added fee
                false, // AnySegWit
            });

            // *** Transaction Tests (from signtx cases) ***
            foreach (var Case in Helper.ReadResource <JArray>("SignedTxTestData"))
            {
                Transaction prevTx = new Transaction();
                prevTx.TryDeserialize(new FastStreamReader(Helper.HexToBytes(Case["TxToSpend"].ToString())), out string _);
                byte[] prevTxHash = prevTx.GetTransactionHash();
                var    utxoDb     = new MockUtxoDatabase();

                for (int i = 0; i < prevTx.TxOutList.Length; i++)
                {
                    var utxo = new MockUtxo()
                    {
                        Amount    = prevTx.TxOutList[i].Amount,
                        Index     = (uint)i,
                        PubScript = prevTx.TxOutList[i].PubScript
                    };

                    utxoDb.Add(prevTxHash, utxo);
                }

                foreach (var item in Case["Cases"])
                {
                    Transaction      tx     = new Transaction();
                    FastStreamReader stream = new FastStreamReader(Helper.HexToBytes(item["SignedTx"].ToString()));
                    if (!tx.TryDeserialize(stream, out string err))
                    {
                        throw new ArgumentException($"Could not deseralize the given tx case: " +
                                                    $"{item["TestName"]}. Error: {err}");
                    }

                    int   sigOpCount = (int)item["SigOpCount"];
                    ulong fee        = (ulong)item["Fee"];

                    yield return(new object[]
                    {
                        utxoDb,
                        new MockMempool(null),
                        c,
                        tx,
                        true, // Verification success
                        null, // Error
                        sigOpCount,
                        fee,
                        tx.WitnessList != null, // AnySegWit
                    });
                }
            }
        }