Skip to content

Commit 363e3d9

Browse files
committed
[test] unit tests for ProcessNewPackage
Key functionality = a transaction with UTXOs not present in UTXO set or mempool can be fully validated instead of being considered an orphan.
1 parent cd9a11a commit 363e3d9

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/test/txvalidation_tests.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <consensus/validation.h>
6+
#include <key_io.h>
67
#include <primitives/transaction.h>
78
#include <script/script.h>
9+
#include <script/standard.h>
810
#include <test/util/setup_common.h>
911
#include <validation.h>
1012

@@ -47,4 +49,42 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_reject_coinbase, TestChain100Setup)
4749
BOOST_CHECK(result.m_state.GetResult() == TxValidationResult::TX_CONSENSUS);
4850
}
4951

52+
BOOST_FIXTURE_TEST_CASE(package_tests, TestChain100Setup)
53+
{
54+
LOCK(cs_main);
55+
unsigned int initialPoolSize = m_node.mempool->size();
56+
57+
// Parent and Child Package
58+
CKey parent_key;
59+
parent_key.MakeNewKey(true);
60+
CScript parent_locking_script = GetScriptForDestination(PKHash(parent_key.GetPubKey()));
61+
auto mtx_parent = CreateValidMempoolTransaction(/* input_transaction */ m_coinbase_txns[0], /* vout */ 0,
62+
/* input_height */ 0, /* input_signing_key */ coinbaseKey,
63+
/* output_destination */ parent_locking_script,
64+
/* output_amount */ CAmount(49 * COIN), /* submit */ false);
65+
CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
66+
67+
CKey child_key;
68+
child_key.MakeNewKey(true);
69+
CScript child_locking_script = GetScriptForDestination(PKHash(child_key.GetPubKey()));
70+
auto mtx_child = CreateValidMempoolTransaction(/* input_transaction */ tx_parent, /* vout */ 0,
71+
/* input_height */ 101, /* input_signing_key */ parent_key,
72+
/* output_destination */ child_locking_script,
73+
/* output_amount */ CAmount(48 * COIN), /* submit */ false);
74+
CTransactionRef tx_child = MakeTransactionRef(mtx_child);
75+
const auto result_parent_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool, {tx_parent, tx_child}, /* test_accept */ true);
76+
BOOST_CHECK_MESSAGE(result_parent_child.m_state.IsValid(),
77+
"Package validation unexpectedly failed: " << result_parent_child.m_state.GetRejectReason());
78+
auto it_parent = result_parent_child.m_tx_results.find(tx_parent->GetWitnessHash());
79+
auto it_child = result_parent_child.m_tx_results.find(tx_child->GetWitnessHash());
80+
BOOST_CHECK(it_parent != result_parent_child.m_tx_results.end());
81+
BOOST_CHECK_MESSAGE(it_parent->second.m_state.IsValid(),
82+
"Package validation unexpectedly failed: " << it_parent->second.m_state.GetRejectReason());
83+
BOOST_CHECK(it_child != result_parent_child.m_tx_results.end());
84+
BOOST_CHECK_MESSAGE(it_child->second.m_state.IsValid(),
85+
"Package validation unexpectedly failed: " << it_child->second.m_state.GetRejectReason());
86+
87+
// Check that mempool size hasn't changed.
88+
BOOST_CHECK_EQUAL(m_node.mempool->size(), initialPoolSize);
89+
}
5090
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)