@@ -114,4 +114,217 @@ BOOST_FIXTURE_TEST_CASE(package_validation_tests, TestChain100Setup)
114
114
// Check that mempool size hasn't changed.
115
115
BOOST_CHECK_EQUAL (m_node.mempool ->size (), initialPoolSize);
116
116
}
117
+
118
+ BOOST_FIXTURE_TEST_CASE (noncontextual_package_tests, TestChain100Setup)
119
+ {
120
+ // The signatures won't be verified so we can just use a placeholder
121
+ CKey placeholder_key;
122
+ placeholder_key.MakeNewKey (true );
123
+ CScript spk = GetScriptForDestination (PKHash (placeholder_key.GetPubKey ()));
124
+ CKey placeholder_key_2;
125
+ placeholder_key_2.MakeNewKey (true );
126
+ CScript spk2 = GetScriptForDestination (PKHash (placeholder_key_2.GetPubKey ()));
127
+
128
+ // Parent and Child Package
129
+ {
130
+ auto mtx_parent = CreateValidMempoolTransaction (m_coinbase_txns[0 ], 0 , 0 , coinbaseKey, spk,
131
+ CAmount (49 * COIN), /* submit */ false );
132
+ CTransactionRef tx_parent = MakeTransactionRef (mtx_parent);
133
+
134
+ auto mtx_child = CreateValidMempoolTransaction (tx_parent, 0 , 101 , placeholder_key, spk2,
135
+ CAmount (48 * COIN), /* submit */ false );
136
+ CTransactionRef tx_child = MakeTransactionRef (mtx_child);
137
+
138
+ PackageValidationState state;
139
+ BOOST_CHECK (CheckPackage ({tx_parent, tx_child}, state));
140
+ BOOST_CHECK (!CheckPackage ({tx_child, tx_parent}, state));
141
+ BOOST_CHECK_EQUAL (state.GetResult (), PackageValidationResult::PCKG_POLICY);
142
+ BOOST_CHECK_EQUAL (state.GetRejectReason (), " package-not-sorted" );
143
+ BOOST_CHECK (IsChildWithParents ({tx_parent, tx_child}));
144
+ }
145
+
146
+ // 24 Parents and 1 Child
147
+ {
148
+ Package package;
149
+ CMutableTransaction child;
150
+ for (int i{0 }; i < 24 ; ++i) {
151
+ auto parent = MakeTransactionRef (CreateValidMempoolTransaction (m_coinbase_txns[i + 1 ],
152
+ 0 , 0 , coinbaseKey, spk, CAmount (48 * COIN), false ));
153
+ package.emplace_back (parent);
154
+ child.vin .push_back (CTxIn (COutPoint (parent->GetHash (), 0 )));
155
+ }
156
+ child.vout .push_back (CTxOut (47 * COIN, spk2));
157
+
158
+ // The child must be in the package.
159
+ BOOST_CHECK (!IsChildWithParents (package));
160
+
161
+ // The parents can be in any order.
162
+ FastRandomContext rng;
163
+ Shuffle (package.begin (), package.end (), rng);
164
+ package.push_back (MakeTransactionRef (child));
165
+
166
+ PackageValidationState state;
167
+ BOOST_CHECK (CheckPackage (package, state));
168
+ BOOST_CHECK (IsChildWithParents (package));
169
+
170
+ package.erase (package.begin ());
171
+ BOOST_CHECK (IsChildWithParents (package));
172
+
173
+ // The package cannot have unrelated transactions.
174
+ package.insert (package.begin (), m_coinbase_txns[0 ]);
175
+ BOOST_CHECK (!IsChildWithParents (package));
176
+ }
177
+
178
+ // 2 Parents and 1 Child where one parent depends on the other.
179
+ {
180
+ CMutableTransaction mtx_parent;
181
+ mtx_parent.vin .push_back (CTxIn (COutPoint (m_coinbase_txns[0 ]->GetHash (), 0 )));
182
+ mtx_parent.vout .push_back (CTxOut (20 * COIN, spk));
183
+ mtx_parent.vout .push_back (CTxOut (20 * COIN, spk2));
184
+ CTransactionRef tx_parent = MakeTransactionRef (mtx_parent);
185
+
186
+ CMutableTransaction mtx_parent_also_child;
187
+ mtx_parent_also_child.vin .push_back (CTxIn (COutPoint (tx_parent->GetHash (), 0 )));
188
+ mtx_parent_also_child.vout .push_back (CTxOut (20 * COIN, spk));
189
+ CTransactionRef tx_parent_also_child = MakeTransactionRef (mtx_parent_also_child);
190
+
191
+ CMutableTransaction mtx_child;
192
+ mtx_child.vin .push_back (CTxIn (COutPoint (tx_parent->GetHash (), 1 )));
193
+ mtx_child.vin .push_back (CTxIn (COutPoint (tx_parent_also_child->GetHash (), 0 )));
194
+ mtx_child.vout .push_back (CTxOut (39 * COIN, spk));
195
+ CTransactionRef tx_child = MakeTransactionRef (mtx_child);
196
+
197
+ PackageValidationState state;
198
+ BOOST_CHECK (IsChildWithParents ({tx_parent, tx_parent_also_child}));
199
+ BOOST_CHECK (IsChildWithParents ({tx_parent, tx_child}));
200
+ BOOST_CHECK (IsChildWithParents ({tx_parent, tx_parent_also_child, tx_child}));
201
+ // IsChildWithParents does not detect unsorted parents.
202
+ BOOST_CHECK (IsChildWithParents ({tx_parent_also_child, tx_parent, tx_child}));
203
+ BOOST_CHECK (CheckPackage ({tx_parent, tx_parent_also_child, tx_child}, state));
204
+ BOOST_CHECK (!CheckPackage ({tx_parent_also_child, tx_parent, tx_child}, state));
205
+ BOOST_CHECK_EQUAL (state.GetResult (), PackageValidationResult::PCKG_POLICY);
206
+ BOOST_CHECK_EQUAL (state.GetRejectReason (), " package-not-sorted" );
207
+ }
208
+ }
209
+
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
+ }
117
330
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments