#include <node/mini_miner.h>
#include <random.h>
#include <txmempool.h>
#include <util/time.h>
#include <test/util/setup_common.h>
#include <test/util/txmempool.h>
#include <boost/test/unit_test.hpp>
#include <optional>
#include <vector>
BOOST_FIXTURE_TEST_SUITE(miniminer_tests, TestingSetup)
const CAmount low_fee{CENT/2000}; const CAmount med_fee{CENT/200}; const CAmount high_fee{CENT/10};
static inline CTransactionRef make_tx(const std::vector<COutPoint>& inputs, size_t num_outputs)
{
CMutableTransaction tx = CMutableTransaction();
tx.vin.resize(inputs.size());
tx.vout.resize(num_outputs);
for (size_t i = 0; i < inputs.size(); ++i) {
tx.vin[i].prevout = inputs[i];
}
for (size_t i = 0; i < num_outputs; ++i) {
tx.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
tx.vout[i].nValue = COIN;
}
return MakeTransactionRef(tx);
}
static inline bool sanity_check(const std::vector<CTransactionRef>& transactions,
const std::map<COutPoint, CAmount>& bumpfees)
{
for (const auto& [outpoint, fee] : bumpfees) {
if (fee < 0) return false;
if (fee == 0) continue;
auto outpoint_ = outpoint; const bool found = std::any_of(transactions.cbegin(), transactions.cend(), [&](const auto& tx) {
return outpoint_.hash == tx->GetHash() && outpoint_.n < tx->vout.size();
});
if (!found) return false;
}
for (const auto& tx : transactions) {
if (tx->vout.size() > 1) {
std::set<CAmount> distinct_bumpfees;
for (size_t i{0}; i < tx->vout.size(); ++i) {
const auto bumpfee = bumpfees.find(COutPoint{tx->GetHash(), static_cast<uint32_t>(i)});
if (bumpfee != bumpfees.end()) distinct_bumpfees.insert(bumpfee->second);
}
if (distinct_bumpfees.size() > 1) return false;
}
}
return true;
}
template <typename Key, typename Value>
Value Find(const std::map<Key, Value>& map, const Key& key)
{
auto it = map.find(key);
BOOST_CHECK_MESSAGE(it != map.end(), strprintf("Cannot find %s", key.ToString()));
return it->second;
}
BOOST_FIXTURE_TEST_CASE(miniminer_negative, TestChain100Setup)
{
CTxMemPool& pool = *Assert(m_node.mempool);
LOCK2(::cs_main, pool.cs);
TestMemPoolEntryHelper entry;
const CAmount positive_base_fee{1000};
const CAmount negative_fee_delta{-50000};
const CAmount negative_modified_fees{positive_base_fee + negative_fee_delta};
BOOST_CHECK(negative_modified_fees < 0);
const auto tx_mod_negative = make_tx({COutPoint{m_coinbase_txns[4]->GetHash(), 0}}, 1);
AddToMempool(pool, entry.Fee(positive_base_fee).FromTx(tx_mod_negative));
pool.PrioritiseTransaction(tx_mod_negative->GetHash(), negative_fee_delta);
const COutPoint only_outpoint{tx_mod_negative->GetHash(), 0};
node::MiniMiner mini_miner_target0(pool, {only_outpoint});
BOOST_CHECK(mini_miner_target0.IsReadyToCalculate());
const CFeeRate feerate_zero(0);
mini_miner_target0.BuildMockTemplate(feerate_zero);
BOOST_CHECK(negative_modified_fees < feerate_zero.GetFee(Assert(pool.GetEntry(tx_mod_negative->GetHash()))->GetTxSize()));
BOOST_CHECK(mini_miner_target0.GetMockTemplateTxids().empty());
node::MiniMiner mini_miner_no_target(pool, {only_outpoint});
BOOST_CHECK(mini_miner_no_target.IsReadyToCalculate());
mini_miner_no_target.BuildMockTemplate(std::nullopt);
const auto template_txids{mini_miner_no_target.GetMockTemplateTxids()};
BOOST_CHECK_EQUAL(template_txids.size(), 1);
BOOST_CHECK(template_txids.count(tx_mod_negative->GetHash().ToUint256()) > 0);
}
BOOST_FIXTURE_TEST_CASE(miniminer_1p1c, TestChain100Setup)
{
CTxMemPool& pool = *Assert(m_node.mempool);
LOCK2(::cs_main, pool.cs);
TestMemPoolEntryHelper entry;
const auto tx0 = make_tx({COutPoint{m_coinbase_txns[0]->GetHash(), 0}}, 2);
AddToMempool(pool, entry.Fee(med_fee).FromTx(tx0));
const auto tx1 = make_tx({COutPoint{tx0->GetHash(), 0}}, 1);
AddToMempool(pool, entry.Fee(med_fee).FromTx(tx1));
const auto tx2 = make_tx({COutPoint{m_coinbase_txns[1]->GetHash(), 0}}, 2);
AddToMempool(pool, entry.Fee(low_fee).FromTx(tx2));
const auto tx3 = make_tx({COutPoint{tx2->GetHash(), 0}}, 1);
AddToMempool(pool, entry.Fee(high_fee).FromTx(tx3));
const auto tx4 = make_tx({COutPoint{m_coinbase_txns[2]->GetHash(), 0}}, 2);
AddToMempool(pool, entry.Fee(low_fee).FromTx(tx4));
const auto tx5 = make_tx({COutPoint{tx4->GetHash(), 0}}, 1);
AddToMempool(pool, entry.Fee(low_fee).FromTx(tx5));
const CAmount tx5_delta{CENT/100};
pool.PrioritiseTransaction(tx5->GetHash(), tx5_delta);
const CAmount tx5_mod_fee{low_fee + tx5_delta};
const auto tx6 = make_tx({COutPoint{m_coinbase_txns[3]->GetHash(), 0}}, 2);
AddToMempool(pool, entry.Fee(high_fee).FromTx(tx6));
const auto tx7 = make_tx({COutPoint{tx6->GetHash(), 0}}, 1);
AddToMempool(pool, entry.Fee(low_fee).FromTx(tx7));
std::vector<COutPoint> all_unspent_outpoints({
COutPoint{tx0->GetHash(), 1},
COutPoint{tx1->GetHash(), 0},
COutPoint{tx2->GetHash(), 1},
COutPoint{tx3->GetHash(), 0},
COutPoint{tx4->GetHash(), 1},
COutPoint{tx5->GetHash(), 0},
COutPoint{tx6->GetHash(), 1},
COutPoint{tx7->GetHash(), 0}
});
for (const auto& outpoint : all_unspent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint));
std::vector<COutPoint> all_spent_outpoints({
COutPoint{tx0->GetHash(), 0},
COutPoint{tx2->GetHash(), 0},
COutPoint{tx4->GetHash(), 0},
COutPoint{tx6->GetHash(), 0}
});
for (const auto& outpoint : all_spent_outpoints) BOOST_CHECK(pool.GetConflictTx(outpoint) != nullptr);
std::vector<COutPoint> all_parent_outputs({
COutPoint{tx0->GetHash(), 0},
COutPoint{tx0->GetHash(), 1},
COutPoint{tx2->GetHash(), 0},
COutPoint{tx2->GetHash(), 1},
COutPoint{tx4->GetHash(), 0},
COutPoint{tx4->GetHash(), 1},
COutPoint{tx6->GetHash(), 0},
COutPoint{tx6->GetHash(), 1}
});
std::vector<CTransactionRef> all_transactions{tx0, tx1, tx2, tx3, tx4, tx5, tx6, tx7};
struct TxDimensions {
int32_t vsize; CAmount mod_fee; CFeeRate feerate;
};
std::map<uint256, TxDimensions> tx_dims;
for (const auto& tx : all_transactions) {
const auto& entry{*Assert(pool.GetEntry(tx->GetHash()))};
tx_dims.emplace(tx->GetHash(), TxDimensions{entry.GetTxSize(), entry.GetModifiedFee(),
CFeeRate(entry.GetModifiedFee(), entry.GetTxSize())});
}
const std::vector<CFeeRate> various_normal_feerates({CFeeRate(0), CFeeRate(500), CFeeRate(999),
CFeeRate(1000), CFeeRate(2000), CFeeRate(2500),
CFeeRate(3333), CFeeRate(7800), CFeeRate(11199),
CFeeRate(23330), CFeeRate(50000), CFeeRate(5*CENT)});
std::vector<COutPoint> nonexistent_outpoints({ COutPoint{Txid::FromUint256(GetRandHash()), 0}, COutPoint{Txid::FromUint256(GetRandHash()), 3} });
for (const auto& outpoint : nonexistent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint));
for (const auto& feerate : various_normal_feerates) {
node::MiniMiner mini_miner(pool, nonexistent_outpoints);
BOOST_CHECK(mini_miner.IsReadyToCalculate());
auto bump_fees = mini_miner.CalculateBumpFees(feerate);
BOOST_CHECK(!mini_miner.IsReadyToCalculate());
BOOST_CHECK(sanity_check(all_transactions, bump_fees));
BOOST_CHECK(bump_fees.size() == nonexistent_outpoints.size());
for (const auto& outpoint: nonexistent_outpoints) {
auto it = bump_fees.find(outpoint);
BOOST_CHECK(it != bump_fees.end());
BOOST_CHECK_EQUAL(it->second, 0);
}
}
for (const auto& target_feerate : various_normal_feerates) {
node::MiniMiner mini_miner(pool, all_unspent_outpoints);
BOOST_CHECK(mini_miner.IsReadyToCalculate());
auto bump_fees = mini_miner.CalculateBumpFees(target_feerate);
BOOST_CHECK(!mini_miner.IsReadyToCalculate());
BOOST_CHECK(sanity_check(all_transactions, bump_fees));
BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size());
const TxDimensions& tx0_dimensions = tx_dims.find(tx0->GetHash())->second;
CAmount bumpfee0 = Find(bump_fees, COutPoint{tx0->GetHash(), 1});
if (target_feerate <= tx0_dimensions.feerate) {
BOOST_CHECK_EQUAL(bumpfee0, 0);
} else {
BOOST_CHECK_EQUAL(bumpfee0, target_feerate.GetFee(tx0_dimensions.vsize) - tx0_dimensions.mod_fee);
}
const TxDimensions& tx2_dimensions = tx_dims.find(tx2->GetHash())->second;
const TxDimensions& tx3_dimensions = tx_dims.find(tx3->GetHash())->second;
const CFeeRate tx2_feerate = CFeeRate(tx2_dimensions.mod_fee + tx3_dimensions.mod_fee, tx2_dimensions.vsize + tx3_dimensions.vsize);
CAmount bumpfee2 = Find(bump_fees, COutPoint{tx2->GetHash(), 1});
if (target_feerate <= tx2_feerate) {
BOOST_CHECK_EQUAL(bumpfee2, 0);
} else {
BOOST_CHECK_EQUAL(bumpfee2, target_feerate.GetFee(tx2_dimensions.vsize) - tx2_dimensions.mod_fee);
}
const TxDimensions& tx4_dimensions = tx_dims.find(tx4->GetHash())->second;
const TxDimensions& tx5_dimensions = tx_dims.find(tx5->GetHash())->second;
const CFeeRate tx4_feerate = CFeeRate(tx4_dimensions.mod_fee + tx5_dimensions.mod_fee, tx4_dimensions.vsize + tx5_dimensions.vsize);
CAmount bumpfee4 = Find(bump_fees, COutPoint{tx4->GetHash(), 1});
if (target_feerate <= tx4_feerate) {
BOOST_CHECK_EQUAL(bumpfee4, 0);
} else {
BOOST_CHECK_EQUAL(bumpfee4, target_feerate.GetFee(tx4_dimensions.vsize) - tx4_dimensions.mod_fee);
}
}
for (const auto& target_feerate : various_normal_feerates) {
node::MiniMiner mini_miner_all_spent(pool, all_spent_outpoints);
BOOST_CHECK(mini_miner_all_spent.IsReadyToCalculate());
auto bump_fees_all_spent = mini_miner_all_spent.CalculateBumpFees(target_feerate);
BOOST_CHECK(!mini_miner_all_spent.IsReadyToCalculate());
BOOST_CHECK_EQUAL(bump_fees_all_spent.size(), all_spent_outpoints.size());
node::MiniMiner mini_miner_all_parents(pool, all_parent_outputs);
BOOST_CHECK(mini_miner_all_parents.IsReadyToCalculate());
auto bump_fees_all_parents = mini_miner_all_parents.CalculateBumpFees(target_feerate);
BOOST_CHECK(!mini_miner_all_parents.IsReadyToCalculate());
BOOST_CHECK_EQUAL(bump_fees_all_parents.size(), all_parent_outputs.size());
for (auto& bump_fees : {bump_fees_all_parents, bump_fees_all_spent}) {
BOOST_CHECK(sanity_check(all_transactions, bump_fees));
const TxDimensions& tx0_dimensions = tx_dims.find(tx0->GetHash())->second;
CAmount it0_spent = Find(bump_fees, COutPoint{tx0->GetHash(), 0});
if (target_feerate <= tx0_dimensions.feerate) {
BOOST_CHECK_EQUAL(it0_spent, 0);
} else {
BOOST_CHECK_EQUAL(it0_spent, target_feerate.GetFee(tx0_dimensions.vsize) - tx0_dimensions.mod_fee);
}
const TxDimensions& tx2_dimensions = tx_dims.find(tx2->GetHash())->second;
const CFeeRate tx2_feerate_unbumped = tx2_dimensions.feerate;
auto it2_spent = Find(bump_fees, COutPoint{tx2->GetHash(), 0});
if (target_feerate <= tx2_feerate_unbumped) {
BOOST_CHECK_EQUAL(it2_spent, 0);
} else {
BOOST_CHECK_EQUAL(it2_spent, target_feerate.GetFee(tx2_dimensions.vsize) - tx2_dimensions.mod_fee);
}
const TxDimensions& tx4_dimensions = tx_dims.find(tx4->GetHash())->second;
const CFeeRate tx4_feerate_unbumped = tx4_dimensions.feerate;
auto it4_spent = Find(bump_fees, COutPoint{tx4->GetHash(), 0});
if (target_feerate <= tx4_feerate_unbumped) {
BOOST_CHECK_EQUAL(it4_spent, 0);
} else {
BOOST_CHECK_EQUAL(it4_spent, target_feerate.GetFee(tx4_dimensions.vsize) - tx4_dimensions.mod_fee);
}
}
}
std::vector<node::MiniMinerMempoolEntry> miniminer_info;
{
const int32_t tx0_vsize{tx_dims.at(tx0->GetHash()).vsize};
const int32_t tx1_vsize{tx_dims.at(tx1->GetHash()).vsize};
const int32_t tx2_vsize{tx_dims.at(tx2->GetHash()).vsize};
const int32_t tx3_vsize{tx_dims.at(tx3->GetHash()).vsize};
const int32_t tx4_vsize{tx_dims.at(tx4->GetHash()).vsize};
const int32_t tx5_vsize{tx_dims.at(tx5->GetHash()).vsize};
const int32_t tx6_vsize{tx_dims.at(tx6->GetHash()).vsize};
const int32_t tx7_vsize{tx_dims.at(tx7->GetHash()).vsize};
miniminer_info.emplace_back(tx0,tx0_vsize,tx0_vsize,med_fee,med_fee);
miniminer_info.emplace_back(tx1, tx1_vsize, tx0_vsize + tx1_vsize, med_fee, 2*med_fee);
miniminer_info.emplace_back(tx2, tx2_vsize, tx2_vsize, low_fee, low_fee);
miniminer_info.emplace_back(tx3, tx3_vsize, tx2_vsize + tx3_vsize, high_fee, low_fee + high_fee);
miniminer_info.emplace_back(tx4, tx4_vsize, tx4_vsize, low_fee, low_fee);
miniminer_info.emplace_back(tx5, tx5_vsize, tx4_vsize + tx5_vsize, tx5_mod_fee, low_fee + tx5_mod_fee);
miniminer_info.emplace_back(tx6, tx6_vsize, tx6_vsize, high_fee, high_fee);
miniminer_info.emplace_back(tx7, tx7_vsize, tx6_vsize + tx7_vsize, low_fee, high_fee + low_fee);
}
std::map<Txid, std::set<Txid>> descendant_caches;
descendant_caches.emplace(tx0->GetHash(), std::set<Txid>{tx0->GetHash(), tx1->GetHash()});
descendant_caches.emplace(tx1->GetHash(), std::set<Txid>{tx1->GetHash()});
descendant_caches.emplace(tx2->GetHash(), std::set<Txid>{tx2->GetHash(), tx3->GetHash()});
descendant_caches.emplace(tx3->GetHash(), std::set<Txid>{tx3->GetHash()});
descendant_caches.emplace(tx4->GetHash(), std::set<Txid>{tx4->GetHash(), tx5->GetHash()});
descendant_caches.emplace(tx5->GetHash(), std::set<Txid>{tx5->GetHash()});
descendant_caches.emplace(tx6->GetHash(), std::set<Txid>{tx6->GetHash(), tx7->GetHash()});
descendant_caches.emplace(tx7->GetHash(), std::set<Txid>{tx7->GetHash()});
node::MiniMiner miniminer_manual(miniminer_info, descendant_caches);
node::MiniMiner miniminer_pool(pool, all_unspent_outpoints);
BOOST_CHECK(miniminer_manual.IsReadyToCalculate());
BOOST_CHECK(miniminer_pool.IsReadyToCalculate());
for (const auto& sequences : {miniminer_manual.Linearize(), miniminer_pool.Linearize()}) {
BOOST_CHECK_EQUAL(Find(sequences, tx6->GetHash()), 0);
BOOST_CHECK_EQUAL(Find(sequences, tx2->GetHash()), 1);
BOOST_CHECK_EQUAL(Find(sequences, tx3->GetHash()), 1);
BOOST_CHECK_EQUAL(Find(sequences, tx4->GetHash()), 2);
BOOST_CHECK_EQUAL(Find(sequences, tx5->GetHash()), 2);
BOOST_CHECK_EQUAL(Find(sequences, tx0->GetHash()), 3);
BOOST_CHECK_EQUAL(Find(sequences, tx1->GetHash()), 3);
BOOST_CHECK_EQUAL(Find(sequences, tx7->GetHash()), 4);
}
}
BOOST_FIXTURE_TEST_CASE(miniminer_overlap, TestChain100Setup)
{
CTxMemPool& pool = *Assert(m_node.mempool);
LOCK2(::cs_main, pool.cs);
TestMemPoolEntryHelper entry;
const auto tx0 = make_tx({COutPoint{m_coinbase_txns[0]->GetHash(), 0}}, 2);
AddToMempool(pool, entry.Fee(low_fee).FromTx(tx0));
const auto tx1 = make_tx({COutPoint{m_coinbase_txns[1]->GetHash(), 0}}, 2);
AddToMempool(pool, entry.Fee(med_fee).FromTx(tx1));
const auto tx2 = make_tx({COutPoint{m_coinbase_txns[2]->GetHash(), 0}}, 2);
AddToMempool(pool, entry.Fee(high_fee).FromTx(tx2));
const auto tx3 = make_tx({COutPoint{tx0->GetHash(), 0}, COutPoint{tx1->GetHash(), 0}, COutPoint{tx2->GetHash(), 0}}, 3);
AddToMempool(pool, entry.Fee(high_fee).FromTx(tx3));
const auto tx4 = make_tx({COutPoint{m_coinbase_txns[3]->GetHash(), 0}}, 2);
AddToMempool(pool, entry.Fee(high_fee).FromTx(tx4));
const auto tx5 = make_tx({COutPoint{tx4->GetHash(), 0}}, 3);
AddToMempool(pool, entry.Fee(low_fee).FromTx(tx5));
const auto tx6 = make_tx({COutPoint{tx5->GetHash(), 0}}, 2);
AddToMempool(pool, entry.Fee(med_fee).FromTx(tx6));
const auto tx7 = make_tx({COutPoint{tx5->GetHash(), 1}}, 2);
AddToMempool(pool, entry.Fee(high_fee).FromTx(tx7));
std::vector<CTransactionRef> all_transactions{tx0, tx1, tx2, tx3, tx4, tx5, tx6, tx7};
std::vector<int64_t> tx_vsizes;
tx_vsizes.reserve(all_transactions.size());
for (const auto& tx : all_transactions) tx_vsizes.push_back(GetVirtualTransactionSize(*tx));
std::vector<COutPoint> all_unspent_outpoints({
COutPoint{tx0->GetHash(), 1},
COutPoint{tx1->GetHash(), 1},
COutPoint{tx2->GetHash(), 1},
COutPoint{tx3->GetHash(), 0},
COutPoint{tx3->GetHash(), 1},
COutPoint{tx3->GetHash(), 2},
COutPoint{tx4->GetHash(), 1},
COutPoint{tx5->GetHash(), 2},
COutPoint{tx6->GetHash(), 0},
COutPoint{tx7->GetHash(), 0}
});
for (const auto& outpoint : all_unspent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint));
const auto tx2_feerate = CFeeRate(high_fee, tx_vsizes[2]);
const auto tx3_feerate = CFeeRate(high_fee, tx_vsizes[3]);
BOOST_CHECK(tx2_feerate > tx3_feerate);
const auto tx3_anc_feerate = CFeeRate(low_fee + med_fee + high_fee + high_fee, tx_vsizes[0] + tx_vsizes[1] + tx_vsizes[2] + tx_vsizes[3]);
const auto& tx3_entry{*Assert(pool.GetEntry(tx3->GetHash()))};
BOOST_CHECK(tx3_anc_feerate == CFeeRate(tx3_entry.GetModFeesWithAncestors(), tx3_entry.GetSizeWithAncestors()));
const auto tx4_feerate = CFeeRate(high_fee, tx_vsizes[4]);
const auto tx6_anc_feerate = CFeeRate(high_fee + low_fee + med_fee, tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[6]);
const auto& tx6_entry{*Assert(pool.GetEntry(tx6->GetHash()))};
BOOST_CHECK(tx6_anc_feerate == CFeeRate(tx6_entry.GetModFeesWithAncestors(), tx6_entry.GetSizeWithAncestors()));
const auto tx7_anc_feerate = CFeeRate(high_fee + low_fee + high_fee, tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[7]);
const auto& tx7_entry{*Assert(pool.GetEntry(tx7->GetHash()))};
BOOST_CHECK(tx7_anc_feerate == CFeeRate(tx7_entry.GetModFeesWithAncestors(), tx7_entry.GetSizeWithAncestors()));
BOOST_CHECK(tx4_feerate > tx6_anc_feerate);
BOOST_CHECK(tx4_feerate > tx7_anc_feerate);
{
node::MiniMiner mini_miner(pool, all_unspent_outpoints);
const CFeeRate very_high_feerate(COIN);
BOOST_CHECK(tx3_anc_feerate < very_high_feerate);
BOOST_CHECK(mini_miner.IsReadyToCalculate());
auto bump_fees = mini_miner.CalculateBumpFees(very_high_feerate);
BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size());
BOOST_CHECK(!mini_miner.IsReadyToCalculate());
BOOST_CHECK(sanity_check(all_transactions, bump_fees));
const auto tx0_bumpfee = bump_fees.find(COutPoint{tx0->GetHash(), 1});
BOOST_CHECK(tx0_bumpfee != bump_fees.end());
BOOST_CHECK_EQUAL(tx0_bumpfee->second, very_high_feerate.GetFee(tx_vsizes[0]) - low_fee);
const auto tx3_bumpfee = bump_fees.find(COutPoint{tx3->GetHash(), 0});
BOOST_CHECK(tx3_bumpfee != bump_fees.end());
BOOST_CHECK_EQUAL(tx3_bumpfee->second,
very_high_feerate.GetFee(tx_vsizes[0] + tx_vsizes[1] + tx_vsizes[2] + tx_vsizes[3]) - (low_fee + med_fee + high_fee + high_fee));
const auto tx6_bumpfee = bump_fees.find(COutPoint{tx6->GetHash(), 0});
BOOST_CHECK(tx6_bumpfee != bump_fees.end());
BOOST_CHECK_EQUAL(tx6_bumpfee->second,
very_high_feerate.GetFee(tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[6]) - (high_fee + low_fee + med_fee));
const auto tx7_bumpfee = bump_fees.find(COutPoint{tx7->GetHash(), 0});
BOOST_CHECK(tx7_bumpfee != bump_fees.end());
BOOST_CHECK_EQUAL(tx7_bumpfee->second,
very_high_feerate.GetFee(tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[7]) - (high_fee + low_fee + high_fee));
node::MiniMiner mini_miner_total_tx3(pool, {COutPoint{tx3->GetHash(), 0}, COutPoint{tx3->GetHash(), 1}});
BOOST_CHECK(mini_miner_total_tx3.IsReadyToCalculate());
const auto tx3_bump_fee = mini_miner_total_tx3.CalculateTotalBumpFees(very_high_feerate);
BOOST_CHECK(!mini_miner_total_tx3.IsReadyToCalculate());
BOOST_CHECK(tx3_bump_fee.has_value());
BOOST_CHECK_EQUAL(tx3_bump_fee.value(),
very_high_feerate.GetFee(tx_vsizes[0] + tx_vsizes[1] + tx_vsizes[2] + tx_vsizes[3]) - (low_fee + med_fee + high_fee + high_fee));
node::MiniMiner mini_miner_tx6_tx7(pool, {COutPoint{tx6->GetHash(), 0}, COutPoint{tx7->GetHash(), 0}});
BOOST_CHECK(mini_miner_tx6_tx7.IsReadyToCalculate());
const auto tx6_tx7_bumpfee = mini_miner_tx6_tx7.CalculateTotalBumpFees(very_high_feerate);
BOOST_CHECK(!mini_miner_tx6_tx7.IsReadyToCalculate());
BOOST_CHECK(tx6_tx7_bumpfee.has_value());
BOOST_CHECK_EQUAL(tx6_tx7_bumpfee.value(),
very_high_feerate.GetFee(tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[6] + tx_vsizes[7]) - (high_fee + low_fee + med_fee + high_fee));
}
{
const auto just_below_tx4 = CFeeRate(tx4_feerate.GetFeePerK() - 5);
node::MiniMiner mini_miner(pool, all_unspent_outpoints);
BOOST_CHECK(mini_miner.IsReadyToCalculate());
auto bump_fees = mini_miner.CalculateBumpFees(just_below_tx4);
BOOST_CHECK(!mini_miner.IsReadyToCalculate());
BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size());
BOOST_CHECK(sanity_check(all_transactions, bump_fees));
const auto tx6_bumpfee = bump_fees.find(COutPoint{tx6->GetHash(), 0});
BOOST_CHECK(tx6_bumpfee != bump_fees.end());
BOOST_CHECK_EQUAL(tx6_bumpfee->second, just_below_tx4.GetFee(tx_vsizes[5] + tx_vsizes[6]) - (low_fee + med_fee));
const auto tx7_bumpfee = bump_fees.find(COutPoint{tx7->GetHash(), 0});
BOOST_CHECK(tx7_bumpfee != bump_fees.end());
BOOST_CHECK_EQUAL(tx7_bumpfee->second, just_below_tx4.GetFee(tx_vsizes[5] + tx_vsizes[7]) - (low_fee + high_fee));
node::MiniMiner mini_miner_tx6_tx7(pool, {COutPoint{tx6->GetHash(), 0}, COutPoint{tx7->GetHash(), 0}});
BOOST_CHECK(mini_miner_tx6_tx7.IsReadyToCalculate());
const auto tx6_tx7_bumpfee = mini_miner_tx6_tx7.CalculateTotalBumpFees(just_below_tx4);
BOOST_CHECK(!mini_miner_tx6_tx7.IsReadyToCalculate());
BOOST_CHECK(tx6_tx7_bumpfee.has_value());
BOOST_CHECK_EQUAL(tx6_tx7_bumpfee.value(), just_below_tx4.GetFee(tx_vsizes[5] + tx_vsizes[6]) - (low_fee + med_fee));
}
{
const auto just_above_tx6 = CFeeRate(med_fee + 10, tx_vsizes[6]);
BOOST_CHECK(just_above_tx6 <= CFeeRate(low_fee + high_fee, tx_vsizes[5] + tx_vsizes[7]));
node::MiniMiner mini_miner(pool, all_unspent_outpoints);
BOOST_CHECK(mini_miner.IsReadyToCalculate());
auto bump_fees = mini_miner.CalculateBumpFees(just_above_tx6);
BOOST_CHECK(!mini_miner.IsReadyToCalculate());
BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size());
BOOST_CHECK(sanity_check(all_transactions, bump_fees));
const auto tx6_bumpfee = bump_fees.find(COutPoint{tx6->GetHash(), 0});
BOOST_CHECK(tx6_bumpfee != bump_fees.end());
BOOST_CHECK_EQUAL(tx6_bumpfee->second, just_above_tx6.GetFee(tx_vsizes[6]) - (med_fee));
const auto tx7_bumpfee = bump_fees.find(COutPoint{tx7->GetHash(), 0});
BOOST_CHECK(tx7_bumpfee != bump_fees.end());
BOOST_CHECK_EQUAL(tx7_bumpfee->second, 0);
}
std::vector<node::MiniMinerMempoolEntry> miniminer_info;
miniminer_info.emplace_back(tx0,tx_vsizes[0], tx_vsizes[0], low_fee, low_fee);
miniminer_info.emplace_back(tx1, tx_vsizes[1], tx_vsizes[1], med_fee, med_fee);
miniminer_info.emplace_back(tx2, tx_vsizes[2], tx_vsizes[2], high_fee, high_fee);
miniminer_info.emplace_back(tx3, tx_vsizes[3], tx_vsizes[0]+tx_vsizes[1]+tx_vsizes[2]+tx_vsizes[3], high_fee, low_fee+med_fee+2*high_fee);
miniminer_info.emplace_back(tx4, tx_vsizes[4], tx_vsizes[4], high_fee, high_fee);
miniminer_info.emplace_back(tx5, tx_vsizes[5], tx_vsizes[4]+tx_vsizes[5], low_fee, low_fee + high_fee);
miniminer_info.emplace_back(tx6, tx_vsizes[6], tx_vsizes[4]+tx_vsizes[5]+tx_vsizes[6], med_fee, high_fee+low_fee+med_fee);
miniminer_info.emplace_back(tx7, tx_vsizes[7], tx_vsizes[4]+tx_vsizes[5]+tx_vsizes[7], high_fee, high_fee+low_fee+high_fee);
std::map<Txid, std::set<Txid>> descendant_caches;
descendant_caches.emplace(tx0->GetHash(), std::set<Txid>{tx0->GetHash(), tx3->GetHash()});
descendant_caches.emplace(tx1->GetHash(), std::set<Txid>{tx1->GetHash(), tx3->GetHash()});
descendant_caches.emplace(tx2->GetHash(), std::set<Txid>{tx2->GetHash(), tx3->GetHash()});
descendant_caches.emplace(tx3->GetHash(), std::set<Txid>{tx3->GetHash()});
descendant_caches.emplace(tx4->GetHash(), std::set<Txid>{tx4->GetHash(), tx5->GetHash(), tx6->GetHash(), tx7->GetHash()});
descendant_caches.emplace(tx5->GetHash(), std::set<Txid>{tx5->GetHash(), tx6->GetHash(), tx7->GetHash()});
descendant_caches.emplace(tx6->GetHash(), std::set<Txid>{tx6->GetHash()});
descendant_caches.emplace(tx7->GetHash(), std::set<Txid>{tx7->GetHash()});
node::MiniMiner miniminer_manual(miniminer_info, descendant_caches);
node::MiniMiner miniminer_pool(pool, all_unspent_outpoints);
BOOST_CHECK(miniminer_manual.IsReadyToCalculate());
BOOST_CHECK(miniminer_pool.IsReadyToCalculate());
for (const auto& sequences : {miniminer_manual.Linearize(), miniminer_pool.Linearize()}) {
BOOST_CHECK_EQUAL(Find(sequences, tx4->GetHash()), 0);
BOOST_CHECK_EQUAL(Find(sequences, tx2->GetHash()), 1);
BOOST_CHECK_EQUAL(Find(sequences, tx5->GetHash()), 2);
BOOST_CHECK_EQUAL(Find(sequences, tx7->GetHash()), 2);
BOOST_CHECK_EQUAL(Find(sequences, tx0->GetHash()), 3);
BOOST_CHECK_EQUAL(Find(sequences, tx1->GetHash()), 3);
BOOST_CHECK_EQUAL(Find(sequences, tx3->GetHash()), 3);
BOOST_CHECK_EQUAL(Find(sequences, tx6->GetHash()), 4);
}
}
BOOST_FIXTURE_TEST_CASE(calculate_cluster, TestChain100Setup)
{
CTxMemPool& pool = *Assert(m_node.mempool);
LOCK2(cs_main, pool.cs);
auto convert_to_uint256_vec = [](const std::vector<Txid>& vec) -> std::vector<uint256> {
std::vector<uint256> out;
std::transform(vec.begin(), vec.end(), std::back_inserter(out),
[](const Txid& txid) { return txid.ToUint256(); });
return out;
};
TestMemPoolEntryHelper entry;
std::vector<Txid> chain_txids;
auto& lasttx = m_coinbase_txns[0];
for (auto i{0}; i < 500; ++i) {
const auto tx = make_tx({COutPoint{lasttx->GetHash(), 0}}, 1);
AddToMempool(pool, entry.Fee(CENT).FromTx(tx));
chain_txids.push_back(tx->GetHash());
lasttx = tx;
}
const auto cluster_500tx = pool.GatherClusters({lasttx->GetHash()});
CTxMemPool::setEntries cluster_500tx_set{cluster_500tx.begin(), cluster_500tx.end()};
BOOST_CHECK_EQUAL(cluster_500tx.size(), cluster_500tx_set.size());
const auto vec_iters_500 = pool.GetIterVec(convert_to_uint256_vec(chain_txids));
for (const auto& iter : vec_iters_500) BOOST_CHECK(cluster_500tx_set.count(iter));
const auto tx_501 = make_tx({COutPoint{lasttx->GetHash(), 0}}, 1);
AddToMempool(pool, entry.Fee(CENT).FromTx(tx_501));
const auto cluster_501 = pool.GatherClusters({tx_501->GetHash()});
BOOST_CHECK_EQUAL(cluster_501.size(), 0);
std::vector<Txid> zigzag_txids;
for (auto p{0}; p < 50; ++p) {
const auto txp = make_tx({COutPoint{Txid::FromUint256(GetRandHash()), 0}}, 2);
AddToMempool(pool, entry.Fee(CENT).FromTx(txp));
zigzag_txids.push_back(txp->GetHash());
}
for (auto c{0}; c < 49; ++c) {
const auto txc = make_tx({COutPoint{zigzag_txids[c], 1}, COutPoint{zigzag_txids[c+1], 0}}, 1);
AddToMempool(pool, entry.Fee(CENT).FromTx(txc));
zigzag_txids.push_back(txc->GetHash());
}
const auto vec_iters_zigzag = pool.GetIterVec(convert_to_uint256_vec(zigzag_txids));
const std::vector<size_t> indices{0, 22, 72, zigzag_txids.size() - 1};
for (const auto index : indices) {
const auto cluster = pool.GatherClusters({zigzag_txids[index]});
BOOST_CHECK_EQUAL(cluster.size(), zigzag_txids.size());
CTxMemPool::setEntries clusterset{cluster.begin(), cluster.end()};
BOOST_CHECK_EQUAL(cluster.size(), clusterset.size());
for (const auto& iter : vec_iters_zigzag) BOOST_CHECK(clusterset.count(iter));
}
}
BOOST_FIXTURE_TEST_CASE(manual_ctor, TestChain100Setup)
{
CTxMemPool& pool = *Assert(m_node.mempool);
LOCK2(cs_main, pool.cs);
{
auto grandparent_zero_fee = make_tx({{m_coinbase_txns.at(0)->GetHash(), 0}}, 1);
auto parent_high_feerate = make_tx({{grandparent_zero_fee->GetHash(), 0}}, 1);
auto grandparent_double_low_feerate = make_tx({{m_coinbase_txns.at(2)->GetHash(), 0}}, 1);
auto parent_med_feerate = make_tx({{grandparent_double_low_feerate->GetHash(), 0}}, 1);
auto grandparent_low_feerate = make_tx({{m_coinbase_txns.at(1)->GetHash(), 0}}, 1);
auto parent_double_low_feerate = make_tx({{grandparent_low_feerate->GetHash(), 0}}, 1);
auto child = make_tx({{parent_high_feerate->GetHash(), 0}, {parent_double_low_feerate->GetHash(), 0}, {parent_med_feerate->GetHash(), 0}}, 1);
const int64_t tx_vsize{100};
const int64_t child_vsize{1000};
std::vector<node::MiniMinerMempoolEntry> miniminer_info;
miniminer_info.emplace_back(grandparent_zero_fee, tx_vsize,tx_vsize, 0,0);
miniminer_info.emplace_back(parent_high_feerate, tx_vsize, 2*tx_vsize, high_fee, high_fee);
miniminer_info.emplace_back(grandparent_double_low_feerate, tx_vsize, tx_vsize, 2*low_fee, 2*low_fee);
miniminer_info.emplace_back(parent_med_feerate, tx_vsize, 2*tx_vsize, med_fee, 2*low_fee+med_fee);
miniminer_info.emplace_back(grandparent_low_feerate, tx_vsize, tx_vsize, low_fee, low_fee);
miniminer_info.emplace_back(parent_double_low_feerate, tx_vsize, 2*tx_vsize, 2*low_fee, 3*low_fee);
miniminer_info.emplace_back(child, child_vsize, 6*tx_vsize+child_vsize, low_fee, high_fee+med_fee+6*low_fee);
std::map<Txid, std::set<Txid>> descendant_caches;
descendant_caches.emplace(grandparent_zero_fee->GetHash(), std::set<Txid>{grandparent_zero_fee->GetHash(), parent_high_feerate->GetHash(), child->GetHash()});
descendant_caches.emplace(grandparent_low_feerate->GetHash(), std::set<Txid>{grandparent_low_feerate->GetHash(), parent_double_low_feerate->GetHash(), child->GetHash()});
descendant_caches.emplace(grandparent_double_low_feerate->GetHash(), std::set<Txid>{grandparent_double_low_feerate->GetHash(), parent_med_feerate->GetHash(), child->GetHash()});
descendant_caches.emplace(parent_high_feerate->GetHash(), std::set<Txid>{parent_high_feerate->GetHash(), child->GetHash()});
descendant_caches.emplace(parent_med_feerate->GetHash(), std::set<Txid>{parent_med_feerate->GetHash(), child->GetHash()});
descendant_caches.emplace(parent_double_low_feerate->GetHash(), std::set<Txid>{parent_double_low_feerate->GetHash(), child->GetHash()});
descendant_caches.emplace(child->GetHash(), std::set<Txid>{child->GetHash()});
node::MiniMiner miniminer_manual(miniminer_info, descendant_caches);
BOOST_CHECK(miniminer_manual.IsReadyToCalculate());
const auto sequences{miniminer_manual.Linearize()};
BOOST_CHECK_EQUAL(sequences.at(grandparent_zero_fee->GetHash()), 0);
BOOST_CHECK_EQUAL(sequences.at(parent_high_feerate->GetHash()), 0);
BOOST_CHECK_EQUAL(sequences.at(grandparent_double_low_feerate->GetHash()), 1);
BOOST_CHECK_EQUAL(sequences.at(parent_med_feerate->GetHash()), 1);
BOOST_CHECK_EQUAL(sequences.at(grandparent_low_feerate->GetHash()), 2);
BOOST_CHECK_EQUAL(sequences.at(parent_double_low_feerate->GetHash()), 2);
BOOST_CHECK_EQUAL(sequences.at(child->GetHash()), 3);
}
}
BOOST_AUTO_TEST_SUITE_END()