mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-10 20:03:34 -03:00
move only: move implementation code to disconnected_transactions.cpp
This commit is contained in:
parent
81dfeddea7
commit
29eb219c12
3 changed files with 100 additions and 69 deletions
|
@ -403,6 +403,7 @@ libbitcoin_node_a_SOURCES = \
|
|||
kernel/coinstats.cpp \
|
||||
kernel/context.cpp \
|
||||
kernel/cs_main.cpp \
|
||||
kernel/disconnected_transactions.cpp \
|
||||
kernel/mempool_persist.cpp \
|
||||
kernel/mempool_removal_reason.cpp \
|
||||
mapport.cpp \
|
||||
|
@ -943,6 +944,7 @@ libbitcoinkernel_la_SOURCES = \
|
|||
kernel/coinstats.cpp \
|
||||
kernel/context.cpp \
|
||||
kernel/cs_main.cpp \
|
||||
kernel/disconnected_transactions.cpp \
|
||||
kernel/mempool_persist.cpp \
|
||||
kernel/mempool_removal_reason.cpp \
|
||||
key.cpp \
|
||||
|
|
89
src/kernel/disconnected_transactions.cpp
Normal file
89
src/kernel/disconnected_transactions.cpp
Normal file
|
@ -0,0 +1,89 @@
|
|||
// Copyright (c) 2023 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <kernel/disconnected_transactions.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <core_memusage.h>
|
||||
#include <memusage.h>
|
||||
#include <primitives/transaction.h>
|
||||
#include <util/hasher.h>
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
// It's almost certainly a logic bug if we don't clear out queuedTx before
|
||||
// destruction, as we add to it while disconnecting blocks, and then we
|
||||
// need to re-process remaining transactions to ensure mempool consistency.
|
||||
// For now, assert() that we've emptied out this object on destruction.
|
||||
// This assert() can always be removed if the reorg-processing code were
|
||||
// to be refactored such that this assumption is no longer true (for
|
||||
// instance if there was some other way we cleaned up the mempool after a
|
||||
// reorg, besides draining this object).
|
||||
DisconnectedBlockTransactions::~DisconnectedBlockTransactions()
|
||||
{
|
||||
assert(queuedTx.empty());
|
||||
assert(iters_by_txid.empty());
|
||||
assert(cachedInnerUsage == 0);
|
||||
}
|
||||
|
||||
std::vector<CTransactionRef> DisconnectedBlockTransactions::LimitMemoryUsage()
|
||||
{
|
||||
std::vector<CTransactionRef> evicted;
|
||||
|
||||
while (!queuedTx.empty() && DynamicMemoryUsage() > m_max_mem_usage) {
|
||||
evicted.emplace_back(queuedTx.front());
|
||||
cachedInnerUsage -= RecursiveDynamicUsage(*queuedTx.front());
|
||||
iters_by_txid.erase(queuedTx.front()->GetHash());
|
||||
queuedTx.pop_front();
|
||||
}
|
||||
return evicted;
|
||||
}
|
||||
|
||||
size_t DisconnectedBlockTransactions::DynamicMemoryUsage() const
|
||||
{
|
||||
return cachedInnerUsage + memusage::DynamicUsage(iters_by_txid) + memusage::DynamicUsage(queuedTx);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::vector<CTransactionRef> DisconnectedBlockTransactions::AddTransactionsFromBlock(const std::vector<CTransactionRef>& vtx)
|
||||
{
|
||||
iters_by_txid.reserve(iters_by_txid.size() + vtx.size());
|
||||
for (auto block_it = vtx.rbegin(); block_it != vtx.rend(); ++block_it) {
|
||||
auto it = queuedTx.insert(queuedTx.end(), *block_it);
|
||||
iters_by_txid.emplace((*block_it)->GetHash(), it);
|
||||
cachedInnerUsage += RecursiveDynamicUsage(**block_it);
|
||||
}
|
||||
return LimitMemoryUsage();
|
||||
}
|
||||
|
||||
void DisconnectedBlockTransactions::removeForBlock(const std::vector<CTransactionRef>& vtx)
|
||||
{
|
||||
// Short-circuit in the common case of a block being added to the tip
|
||||
if (queuedTx.empty()) {
|
||||
return;
|
||||
}
|
||||
for (const auto& tx : vtx) {
|
||||
auto iter = iters_by_txid.find(tx->GetHash());
|
||||
if (iter != iters_by_txid.end()) {
|
||||
auto list_iter = iter->second;
|
||||
iters_by_txid.erase(iter);
|
||||
cachedInnerUsage -= RecursiveDynamicUsage(**list_iter);
|
||||
queuedTx.erase(list_iter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DisconnectedBlockTransactions::clear()
|
||||
{
|
||||
cachedInnerUsage = 0;
|
||||
iters_by_txid.clear();
|
||||
queuedTx.clear();
|
||||
}
|
||||
|
||||
std::list<CTransactionRef> DisconnectedBlockTransactions::take()
|
||||
{
|
||||
std::list<CTransactionRef> ret = std::move(queuedTx);
|
||||
clear();
|
||||
return ret;
|
||||
}
|
|
@ -5,8 +5,6 @@
|
|||
#ifndef BITCOIN_KERNEL_DISCONNECTED_TRANSACTIONS_H
|
||||
#define BITCOIN_KERNEL_DISCONNECTED_TRANSACTIONS_H
|
||||
|
||||
#include <core_memusage.h>
|
||||
#include <memusage.h>
|
||||
#include <primitives/transaction.h>
|
||||
#include <util/hasher.h>
|
||||
|
||||
|
@ -47,39 +45,15 @@ private:
|
|||
std::unordered_map<uint256, TxList::iterator, SaltedTxidHasher> iters_by_txid;
|
||||
|
||||
/** Trim the earliest-added entries until we are within memory bounds. */
|
||||
std::vector<CTransactionRef> LimitMemoryUsage()
|
||||
{
|
||||
std::vector<CTransactionRef> evicted;
|
||||
|
||||
while (!queuedTx.empty() && DynamicMemoryUsage() > m_max_mem_usage) {
|
||||
evicted.emplace_back(queuedTx.front());
|
||||
cachedInnerUsage -= RecursiveDynamicUsage(*queuedTx.front());
|
||||
iters_by_txid.erase(queuedTx.front()->GetHash());
|
||||
queuedTx.pop_front();
|
||||
}
|
||||
return evicted;
|
||||
}
|
||||
std::vector<CTransactionRef> LimitMemoryUsage();
|
||||
|
||||
public:
|
||||
DisconnectedBlockTransactions(size_t max_mem_usage) : m_max_mem_usage{max_mem_usage} {}
|
||||
DisconnectedBlockTransactions(size_t max_mem_usage)
|
||||
: m_max_mem_usage{max_mem_usage} {}
|
||||
|
||||
// It's almost certainly a logic bug if we don't clear out queuedTx before
|
||||
// destruction, as we add to it while disconnecting blocks, and then we
|
||||
// need to re-process remaining transactions to ensure mempool consistency.
|
||||
// For now, assert() that we've emptied out this object on destruction.
|
||||
// This assert() can always be removed if the reorg-processing code were
|
||||
// to be refactored such that this assumption is no longer true (for
|
||||
// instance if there was some other way we cleaned up the mempool after a
|
||||
// reorg, besides draining this object).
|
||||
~DisconnectedBlockTransactions() {
|
||||
assert(queuedTx.empty());
|
||||
assert(iters_by_txid.empty());
|
||||
assert(cachedInnerUsage == 0);
|
||||
}
|
||||
~DisconnectedBlockTransactions();
|
||||
|
||||
size_t DynamicMemoryUsage() const {
|
||||
return cachedInnerUsage + memusage::DynamicUsage(iters_by_txid) + memusage::DynamicUsage(queuedTx);
|
||||
}
|
||||
size_t DynamicMemoryUsage() const;
|
||||
|
||||
/** Add transactions from the block, iterating through vtx in reverse order. Callers should call
|
||||
* this function for blocks in descending order by block height.
|
||||
|
@ -88,50 +62,16 @@ public:
|
|||
* corresponding entry in iters_by_txid.
|
||||
* @returns vector of transactions that were evicted for size-limiting.
|
||||
*/
|
||||
[[nodiscard]] std::vector<CTransactionRef> AddTransactionsFromBlock(const std::vector<CTransactionRef>& vtx)
|
||||
{
|
||||
iters_by_txid.reserve(iters_by_txid.size() + vtx.size());
|
||||
for (auto block_it = vtx.rbegin(); block_it != vtx.rend(); ++block_it) {
|
||||
auto it = queuedTx.insert(queuedTx.end(), *block_it);
|
||||
iters_by_txid.emplace((*block_it)->GetHash(), it);
|
||||
cachedInnerUsage += RecursiveDynamicUsage(**block_it);
|
||||
}
|
||||
return LimitMemoryUsage();
|
||||
}
|
||||
[[nodiscard]] std::vector<CTransactionRef> AddTransactionsFromBlock(const std::vector<CTransactionRef>& vtx);
|
||||
|
||||
/** Remove any entries that are in this block. */
|
||||
void removeForBlock(const std::vector<CTransactionRef>& vtx)
|
||||
{
|
||||
// Short-circuit in the common case of a block being added to the tip
|
||||
if (queuedTx.empty()) {
|
||||
return;
|
||||
}
|
||||
for (const auto& tx : vtx) {
|
||||
auto iter = iters_by_txid.find(tx->GetHash());
|
||||
if (iter != iters_by_txid.end()) {
|
||||
auto list_iter = iter->second;
|
||||
iters_by_txid.erase(iter);
|
||||
cachedInnerUsage -= RecursiveDynamicUsage(**list_iter);
|
||||
queuedTx.erase(list_iter);
|
||||
}
|
||||
}
|
||||
}
|
||||
void removeForBlock(const std::vector<CTransactionRef>& vtx);
|
||||
|
||||
size_t size() const { return queuedTx.size(); }
|
||||
|
||||
void clear()
|
||||
{
|
||||
cachedInnerUsage = 0;
|
||||
iters_by_txid.clear();
|
||||
queuedTx.clear();
|
||||
}
|
||||
void clear();
|
||||
|
||||
/** Clear all data structures and return the list of transactions. */
|
||||
std::list<CTransactionRef> take()
|
||||
{
|
||||
std::list<CTransactionRef> ret = std::move(queuedTx);
|
||||
clear();
|
||||
return ret;
|
||||
}
|
||||
std::list<CTransactionRef> take();
|
||||
};
|
||||
#endif // BITCOIN_KERNEL_DISCONNECTED_TRANSACTIONS_H
|
||||
|
|
Loading…
Reference in a new issue