From 5a83f55c96661a886dd6f5231920b2f730cf6773 Mon Sep 17 00:00:00 2001 From: glozow Date: Tue, 31 Oct 2023 09:29:59 +0000 Subject: [MiniMiner] allow manual construction with non-mempool txns This is primarily intended for linearizing a package of transactions prior to submitting them to mempool. Note that, if this ctor is used, bump fees will not be calculated because we haven't instructed MiniMiner which outpoints for which we want bump fees to be calculated. --- src/node/mini_miner.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ src/node/mini_miner.h | 17 +++++++++++++++++ 2 files changed, 59 insertions(+) (limited to 'src/node') diff --git a/src/node/mini_miner.cpp b/src/node/mini_miner.cpp index 95eac7a243..c7bb4a6a42 100644 --- a/src/node/mini_miner.cpp +++ b/src/node/mini_miner.cpp @@ -132,6 +132,48 @@ MiniMiner::MiniMiner(const CTxMemPool& mempool, const std::vector& ou SanityCheck(); } +MiniMiner::MiniMiner(const std::vector& manual_entries, + const std::map>& descendant_caches) +{ + for (const auto& entry : manual_entries) { + const auto& txid = entry.GetTx().GetHash(); + // We need to know the descendant set of every transaction. + if (!Assume(descendant_caches.count(txid) > 0)) { + m_ready_to_calculate = false; + return; + } + // Just forward these args onto MiniMinerMempoolEntry + auto [mapiter, success] = m_entries_by_txid.emplace(txid, entry); + // Txids must be unique; this txid shouldn't already be an entry in m_entries_by_txid + if (Assume(success)) m_entries.push_back(mapiter); + } + // Descendant cache is already built, but we need to translate them to m_entries_by_txid iters. + for (const auto& [txid, desc_txids] : descendant_caches) { + // Descendant cache should include at least the tx itself. + if (!Assume(!desc_txids.empty())) { + m_ready_to_calculate = false; + return; + } + std::vector cached_descendants; + for (const auto& desc_txid : desc_txids) { + auto desc_it{m_entries_by_txid.find(desc_txid)}; + // Descendants should only include transactions with corresponding entries. + if (!Assume(desc_it != m_entries_by_txid.end())) { + m_ready_to_calculate = false; + return; + } else { + cached_descendants.emplace_back(desc_it); + } + } + m_descendant_set_by_txid.emplace(txid, cached_descendants); + } + Assume(m_to_be_replaced.empty()); + Assume(m_requested_outpoints_by_txid.empty()); + Assume(m_bump_fees.empty()); + SanityCheck(); +} + + // Compare by min(ancestor feerate, individual feerate), then iterator // // Under the ancestor-based mining approach, high-feerate children can pay for parents, but high-feerate diff --git a/src/node/mini_miner.h b/src/node/mini_miner.h index ae26202965..f1713bc868 100644 --- a/src/node/mini_miner.h +++ b/src/node/mini_miner.h @@ -120,8 +120,25 @@ public: /** Returns set of txids in the block template if one has been constructed. */ std::set GetMockTemplateTxids() const { return m_in_block; } + /** Constructor that takes a list of outpoints that may or may not belong to transactions in the + * mempool. Copies out information about the relevant transactions in the mempool into + * MiniMinerMempoolEntrys. + */ MiniMiner(const CTxMemPool& mempool, const std::vector& outpoints); + /** Constructor in which the MiniMinerMempoolEntry entries have been constructed manually, + * presumably because these transactions are not in the mempool (yet). It is assumed that all + * entries are unique and their values are correct, otherwise results computed by MiniMiner may + * be incorrect. Callers should check IsReadyToCalculate() after construction. + * @param[in] descendant_caches A map from each transaction to the set of txids of this + * transaction's descendant set, including itself. Each tx in + * manual_entries must have a corresponding entry in this map, and + * all of the txids in a descendant set must correspond to a tx in + * manual_entries. + */ + MiniMiner(const std::vector& manual_entries, + const std::map>& descendant_caches); + /** Construct a new block template and, for each outpoint corresponding to a transaction that * did not make it into the block, calculate the cost of bumping those transactions (and their * ancestors) to the minimum feerate. Returns a map from outpoint to bump fee, or an empty map -- cgit v1.2.3