mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-05 14:06:27 -05:00
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.
This commit is contained in:
parent
bd4407817e
commit
413f4bb52b
8 changed files with 64 additions and 9 deletions
|
@ -198,6 +198,7 @@ BITCOIN_CORE_H = \
|
|||
node/chainstate.h \
|
||||
node/coin.h \
|
||||
node/context.h \
|
||||
node/mempool_persist_args.h \
|
||||
node/miner.h \
|
||||
node/minisketchwrapper.h \
|
||||
node/psbt.h \
|
||||
|
@ -380,6 +381,7 @@ libbitcoin_node_a_SOURCES = \
|
|||
node/context.cpp \
|
||||
node/eviction.cpp \
|
||||
node/interfaces.cpp \
|
||||
node/mempool_persist_args.cpp \
|
||||
node/miner.cpp \
|
||||
node/minisketchwrapper.cpp \
|
||||
node/psbt.cpp \
|
||||
|
|
|
@ -41,6 +41,7 @@
|
|||
#include <node/chainstate.h>
|
||||
#include <node/context.h>
|
||||
#include <node/interface_ui.h>
|
||||
#include <node/mempool_persist_args.h>
|
||||
#include <node/miner.h>
|
||||
#include <policy/feerate.h>
|
||||
#include <policy/fees.h>
|
||||
|
@ -111,6 +112,8 @@ using node::CleanupBlockRevFiles;
|
|||
using node::DEFAULT_PRINTPRIORITY;
|
||||
using node::DEFAULT_STOPAFTERBLOCKIMPORT;
|
||||
using node::LoadChainstate;
|
||||
using node::MempoolPath;
|
||||
using node::ShouldPersistMempool;
|
||||
using node::NodeContext;
|
||||
using node::ThreadImport;
|
||||
using node::VerifyLoadedChainstate;
|
||||
|
@ -246,8 +249,8 @@ void Shutdown(NodeContext& node)
|
|||
node.addrman.reset();
|
||||
node.netgroupman.reset();
|
||||
|
||||
if (node.mempool && node.mempool->IsLoaded() && node.args->GetBoolArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
|
||||
DumpMempool(*node.mempool);
|
||||
if (node.mempool && node.mempool->IsLoaded() && ShouldPersistMempool(*node.args)) {
|
||||
DumpMempool(*node.mempool, MempoolPath(*node.args));
|
||||
}
|
||||
|
||||
// Drop transactions we were still watching, and record fee estimations.
|
||||
|
|
23
src/node/mempool_persist_args.cpp
Normal file
23
src/node/mempool_persist_args.cpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
// Copyright (c) 2022 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 <node/mempool_persist_args.h>
|
||||
|
||||
#include <fs.h>
|
||||
#include <util/system.h>
|
||||
#include <validation.h>
|
||||
|
||||
namespace node {
|
||||
|
||||
bool ShouldPersistMempool(const ArgsManager& argsman)
|
||||
{
|
||||
return argsman.GetBoolArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL);
|
||||
}
|
||||
|
||||
fs::path MempoolPath(const ArgsManager& argsman)
|
||||
{
|
||||
return argsman.GetDataDirNet() / "mempool.dat";
|
||||
}
|
||||
|
||||
} // namespace node
|
19
src/node/mempool_persist_args.h
Normal file
19
src/node/mempool_persist_args.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Copyright (c) 2022 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_NODE_MEMPOOL_PERSIST_ARGS_H
|
||||
#define BITCOIN_NODE_MEMPOOL_PERSIST_ARGS_H
|
||||
|
||||
#include <fs.h>
|
||||
|
||||
class ArgsManager;
|
||||
|
||||
namespace node {
|
||||
|
||||
bool ShouldPersistMempool(const ArgsManager& argsman);
|
||||
fs::path MempoolPath(const ArgsManager& argsman);
|
||||
|
||||
} // namespace node
|
||||
|
||||
#endif // BITCOIN_NODE_MEMPOOL_PERSIST_ARGS_H
|
|
@ -8,6 +8,7 @@
|
|||
#include <chainparams.h>
|
||||
#include <core_io.h>
|
||||
#include <fs.h>
|
||||
#include <node/mempool_persist_args.h>
|
||||
#include <policy/rbf.h>
|
||||
#include <policy/settings.h>
|
||||
#include <primitives/transaction.h>
|
||||
|
@ -19,6 +20,8 @@
|
|||
#include <util/moneystr.h>
|
||||
|
||||
using node::DEFAULT_MAX_RAW_TX_FEE_RATE;
|
||||
using node::MempoolPath;
|
||||
using node::ShouldPersistMempool;
|
||||
using node::NodeContext;
|
||||
|
||||
static RPCHelpMan sendrawtransaction()
|
||||
|
@ -721,12 +724,14 @@ static RPCHelpMan savemempool()
|
|||
throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
|
||||
}
|
||||
|
||||
if (!DumpMempool(mempool)) {
|
||||
const fs::path& dump_path = MempoolPath(args);
|
||||
|
||||
if (!DumpMempool(mempool, dump_path)) {
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk");
|
||||
}
|
||||
|
||||
UniValue ret(UniValue::VOBJ);
|
||||
ret.pushKV("filename", fs::path((args.GetDataDirNet() / "mempool.dat")).u8string());
|
||||
ret.pushKV("filename", dump_path.u8string());
|
||||
|
||||
return ret;
|
||||
},
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <chainparamsbase.h>
|
||||
#include <mempool_args.h>
|
||||
#include <node/mempool_persist_args.h>
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
#include <test/fuzz/fuzz.h>
|
||||
#include <test/fuzz/util.h>
|
||||
|
@ -15,6 +16,8 @@
|
|||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
using node::MempoolPath;
|
||||
|
||||
namespace {
|
||||
const TestingSetup* g_setup;
|
||||
} // namespace
|
||||
|
@ -37,5 +40,5 @@ FUZZ_TARGET_INIT(validation_load_mempool, initialize_validation_load_mempool)
|
|||
return fuzzed_file_provider.open();
|
||||
};
|
||||
(void)LoadMempool(pool, g_setup->m_node.chainman->ActiveChainstate(), fuzzed_fopen);
|
||||
(void)DumpMempool(pool, fuzzed_fopen, true);
|
||||
(void)DumpMempool(pool, MempoolPath(g_setup->m_args), fuzzed_fopen, true);
|
||||
}
|
||||
|
|
|
@ -4726,7 +4726,7 @@ bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mocka
|
|||
return true;
|
||||
}
|
||||
|
||||
bool DumpMempool(const CTxMemPool& pool, FopenFn mockable_fopen_function, bool skip_file_commit)
|
||||
bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mockable_fopen_function, bool skip_file_commit)
|
||||
{
|
||||
auto start = SteadyClock::now();
|
||||
|
||||
|
@ -4749,7 +4749,7 @@ bool DumpMempool(const CTxMemPool& pool, FopenFn mockable_fopen_function, bool s
|
|||
auto mid = SteadyClock::now();
|
||||
|
||||
try {
|
||||
FILE* filestr{mockable_fopen_function(gArgs.GetDataDirNet() / "mempool.dat.new", "wb")};
|
||||
FILE* filestr{mockable_fopen_function(dump_path + ".new", "wb")};
|
||||
if (!filestr) {
|
||||
return false;
|
||||
}
|
||||
|
@ -4775,7 +4775,7 @@ bool DumpMempool(const CTxMemPool& pool, FopenFn mockable_fopen_function, bool s
|
|||
if (!skip_file_commit && !FileCommit(file.Get()))
|
||||
throw std::runtime_error("FileCommit failed");
|
||||
file.fclose();
|
||||
if (!RenameOver(gArgs.GetDataDirNet() / "mempool.dat.new", gArgs.GetDataDirNet() / "mempool.dat")) {
|
||||
if (!RenameOver(dump_path + ".new", dump_path)) {
|
||||
throw std::runtime_error("Rename failed");
|
||||
}
|
||||
auto last = SteadyClock::now();
|
||||
|
|
|
@ -1017,7 +1017,7 @@ bool DeploymentEnabled(const ChainstateManager& chainman, DEP dep)
|
|||
using FopenFn = std::function<FILE*(const fs::path&, const char*)>;
|
||||
|
||||
/** Dump the mempool to disk. */
|
||||
bool DumpMempool(const CTxMemPool& pool, FopenFn mockable_fopen_function = fsbridge::fopen, bool skip_file_commit = false);
|
||||
bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mockable_fopen_function = fsbridge::fopen, bool skip_file_commit = false);
|
||||
|
||||
/** Load the mempool from disk. */
|
||||
bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mockable_fopen_function = fsbridge::fopen);
|
||||
|
|
Loading…
Add table
Reference in a new issue