Merge #19556: Remove mempool global

fafb381af8279b2d2ca768df0bf68d7eb036a2f9 Remove mempool global (MarcoFalke)
fa0359c5b30730744aa8a7cd9ffab79ded91041f Remove mempool global from p2p (MarcoFalke)
eeee1104d78eb59a582ee1709ff4ac2c33ee1190 Remove mempool global from init (MarcoFalke)

Pull request description:

  This refactor unlocks some nice potential features, such as, but not limited to:
  * Removing the fee estimates global (would avoid slightly fragile workarounds such as #18766)
  * Making the mempool optional for a "blocksonly" operation mode

  Even absent those features, the new code without the global should be easier to maintain, read and write tests for.

ACKs for top commit:
  jnewbery:
    utACK fafb381af8279b2d2ca768df0bf68d7eb036a2f9
  hebasto:
    ACK fafb381af8279b2d2ca768df0bf68d7eb036a2f9, I have reviewed the code and it looks OK, I agree it can be merged.
  darosior:
    ACK fafb381af8279b2d2ca768df0bf68d7eb036a2f9

Tree-SHA512: a2e696dc377e2e81eaf9c389e6d13dde4a48d81f3538df88f4da502d3012dd61078495140ab5a5854f360a06249fe0e1f6a094c4e006d8b5cc2552a946becf26
This commit is contained in:
MarcoFalke 2020-09-07 09:47:14 +02:00
commit 07087051af
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548
9 changed files with 38 additions and 28 deletions

View File

@ -187,7 +187,7 @@ void Shutdown(NodeContext& node)
/// Be sure that anything that writes files or flushes caches only does this if the respective /// Be sure that anything that writes files or flushes caches only does this if the respective
/// module was initialized. /// module was initialized.
util::ThreadRename("shutoff"); util::ThreadRename("shutoff");
mempool.AddTransactionsUpdated(1); if (node.mempool) node.mempool->AddTransactionsUpdated(1);
StopHTTPRPC(); StopHTTPRPC();
StopREST(); StopREST();
@ -231,8 +231,8 @@ void Shutdown(NodeContext& node)
node.connman.reset(); node.connman.reset();
node.banman.reset(); node.banman.reset();
if (::mempool.IsLoaded() && node.args->GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) { if (node.mempool && node.mempool->IsLoaded() && node.args->GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
DumpMempool(::mempool); DumpMempool(*node.mempool);
} }
if (fFeeEstimatesInitialized) if (fFeeEstimatesInitialized)
@ -302,7 +302,7 @@ void Shutdown(NodeContext& node)
GetMainSignals().UnregisterBackgroundSignalScheduler(); GetMainSignals().UnregisterBackgroundSignalScheduler();
globalVerifyHandle.reset(); globalVerifyHandle.reset();
ECC_Stop(); ECC_Stop();
node.mempool = nullptr; node.mempool.reset();
node.chainman = nullptr; node.chainman = nullptr;
node.scheduler.reset(); node.scheduler.reset();
@ -738,10 +738,7 @@ static void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImp
return; return;
} }
} // End scope of CImportingNow } // End scope of CImportingNow
if (args.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) { chainman.ActiveChainstate().LoadMempool(args);
LoadMempool(::mempool);
}
::mempool.SetIsLoaded(!ShutdownRequested());
} }
/** Sanity checks /** Sanity checks
@ -1054,11 +1051,6 @@ bool AppInitParameterInteraction(const ArgsManager& args)
} }
} }
// Checkmempool and checkblockindex default to true in regtest mode
int ratio = std::min<int>(std::max<int>(args.GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
if (ratio != 0) {
mempool.setSanityCheck(1.0 / ratio);
}
fCheckBlockIndex = args.GetBoolArg("-checkblockindex", chainparams.DefaultConsistencyChecks()); fCheckBlockIndex = args.GetBoolArg("-checkblockindex", chainparams.DefaultConsistencyChecks());
fCheckpointsEnabled = args.GetBoolArg("-checkpoints", DEFAULT_CHECKPOINTS_ENABLED); fCheckpointsEnabled = args.GetBoolArg("-checkpoints", DEFAULT_CHECKPOINTS_ENABLED);
@ -1368,10 +1360,18 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface, args.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME)); node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface, args.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
assert(!node.connman); assert(!node.connman);
node.connman = MakeUnique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()), args.GetBoolArg("-networkactive", true)); node.connman = MakeUnique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()), args.GetBoolArg("-networkactive", true));
// Make mempool generally available in the node context. For example the connection manager, wallet, or RPC threads, // Make mempool generally available in the node context. For example the connection manager, wallet, or RPC threads,
// which are all started after this, may use it from the node context. // which are all started after this, may use it from the node context.
assert(!node.mempool); assert(!node.mempool);
node.mempool = &::mempool; node.mempool = MakeUnique<CTxMemPool>(&::feeEstimator);
if (node.mempool) {
int ratio = std::min<int>(std::max<int>(args.GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
if (ratio != 0) {
node.mempool->setSanityCheck(1.0 / ratio);
}
}
assert(!node.chainman); assert(!node.chainman);
node.chainman = &g_chainman; node.chainman = &g_chainman;
ChainstateManager& chainman = *Assert(node.chainman); ChainstateManager& chainman = *Assert(node.chainman);
@ -1559,7 +1559,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
chainman.m_total_coinstip_cache = nCoinCacheUsage; chainman.m_total_coinstip_cache = nCoinCacheUsage;
chainman.m_total_coinsdb_cache = nCoinDBCache; chainman.m_total_coinsdb_cache = nCoinDBCache;
UnloadBlockIndex(node.mempool); UnloadBlockIndex(node.mempool.get());
// new CBlockTreeDB tries to delete the existing file, which // new CBlockTreeDB tries to delete the existing file, which
// fails if it's still open from the previous loop. Close it first: // fails if it's still open from the previous loop. Close it first:

View File

@ -1710,7 +1710,7 @@ void static ProcessGetBlockData(CNode& pfrom, const CChainParams& chainparams, c
} }
//! Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed). //! Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed).
CTransactionRef static FindTxForGetData(const CNode& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now) LOCKS_EXCLUDED(cs_main) static CTransactionRef FindTxForGetData(const CTxMemPool& mempool, const CNode& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now) LOCKS_EXCLUDED(cs_main)
{ {
auto txinfo = mempool.info(gtxid); auto txinfo = mempool.info(gtxid);
if (txinfo.tx) { if (txinfo.tx) {
@ -1766,7 +1766,7 @@ void static ProcessGetData(CNode& pfrom, const CChainParams& chainparams, CConnm
continue; continue;
} }
CTransactionRef tx = FindTxForGetData(pfrom, ToGenTxid(inv), mempool_req, now); CTransactionRef tx = FindTxForGetData(mempool, pfrom, ToGenTxid(inv), mempool_req, now);
if (tx) { if (tx) {
// WTX and WITNESS_TX imply we serialize with witness // WTX and WITNESS_TX imply we serialize with witness
int nSendFlags = (inv.IsMsgTx() ? SERIALIZE_TRANSACTION_NO_WITNESS : 0); int nSendFlags = (inv.IsMsgTx() ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
@ -2732,7 +2732,7 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
} }
} else if (inv.IsGenTxMsg()) { } else if (inv.IsGenTxMsg()) {
const GenTxid gtxid = ToGenTxid(inv); const GenTxid gtxid = ToGenTxid(inv);
const bool fAlreadyHave = AlreadyHaveTx(gtxid, mempool); const bool fAlreadyHave = AlreadyHaveTx(gtxid, m_mempool);
LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId()); LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
pfrom.AddKnownTx(inv.hash); pfrom.AddKnownTx(inv.hash);
@ -3003,7 +3003,7 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
std::list<CTransactionRef> lRemovedTxn; std::list<CTransactionRef> lRemovedTxn;
// We do the AlreadyHave() check using wtxid, rather than txid - in the // We do the AlreadyHaveTx() check using wtxid, rather than txid - in the
// absence of witness malleation, this is strictly better, because the // absence of witness malleation, this is strictly better, because the
// recent rejects filter may contain the wtxid but rarely contains // recent rejects filter may contain the wtxid but rarely contains
// the txid of a segwit transaction that has been rejected. // the txid of a segwit transaction that has been rejected.

View File

@ -9,6 +9,7 @@
#include <net.h> #include <net.h>
#include <net_processing.h> #include <net_processing.h>
#include <scheduler.h> #include <scheduler.h>
#include <txmempool.h>
NodeContext::NodeContext() {} NodeContext::NodeContext() {}
NodeContext::~NodeContext() {} NodeContext::~NodeContext() {}

View File

@ -35,7 +35,7 @@ class WalletClient;
//! be used without pulling in unwanted dependencies or functionality. //! be used without pulling in unwanted dependencies or functionality.
struct NodeContext { struct NodeContext {
std::unique_ptr<CConnman> connman; std::unique_ptr<CConnman> connman;
CTxMemPool* mempool{nullptr}; // Currently a raw pointer because the memory is not managed by this struct std::unique_ptr<CTxMemPool> mempool;
std::unique_ptr<PeerLogicValidation> peer_logic; std::unique_ptr<PeerLogicValidation> peer_logic;
ChainstateManager* chainman{nullptr}; // Currently a raw pointer because the memory is not managed by this struct ChainstateManager* chainman{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
std::unique_ptr<BanMan> banman; std::unique_ptr<BanMan> banman;

View File

@ -102,7 +102,7 @@ static CTxMemPool* GetMemPool(const util::Ref& context, HTTPRequest* req)
RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found"); RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found");
return nullptr; return nullptr;
} }
return node->mempool; return node->mempool.get();
} }
static RetFormat ParseDataFormat(std::string& param, const std::string& strReq) static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
@ -393,7 +393,7 @@ static bool rest_tx(const util::Ref& context, HTTPRequest* req, const std::strin
const NodeContext* const node = GetNodeContext(context, req); const NodeContext* const node = GetNodeContext(context, req);
if (!node) return false; if (!node) return false;
uint256 hashBlock = uint256(); uint256 hashBlock = uint256();
const CTransactionRef tx = GetTransaction(/* block_index */ nullptr, node->mempool, hash, Params().GetConsensus(), hashBlock); const CTransactionRef tx = GetTransaction(/* block_index */ nullptr, node->mempool.get(), hash, Params().GetConsensus(), hashBlock);
if (!tx) { if (!tx) {
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
} }

