Skip to content

Commit 046e8ff

Browse files
committed
[unit test] package submission
1 parent e12fafd commit 046e8ff

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

src/test/txpackage_tests.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,124 @@ BOOST_FIXTURE_TEST_CASE(noncontextual_package_tests, TestChain100Setup)
207207
}
208208
}
209209

210+
BOOST_FIXTURE_TEST_CASE(package_submission_tests, TestChain100Setup)
211+
{
212+
LOCK(cs_main);
213+
unsigned int expected_pool_size = m_node.mempool->size();
214+
CKey parent_key;
215+
parent_key.MakeNewKey(true);
216+
CScript parent_locking_script = GetScriptForDestination(PKHash(parent_key.GetPubKey()));
217+
218+
// Unrelated transactions are not allowed in package submission.
219+
Package package_unrelated;
220+
for (size_t i{0}; i < 10; ++i) {
221+
auto mtx = CreateValidMempoolTransaction(/* input_transaction */ m_coinbase_txns[i + 25], /* vout */ 0,
222+
/* input_height */ 0, /* input_signing_key */ coinbaseKey,
223+
/* output_destination */ parent_locking_script,
224+
/* output_amount */ CAmount(49 * COIN), /* submit */ false);
225+
package_unrelated.emplace_back(MakeTransactionRef(mtx));
226+
}
227+
auto result_unrelated_submit = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
228+
package_unrelated, /* test_accept */ false);
229+
BOOST_CHECK(result_unrelated_submit.m_state.IsInvalid());
230+
BOOST_CHECK_EQUAL(result_unrelated_submit.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
231+
BOOST_CHECK_EQUAL(result_unrelated_submit.m_state.GetRejectReason(), "package-not-child-with-parents");
232+
BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
233+
234+
// Parent and Child (and Grandchild) Package
235+
Package package_parent_child;
236+
Package package_3gen;
237+
auto mtx_parent = CreateValidMempoolTransaction(/* input_transaction */ m_coinbase_txns[0], /* vout */ 0,
238+
/* input_height */ 0, /* input_signing_key */ coinbaseKey,
239+
/* output_destination */ parent_locking_script,
240+
/* output_amount */ CAmount(49 * COIN), /* submit */ false);
241+
CTransactionRef tx_parent = MakeTransactionRef(mtx_parent);
242+
package_parent_child.push_back(tx_parent);
243+
package_3gen.push_back(tx_parent);
244+
245+
CKey child_key;
246+
child_key.MakeNewKey(true);
247+
CScript child_locking_script = GetScriptForDestination(PKHash(child_key.GetPubKey()));
248+
auto mtx_child = CreateValidMempoolTransaction(/* input_transaction */ tx_parent, /* vout */ 0,
249+
/* input_height */ 101, /* input_signing_key */ parent_key,
250+
/* output_destination */ child_locking_script,
251+
/* output_amount */ CAmount(48 * COIN), /* submit */ false);
252+
CTransactionRef tx_child = MakeTransactionRef(mtx_child);
253+
package_parent_child.push_back(tx_child);
254+
package_3gen.push_back(tx_child);
255+
256+
CKey grandchild_key;
257+
grandchild_key.MakeNewKey(true);
258+
CScript grandchild_locking_script = GetScriptForDestination(PKHash(grandchild_key.GetPubKey()));
259+
auto mtx_grandchild = CreateValidMempoolTransaction(/* input_transaction */ tx_child, /* vout */ 0,
260+
/* input_height */ 101, /* input_signing_key */ child_key,
261+
/* output_destination */ grandchild_locking_script,
262+
/* output_amount */ CAmount(47 * COIN), /* submit */ false);
263+
CTransactionRef tx_grandchild = MakeTransactionRef(mtx_grandchild);
264+
package_3gen.push_back(tx_grandchild);
265+
266+
// 3 Generations is not allowed.
267+
{
268+
auto result_3gen_submit = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
269+
package_3gen, /* test_accept */ false);
270+
BOOST_CHECK(result_3gen_submit.m_state.IsInvalid());
271+
BOOST_CHECK_EQUAL(result_3gen_submit.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
272+
BOOST_CHECK_EQUAL(result_3gen_submit.m_state.GetRejectReason(), "package-not-child-with-parents");
273+
BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
274+
}
275+
276+
// Child with missing parent.
277+
mtx_child.vin.push_back(CTxIn(COutPoint(package_unrelated[0]->GetHash(), 0)));
278+
Package package_missing_parent;
279+
package_missing_parent.push_back(tx_parent);
280+
package_missing_parent.push_back(MakeTransactionRef(mtx_child));
281+
{
282+
const auto result_missing_parent = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
283+
package_missing_parent, /* test_accept */ false);
284+
BOOST_CHECK(result_missing_parent.m_state.IsInvalid());
285+
BOOST_CHECK_EQUAL(result_missing_parent.m_state.GetResult(), PackageValidationResult::PCKG_POLICY);
286+
BOOST_CHECK_EQUAL(result_missing_parent.m_state.GetRejectReason(), "package-not-child-with-unconfirmed-parents");
287+
BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
288+
289+
}
290+
291+
// Submit package with parent + child.
292+
{
293+
const auto submit_parent_child = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
294+
package_parent_child, /* test_accept */ false);
295+
expected_pool_size += 2;
296+
BOOST_CHECK_MESSAGE(submit_parent_child.m_state.IsValid(),
297+
"Package validation unexpectedly failed: " << submit_parent_child.m_state.GetRejectReason());
298+
auto it_parent = submit_parent_child.m_tx_results.find(tx_parent->GetWitnessHash());
299+
auto it_child = submit_parent_child.m_tx_results.find(tx_child->GetWitnessHash());
300+
BOOST_CHECK(it_parent != submit_parent_child.m_tx_results.end());
301+
BOOST_CHECK(it_parent->second.m_state.IsValid());
302+
BOOST_CHECK(it_child != submit_parent_child.m_tx_results.end());
303+
BOOST_CHECK(it_child->second.m_state.IsValid());
304+
305+
BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
306+
BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_parent->GetHash())));
307+
BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_child->GetHash())));
308+
}
309+
310+
// Already-in-mempool transactions should be detected and de-duplicated.
311+
{
312+
const auto submit_deduped = ProcessNewPackage(m_node.chainman->ActiveChainstate(), *m_node.mempool,
313+
package_parent_child, /* test_accept */ false);
314+
BOOST_CHECK_MESSAGE(submit_deduped.m_state.IsValid(),
315+
"Package validation unexpectedly failed: " << submit_deduped.m_state.GetRejectReason());
316+
auto it_parent_deduped = submit_deduped.m_tx_results.find(tx_parent->GetWitnessHash());
317+
auto it_child_deduped = submit_deduped.m_tx_results.find(tx_child->GetWitnessHash());
318+
BOOST_CHECK(it_parent_deduped != submit_deduped.m_tx_results.end());
319+
BOOST_CHECK(it_parent_deduped->second.m_state.IsValid());
320+
BOOST_CHECK(it_parent_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
321+
BOOST_CHECK(it_child_deduped != submit_deduped.m_tx_results.end());
322+
BOOST_CHECK(it_child_deduped->second.m_state.IsValid());
323+
BOOST_CHECK(it_child_deduped->second.m_result_type == MempoolAcceptResult::ResultType::MEMPOOL_ENTRY);
324+
325+
BOOST_CHECK_EQUAL(m_node.mempool->size(), expected_pool_size);
326+
BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_parent->GetHash())));
327+
BOOST_CHECK(m_node.mempool->exists(GenTxid::Txid(tx_child->GetHash())));
328+
}
329+
}
210330
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)