Skip to content

Commit effe81f

Browse files
committed
Move g_is_mempool_loaded into CTxMemPool::m_is_loaded
So the loaded state is explicitly mempool-specific.
1 parent bb8ae2c commit effe81f

File tree

6 files changed

+38
-20
lines changed

6 files changed

+38
-20
lines changed

src/init.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ void Shutdown(InitInterfaces& interfaces)
235235
g_banman.reset();
236236
g_txindex.reset();
237237

238-
if (g_is_mempool_loaded && gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
239-
DumpMempool();
238+
if (::mempool.IsLoaded() && gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
239+
DumpMempool(::mempool);
240240
}
241241

242242
if (fFeeEstimatesInitialized)
@@ -725,9 +725,9 @@ static void ThreadImport(std::vector<fs::path> vImportFiles)
725725
}
726726
} // End scope of CImportingNow
727727
if (gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
728-
LoadMempool();
728+
LoadMempool(::mempool);
729729
}
730-
g_is_mempool_loaded = !ShutdownRequested();
730+
::mempool.SetIsLoaded(!ShutdownRequested());
731731
}
732732

733733
/** Sanity checks

src/rpc/blockchain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ static UniValue getchaintips(const JSONRPCRequest& request)
14841484
UniValue MempoolInfoToJSON(const CTxMemPool& pool)
14851485
{
14861486
UniValue ret(UniValue::VOBJ);
1487-
ret.pushKV("loaded", g_is_mempool_loaded);
1487+
ret.pushKV("loaded", pool.IsLoaded());
14881488
ret.pushKV("size", (int64_t)pool.size());
14891489
ret.pushKV("bytes", (int64_t)pool.GetTotalTxSize());
14901490
ret.pushKV("usage", (int64_t)pool.DynamicMemoryUsage());
@@ -2056,11 +2056,11 @@ static UniValue savemempool(const JSONRPCRequest& request)
20562056
}.ToString());
20572057
}
20582058

2059-
if (!g_is_mempool_loaded) {
2059+
if (!::mempool.IsLoaded()) {
20602060
throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
20612061
}
20622062

2063-
if (!DumpMempool()) {
2063+
if (!DumpMempool(::mempool)) {
20642064
throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk");
20652065
}
20662066

src/txmempool.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,4 +1090,16 @@ void CTxMemPool::GetTransactionAncestry(const uint256& txid, size_t& ancestors,
10901090
}
10911091
}
10921092

1093+
bool CTxMemPool::IsLoaded() const
1094+
{
1095+
LOCK(cs);
1096+
return m_is_loaded;
1097+
}
1098+
1099+
void CTxMemPool::SetIsLoaded(bool loaded)
1100+
{
1101+
LOCK(cs);
1102+
m_is_loaded = loaded;
1103+
}
1104+
10931105
SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}

src/txmempool.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ class CTxMemPool
455455

456456
void trackPackageRemoved(const CFeeRate& rate) EXCLUSIVE_LOCKS_REQUIRED(cs);
457457

458+
bool m_is_loaded GUARDED_BY(cs){false};
459+
458460
public:
459461

460462
static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
@@ -672,6 +674,12 @@ class CTxMemPool
672674
*/
673675
void GetTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) const;
674676

