Skip to content

Commit

Permalink
[fees]: add method that linearize transaction removed from mempool
Browse files Browse the repository at this point in the history
Co-authored-by: willcl-ark <will@256k1.dev>
  • Loading branch information
ismaelsadeeq and willcl-ark committed Jul 11, 2024
1 parent 2ee7eeb commit 778f545
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/policy/fees_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.


#include <node/mini_miner.h>
#include <policy/feerate.h>
#include <policy/fees_util.h>
#include <policy/policy.h>
#include <primitives/transaction.h>
Expand Down Expand Up @@ -65,3 +67,34 @@ TxAncestorsAndDescendants GetTxAncestorsAndDescendants(const std::vector<Removed
}
return visited_txs;
}

node::LinearizationResult LinearizeTransactions(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block)
{
// Cache all the transactions for efficient lookup
std::map<Txid, TransactionInfo> tx_caches;
for (const auto& tx : txs_removed_for_block) {
tx_caches.emplace(tx.info.m_tx->GetHash(), TransactionInfo(tx.info.m_tx, tx.info.m_fee, tx.info.m_virtual_transaction_size, tx.info.txHeight));
}

const auto& txAncestorsAndDescendants = GetTxAncestorsAndDescendants(txs_removed_for_block);
std::vector<node::MiniMinerMempoolEntry> transactions;
std::map<Txid, std::set<Txid>> descendant_caches;
transactions.reserve(txAncestorsAndDescendants.size());

for (const auto& transaction : txAncestorsAndDescendants) {
const auto& txid = transaction.first;
const auto& [ancestors, descendants] = transaction.second;
int64_t vsize_ancestor = 0;
CAmount fee_with_ancestors = 0;
for (auto& ancestor_id : ancestors) {
const auto& ancestor = tx_caches.find(ancestor_id)->second;
vsize_ancestor += ancestor.m_virtual_transaction_size;
fee_with_ancestors += ancestor.m_fee;
}

descendant_caches.emplace(txid, descendants);
auto tx_info = tx_caches.find(txid)->second;
transactions.emplace_back(tx_info.m_tx, tx_info.m_virtual_transaction_size, vsize_ancestor, tx_info.m_fee, fee_with_ancestors);
}
return node::MiniMiner(transactions, descendant_caches).Linearize();
}
4 changes: 4 additions & 0 deletions src/policy/fees_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#define BITCOIN_POLICY_FEES_UTIL_H

#include <kernel/mempool_entry.h>
#include <node/mini_miner.h>
#include <policy/feerate.h>
#include <primitives/transaction.h>

#include <map>
#include <set>
Expand Down Expand Up @@ -49,4 +51,6 @@ using TxAncestorsAndDescendants = std::map<Txid, std::tuple<std::set<Txid>, std:
*/
TxAncestorsAndDescendants GetTxAncestorsAndDescendants(const std::vector<RemovedMempoolTransactionInfo>& transactions);

node::LinearizationResult LinearizeTransactions(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block);

#endif // BITCOIN_POLICY_FEES_UTIL_H

0 comments on commit 778f545

Please sign in to comment.