99#include < primitives/transaction.h>
1010#include < script/script.h>
1111#include < test/util/random.h>
12+ #include < test/util/script.h>
1213#include < test/util/setup_common.h>
1314#include < validation.h>
1415
@@ -47,7 +48,7 @@ BOOST_FIXTURE_TEST_CASE(package_sanitization_tests, TestChain100Setup)
4748 package_too_many.emplace_back (create_placeholder_tx (1 , 1 ));
4849 }
4950 PackageValidationState state_too_many;
50- BOOST_CHECK (!CheckPackage (package_too_many, state_too_many));
51+ BOOST_CHECK (!CheckPackage (package_too_many, state_too_many, /* require_sorted= */ true ));
5152 BOOST_CHECK_EQUAL (state_too_many.GetResult (), PackageValidationResult::PCKG_POLICY);
5253 BOOST_CHECK_EQUAL (state_too_many.GetRejectReason (), " package-too-many-transactions" );
5354
@@ -62,7 +63,7 @@ BOOST_FIXTURE_TEST_CASE(package_sanitization_tests, TestChain100Setup)
6263 }
6364 BOOST_CHECK (package_too_large.size () <= MAX_PACKAGE_COUNT);
6465 PackageValidationState state_too_large;
65- BOOST_CHECK (!CheckPackage (package_too_large, state_too_large));
66+ BOOST_CHECK (!CheckPackage (package_too_large, state_too_large, /* require_sorted= */ true ));
6667 BOOST_CHECK_EQUAL (state_too_large.GetResult (), PackageValidationResult::PCKG_POLICY);
6768 BOOST_CHECK_EQUAL (state_too_large.GetRejectReason (), " package-too-large" );
6869
@@ -73,9 +74,39 @@ BOOST_FIXTURE_TEST_CASE(package_sanitization_tests, TestChain100Setup)
7374 package_duplicate_txids_empty.emplace_back (MakeTransactionRef (empty_tx));
7475 }
7576 PackageValidationState state_duplicates;
76- BOOST_CHECK (!CheckPackage (package_duplicate_txids_empty, state_duplicates));
77+ BOOST_CHECK (!CheckPackage (package_duplicate_txids_empty, state_duplicates, /* require_sorted= */ true ));
7778 BOOST_CHECK_EQUAL (state_duplicates.GetResult (), PackageValidationResult::PCKG_POLICY);
7879 BOOST_CHECK_EQUAL (state_duplicates.GetRejectReason (), " package-contains-duplicates" );
80+ BOOST_CHECK (!IsConsistentPackage (package_duplicate_txids_empty));
81+
82+ // Packages can't have transactions spending the same prevout
83+ CMutableTransaction tx_zero_1;
84+ CMutableTransaction tx_zero_2;
85+ COutPoint same_prevout{InsecureRand256 (), 0 };
86+ tx_zero_1.vin .emplace_back (same_prevout);
87+ tx_zero_2.vin .emplace_back (same_prevout);
88+ // Different vouts (not the same tx)
89+ tx_zero_1.vout .emplace_back (CENT, P2WSH_OP_TRUE);
90+ tx_zero_2.vout .emplace_back (2 * CENT, P2WSH_OP_TRUE);
91+ Package package_conflicts{MakeTransactionRef (tx_zero_1), MakeTransactionRef (tx_zero_2)};
92+ BOOST_CHECK (!IsConsistentPackage (package_conflicts));
93+ // Transactions are considered sorted when they have no dependencies.
94+ BOOST_CHECK (IsTopoSortedPackage (package_conflicts));
95+ PackageValidationState state_conflicts;
96+ BOOST_CHECK (!CheckPackage (package_conflicts, state_conflicts, /* require_sorted=*/ true ));
97+ BOOST_CHECK_EQUAL (state_conflicts.GetResult (), PackageValidationResult::PCKG_POLICY);
98+ BOOST_CHECK_EQUAL (state_conflicts.GetRejectReason (), " conflict-in-package" );
99+
100+ // IsConsistentPackage only cares about conflicts between transactions, not about a transaction
101+ // conflicting with itself (i.e. duplicate prevouts in vin).
102+ CMutableTransaction dup_tx;
103+ const COutPoint rand_prevout{InsecureRand256 (), 0 };
104+ dup_tx.vin .emplace_back (rand_prevout);
105+ dup_tx.vin .emplace_back (rand_prevout);
106+ Package package_with_dup_tx{MakeTransactionRef (dup_tx)};
107+ BOOST_CHECK (IsConsistentPackage (package_with_dup_tx));
108+ package_with_dup_tx.emplace_back (create_placeholder_tx (1 , 1 ));
109+ BOOST_CHECK (IsConsistentPackage (package_with_dup_tx));
79110}
80111
81112BOOST_FIXTURE_TEST_CASE (package_validation_tests, TestChain100Setup)
@@ -157,8 +188,8 @@ BOOST_FIXTURE_TEST_CASE(noncontextual_package_tests, TestChain100Setup)
157188 CTransactionRef tx_child = MakeTransactionRef (mtx_child);
158189
159190 PackageValidationState state;
160- BOOST_CHECK (CheckPackage ({tx_parent, tx_child}, state));
161- BOOST_CHECK (!CheckPackage ({tx_child, tx_parent}, state));
191+ BOOST_CHECK (CheckPackage ({tx_parent, tx_child}, state, /* require_sorted= */ true ));
192+ BOOST_CHECK (!CheckPackage ({tx_child, tx_parent}, state, /* require_sorted= */ true ));
162193 BOOST_CHECK_EQUAL (state.GetResult (), PackageValidationResult::PCKG_POLICY);
163194 BOOST_CHECK_EQUAL (state.GetRejectReason (), " package-not-sorted" );
164195 BOOST_CHECK (IsChildWithParents ({tx_parent, tx_child}));
@@ -186,7 +217,7 @@ BOOST_FIXTURE_TEST_CASE(noncontextual_package_tests, TestChain100Setup)
186217 package.push_back (MakeTransactionRef (child));
187218
188219 PackageValidationState state;
189- BOOST_CHECK (CheckPackage (package, state));
220+ BOOST_CHECK (CheckPackage (package, state, /* require_sorted= */ true ));
190221 BOOST_CHECK (IsChildWithParents (package));
191222 BOOST_CHECK (IsChildWithParentsTree (package));
192223
@@ -224,8 +255,8 @@ BOOST_FIXTURE_TEST_CASE(noncontextual_package_tests, TestChain100Setup)
224255 BOOST_CHECK (!IsChildWithParentsTree ({tx_parent, tx_parent_also_child, tx_child}));
225256 // IsChildWithParents does not detect unsorted parents.
226257 BOOST_CHECK (IsChildWithParents ({tx_parent_also_child, tx_parent, tx_child}));
227- BOOST_CHECK (CheckPackage ({tx_parent, tx_parent_also_child, tx_child}, state));
228- BOOST_CHECK (!CheckPackage ({tx_parent_also_child, tx_parent, tx_child}, state));
258+ BOOST_CHECK (CheckPackage ({tx_parent, tx_parent_also_child, tx_child}, state, /* require_sorted= */ true ));
259+ BOOST_CHECK (!CheckPackage ({tx_parent_also_child, tx_parent, tx_child}, state, /* require_sorted= */ true ));
229260 BOOST_CHECK_EQUAL (state.GetResult (), PackageValidationResult::PCKG_POLICY);
230261 BOOST_CHECK_EQUAL (state.GetRejectReason (), " package-not-sorted" );
231262 }
0 commit comments