Skip to content

Commit 91af6b9

Browse files
validation: Make DumpMempool(...) and LoadMempool(...) easier to test/fuzz/mock
1 parent af322c7 commit 91af6b9

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/validation.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5010,11 +5010,11 @@ CBlockFileInfo* GetBlockFileInfo(size_t n)
50105010

50115011
static const uint64_t MEMPOOL_DUMP_VERSION = 1;
50125012

5013-
bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate)
5013+
bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mockable_fopen_function)
50145014
{
50155015
const CChainParams& chainparams = Params();
50165016
int64_t nExpiryTimeout = gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
5017-
FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat", "rb");
5017+
FILE* filestr{mockable_fopen_function(GetDataDir() / "mempool.dat", "rb")};
50185018
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
50195019
if (file.IsNull()) {
50205020
LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
@@ -5095,7 +5095,7 @@ bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate)
50955095
return true;
50965096
}
50975097

5098-
bool DumpMempool(const CTxMemPool& pool)
5098+
bool DumpMempool(const CTxMemPool& pool, FopenFn mockable_fopen_function, bool skip_file_commit)
50995099
{
51005100
int64_t start = GetTimeMicros();
51015101

@@ -5118,7 +5118,7 @@ bool DumpMempool(const CTxMemPool& pool)
51185118
int64_t mid = GetTimeMicros();
51195119

51205120
try {
5121-
FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat.new", "wb");
5121+
FILE* filestr{mockable_fopen_function(GetDataDir() / "mempool.dat.new", "wb")};
51225122
if (!filestr) {
51235123
return false;
51245124
}
@@ -5141,7 +5141,7 @@ bool DumpMempool(const CTxMemPool& pool)
51415141
LogPrintf("Writing %d unbroadcast transactions to disk.\n", unbroadcast_txids.size());
51425142
file << unbroadcast_txids;
51435143

5144-
if (!FileCommit(file.Get()))
5144+
if (!skip_file_commit && !FileCommit(file.Get()))
51455145
throw std::runtime_error("FileCommit failed");
51465146
file.fclose();
51475147
if (!RenameOver(GetDataDir() / "mempool.dat.new", GetDataDir() / "mempool.dat")) {

src/validation.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,11 +1013,13 @@ int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Para
10131013
/** Get block file info entry for one block file */
10141014
CBlockFileInfo* GetBlockFileInfo(size_t n);
10151015

1016+
using FopenFn = std::function<FILE*(const fs::path&, const char*)>;
1017+
10161018
/** Dump the mempool to disk. */
1017-
bool DumpMempool(const CTxMemPool& pool);
1019+
bool DumpMempool(const CTxMemPool& pool, FopenFn mockable_fopen_function = fsbridge::fopen, bool skip_file_commit = false);
10181020

10191021
/** Load the mempool from disk. */
1020-
bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate);
1022+
bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mockable_fopen_function = fsbridge::fopen);
10211023

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

0 commit comments

Comments
 (0)