Branch data Line data Source code
# 1 : : // Copyright (c) 2017-2020 The Bitcoin Core developers
# 2 : : // Distributed under the MIT software license, see the accompanying
# 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 4 : :
# 5 : : #include <consensus/validation.h>
# 6 : : #include <key_io.h>
# 7 : : #include <policy/packages.h>
# 8 : : #include <policy/policy.h>
# 9 : : #include <primitives/transaction.h>
# 10 : : #include <script/script.h>
# 11 : : #include <script/standard.h>
# 12 : : #include <test/util/setup_common.h>
# 13 : : #include <validation.h>
# 14 : :
# 15 : : #include <boost/test/unit_test.hpp>
# 16 : :
# 17 : :
# 18 : : BOOST_AUTO_TEST_SUITE(txvalidation_tests)
# 19 : :
# 20 : : /**
# 21 : : * Ensure that the mempool won't accept coinbase transactions.
# 22 : : */
# 23 : : BOOST_FIXTURE_TEST_CASE(tx_mempool_reject_coinbase, TestChain100Setup)
# 24 : 2 : {
# 25 : 2 : CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
# 26 : 2 : CMutableTransaction coinbaseTx;
# 27 : :
# 28 : 2 : coinbaseTx.nVersion = 1;
# 29 : 2 : coinbaseTx.vin.resize(1);
# 30 : 2 : coinbaseTx.vout.resize(1);
# 31 : 2 : coinbaseTx.vin[0].scriptSig = CScript() << OP_11 << OP_EQUAL;
# 32 : 2 : coinbaseTx.vout[0].nValue = 1 * CENT;
# 33 : 2 : coinbaseTx.vout[0].scriptPubKey = scriptPubKey;
# 34 : :
# 35 : 2 : BOOST_CHECK(CTransaction(coinbaseTx).IsCoinBase());
# 36 : :
# 37 : 2 : LOCK(cs_main);
# 38 : :
# 39 : 2 : unsigned int initialPoolSize = m_node.mempool->size();
# 40 : 2 : const MempoolAcceptResult result = AcceptToMemoryPool(::ChainstateActive(), *m_node.mempool, MakeTransactionRef(coinbaseTx),
# 41 : 2 : true /* bypass_limits */);
# 42 : :
# 43 : 2 : BOOST_CHECK(result.m_result_type == MempoolAcceptResult::ResultType::INVALID);
# 44 : :
# 45 : : // Check that the transaction hasn't been added to mempool.
# 46 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), initialPoolSize);
# 47 : :
# 48 : : // Check that the validation state reflects the unsuccessful attempt.
# 49 : 2 : BOOST_CHECK(result.m_state.IsInvalid());
# 50 : 2 : BOOST_CHECK_EQUAL(result.m_state.GetRejectReason(), "coinbase");
# 51 : 2 : BOOST_CHECK(result.m_state.GetResult() == TxValidationResult::TX_CONSENSUS);
# 52 : 2 : }
# 53 : :
# 54 : : // Create placeholder transactions that have no meaning.
# 55 : : inline CTransactionRef create_placeholder_tx(size_t num_inputs, size_t num_outputs)
# 56 : 56 : {
# 57 : 56 : CMutableTransaction mtx = CMutableTransaction();
# 58 : 56 : mtx.vin.resize(num_inputs);
# 59 : 56 : mtx.vout.resize(num_outputs);
# 60 : 56 : auto random_script = CScript() << ToByteVector(InsecureRand256()) << ToByteVector(InsecureRand256());
# 61 [ + + ]: 2406 : for (size_t i{0}; i < num_inputs; ++i) {
# 62 : 2350 : mtx.vin[i].prevout.hash = InsecureRand256();
# 63 : 2350 : mtx.vin[i].prevout.n = 0;
# 64 : 2350 : mtx.vin[i].scriptSig = random_script;
# 65 : 2350 : }
# 66 [ + + ]: 2406 : for (size_t o{0}; o < num_outputs; ++o) {
# 67 : 2350 : mtx.vout[o].nValue = 1 * CENT;
# 68 : 2350 : mtx.vout[o].scriptPubKey = random_script;
# 69 : 2350 : }
# 70 : 56 : return MakeTransactionRef(mtx);
# 71 : 56 : }
# 72 : :
# 73 : : BOOST_FIXTURE_TEST_CASE(package_tests, TestChain100Setup)
# 74 : 2 : {
# 75 : 2 : LOCK(cs_main);
# 76 : 2 : unsigned int initialPoolSize = m_node.mempool->size();
# 77 : :
# 78 : : // Parent and Child Package
# 79 : 2 : CKey parent_key;
# 80 : 2 : parent_key.MakeNewKey(true);
# 81 : 2 : CScript parent_locking_script = GetScriptForDestination(PKHash(parent_key.GetPubKey()));
# 82 : 2 : auto mtx_parent = CreateValidMempoolTransaction(/* input_transaction */ m_coinbase_txns[0], /* vout */ 0,
# 83 : 2 : /* input_height */ 0, /* input_signing_key */ coinbaseKey,
# 84 : 2 : /* output_destination */ parent_locking_script,
# 85 : 2 : /* output_amount */ CAmount(49 * COIN), /* submit */ false);
# 86 : 2 : CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
# 87 : :
# 88 : 2 : CKey child_key;
# 89 : 2 : child_key.MakeNewKey(true);
# 90 : 2 : CScript child_locking_script = GetScriptForDestination(PKHash(child_key.GetPubKey()));
# 91 : 2 : auto mtx_child = CreateValidMempoolTransaction(/* input_transaction */ tx_parent, /* vout */ 0,
# 92 : 2 : /* input_height */ 101, /* input_signing_key */ parent_key,
# 93 : 2 : /* output_destination */ child_locking_script,
# 94 : 2 : /* output_amount */ CAmount(48 * COIN), /* submit */ false);
# 95 : 2 : CTransactionRef tx_child = MakeTransactionRef(mtx_child);
# 96 : 2 : const auto result_parent_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, {tx_parent, tx_child}, /* test_accept */ true);
# 97 : 2 : BOOST_CHECK_MESSAGE(result_parent_child.m_state.IsValid(),
# 98 : 2 : "Package validation unexpectedly failed: " << result_parent_child.m_state.GetRejectReason());
# 99 : 2 : auto it_parent = result_parent_child.m_tx_results.find(tx_parent->GetWitnessHash());
# 100 : 2 : auto it_child = result_parent_child.m_tx_results.find(tx_child->GetWitnessHash());
# 101 : 2 : BOOST_CHECK(it_parent != result_parent_child.m_tx_results.end());
# 102 : 2 : BOOST_CHECK_MESSAGE(it_parent->second.m_state.IsValid(),
# 103 : 2 : "Package validation unexpectedly failed: " << it_parent->second.m_state.GetRejectReason());
# 104 : 2 : BOOST_CHECK(it_child != result_parent_child.m_tx_results.end());
# 105 : 2 : BOOST_CHECK_MESSAGE(it_child->second.m_state.IsValid(),
# 106 : 2 : "Package validation unexpectedly failed: " << it_child->second.m_state.GetRejectReason());
# 107 : :
# 108 : : // Packages can't have more than 25 transactions.
# 109 : 2 : Package package_too_many;
# 110 : 2 : package_too_many.reserve(MAX_PACKAGE_COUNT + 1);
# 111 [ + + ]: 54 : for (size_t i{0}; i < MAX_PACKAGE_COUNT + 1; ++i) {
# 112 : 52 : package_too_many.emplace_back(create_placeholder_tx(1, 1));
# 113 : 52 : }
# 114 : 2 : auto result_too_many = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_too_many, /* test_accept */ true);
# 115 : 2 : BOOST_CHECK(result_too_many.m_state.IsInvalid());
# 116 : 2 : BOOST_CHECK_EQUAL(result_too_many.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
# 117 : 2 : BOOST_CHECK_EQUAL(result_too_many.m_state.GetRejectReason(), "package-too-many-transactions");
# 118 : :
# 119 : : // Packages can't have a total size of more than 101KvB.
# 120 : 2 : CTransactionRef large_ptx = create_placeholder_tx(150, 150);
# 121 : 2 : Package package_too_large;
# 122 : 2 : auto size_large = GetVirtualTransactionSize(*large_ptx);
# 123 : 2 : size_t total_size{0};
# 124 [ + + ]: 10 : while (total_size <= MAX_PACKAGE_SIZE * 1000) {
# 125 : 8 : package_too_large.push_back(large_ptx);
# 126 : 8 : total_size += size_large;
# 127 : 8 : }
# 128 : 2 : BOOST_CHECK(package_too_large.size() <= MAX_PACKAGE_COUNT);
# 129 : 2 : auto result_too_large = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, package_too_large, /* test_accept */ true);
# 130 : 2 : BOOST_CHECK(result_too_large.m_state.IsInvalid());
# 131 : 2 : BOOST_CHECK_EQUAL(result_too_large.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
# 132 : 2 : BOOST_CHECK_EQUAL(result_too_large.m_state.GetRejectReason(), "package-too-large");
# 133 : :
# 134 : : // A single, giant transaction submitted through ProcessNewPackage fails on single tx policy.
# 135 : 2 : CTransactionRef giant_ptx = create_placeholder_tx(999, 999);
# 136 : 2 : BOOST_CHECK(GetVirtualTransactionSize(*giant_ptx) > MAX_PACKAGE_SIZE * 1000);
# 137 : 2 : auto result_single_large = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, {giant_ptx}, /* test_accept */ true);
# 138 : 2 : BOOST_CHECK(result_single_large.m_state.IsInvalid());
# 139 : 2 : BOOST_CHECK_EQUAL(result_single_large.m_state.GetResult(), PackageValidationResult::PCKG_TX);
# 140 : 2 : BOOST_CHECK_EQUAL(result_single_large.m_state.GetRejectReason(), "transaction failed");
# 141 : 2 : auto it_giant_tx = result_single_large.m_tx_results.find(giant_ptx->GetWitnessHash());
# 142 : 2 : BOOST_CHECK(it_giant_tx != result_single_large.m_tx_results.end());
# 143 : 2 : BOOST_CHECK_EQUAL(it_giant_tx->second.m_state.GetRejectReason(), "tx-size");
# 144 : :
# 145 : : // Check that mempool size hasn't changed.
# 146 : 2 : BOOST_CHECK_EQUAL(m_node.mempool->size(), initialPoolSize);
# 147 : 2 : }
# 148 : : BOOST_AUTO_TEST_SUITE_END()
|