Skip to content

Commit

Permalink
Merge #19556: Remove mempool global
Browse files Browse the repository at this point in the history
fafb381 Remove mempool global (MarcoFalke)
fa0359c Remove mempool global from p2p (MarcoFalke)
eeee110 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 fafb381
  hebasto:
    ACK fafb381, I have reviewed the code and it looks OK, I agree it can be merged.
  darosior:
    ACK fafb381

Tree-SHA512: a2e696dc377e2e81eaf9c389e6d13dde4a48d81f3538df88f4da502d3012dd61078495140ab5a5854f360a06249fe0e1f6a094c4e006d8b5cc2552a946becf26
  • Loading branch information
MarcoFalke committed Sep 7, 2020
2 parents 78cb45d + fafb381 commit 0708705
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 28 deletions.
30 changes: 15 additions & 15 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void Shutdown(NodeContext& node)
/// Be sure that anything that writes files or flushes caches only does this if the respective
/// module was initialized.
util::ThreadRename("shutoff");
mempool.AddTransactionsUpdated(1);
if (node.mempool) node.mempool->AddTransactionsUpdated(1);

StopHTTPRPC();
StopREST();
Expand Down Expand Up @@ -231,8 +231,8 @@ void Shutdown(NodeContext& node)
node.connman.reset();
node.banman.reset();

if (::mempool.IsLoaded() && node.args->GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
DumpMempool(::mempool);
if (node.mempool && node.mempool->IsLoaded() && node.args->GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
DumpMempool(*node.mempool);
}

if (fFeeEstimatesInitialized)
Expand Down Expand Up @@ -302,7 +302,7 @@ void Shutdown(NodeContext& node)
GetMainSignals().UnregisterBackgroundSignalScheduler();
globalVerifyHandle.reset();
ECC_Stop();
node.mempool = nullptr;
node.mempool.reset();
node.chainman = nullptr;
node.scheduler.reset();

Expand Down Expand Up @@ -738,10 +738,7 @@ static void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImp
return;
}
} // End scope of CImportingNow
if (args.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
LoadMempool(::mempool);
}
::mempool.SetIsLoaded(!ShutdownRequested());
chainman.ActiveChainstate().LoadMempool(args);
}

/** Sanity checks
Expand Down Expand Up @@ -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());
fCheckpointsEnabled = args.GetBoolArg("-checkpoints", DEFAULT_CHECKPOINTS_ENABLED);

Expand Down Expand Up @@ -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));
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));

// 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.
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);
node.chainman = &g_chainman;
ChainstateManager& chainman = *Assert(node.chainman);
Expand Down Expand Up @@ -1559,7 +1559,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
chainman.m_total_coinstip_cache = nCoinCacheUsage;
chainman.m_total_coinsdb_cache = nCoinDBCache;

UnloadBlockIndex(node.mempool);
UnloadBlockIndex(node.mempool.get());

// new CBlockTreeDB tries to delete the existing file, which
// fails if it's still open from the previous loop. Close it first:
Expand Down
8 changes: 4 additions & 4 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
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);
if (txinfo.tx) {
Expand Down Expand Up @@ -1766,7 +1766,7 @@ void static ProcessGetData(CNode& pfrom, const CChainParams& chainparams, CConnm
continue;
}

CTransactionRef tx = FindTxForGetData(pfrom, ToGenTxid(inv), mempool_req, now);
CTransactionRef tx = FindTxForGetData(mempool, pfrom, ToGenTxid(inv), mempool_req, now);
if (tx) {
// WTX and WITNESS_TX imply we serialize with witness
int nSendFlags = (inv.IsMsgTx() ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
Expand Down Expand Up @@ -2732,7 +2732,7 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
}
} else if (inv.IsGenTxMsg()) {
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());

pfrom.AddKnownTx(inv.hash);
Expand Down Expand Up @@ -3003,7 +3003,7 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty

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
// recent rejects filter may contain the wtxid but rarely contains
// the txid of a segwit transaction that has been rejected.
Expand Down
1 change: 1 addition & 0 deletions src/node/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <net.h>
#include <net_processing.h>
#include <scheduler.h>
#include <txmempool.h>

NodeContext::NodeContext() {}
NodeContext::~NodeContext() {}
2 changes: 1 addition & 1 deletion src/node/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class WalletClient;
//! be used without pulling in unwanted dependencies or functionality.
struct NodeContext {
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;
ChainstateManager* chainman{nullptr}; // Currently a raw pointer because the memory is not managed by this struct
std::unique_ptr<BanMan> banman;
Expand Down
4 changes: 2 additions & 2 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static CTxMemPool* GetMemPool(const util::Ref& context, HTTPRequest* req)
RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found");
return nullptr;
}
return node->mempool;
return node->mempool.get();
}

static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
Expand Down Expand Up @@ -393,7 +393,7 @@ static bool rest_tx(const util::Ref& context, HTTPRequest* req, const std::strin
const NodeContext* const node = GetNodeContext(context, req);
if (!node) return false;
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) {
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
}
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ static UniValue getrawtransaction(const JSONRPCRequest& request)
}

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) {
std::string errmsg;
if (blockindex) {
Expand Down
6 changes: 3 additions & 3 deletions src/test/util/setup_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const

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.chainman = &::g_chainman;
Expand Down Expand Up @@ -187,8 +187,8 @@ TestingSetup::~TestingSetup()
m_node.connman.reset();
m_node.banman.reset();
m_node.args = nullptr;
UnloadBlockIndex(m_node.mempool);
m_node.mempool = nullptr;
UnloadBlockIndex(m_node.mempool.get());
m_node.mempool.reset();
m_node.scheduler.reset();
m_node.chainman->Reset();
m_node.chainman = nullptr;
Expand Down
9 changes: 8 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ arith_uint256 nMinimumChainWork;
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);

CBlockPolicyEstimator feeEstimator;
CTxMemPool mempool(&feeEstimator);

// Internal stuff
namespace {
Expand Down Expand Up @@ -4227,6 +4226,14 @@ bool static LoadBlockIndexDB(ChainstateManager& chainman, const CChainParams& ch
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)
{
AssertLockHeld(cs_main);
Expand Down
4 changes: 3 additions & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ enum class SynchronizationState {

extern RecursiveMutex cs_main;
extern CBlockPolicyEstimator feeEstimator;
extern CTxMemPool mempool;
typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
extern Mutex g_best_block_mutex;
extern std::condition_variable g_best_block_cv;
Expand Down Expand Up @@ -671,6 +670,9 @@ class CChainState {
*/
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. */
bool LoadChainTip(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

Expand Down

0 comments on commit 0708705

Please sign in to comment.