13#include <boost/test/unit_test.hpp>
23 tx.
vin.resize(inputs.size());
24 tx.
vout.resize(output_values.size());
25 for (
size_t i = 0; i < inputs.size(); ++i) {
26 tx.
vin[i].prevout.hash = inputs[i]->GetHash();
27 tx.
vin[i].prevout.n = 0;
30 witness.
stack.emplace_back(i + 10);
31 tx.
vin[i].scriptWitness = witness;
33 for (
size_t i = 0; i < output_values.size(); ++i) {
35 tx.
vout[i].nValue = output_values[i];
47 auto tx_to_spend = tx;
48 for (int32_t i{0}; i < num_descendants; ++i) {
49 auto next_tx =
make_tx({tx_to_spend}, {(50 - i) *
CENT});
51 BOOST_CHECK(pool.GetIter(next_tx->GetHash()).has_value());
52 tx_to_spend = next_tx;
69 const auto tx1 =
make_tx( {m_coinbase_txns[0]}, {10 *
COIN});
75 const auto tx3 =
make_tx( {m_coinbase_txns[1]}, {1099 *
CENT});
81 const auto tx5 =
make_tx( {m_coinbase_txns[2]}, {1099 *
CENT});
90 const auto tx7 =
make_tx( {m_coinbase_txns[3]}, {999 *
CENT});
92 const auto tx8 =
make_tx( {m_coinbase_txns[4]}, {999 *
CENT});
96 const auto tx11 =
make_tx( {m_coinbase_txns[7]}, {995 *
CENT});
98 const auto tx12 =
make_tx( {m_coinbase_txns[8]}, {995 *
CENT});
102 const auto tx13 =
make_tx( {m_coinbase_txns[9]}, {995 *
CENT, 995 *
CENT});
105 const auto entry1_normal = pool.
GetIter(tx1->GetHash()).value();
106 const auto entry2_normal = pool.
GetIter(tx2->GetHash()).value();
107 const auto entry3_low = pool.
GetIter(tx3->GetHash()).value();
108 const auto entry4_high = pool.
GetIter(tx4->GetHash()).value();
109 const auto entry5_low = pool.
GetIter(tx5->GetHash()).value();
110 const auto entry6_low_prioritised = pool.
GetIter(tx6->GetHash()).value();
111 const auto entry7_high = pool.
GetIter(tx7->GetHash()).value();
112 const auto entry8_high = pool.
GetIter(tx8->GetHash()).value();
168 const int NUM_OUTPUTS = 51;
169 std::vector<CAmount> output_values;
170 output_values.reserve(NUM_OUTPUTS);
171 for (
int i = 0; i < NUM_OUTPUTS; ++i) {
172 output_values.push_back(1 *
COIN);
175 const auto parent_tx_1 =
make_tx( {m_coinbase_txns[0]}, output_values);
176 const auto parent_tx_2 =
make_tx( {m_coinbase_txns[1]}, output_values);
180 std::vector<CTransactionRef> direct_children;
183 for (
const auto& parent_tx : {parent_tx_1, parent_tx_2}) {
184 for (
auto i = 0; i < NUM_OUTPUTS; ++i) {
187 tx.vin[0].prevout.n = i;
200 const auto parent_entry_1 = pool.
GetIter(parent_tx_1->GetHash()).value();
201 const auto parent_entry_2 = pool.
GetIter(parent_tx_2->GetHash()).value();
202 const auto conflicting_transaction =
make_tx({parent_tx_1, parent_tx_2}, {50 *
CENT});
206 {parent_entry_1, parent_entry_2},
207 all_conflicts) == std::nullopt);
214 dummy) == std::nullopt);
222 for (
const auto& child : direct_children) {
228 for (
auto i = 0; i < 100; ++i) {
229 conflicts.insert(pool.
GetIter(direct_children[i]->GetHash()).value());
234 dummy) == std::nullopt);
237 conflicts.insert(pool.
GetIter(direct_children[100]->GetHash()).value());
254 const auto tx1 =
make_tx( {m_coinbase_txns[0], m_coinbase_txns[1]}, {10 *
COIN});
259 const auto entry1 = pool.
GetIter(tx1->GetHash()).value();
260 const auto tx1_fee = entry1->GetModifiedFee();
261 const auto entry2 = pool.
GetIter(tx2->GetHash()).value();
262 const auto tx2_fee = entry2->GetModifiedFee();
265 const auto tx1_conflict =
make_tx( {m_coinbase_txns[0], m_coinbase_txns[2]}, {10 *
COIN});
266 const auto tx3 =
make_tx( {tx1_conflict}, {995 *
CENT});
267 auto entry3 = entry.
FromTx(tx3);
273 changeset->StageRemoval(entry1);
274 changeset->StageRemoval(entry2);
275 changeset->StageAddition(tx1_conflict, tx1_fee, 0, 1, 0,
false, 4,
LockPoints());
276 changeset->StageAddition(tx3, tx2_fee, 0, 1, 0,
false, 4,
LockPoints());
280 BOOST_CHECK(res1.value().second ==
"insufficient feerate: does not improve feerate diagram");
285 changeset->StageRemoval(entry1);
286 changeset->StageRemoval(entry2);
287 changeset->StageAddition(tx1_conflict, tx1_fee+1, 0, 1, 0,
false, 4,
LockPoints());
288 changeset->StageAddition(tx3, tx2_fee, 0, 1, 0,
false, 4,
LockPoints());
295 changeset->StageRemoval(entry1);
296 changeset->StageRemoval(entry2);
297 changeset->StageAddition(tx1_conflict, tx1_fee+1, 0, 1, 0,
false, 4,
LockPoints());
298 changeset->StageAddition(tx3, tx2_fee, 0, 1, 0,
false, 4,
LockPoints());
302 BOOST_CHECK(res2.value().second ==
"insufficient feerate: does not improve feerate diagram");
312 changeset->StageRemoval(entry1);
313 changeset->StageRemoval(entry2);
314 changeset->StageAddition(tx1_conflict, tx1_fee, 0, 1, 0,
false, 4,
LockPoints());
315 changeset->StageAddition(entry4.GetSharedTx(), tx2_fee, 0, 1, 0,
false, 4,
LockPoints());
322 const auto entry5 = pool.
GetIter(tx5->GetHash()).value();
325 changeset->StageRemoval(entry1);
326 changeset->StageRemoval(entry2);
327 changeset->StageRemoval(entry5);
328 changeset->StageAddition(tx1_conflict, tx1_fee, 0, 1, 0,
false, 4,
LockPoints());
329 changeset->StageAddition(entry4.GetSharedTx(), tx2_fee + entry5->GetModifiedFee() + 1, 0, 1, 0,
false, 4,
LockPoints());
345 const auto low_tx =
make_tx( {m_coinbase_txns[0]}, {10 *
COIN});
348 const auto entry_low = pool.
GetIter(low_tx->GetHash()).value();
349 const auto low_size = entry_low->GetAdjustedWeight();
351 const auto replacement_tx =
make_tx( {m_coinbase_txns[0]}, {9 *
COIN});
352 auto entry_replacement = entry.
FromTx(replacement_tx);
357 changeset->StageRemoval(entry_low);
358 changeset->StageAddition(replacement_tx, 0, 0, 1, 0,
false, 4,
LockPoints());
359 const auto replace_one{changeset->CalculateChunksForRBF()};
361 std::vector<FeeFrac> expected_old_chunks{{
low_fee, low_size}};
362 BOOST_CHECK(replace_one->first == expected_old_chunks);
363 std::vector<FeeFrac> expected_new_chunks{{0, entry_replacement.GetAdjustedWeight()}};
364 BOOST_CHECK(replace_one->second == expected_new_chunks);
370 changeset->StageRemoval(entry_low);
372 const auto replace_one_fee{changeset->CalculateChunksForRBF()};
374 std::vector<FeeFrac> expected_old_diagram{{
low_fee, low_size}};
375 BOOST_CHECK(replace_one_fee->first == expected_old_diagram);
376 std::vector<FeeFrac> expected_new_diagram{{
high_fee, entry_replacement.GetAdjustedWeight()}};
377 BOOST_CHECK(replace_one_fee->second == expected_new_diagram);
381 const auto high_tx =
make_tx( {low_tx}, {995 *
CENT});
383 const auto entry_high = pool.
GetIter(high_tx->GetHash()).value();
384 const auto high_size = entry_high->GetAdjustedWeight();
388 changeset->StageRemoval(entry_low);
389 changeset->StageRemoval(entry_high);
391 const auto replace_single_chunk{changeset->CalculateChunksForRBF()};
393 std::vector<FeeFrac> expected_old_chunks{{
low_fee +
high_fee, low_size + high_size}};
394 BOOST_CHECK(replace_single_chunk->first == expected_old_chunks);
395 std::vector<FeeFrac> expected_new_chunks{{
high_fee, entry_replacement.GetAdjustedWeight()}};
396 BOOST_CHECK(replace_single_chunk->second == expected_new_chunks);
402 changeset->StageRemoval(entry_high);
404 const auto replace_cpfp_child{changeset->CalculateChunksForRBF()};
406 std::vector<FeeFrac> expected_old_chunks{{
low_fee +
high_fee, low_size + high_size}};
407 BOOST_CHECK(replace_cpfp_child->first == expected_old_chunks);
408 std::vector<FeeFrac> expected_new_chunks{{
high_fee, entry_replacement.GetAdjustedWeight()}, {
low_fee, low_size}};
409 BOOST_CHECK(replace_cpfp_child->second == expected_new_chunks);
413 const auto high_tx_2 =
make_tx( {m_coinbase_txns[1]}, {10 *
COIN});
415 const auto entry_high_2 = pool.
GetIter(high_tx_2->GetHash()).value();
416 const auto high_size_2 = entry_high_2->GetAdjustedWeight();
418 const auto low_tx_2 =
make_tx( {high_tx_2}, {9 *
COIN});
420 const auto entry_low_2 = pool.
GetIter(low_tx_2->GetHash()).value();
421 const auto low_size_2 = entry_low_2->GetAdjustedWeight();
425 changeset->StageRemoval(entry_high_2);
426 changeset->StageRemoval(entry_low_2);
428 const auto replace_two_chunks_single_cluster{changeset->CalculateChunksForRBF()};
429 BOOST_CHECK(replace_two_chunks_single_cluster.has_value());
430 std::vector<FeeFrac> expected_old_chunks{{
high_fee, high_size_2}, {
low_fee, low_size_2}};
431 BOOST_CHECK(replace_two_chunks_single_cluster->first == expected_old_chunks);
432 std::vector<FeeFrac> expected_new_chunks{{
high_fee, low_size_2}};
433 BOOST_CHECK(replace_two_chunks_single_cluster->second == expected_new_chunks);
437 const auto conflict_1 =
make_tx( {m_coinbase_txns[2]}, {10 *
COIN});
439 const auto conflict_1_entry = pool.
GetIter(conflict_1->GetHash()).value();
441 const auto conflict_2 =
make_tx( {m_coinbase_txns[3]}, {10 *
COIN});
443 const auto conflict_2_entry = pool.
GetIter(conflict_2->GetHash()).value();
445 const auto conflict_3 =
make_tx( {m_coinbase_txns[4]}, {10 *
COIN});
447 const auto conflict_3_entry = pool.
GetIter(conflict_3->GetHash()).value();
451 changeset->StageRemoval(conflict_1_entry);
452 changeset->StageRemoval(conflict_2_entry);
453 changeset->StageRemoval(conflict_3_entry);
455 const auto replace_multiple_clusters{changeset->CalculateChunksForRBF()};
456 BOOST_CHECK(replace_multiple_clusters.has_value());
457 BOOST_CHECK(replace_multiple_clusters->first.size() == 3);
458 BOOST_CHECK(replace_multiple_clusters->second.size() == 1);
462 const auto conflict_1_child =
make_tx({conflict_1}, {995 *
CENT});
464 const auto conflict_1_child_entry = pool.
GetIter(conflict_1_child->GetHash()).value();
468 changeset->StageRemoval(conflict_1_entry);
469 changeset->StageRemoval(conflict_2_entry);
470 changeset->StageRemoval(conflict_3_entry);
471 changeset->StageRemoval(conflict_1_child_entry);
473 const auto replace_multiple_clusters_2{changeset->CalculateChunksForRBF()};
475 BOOST_CHECK(replace_multiple_clusters_2.has_value());
476 BOOST_CHECK(replace_multiple_clusters_2->first.size() == 4);
477 BOOST_CHECK(replace_multiple_clusters_2->second.size() == 1);
486 std::vector<FeeFrac> old_chunks{{{950, 300}, {100, 100}}};
487 std::vector<FeeFrac> new_chunks{{{1000, 300}, {50, 100}}};
493 old_chunks = {{950, 300}, {100, 100}};
494 new_chunks = {{1000, 300}, {0, 100}};
500 old_chunks = {{950, 300}, {100, 100}};
501 new_chunks = {{1100, 300}};
508 old_chunks = {{950, 300}, {100, 100}};
509 new_chunks = {{1100, 100}, {0, 100}};
515 old_chunks = {{950, 300}, {100, 100}};
516 new_chunks = {{750, 100}, {249, 250}, {151, 650}};
522 old_chunks = {{950, 300}, {100, 100}};
523 new_chunks = {{750, 100}, {250, 250}, {150, 150}};
529 old_chunks = {{950, 300}, {100, 100}};
530 new_chunks = {{950, 300}, {100, 100}};
536 old_chunks = {{950, 300}, {100, 99}};
537 new_chunks = {{950, 300}, {100, 100}};
544 old_chunks = {{950, 300}, {100, 99}};
545 new_chunks = {{950, 300}, {100, 100}, {0, 1}, {0, 1}};
551 new_chunks = {{950, 300}, {100, 100}, {0, 1}, {0, 1}, {1, 1}};
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr CAmount COIN
The amount of satoshis in one BTC.
TryAddToMempool(pool, CTxMemPoolEntry(tx, fee, 0, 1, 0, false, 4, lp))
#define Assert(val)
Identity function.
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Serialized script, used inside transaction inputs and outputs.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
void PrioritiseTransaction(const Txid &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
std::unique_ptr< ChangeSet > GetChangeSet() EXCLUSIVE_LOCKS_REQUIRED(cs)
std::optional< txiter > GetIter(const Txid &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
std::set< txiter, CompareIteratorByHash > setEntries
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
static transaction_identifier FromUint256(const uint256 &id)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(headers.FindFirst("key"), "value")
#define BOOST_CHECK(expr)
std::optional< std::pair< DiagramCheckError, std::string > > ImprovesFeerateDiagram(CTxMemPool::ChangeSet &changeset)
The replacement transaction must improve the feerate diagram of the mempool.
std::optional< std::string > PaysForRBF(CAmount original_fees, CAmount replacement_fees, size_t replacement_vsize, CFeeRate relay_fee, const Txid &txid)
The replacement transaction must pay more fees than the original transactions.
std::optional< std::string > EntriesAndTxidsDisjoint(const CTxMemPool::setEntries &ancestors, const std::set< Txid > &direct_conflicts, const Txid &txid)
Check the intersection between two sets of transactions (a set of mempool entries and a set of txids)...
std::optional< std::string > GetEntriesForConflicts(const CTransaction &tx, CTxMemPool &pool, const CTxMemPool::setEntries &iters_conflicting, CTxMemPool::setEntries &all_conflicts)
Get all descendants of iters_conflicting.
@ FAILURE
New diagram wasn't strictly superior
static constexpr unsigned int DEFAULT_INCREMENTAL_RELAY_FEE
Default for -incrementalrelayfee, which sets the minimum feerate increase for mempool limiting or rep...
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
uint256 GetRandHash() noexcept
Generate a random uint256.
static CTransactionRef make_tx(const std::vector< CTransactionRef > &inputs, const std::vector< CAmount > &output_values)
static CTransactionRef add_descendants(const CTransactionRef &tx, int32_t num_descendants, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(
BOOST_AUTO_TEST_CASE(feerate_chunks_utilities)
BOOST_FIXTURE_TEST_CASE(rbf_helper_functions, TestChain100Setup)
static constexpr CAmount CENT
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< std::vector< unsigned char > > stack
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
TestMemPoolEntryHelper & Fee(CAmount _fee)
std::unique_ptr< CTxMemPool > mempool
#define EXCLUSIVE_LOCKS_REQUIRED(...)
std::partial_ordering CompareChunks(std::span< const FeeFrac > chunks0, std::span< const FeeFrac > chunks1)
Compare the feerate diagrams implied by the provided sorted chunks data.