View File

@ -191,7 +191,7 @@ static UniValue getrawtransaction(const JSONRPCRequest& request)
} }
uint256 hash_block; uint256 hash_block;
const CTransactionRef tx = GetTransaction(blockindex, node.mempool, hash, Params().GetConsensus(), hash_block); const CTransactionRef tx = GetTransaction(blockindex, node.mempool.get(), hash, Params().GetConsensus(), hash_block);
if (!tx) { if (!tx) {
std::string errmsg; std::string errmsg;
if (blockindex) { if (blockindex) {

View File

@ -141,7 +141,7 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
pblocktree.reset(new CBlockTreeDB(1 << 20, true)); pblocktree.reset(new CBlockTreeDB(1 << 20, true));
m_node.mempool = &::mempool; m_node.mempool = MakeUnique<CTxMemPool>(&::feeEstimator);
m_node.mempool->setSanityCheck(1.0); m_node.mempool->setSanityCheck(1.0);
m_node.chainman = &::g_chainman; m_node.chainman = &::g_chainman;
@ -187,8 +187,8 @@ TestingSetup::~TestingSetup()
m_node.connman.reset(); m_node.connman.reset();
m_node.banman.reset(); m_node.banman.reset();
m_node.args = nullptr; m_node.args = nullptr;
UnloadBlockIndex(m_node.mempool); UnloadBlockIndex(m_node.mempool.get());
m_node.mempool = nullptr; m_node.mempool.reset();
m_node.scheduler.reset(); m_node.scheduler.reset();
m_node.chainman->Reset(); m_node.chainman->Reset();
m_node.chainman = nullptr; m_node.chainman = nullptr;

View File

@ -148,7 +148,6 @@ arith_uint256 nMinimumChainWork;
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE); CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
CBlockPolicyEstimator feeEstimator; CBlockPolicyEstimator feeEstimator;
CTxMemPool mempool(&feeEstimator);
// Internal stuff // Internal stuff
namespace { namespace {
@ -4227,6 +4226,14 @@ bool static LoadBlockIndexDB(ChainstateManager& chainman, const CChainParams& ch
return true; return true;
} }
void CChainState::LoadMempool(const ArgsManager& args)
{
if (args.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
::LoadMempool(m_mempool);
}
m_mempool.SetIsLoaded(!ShutdownRequested());
}
bool CChainState::LoadChainTip(const CChainParams& chainparams) bool CChainState::LoadChainTip(const CChainParams& chainparams)
{ {
AssertLockHeld(cs_main); AssertLockHeld(cs_main);

View File

@ -113,7 +113,6 @@ enum class SynchronizationState {
extern RecursiveMutex cs_main; extern RecursiveMutex cs_main;
extern CBlockPolicyEstimator feeEstimator; extern CBlockPolicyEstimator feeEstimator;
extern CTxMemPool mempool;
typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap; typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
extern Mutex g_best_block_mutex; extern Mutex g_best_block_mutex;
extern std::condition_variable g_best_block_cv; extern std::condition_variable g_best_block_cv;
@ -671,6 +670,9 @@ public:
*/ */
void CheckBlockIndex(const Consensus::Params& consensusParams); void CheckBlockIndex(const Consensus::Params& consensusParams);
/** Load the persisted mempool from disk */
void LoadMempool(const ArgsManager& args);
/** Update the chain tip based on database information, i.e. CoinsTip()'s best block. */ /** Update the chain tip based on database information, i.e. CoinsTip()'s best block. */
bool LoadChainTip(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool LoadChainTip(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);