Skip to content

Commit 413f4bb

Browse files
committed
DumpMempool: Pass in dump_path, stop using gArgs
Also introduce node::{ShouldPersistMempool,MempoolPath} helper functions in node/mempool_persist_args.{h,cpp} which are used by non-kernel DumpMempool callers to determine whether or not to automatically dump the mempool and where to dump it to.
1 parent bd44078 commit 413f4bb

8 files changed

+64
-9
lines changed

src/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ BITCOIN_CORE_H = \
198198
node/chainstate.h \
199199
node/coin.h \
200200
node/context.h \
201+
node/mempool_persist_args.h \
201202
node/miner.h \
202203
node/minisketchwrapper.h \
203204
node/psbt.h \
@@ -380,6 +381,7 @@ libbitcoin_node_a_SOURCES = \
380381
node/context.cpp \
381382
node/eviction.cpp \
382383
node/interfaces.cpp \
384+
node/mempool_persist_args.cpp \
383385
node/miner.cpp \
384386
node/minisketchwrapper.cpp \
385387
node/psbt.cpp \

src/init.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <node/chainstate.h>
4242
#include <node/context.h>
4343
#include <node/interface_ui.h>
44+
#include <node/mempool_persist_args.h>
4445
#include <node/miner.h>
4546
#include <policy/feerate.h>
4647
#include <policy/fees.h>
@@ -111,6 +112,8 @@ using node::CleanupBlockRevFiles;
111112
using node::DEFAULT_PRINTPRIORITY;
112113
using node::DEFAULT_STOPAFTERBLOCKIMPORT;
113114
using node::LoadChainstate;
115+
using node::MempoolPath;
116+
using node::ShouldPersistMempool;
114117
using node::NodeContext;
115118
using node::ThreadImport;
116119
using node::VerifyLoadedChainstate;
@@ -246,8 +249,8 @@ void Shutdown(NodeContext& node)
246249
node.addrman.reset();
247250
node.netgroupman.reset();
248251

249-
if (node.mempool && node.mempool->IsLoaded() && node.args->GetBoolArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
250-
DumpMempool(*node.mempool);
252+
if (node.mempool && node.mempool->IsLoaded() && ShouldPersistMempool(*node.args)) {
253+
DumpMempool(*node.mempool, MempoolPath(*node.args));
251254
}
252255

253256
// Drop transactions we were still watching, and record fee estimations.

src/node/mempool_persist_args.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <node/mempool_persist_args.h>
6+
7+
#include <fs.h>
8+
#include <util/system.h>
9+
#include <validation.h>
10+
11+
namespace node {
12+
13+
bool ShouldPersistMempool(const ArgsManager& argsman)
14+
{
15+
return argsman.GetBoolArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL);
16+
}
17+
18+
fs::path MempoolPath(const ArgsManager& argsman)
19+
{
20+
return argsman.GetDataDirNet() / "mempool.dat";
21+
}
22+
23+
} // namespace node

src/node/mempool_persist_args.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_NODE_MEMPOOL_PERSIST_ARGS_H
6+
#define BITCOIN_NODE_MEMPOOL_PERSIST_ARGS_H
7+
8+
#include <fs.h>
9+
10+
class ArgsManager;
11+
12+
namespace node {
13+
14+
bool ShouldPersistMempool(const ArgsManager& argsman);
15+
fs::path MempoolPath(const ArgsManager& argsman);
16+
17+
} // namespace node
18+
19+
#endif // BITCOIN_NODE_MEMPOOL_PERSIST_ARGS_H

src/rpc/mempool.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <chainparams.h>
99
#include <core_io.h>
1010
#include <fs.h>
11+
#include <node/mempool_persist_args.h>
1112
#include <policy/rbf.h>
1213
#include <policy/settings.h>
1314
#include <primitives/transaction.h>
@@ -19,6 +20,8 @@
1920
#include <util/moneystr.h>
2021

2122
using node::DEFAULT_MAX_RAW_TX_FEE_RATE;
23+
using node::MempoolPath;
24+
using node::ShouldPersistMempool;
2225
using node::NodeContext;
2326

