Implement new RBF logic for cluster mempool

With a total ordering on mempool transactions, we are now able to calculate a
transaction's mining score at all times. Use this to improve the RBF logic:

- we no longer enforce a "no new unconfirmed parents" rule

- we now require that the mempool's feerate diagram must improve in order
  to accept a replacement

- the topology restrictions for conflicts in the package rbf setting have been
  eliminated

Revert the temporary change to mempool_ephemeral_dust.py that were previously
made due to RBF validation checks being reordered.

Co-authored-by: Gregory Sanders <gsanders87@gmail.com>, glozow <gloriajzhao@gmail.com>
This commit is contained in:
Suhas Daftuar
2023-09-21 13:19:32 -04:00
parent ff8f115dec
commit 216e693729
11 changed files with 182 additions and 758 deletions

View File

@@ -37,37 +37,6 @@ static inline CTransactionRef make_tx(const std::vector<CTransactionRef>& inputs
return MakeTransactionRef(tx);
}
// Make two child transactions from parent (which must have at least 2 outputs).
// Each tx will have the same outputs, using the amounts specified in output_values.
static inline std::pair<CTransactionRef, CTransactionRef> make_two_siblings(const CTransactionRef parent,
const std::vector<CAmount>& output_values)
{
assert(parent->vout.size() >= 2);
// First tx takes first parent output
CMutableTransaction tx1 = CMutableTransaction();
tx1.vin.resize(1);
tx1.vout.resize(output_values.size());
tx1.vin[0].prevout.hash = parent->GetHash();
tx1.vin[0].prevout.n = 0;
// Add a witness so wtxid != txid
CScriptWitness witness;
witness.stack.emplace_back(10);
tx1.vin[0].scriptWitness = witness;
for (size_t i = 0; i < output_values.size(); ++i) {
tx1.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
tx1.vout[i].nValue = output_values[i];
}
// Second tx takes second parent output
CMutableTransaction tx2 = tx1;
tx2.vin[0].prevout.n = 1;
return std::make_pair(MakeTransactionRef(tx1), MakeTransactionRef(tx2));
}
static CTransactionRef add_descendants(const CTransactionRef& tx, int32_t num_descendants, CTxMemPool& pool)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)
{
@@ -79,39 +48,13 @@ static CTransactionRef add_descendants(const CTransactionRef& tx, int32_t num_de
for (int32_t i{0}; i < num_descendants; ++i) {
auto next_tx = make_tx(/*inputs=*/{tx_to_spend}, /*output_values=*/{(50 - i) * CENT});
AddToMempool(pool, entry.FromTx(next_tx));
BOOST_CHECK(pool.GetIter(next_tx->GetHash()).has_value());
tx_to_spend = next_tx;
}
// Return last created tx
return tx_to_spend;
}
static CTransactionRef add_descendant_to_parents(const std::vector<CTransactionRef>& parents, CTxMemPool& pool)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)
{
AssertLockHeld(::cs_main);
AssertLockHeld(pool.cs);
TestMemPoolEntryHelper entry;
// Assumes this isn't already spent in mempool
auto child_tx = make_tx(/*inputs=*/parents, /*output_values=*/{50 * CENT});
AddToMempool(pool, entry.FromTx(child_tx));
// Return last created tx
return child_tx;
}
// Makes two children for a single parent
static std::pair<CTransactionRef, CTransactionRef> add_children_to_parent(const CTransactionRef parent, CTxMemPool& pool)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)
{
AssertLockHeld(::cs_main);
AssertLockHeld(pool.cs);
TestMemPoolEntryHelper entry;
// Assumes this isn't already spent in mempool
auto children_tx = make_two_siblings(/*parent=*/parent, /*output_values=*/{50 * CENT});
AddToMempool(pool, entry.FromTx(children_tx.first));
AddToMempool(pool, entry.FromTx(children_tx.second));
return children_tx;
}
BOOST_FIXTURE_TEST_CASE(rbf_helper_functions, TestChain100Setup)
{
CTxMemPool& pool = *Assert(m_node.mempool);
@@ -149,12 +92,6 @@ BOOST_FIXTURE_TEST_CASE(rbf_helper_functions, TestChain100Setup)
const auto tx8 = make_tx(/*inputs=*/ {m_coinbase_txns[4]}, /*output_values=*/ {999 * CENT});
AddToMempool(pool, entry.Fee(high_fee).FromTx(tx8));
// Normal txs, will chain txns right before CheckConflictTopology test
const auto tx9 = make_tx(/*inputs=*/ {m_coinbase_txns[5]}, /*output_values=*/ {995 * CENT});
AddToMempool(pool, entry.Fee(normal_fee).FromTx(tx9));
const auto tx10 = make_tx(/*inputs=*/ {m_coinbase_txns[6]}, /*output_values=*/ {995 * CENT});
AddToMempool(pool, entry.Fee(normal_fee).FromTx(tx10));
// Will make these two parents of single child
const auto tx11 = make_tx(/*inputs=*/ {m_coinbase_txns[7]}, /*output_values=*/ {995 * CENT});
AddToMempool(pool, entry.Fee(normal_fee).FromTx(tx11));
@@ -173,11 +110,6 @@ BOOST_FIXTURE_TEST_CASE(rbf_helper_functions, TestChain100Setup)
const auto entry6_low_prioritised = pool.GetIter(tx6->GetHash()).value();
const auto entry7_high = pool.GetIter(tx7->GetHash()).value();
const auto entry8_high = pool.GetIter(tx8->GetHash()).value();
const auto entry9_unchained = pool.GetIter(tx9->GetHash()).value();
const auto entry10_unchained = pool.GetIter(tx10->GetHash()).value();
const auto entry11_unchained = pool.GetIter(tx11->GetHash()).value();
const auto entry12_unchained = pool.GetIter(tx12->GetHash()).value();
const auto entry13_unchained = pool.GetIter(tx13->GetHash()).value();
BOOST_CHECK_EQUAL(entry1_normal->GetFee(), normal_fee);
BOOST_CHECK_EQUAL(entry2_normal->GetFee(), normal_fee);
@@ -198,23 +130,6 @@ BOOST_FIXTURE_TEST_CASE(rbf_helper_functions, TestChain100Setup)
const auto unused_txid = Txid::FromUint256(GetRandHash());
// Tests for PaysMoreThanConflicts
// These tests use feerate, not absolute fee.
BOOST_CHECK(PaysMoreThanConflicts(/*iters_conflicting=*/set_12_normal,
/*replacement_feerate=*/CFeeRate(entry1_normal->GetModifiedFee() + 1, entry1_normal->GetTxSize() + 2),
/*txid=*/unused_txid).has_value());
// Replacement must be strictly greater than the originals.
BOOST_CHECK(PaysMoreThanConflicts(set_12_normal, CFeeRate(entry1_normal->GetModifiedFee(), entry1_normal->GetTxSize()), unused_txid).has_value());
BOOST_CHECK(PaysMoreThanConflicts(set_12_normal, CFeeRate(entry1_normal->GetModifiedFee() + 1, entry1_normal->GetTxSize()), unused_txid) == std::nullopt);
// These tests use modified fees (including prioritisation), not base fees.
BOOST_CHECK(PaysMoreThanConflicts({entry5_low}, CFeeRate(entry5_low->GetModifiedFee() + 1, entry5_low->GetTxSize()), unused_txid) == std::nullopt);
BOOST_CHECK(PaysMoreThanConflicts({entry6_low_prioritised}, CFeeRate(entry6_low_prioritised->GetFee() + 1, entry6_low_prioritised->GetTxSize()), unused_txid).has_value());
BOOST_CHECK(PaysMoreThanConflicts({entry6_low_prioritised}, CFeeRate(entry6_low_prioritised->GetModifiedFee() + 1, entry6_low_prioritised->GetTxSize()), unused_txid) == std::nullopt);
// PaysMoreThanConflicts checks individual feerate, not ancestor feerate. This test compares
// replacement_feerate and entry4_high's feerate, which are the same. The replacement_feerate is
// considered too low even though entry4_high has a low ancestor feerate.
BOOST_CHECK(PaysMoreThanConflicts(set_34_cpfp, CFeeRate(entry4_high->GetModifiedFee(), entry4_high->GetTxSize()), unused_txid).has_value());
// Tests for EntriesAndTxidsDisjoint
BOOST_CHECK(EntriesAndTxidsDisjoint(empty_set, {tx1->GetHash()}, unused_txid) == std::nullopt);
BOOST_CHECK(EntriesAndTxidsDisjoint(set_12_normal, {tx3->GetHash()}, unused_txid) == std::nullopt);
@@ -244,112 +159,91 @@ BOOST_FIXTURE_TEST_CASE(rbf_helper_functions, TestChain100Setup)
BOOST_CHECK(PaysForRBF(high_fee, high_fee + 4, 20, higher_relay_feerate, unused_txid) == std::nullopt);
BOOST_CHECK(PaysForRBF(low_fee, high_fee, 99999999, incremental_relay_feerate, unused_txid).has_value());
BOOST_CHECK(PaysForRBF(low_fee, high_fee + 99999999, 99999999, incremental_relay_feerate, unused_txid) == std::nullopt);
}
// Tests for GetEntriesForConflicts
CTxMemPool::setEntries all_parents{entry1_normal, entry3_low, entry5_low, entry7_high, entry8_high};
CTxMemPool::setEntries all_children{entry2_normal, entry4_high, entry6_low_prioritised};
const std::vector<CTransactionRef> parent_inputs({m_coinbase_txns[0], m_coinbase_txns[1], m_coinbase_txns[2],
m_coinbase_txns[3], m_coinbase_txns[4]});
const auto conflicts_with_parents = make_tx(parent_inputs, {50 * CENT});
CTxMemPool::setEntries all_conflicts;
BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicts_with_parents.get(),
/*pool=*/ pool,
/*iters_conflicting=*/ all_parents,
/*all_conflicts=*/ all_conflicts) == std::nullopt);
BOOST_CHECK(all_conflicts == all_entries);
auto conflicts_size = all_conflicts.size();
all_conflicts.clear();
BOOST_FIXTURE_TEST_CASE(rbf_conflicts_calculator, TestChain100Setup)
{
CTxMemPool& pool = *Assert(m_node.mempool);
LOCK2(::cs_main, pool.cs);
TestMemPoolEntryHelper entry;
add_descendants(tx2, 23, pool);
BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts) == std::nullopt);
conflicts_size += 23;
BOOST_CHECK_EQUAL(all_conflicts.size(), conflicts_size);
all_conflicts.clear();
const CAmount normal_fee{CENT/10};
add_descendants(tx4, 23, pool);
BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts) == std::nullopt);
conflicts_size += 23;
BOOST_CHECK_EQUAL(all_conflicts.size(), conflicts_size);
all_conflicts.clear();
add_descendants(tx6, 23, pool);
BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts) == std::nullopt);
conflicts_size += 23;
BOOST_CHECK_EQUAL(all_conflicts.size(), conflicts_size);
all_conflicts.clear();
add_descendants(tx7, 23, pool);
BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts) == std::nullopt);
conflicts_size += 23;
BOOST_CHECK_EQUAL(all_conflicts.size(), conflicts_size);
BOOST_CHECK_EQUAL(all_conflicts.size(), 100);
all_conflicts.clear();
// Exceeds maximum number of conflicts.
add_descendants(tx8, 1, pool);
BOOST_CHECK(GetEntriesForConflicts(*conflicts_with_parents.get(), pool, all_parents, all_conflicts).has_value());
// Tests for HasNoNewUnconfirmed
const auto spends_unconfirmed = make_tx({tx1}, {36 * CENT});
for (const auto& input : spends_unconfirmed->vin) {
// Spends unconfirmed inputs.
BOOST_CHECK(pool.exists(input.prevout.hash));
// Create two parent transactions with 51 outputs each
const int NUM_OUTPUTS = 51;
std::vector<CAmount> output_values;
output_values.reserve(NUM_OUTPUTS);
for (int i = 0; i < NUM_OUTPUTS; ++i) {
output_values.push_back(1 * COIN);
}
BOOST_CHECK(HasNoNewUnconfirmed(/*tx=*/ *spends_unconfirmed.get(),
/*pool=*/ pool,
/*iters_conflicting=*/ all_entries) == std::nullopt);
BOOST_CHECK(HasNoNewUnconfirmed(*spends_unconfirmed.get(), pool, {entry2_normal}) == std::nullopt);
BOOST_CHECK(HasNoNewUnconfirmed(*spends_unconfirmed.get(), pool, empty_set).has_value());
const auto spends_new_unconfirmed = make_tx({tx1, tx8}, {36 * CENT});
BOOST_CHECK(HasNoNewUnconfirmed(*spends_new_unconfirmed.get(), pool, {entry2_normal}).has_value());
BOOST_CHECK(HasNoNewUnconfirmed(*spends_new_unconfirmed.get(), pool, all_entries).has_value());
const auto parent_tx_1 = make_tx(/*inputs=*/ {m_coinbase_txns[0]}, /*output_values=*/ output_values);
const auto parent_tx_2 = make_tx(/*inputs=*/ {m_coinbase_txns[1]}, /*output_values=*/ output_values);
AddToMempool(pool, entry.Fee(normal_fee).FromTx(parent_tx_1));
AddToMempool(pool, entry.Fee(normal_fee).FromTx(parent_tx_2));
const auto spends_conflicting_confirmed = make_tx({m_coinbase_txns[0], m_coinbase_txns[1]}, {45 * CENT});
BOOST_CHECK(HasNoNewUnconfirmed(*spends_conflicting_confirmed.get(), pool, {entry1_normal, entry3_low}) == std::nullopt);
std::vector<CTransactionRef> direct_children;
// Tests for CheckConflictTopology
// Create individual spends of these outputs
for (const auto& parent_tx : {parent_tx_1, parent_tx_2}) {
for (auto i = 0; i < NUM_OUTPUTS; ++i) {
auto pretx = make_tx(/*inputs=*/ {parent_tx}, /*output_values=*/ {995 * CENT});
CMutableTransaction tx(*pretx);
tx.vin[0].prevout.n = i;
AddToMempool(pool, entry.Fee(normal_fee).FromTx(tx));
BOOST_CHECK(pool.GetIter(tx.GetHash()).has_value());
direct_children.push_back(MakeTransactionRef(tx));
}
}
// Tx4 has 23 descendants
BOOST_CHECK_EQUAL(pool.CheckConflictTopology(set_34_cpfp).value(), strprintf("%s has 24 descendants, max 1 allowed", entry3_low->GetSharedTx()->GetHash().ToString()));
// At this point, we should have 2 clusters in the mempool, each with 52
// transactions.
// No descendants yet
BOOST_CHECK(pool.CheckConflictTopology({entry9_unchained}) == std::nullopt);
// parent_tx and all children are in one cluster, so we can have as many
// conflicts within this cluster as we want without violating the RBF conflicts
// limit.
const auto parent_entry_1 = pool.GetIter(parent_tx_1->GetHash()).value();
const auto parent_entry_2 = pool.GetIter(parent_tx_2->GetHash()).value();
const auto conflicting_transaction = make_tx({parent_tx_1, parent_tx_2}, {50 * CENT});
CTxMemPool::setEntries all_conflicts, dummy;
BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicting_transaction.get(),
/*pool=*/ pool,
/*iters_conflicting=*/ {parent_entry_1, parent_entry_2},
/*all_conflicts=*/ all_conflicts) == std::nullopt);
// Add 1 descendant, still ok
add_descendants(tx9, 1, pool);
BOOST_CHECK(pool.CheckConflictTopology({entry9_unchained}) == std::nullopt);
dummy.clear();
// Conflicting directly with all those conflicts doesn't change anything.
BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicting_transaction.get(),
/*pool=*/ pool,
/*iters_conflicting=*/ all_conflicts,
/*all_conflicts=*/ dummy) == std::nullopt);
BOOST_CHECK_EQUAL(all_conflicts.size(), dummy.size());
dummy.clear();
// N direct conflicts; ok
BOOST_CHECK(pool.CheckConflictTopology({entry9_unchained, entry10_unchained, entry11_unchained}) == std::nullopt);
// If we mine the parent_tx's, then the clusters split (102 clusters).
pool.removeForBlock({parent_tx_1, parent_tx_2}, /* dummy */ 1);
// Add 1 descendant, still ok, even if it's considered a direct conflict as well
const auto child_tx = add_descendants(tx10, 1, pool);
const auto entry10_child = pool.GetIter(child_tx->GetHash()).value();
BOOST_CHECK(pool.CheckConflictTopology({entry9_unchained, entry10_unchained, entry11_unchained}) == std::nullopt);
BOOST_CHECK(pool.CheckConflictTopology({entry9_unchained, entry10_unchained, entry11_unchained, entry10_child}) == std::nullopt);
// Add some descendants now to each of the direct children (we can do this now that the clusters have split).
for (const auto& child : direct_children) {
add_descendants(child, 10, pool);
}
// One more, size 3 cluster too much
const auto grand_child_tx = add_descendants(child_tx, 1, pool);
const auto entry10_grand_child = pool.GetIter(grand_child_tx->GetHash()).value();
BOOST_CHECK_EQUAL(pool.CheckConflictTopology({entry9_unchained, entry10_unchained, entry11_unchained}).value(), strprintf("%s has 2 descendants, max 1 allowed", entry10_unchained->GetSharedTx()->GetHash().ToString()));
// even if direct conflict is descendent itself
BOOST_CHECK_EQUAL(pool.CheckConflictTopology({entry9_unchained, entry10_grand_child, entry11_unchained}).value(), strprintf("%s has 2 ancestors, max 1 allowed", entry10_grand_child->GetSharedTx()->GetHash().ToString()));
// Make a single child from two singleton parents
const auto two_parent_child_tx = add_descendant_to_parents({tx11, tx12}, pool);
const auto entry_two_parent_child = pool.GetIter(two_parent_child_tx->GetHash()).value();
BOOST_CHECK_EQUAL(pool.CheckConflictTopology({entry11_unchained}).value(), strprintf("%s is not the only parent of child %s", entry11_unchained->GetSharedTx()->GetHash().ToString(), entry_two_parent_child->GetSharedTx()->GetHash().ToString()));
BOOST_CHECK_EQUAL(pool.CheckConflictTopology({entry12_unchained}).value(), strprintf("%s is not the only parent of child %s", entry12_unchained->GetSharedTx()->GetHash().ToString(), entry_two_parent_child->GetSharedTx()->GetHash().ToString()));
BOOST_CHECK_EQUAL(pool.CheckConflictTopology({entry_two_parent_child}).value(), strprintf("%s has 2 ancestors, max 1 allowed", entry_two_parent_child->GetSharedTx()->GetHash().ToString()));
// Single parent with two children, we will conflict with the siblings directly only
const auto two_siblings = add_children_to_parent(tx13, pool);
const auto entry_sibling_1 = pool.GetIter(two_siblings.first->GetHash()).value();
const auto entry_sibling_2 = pool.GetIter(two_siblings.second->GetHash()).value();
BOOST_CHECK_EQUAL(pool.CheckConflictTopology({entry_sibling_1}).value(), strprintf("%s is not the only child of parent %s", entry_sibling_1->GetSharedTx()->GetHash().ToString(), entry13_unchained->GetSharedTx()->GetHash().ToString()));
BOOST_CHECK_EQUAL(pool.CheckConflictTopology({entry_sibling_2}).value(), strprintf("%s is not the only child of parent %s", entry_sibling_2->GetSharedTx()->GetHash().ToString(), entry13_unchained->GetSharedTx()->GetHash().ToString()));
// We can conflict with 100 different clusters, even if they have lots of transactions.
CTxMemPool::setEntries conflicts;
for (auto i = 0; i < 100; ++i) {
conflicts.insert(pool.GetIter(direct_children[i]->GetHash()).value());
}
BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicting_transaction.get(),
/*pool=*/ pool,
/*iters_conflicting=*/ conflicts,
/*all_conflicts=*/ dummy) == std::nullopt);
// Conflicting with 1 more distinct cluster causes failure, however.
conflicts.insert(pool.GetIter(direct_children[100]->GetHash()).value());
BOOST_CHECK(GetEntriesForConflicts(/*tx=*/ *conflicting_transaction.get(),
/*pool=*/ pool,
/*iters_conflicting=*/ conflicts,
/*all_conflicts=*/ dummy).has_value());
}
BOOST_FIXTURE_TEST_CASE(improves_feerate, TestChain100Setup)
@@ -427,7 +321,7 @@ BOOST_FIXTURE_TEST_CASE(improves_feerate, TestChain100Setup)
BOOST_CHECK(ImprovesFeerateDiagram(*changeset) == std::nullopt);
changeset.reset();
// Adding a grandchild makes the cluster size 3, which is uncalculable
// Adding a grandchild makes the cluster size 3, which is also calculable
const auto tx5 = make_tx(/*inputs=*/ {tx2}, /*output_values=*/ {995 * CENT});
AddToMempool(pool, entry.Fee(normal_fee).FromTx(tx5));
const auto entry5 = pool.GetIter(tx5->GetHash()).value();
@@ -439,9 +333,7 @@ BOOST_FIXTURE_TEST_CASE(improves_feerate, TestChain100Setup)
changeset->StageAddition(tx1_conflict, tx1_fee, 0, 1, 0, false, 4, LockPoints());
changeset->StageAddition(entry4.GetSharedTx(), tx2_fee + entry5->GetModifiedFee() + 1, 0, 1, 0, false, 4, LockPoints());
const auto res3 = ImprovesFeerateDiagram(*changeset);
BOOST_CHECK(res3.has_value());
BOOST_CHECK(res3.value().first == DiagramCheckError::UNCALCULABLE);
BOOST_CHECK_MESSAGE(res3.value().second == strprintf("%s has 2 descendants, max 1 allowed", tx1->GetHash().GetHex()), res3.value().second);
BOOST_CHECK(res3 == std::nullopt);
}
BOOST_FIXTURE_TEST_CASE(calc_feerate_diagram_rbf, TestChain100Setup)
@@ -451,7 +343,6 @@ BOOST_FIXTURE_TEST_CASE(calc_feerate_diagram_rbf, TestChain100Setup)
TestMemPoolEntryHelper entry;
const CAmount low_fee{CENT/100};
const CAmount normal_fee{CENT/10};
const CAmount high_fee{CENT};
// low -> high -> medium fee transactions that would result in two chunks together since they
@@ -460,7 +351,7 @@ BOOST_FIXTURE_TEST_CASE(calc_feerate_diagram_rbf, TestChain100Setup)
AddToMempool(pool, entry.Fee(low_fee).FromTx(low_tx));
const auto entry_low = pool.GetIter(low_tx->GetHash()).value();
const auto low_size = entry_low->GetTxSize();
const auto low_size = entry_low->GetAdjustedWeight();
const auto replacement_tx = make_tx(/*inputs=*/ {m_coinbase_txns[0]}, /*output_values=*/ {9 * COIN});
auto entry_replacement = entry.FromTx(replacement_tx);
@@ -474,7 +365,7 @@ BOOST_FIXTURE_TEST_CASE(calc_feerate_diagram_rbf, TestChain100Setup)
BOOST_CHECK(replace_one.has_value());
std::vector<FeeFrac> expected_old_chunks{{low_fee, low_size}};
BOOST_CHECK(replace_one->first == expected_old_chunks);
std::vector<FeeFrac> expected_new_chunks{{0, int32_t(entry_replacement.GetTxSize())}};
std::vector<FeeFrac> expected_new_chunks{{0, entry_replacement.GetAdjustedWeight()}};
BOOST_CHECK(replace_one->second == expected_new_chunks);
}
@@ -487,7 +378,7 @@ BOOST_FIXTURE_TEST_CASE(calc_feerate_diagram_rbf, TestChain100Setup)
BOOST_CHECK(replace_one_fee.has_value());
std::vector<FeeFrac> expected_old_diagram{{low_fee, low_size}};
BOOST_CHECK(replace_one_fee->first == expected_old_diagram);
std::vector<FeeFrac> expected_new_diagram{{high_fee, low_size}};
std::vector<FeeFrac> expected_new_diagram{{high_fee, entry_replacement.GetAdjustedWeight()}};
BOOST_CHECK(replace_one_fee->second == expected_new_diagram);
}
@@ -495,7 +386,7 @@ BOOST_FIXTURE_TEST_CASE(calc_feerate_diagram_rbf, TestChain100Setup)
const auto high_tx = make_tx(/*inputs=*/ {low_tx}, /*output_values=*/ {995 * CENT});
AddToMempool(pool, entry.Fee(high_fee).FromTx(high_tx));
const auto entry_high = pool.GetIter(high_tx->GetHash()).value();
const auto high_size = entry_high->GetTxSize();
const auto high_size = entry_high->GetAdjustedWeight();
{
auto changeset = pool.GetChangeSet();
@@ -506,7 +397,7 @@ BOOST_FIXTURE_TEST_CASE(calc_feerate_diagram_rbf, TestChain100Setup)
BOOST_CHECK(replace_single_chunk.has_value());
std::vector<FeeFrac> expected_old_chunks{{low_fee + high_fee, low_size + high_size}};
BOOST_CHECK(replace_single_chunk->first == expected_old_chunks);
std::vector<FeeFrac> expected_new_chunks{{high_fee, low_size}};
std::vector<FeeFrac> expected_new_chunks{{high_fee, entry_replacement.GetAdjustedWeight()}};
BOOST_CHECK(replace_single_chunk->second == expected_new_chunks);
}
@@ -519,36 +410,20 @@ BOOST_FIXTURE_TEST_CASE(calc_feerate_diagram_rbf, TestChain100Setup)
BOOST_CHECK(replace_cpfp_child.has_value());
std::vector<FeeFrac> expected_old_chunks{{low_fee + high_fee, low_size + high_size}};
BOOST_CHECK(replace_cpfp_child->first == expected_old_chunks);
std::vector<FeeFrac> expected_new_chunks{{high_fee, low_size}, {low_fee, low_size}};
std::vector<FeeFrac> expected_new_chunks{{high_fee, entry_replacement.GetAdjustedWeight()}, {low_fee, low_size}};
BOOST_CHECK(replace_cpfp_child->second == expected_new_chunks);
}
// third transaction causes the topology check to fail
const auto normal_tx = make_tx(/*inputs=*/ {high_tx}, /*output_values=*/ {995 * CENT});
AddToMempool(pool, entry.Fee(normal_fee).FromTx(normal_tx));
const auto entry_normal = pool.GetIter(normal_tx->GetHash()).value();
{
auto changeset = pool.GetChangeSet();
changeset->StageRemoval(entry_low);
changeset->StageRemoval(entry_high);
changeset->StageRemoval(entry_normal);
changeset->StageAddition(replacement_tx, high_fee, 0, 1, 0, false, 4, LockPoints());
const auto replace_too_large{changeset->CalculateChunksForRBF()};
BOOST_CHECK(!replace_too_large.has_value());
BOOST_CHECK_EQUAL(util::ErrorString(replace_too_large).original, strprintf("%s has both ancestor and descendant, exceeding cluster limit of 2", high_tx->GetHash().GetHex()));
}
// Make a size 2 cluster that is itself two chunks; evict both txns
const auto high_tx_2 = make_tx(/*inputs=*/ {m_coinbase_txns[1]}, /*output_values=*/ {10 * COIN});
AddToMempool(pool, entry.Fee(high_fee).FromTx(high_tx_2));
const auto entry_high_2 = pool.GetIter(high_tx_2->GetHash()).value();
const auto high_size_2 = entry_high_2->GetTxSize();
const auto high_size_2 = entry_high_2->GetAdjustedWeight();
const auto low_tx_2 = make_tx(/*inputs=*/ {high_tx_2}, /*output_values=*/ {9 * COIN});
AddToMempool(pool, entry.Fee(low_fee).FromTx(low_tx_2));
const auto entry_low_2 = pool.GetIter(low_tx_2->GetHash()).value();
const auto low_size_2 = entry_low_2->GetTxSize();
const auto low_size_2 = entry_low_2->GetAdjustedWeight();
{
auto changeset = pool.GetChangeSet();
@@ -563,7 +438,7 @@ BOOST_FIXTURE_TEST_CASE(calc_feerate_diagram_rbf, TestChain100Setup)
BOOST_CHECK(replace_two_chunks_single_cluster->second == expected_new_chunks);
}
// You can have more than two direct conflicts if there are multiple affected clusters, all of size 2 or less
// You can have more than two direct conflicts
const auto conflict_1 = make_tx(/*inputs=*/ {m_coinbase_txns[2]}, /*output_values=*/ {10 * COIN});
AddToMempool(pool, entry.Fee(low_fee).FromTx(conflict_1));
const auto conflict_1_entry = pool.GetIter(conflict_1->GetHash()).value();
@@ -606,25 +481,6 @@ BOOST_FIXTURE_TEST_CASE(calc_feerate_diagram_rbf, TestChain100Setup)
BOOST_CHECK(replace_multiple_clusters_2->first.size() == 4);
BOOST_CHECK(replace_multiple_clusters_2->second.size() == 1);
}
// Add another descendant to conflict_1, making the cluster size > 2 should fail at this point.
const auto conflict_1_grand_child = make_tx(/*inputs=*/{conflict_1_child}, /*output_values=*/ {995 * CENT});
AddToMempool(pool, entry.Fee(high_fee).FromTx(conflict_1_grand_child));
const auto conflict_1_grand_child_entry = pool.GetIter(conflict_1_child->GetHash()).value();
{
auto changeset = pool.GetChangeSet();
changeset->StageRemoval(conflict_1_entry);
changeset->StageRemoval(conflict_2_entry);
changeset->StageRemoval(conflict_3_entry);
changeset->StageRemoval(conflict_1_child_entry);
changeset->StageRemoval(conflict_1_grand_child_entry);
changeset->StageAddition(replacement_tx, high_fee, 0, 1, 0, false, 4, LockPoints());
const auto replace_cluster_size_3{changeset->CalculateChunksForRBF()};
BOOST_CHECK(!replace_cluster_size_3.has_value());
BOOST_CHECK_EQUAL(util::ErrorString(replace_cluster_size_3).original, strprintf("%s has 2 descendants, max 1 allowed", conflict_1->GetHash().GetHex()));
}
}
BOOST_AUTO_TEST_CASE(feerate_chunks_utilities)