Skip to content

Commit b857ac6

Browse files
committed
test/fuzz: Invoke LoadMempool via CChainState
Not only does this increase coverage, it is also more correct in that when ::LoadMempool is called with a mempool and chainstate, it calls AcceptToMemoryPool with just the chainstate. AcceptToMemoryPool will then act on the chainstate's mempool via CChainState::GetMempool, which may be different from the mempool originally passed to ::LoadMempool. (In this fuzz test's case, it definitely is different) Also, move DummyChainstate to its own file since it's now used by the validation_load_mempool fuzz test to replace CChainState's m_mempool.
1 parent b326725 commit b857ac6

6 files changed

+29
-13
lines changed

src/Makefile.test_fuzz.include

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ EXTRA_LIBRARIES += \
1010
TEST_FUZZ_H = \
1111
test/fuzz/fuzz.h \
1212
test/fuzz/FuzzedDataProvider.h \
13+
test/fuzz/mempool_utils.h \
1314
test/fuzz/util.h
1415

1516
libtest_fuzz_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(NATPMP_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS)

src/test/fuzz/mempool_utils.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_TEST_FUZZ_MEMPOOL_UTILS_H
6+
#define BITCOIN_TEST_FUZZ_MEMPOOL_UTILS_H
7+
8+
#include <validation.h>
9+
10+
class DummyChainState final : public CChainState
11+
{
12+
public:
13+
void SetMempool(CTxMemPool* mempool)
14+
{
15+
m_mempool = mempool;
16+
}
17+
};
18+
19+
#endif // BITCOIN_TEST_FUZZ_MEMPOOL_UTILS_H

src/test/fuzz/tx_pool.cpp

+1-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <node/miner.h>
99
#include <test/fuzz/FuzzedDataProvider.h>
1010
#include <test/fuzz/fuzz.h>
11+
#include <test/fuzz/mempool_utils.h>
1112
#include <test/fuzz/util.h>
1213
#include <test/util/mining.h>
1314
#include <test/util/script.h>
@@ -34,15 +35,6 @@ struct MockedTxPool : public CTxMemPool {
3435
}
3536
};
3637

37-
class DummyChainState final : public CChainState
38-
{
39-
public:
40-
void SetMempool(CTxMemPool* mempool)
41-
{
42-
m_mempool = mempool;
43-
}
44-
};
45-
4638
void initialize_tx_pool()
4739
{
4840
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();

src/test/fuzz/validation_load_mempool.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <node/mempool_persist_args.h>
88
#include <test/fuzz/FuzzedDataProvider.h>
99
#include <test/fuzz/fuzz.h>
10+
#include <test/fuzz/mempool_utils.h>
1011
#include <test/fuzz/util.h>
1112
#include <test/util/setup_common.h>
1213
#include <txmempool.h>
@@ -36,9 +37,12 @@ FUZZ_TARGET_INIT(validation_load_mempool, initialize_validation_load_mempool)
3637

3738
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
3839

40+
auto& chainstate{static_cast<DummyChainState&>(g_setup->m_node.chainman->ActiveChainstate())};
41+
chainstate.SetMempool(&pool);
42+
3943
auto fuzzed_fopen = [&](const fs::path&, const char*) {
4044
return fuzzed_file_provider.open();
4145
};
42-
(void)LoadMempool(pool, g_setup->m_node.chainman->ActiveChainstate(), fuzzed_fopen);
46+
(void)chainstate.LoadMempool(g_setup->m_args, fuzzed_fopen);
4347
(void)DumpMempool(pool, MempoolPath(g_setup->m_args), fuzzed_fopen, true);
4448
}

src/validation.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3865,11 +3865,11 @@ void PruneBlockFilesManual(CChainState& active_chainstate, int nManualPruneHeigh
38653865
}
38663866
}
38673867

3868-
void CChainState::LoadMempool(const ArgsManager& args)
3868+
void CChainState::LoadMempool(const ArgsManager& args, FopenFn mockable_fopen_function)
38693869
{
38703870
if (!m_mempool) return;
38713871
if (args.GetBoolArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
3872-
::LoadMempool(*m_mempool, *this);
3872+
::LoadMempool(*m_mempool, *this, mockable_fopen_function);
38733873
}
38743874
m_mempool->SetLoadTried(!ShutdownRequested());
38753875
}

src/validation.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ class CChainState
679679
void CheckBlockIndex();
680680

681681
/** Load the persisted mempool from disk */
682-
void LoadMempool(const ArgsManager& args);
682+
void LoadMempool(const ArgsManager& args, fsbridge::FopenFn mockable_fopen_function = fsbridge::fopen);
683683

684684
/** Update the chain tip based on database information, i.e. CoinsTip()'s best block. */
685685
bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main);

0 commit comments

Comments
 (0)