2427
static RPCHelpMan sendrawtransaction()
@@ -721,12 +724,14 @@ static RPCHelpMan savemempool()
721724
throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
722725
}
723726

724-
if (!DumpMempool(mempool)) {
727+
const fs::path& dump_path = MempoolPath(args);
728+
729+
if (!DumpMempool(mempool, dump_path)) {
725730
throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk");
726731
}
727732

728733
UniValue ret(UniValue::VOBJ);
729-
ret.pushKV("filename", fs::path((args.GetDataDirNet() / "mempool.dat")).u8string());
734+
ret.pushKV("filename", dump_path.u8string());
730735

731736
return ret;
732737
},

src/test/fuzz/validation_load_mempool.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <chainparamsbase.h>
66
#include <mempool_args.h>
7+
#include <node/mempool_persist_args.h>
78
#include <test/fuzz/FuzzedDataProvider.h>
89
#include <test/fuzz/fuzz.h>
910
#include <test/fuzz/util.h>
@@ -15,6 +16,8 @@
1516
#include <cstdint>
1617
#include <vector>
1718

19+
using node::MempoolPath;
20+
1821
namespace {
1922
const TestingSetup* g_setup;
2023
} // namespace
@@ -37,5 +40,5 @@ FUZZ_TARGET_INIT(validation_load_mempool, initialize_validation_load_mempool)
3740
return fuzzed_file_provider.open();
3841
};
3942
(void)LoadMempool(pool, g_setup->m_node.chainman->ActiveChainstate(), fuzzed_fopen);
40-
(void)DumpMempool(pool, fuzzed_fopen, true);
43+
(void)DumpMempool(pool, MempoolPath(g_setup->m_args), fuzzed_fopen, true);
4144
}

src/validation.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -4726,7 +4726,7 @@ bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mocka
47264726
return true;
47274727
}
47284728

4729-
bool DumpMempool(const CTxMemPool& pool, FopenFn mockable_fopen_function, bool skip_file_commit)
4729+
bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mockable_fopen_function, bool skip_file_commit)
47304730
{
47314731
auto start = SteadyClock::now();
47324732

@@ -4749,7 +4749,7 @@ bool DumpMempool(const CTxMemPool& pool, FopenFn mockable_fopen_function, bool s
47494749
auto mid = SteadyClock::now();
47504750

47514751
try {
4752-
FILE* filestr{mockable_fopen_function(gArgs.GetDataDirNet() / "mempool.dat.new", "wb")};
4752+
FILE* filestr{mockable_fopen_function(dump_path + ".new", "wb")};
47534753
if (!filestr) {
47544754
return false;
47554755
}
@@ -4775,7 +4775,7 @@ bool DumpMempool(const CTxMemPool& pool, FopenFn mockable_fopen_function, bool s
47754775
if (!skip_file_commit && !FileCommit(file.Get()))
47764776
throw std::runtime_error("FileCommit failed");
47774777
file.fclose();
4778-
if (!RenameOver(gArgs.GetDataDirNet() / "mempool.dat.new", gArgs.GetDataDirNet() / "mempool.dat")) {
4778+
if (!RenameOver(dump_path + ".new", dump_path)) {
47794779
throw std::runtime_error("Rename failed");
47804780
}
47814781
auto last = SteadyClock::now();

src/validation.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ bool DeploymentEnabled(const ChainstateManager& chainman, DEP dep)
10171017
using FopenFn = std::function<FILE*(const fs::path&, const char*)>;
10181018

10191019
/** Dump the mempool to disk. */
1020-
bool DumpMempool(const CTxMemPool& pool, FopenFn mockable_fopen_function = fsbridge::fopen, bool skip_file_commit = false);
1020+
bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mockable_fopen_function = fsbridge::fopen, bool skip_file_commit = false);
10211021

10221022
/** Load the mempool from disk. */
10231023
bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mockable_fopen_function = fsbridge::fopen);

0 commit comments

Comments
 (0)