Bitcoin Core  22.99.0
P2P Digital Currency
txpackage_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 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>
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 BOOST_AUTO_TEST_SUITE(txpackage_tests)
18 
19 // Create placeholder transactions that have no meaning.
20 inline CTransactionRef create_placeholder_tx(size_t num_inputs, size_t num_outputs)
21 {
23  mtx.vin.resize(num_inputs);
24  mtx.vout.resize(num_outputs);
25  auto random_script = CScript() << ToByteVector(InsecureRand256()) << ToByteVector(InsecureRand256());
26  for (size_t i{0}; i < num_inputs; ++i) {
27  mtx.vin[i].prevout.hash = InsecureRand256();
28  mtx.vin[i].prevout.n = 0;
29  mtx.vin[i].scriptSig = random_script;
30  }
31  for (size_t o{0}; o < num_outputs; ++o) {
32  mtx.vout[o].nValue = 1 * CENT;
33  mtx.vout[o].scriptPubKey = random_script;
34  }
35  return MakeTransactionRef(mtx);
36 }
37 
38 BOOST_FIXTURE_TEST_CASE(package_sanitization_tests, TestChain100Setup)
39 {
40  // Packages can't have more than 25 transactions.
41  Package package_too_many;
42  package_too_many.reserve(MAX_PACKAGE_COUNT + 1);
43  for (size_t i{0}; i < MAX_PACKAGE_COUNT + 1; ++i) {
44  package_too_many.emplace_back(create_placeholder_tx(1, 1));
45  }
46  PackageValidationState state_too_many;
47  BOOST_CHECK(!CheckPackage(package_too_many, state_too_many));
49  BOOST_CHECK_EQUAL(state_too_many.GetRejectReason(), "package-too-many-transactions");
50 
51  // Packages can't have a total size of more than 101KvB.
52  CTransactionRef large_ptx = create_placeholder_tx(150, 150);
53  Package package_too_large;
54  auto size_large = GetVirtualTransactionSize(*large_ptx);
55  size_t total_size{0};
56  while (total_size <= MAX_PACKAGE_SIZE * 1000) {
57  package_too_large.push_back(large_ptx);
58  total_size += size_large;
59  }
60  BOOST_CHECK(package_too_large.size() <= MAX_PACKAGE_COUNT);
61  PackageValidationState state_too_large;
62  BOOST_CHECK(!CheckPackage(package_too_large, state_too_large));
64  BOOST_CHECK_EQUAL(state_too_large.GetRejectReason(), "package-too-large");
65 }
66 
67 BOOST_FIXTURE_TEST_CASE(package_validation_tests, TestChain100Setup)
68 {
69  LOCK(cs_main);
70  unsigned int initialPoolSize = m_node.mempool->size();
71 
72  // Parent and Child Package
73  CKey parent_key;
74  parent_key.MakeNewKey(true);
75  CScript parent_locking_script = GetScriptForDestination(PKHash(parent_key.GetPubKey()));
76  auto mtx_parent = CreateValidMempoolTransaction(/*input_transaction=*/ m_coinbase_txns[0], /*input_vout=*/0,
77  /*input_height=*/ 0, /*input_signing_key=*/coinbaseKey,
78  /*output_destination=*/ parent_locking_script,
79  /*output_amount=*/ CAmount(49 * COIN), /*submit=*/false);
80  CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
81 
82  CKey child_key;
83  child_key.MakeNewKey(true);
84  CScript child_locking_script = GetScriptForDestination(PKHash(child_key.GetPubKey()));
85  auto mtx_child = CreateValidMempoolTransaction(/*input_transaction=*/ tx_parent, /*input_vout=*/0,
86  /*input_height=*/ 101, /*input_signing_key=*/parent_key,
87  /*output_destination=*/child_locking_script,
88  /*output_amount=*/ CAmount(48 * COIN), /*submit=*/false);
89  CTransactionRef tx_child = MakeTransactionRef(mtx_child);
90  const auto result_parent_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, {tx_parent, tx_child}, /*test_accept=*/true);
91  BOOST_CHECK_MESSAGE(result_parent_child.m_state.IsValid(),
92  "Package validation unexpectedly failed: " << result_parent_child.m_state.GetRejectReason());
93  auto it_parent = result_parent_child.m_tx_results.find(tx_parent->GetWitnessHash());
94  auto it_child = result_parent_child.m_tx_results.find(tx_child->GetWitnessHash());
95  BOOST_CHECK(it_parent != result_parent_child.m_tx_results.end());
96  BOOST_CHECK_MESSAGE(it_parent->second.m_state.IsValid(),
97  "Package validation unexpectedly failed: " << it_parent->second.m_state.GetRejectReason());
98  BOOST_CHECK(it_child != result_parent_child.m_tx_results.end());
99  BOOST_CHECK_MESSAGE(it_child->second.m_state.IsValid(),
100  "Package validation unexpectedly failed: " << it_child->second.m_state.GetRejectReason());
101 
102 
103  // A single, giant transaction submitted through ProcessNewPackage fails on single tx policy.
104  CTransactionRef giant_ptx = create_placeholder_tx(999, 999);
106  auto result_single_large = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, {giant_ptx}, /*test_accept=*/true);
107  BOOST_CHECK(result_single_large.m_state.IsInvalid());
108  BOOST_CHECK_EQUAL(result_single_large.m_state.GetResult(), PackageValidationResult::PCKG_TX);
109  BOOST_CHECK_EQUAL(result_single_large.m_state.GetRejectReason(), "transaction failed");
110  auto it_giant_tx = result_single_large.m_tx_results.find(giant_ptx->GetWitnessHash());
111  BOOST_CHECK(it_giant_tx != result_single_large.m_tx_results.end());
112  BOOST_CHECK_EQUAL(it_giant_tx->second.m_state.GetRejectReason(), "tx-size");
113 
114  // Check that mempool size hasn't changed.
115  BOOST_CHECK_EQUAL(m_node.mempool->size(), initialPoolSize);
116 }
policy.h
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:346
BOOST_FIXTURE_TEST_CASE
BOOST_FIXTURE_TEST_CASE(package_sanitization_tests, TestChain100Setup)
Definition: txpackage_tests.cpp:38
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
PackageValidationResult::PCKG_TX
@ PCKG_TX
At least one tx is invalid.
CKey::MakeNewKey
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:160
key_io.h
setup_common.h
NodeContext::mempool
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:44
transaction.h
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:310
InsecureRand256
static uint256 InsecureRand256()
Definition: setup_common.h:66
validation.h
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
create_placeholder_tx
CTransactionRef create_placeholder_tx(size_t num_inputs, size_t num_outputs)
Definition: txpackage_tests.cpp:20
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
packages.h
PackageValidationResult::PCKG_POLICY
@ PCKG_POLICY
The package itself is invalid (e.g. too many transactions).
ToByteVector
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:60
PackageValidationState
Definition: packages.h:34
m_node
NodeContext m_node
Definition: bitcoin-gui.cpp:36
CENT
static constexpr CAmount CENT
Definition: setup_common.h:71
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
standard.h
TestChain100Setup
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:116
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
script.h
MAX_PACKAGE_COUNT
static constexpr uint32_t MAX_PACKAGE_COUNT
Default maximum number of transactions in a package.
Definition: packages.h:15
ValidationState::GetResult
Result GetResult() const
Definition: validation.h:123
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:347
PKHash
Definition: standard.h:79
CKey
An encapsulated private key.
Definition: key.h:26
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:118
NodeContext::chainman
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:47
LOCK
#define LOCK(cs)
Definition: sync.h:226
Package
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:32
ValidationState::GetRejectReason
std::string GetRejectReason() const
Definition: validation.h:124
CheckPackage
bool CheckPackage(const Package &txns, PackageValidationState &state)
Context-free package policy checks:
Definition: packages.cpp:14
MAX_PACKAGE_SIZE
static constexpr uint32_t MAX_PACKAGE_SIZE
Default maximum total virtual size of transactions in a package in KvB.
Definition: packages.h:17
COIN
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
GetVirtualTransactionSize
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition: policy.cpp:282
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
ProcessNewPackage
PackageMempoolAcceptResult ProcessNewPackage(CChainState &active_chainstate, CTxMemPool &pool, const Package &package, bool test_accept)
Atomically test acceptance of a package.
Definition: validation.cpp:1120