677+
/** @returns true if the mempool is fully loaded */
678+
bool IsLoaded() const;
679+
680+
/** Sets the current loaded state */
681+
void SetIsLoaded(bool loaded);
682+
675683
unsigned long size() const
676684
{
677685
LOCK(cs);

src/validation.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ CAmount maxTxFee = DEFAULT_TRANSACTION_MAXFEE;
251251

252252
CBlockPolicyEstimator feeEstimator;
253253
CTxMemPool mempool(&feeEstimator);
254-
std::atomic_bool g_is_mempool_loaded{false};
255254

256255
/** Constant stuff for coinbase transactions we create: */
257256
CScript COINBASE_FLAGS;
@@ -4648,7 +4647,7 @@ int VersionBitsTipStateSinceHeight(const Consensus::Params& params, Consensus::D
46484647

46494648
static const uint64_t MEMPOOL_DUMP_VERSION = 1;
46504649

4651-
bool LoadMempool()
4650+
bool LoadMempool(CTxMemPool& pool)
46524651
{
46534652
const CChainParams& chainparams = Params();
46544653
int64_t nExpiryTimeout = gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
@@ -4683,12 +4682,12 @@ bool LoadMempool()
46834682

46844683
CAmount amountdelta = nFeeDelta;
46854684
if (amountdelta) {
4686-
mempool.PrioritiseTransaction(tx->GetHash(), amountdelta);
4685+
pool.PrioritiseTransaction(tx->GetHash(), amountdelta);
46874686
}
46884687
CValidationState state;
46894688
if (nTime + nExpiryTimeout > nNow) {
46904689
LOCK(cs_main);
4691-
AcceptToMemoryPoolWithTime(chainparams, mempool, state, tx, nullptr /* pfMissingInputs */, nTime,
4690+
AcceptToMemoryPoolWithTime(chainparams, pool, state, tx, nullptr /* pfMissingInputs */, nTime,
46924691
nullptr /* plTxnReplaced */, false /* bypass_limits */, 0 /* nAbsurdFee */,
46934692
false /* test_accept */);
46944693
if (state.IsValid()) {
@@ -4698,7 +4697,7 @@ bool LoadMempool()
46984697
// wallet(s) having loaded it while we were processing
46994698
// mempool transactions; consider these as valid, instead of
47004699
// failed, but mark them as 'already there'
4701-
if (mempool.exists(tx->GetHash())) {
4700+
if (pool.exists(tx->GetHash())) {
47024701
++already_there;
47034702
} else {
47044703
++failed;
@@ -4714,7 +4713,7 @@ bool LoadMempool()
47144713
file >> mapDeltas;
47154714

47164715
for (const auto& i : mapDeltas) {
4717-
mempool.PrioritiseTransaction(i.first, i.second);
4716+
pool.PrioritiseTransaction(i.first, i.second);
47184717
}
47194718
} catch (const std::exception& e) {
47204719
LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
@@ -4725,7 +4724,7 @@ bool LoadMempool()
47254724
return true;
47264725
}
47274726

4728-
bool DumpMempool()
4727+
bool DumpMempool(const CTxMemPool& pool)
47294728
{
47304729
int64_t start = GetTimeMicros();
47314730

@@ -4736,11 +4735,11 @@ bool DumpMempool()
47364735
LOCK(dump_mutex);
47374736

47384737
{
4739-
LOCK(mempool.cs);
4740-
for (const auto &i : mempool.mapDeltas) {
4738+
LOCK(pool.cs);
4739+
for (const auto &i : pool.mapDeltas) {
47414740
mapDeltas[i.first] = i.second;
47424741
}
4743-
vinfo = mempool.infoAll();
4742+
vinfo = pool.infoAll();
47444743
}
47454744

47464745
int64_t mid = GetTimeMicros();

src/validation.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ extern CScript COINBASE_FLAGS;
149149
extern CCriticalSection cs_main;
150150
extern CBlockPolicyEstimator feeEstimator;
151151
extern CTxMemPool mempool;
152-
extern std::atomic_bool g_is_mempool_loaded;
153152
typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
154153
extern BlockMap& mapBlockIndex GUARDED_BY(cs_main);
155154
extern const std::string strMessageMagic;
@@ -486,10 +485,10 @@ static const unsigned int REJECT_HIGHFEE = 0x100;
486485
CBlockFileInfo* GetBlockFileInfo(size_t n);
487486

488487
/** Dump the mempool to disk. */
489-
bool DumpMempool();
488+
bool DumpMempool(const CTxMemPool& pool);
490489

491490
/** Load the mempool from disk. */
492-
bool LoadMempool();
491+
bool LoadMempool(CTxMemPool& pool);
493492

494493
//! Check whether the block associated with this index entry is pruned or not.
495494
inline bool IsBlockPruned(const CBlockIndex* pblockindex)

0 commit comments

Comments
 (0)