Skip to content

Commit 29818f5

Browse files
committed
[refactor] Check CTxMemPool options in ctor
This ensures that the tests run the same checks on the mempool options that the init code also applies.
1 parent 160d236 commit 29818f5

File tree

11 files changed

+43
-20
lines changed

11 files changed

+43
-20
lines changed

src/init.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,16 +1463,15 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14631463
if (!result) {
14641464
return InitError(util::ErrorString(result));
14651465
}
1466-
mempool_opts.check_ratio = std::clamp<int>(mempool_opts.check_ratio, 0, 1'000'000);
1467-
1468-
int64_t descendant_limit_bytes = mempool_opts.limits.descendant_size_vbytes * 40;
1469-
if (mempool_opts.max_size_bytes < 0 || mempool_opts.max_size_bytes < descendant_limit_bytes) {
1470-
return InitError(strprintf(_("-maxmempool must be at least %d MB"), std::ceil(descendant_limit_bytes / 1'000'000.0)));
1471-
}
1472-
LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", cache_sizes.coins * (1.0 / 1024 / 1024), mempool_opts.max_size_bytes * (1.0 / 1024 / 1024));
14731466

14741467
for (bool fLoaded = false; !fLoaded && !ShutdownRequested();) {
1475-
node.mempool = std::make_unique<CTxMemPool>(mempool_opts);
1468+
std::optional<bilingual_str> mempool_error{};
1469+
node.mempool = std::make_unique<CTxMemPool>(mempool_opts, mempool_error);
1470+
if (mempool_error) {
1471+
return InitError(mempool_error.value());
1472+
}
1473+
LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", cache_sizes.coins * (1.0 / 1024 / 1024), mempool_opts.max_size_bytes * (1.0 / 1024 / 1024));
1474+
14761475

14771476
node.chainman = std::make_unique<ChainstateManager>(node.kernel->interrupt, chainman_opts, blockman_opts);
14781477
ChainstateManager& chainman = *node.chainman;

src/test/fuzz/mini_miner.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <txmempool.h>
1515

1616
#include <deque>
17+
#include <optional>
1718
#include <vector>
1819

1920
namespace {
@@ -33,7 +34,9 @@ void initialize_miner()
3334
FUZZ_TARGET(mini_miner, .init = initialize_miner)
3435
{
3536
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
36-
CTxMemPool pool{CTxMemPool::Options{}};
37+
std::optional<bilingual_str> error{};
38+
CTxMemPool pool{CTxMemPool::Options{}, error};
39+
Assert(!error);
3740
std::vector<COutPoint> outpoints;
3841
std::deque<COutPoint> available_coins = g_available_coins;
3942
LOCK2(::cs_main, pool.cs);
@@ -109,7 +112,9 @@ FUZZ_TARGET(mini_miner, .init = initialize_miner)
109112
FUZZ_TARGET(mini_miner_selection, .init = initialize_miner)
110113
{
111114
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
112-
CTxMemPool pool{CTxMemPool::Options{}};
115+
std::optional<bilingual_str> error{};
116+
CTxMemPool pool{CTxMemPool::Options{}, error};
117+
Assert(!error);
113118
// Make a copy to preserve determinism.
114119
std::deque<COutPoint> available_coins = g_available_coins;
115120
std::vector<CTransactionRef> transactions;

src/test/fuzz/package_eval.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ CTxMemPool MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeConte
125125
mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
126126

127127
// ...and construct a CTxMemPool from it
128-
return CTxMemPool{mempool_opts};
128+
std::optional<bilingual_str> error{};
129+
return CTxMemPool{mempool_opts, error};
130+
Assert(!error);
129131
}
130132

131133
FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)

src/test/fuzz/partially_downloaded_block.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
5252

5353
CBlockHeaderAndShortTxIDs cmpctblock{*block};
5454

55-
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
55+
std::optional<bilingual_str> error{};
56+
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
57+
Assert(!error);
5658
PartiallyDownloadedBlock pdb{&pool};
5759

5860
// Set of available transactions (mempool or extra_txn)

src/test/fuzz/rbf.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ FUZZ_TARGET(rbf, .init = initialize_rbf)
3838
return;
3939
}
4040

41-
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
41+
std::optional<bilingual_str> error{};
42+
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
43+
Assert(!error);
4244

4345
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
4446
{

src/test/fuzz/tx_pool.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ CTxMemPool MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeConte
127127
mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
128128

129129
// ...and construct a CTxMemPool from it
130-
return CTxMemPool{mempool_opts};
130+
std::optional<bilingual_str> error{};
131+
return CTxMemPool{mempool_opts, error};
132+
Assert(!error);
131133
}
132134

133135
void CheckATMPInvariants(const MempoolAcceptResult& res, bool txid_in_mempool, bool wtxid_in_mempool)

src/test/fuzz/validation_load_mempool.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ FUZZ_TARGET(validation_load_mempool, .init = initialize_validation_load_mempool)
4040
SetMockTime(ConsumeTime(fuzzed_data_provider));
4141
FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
4242

43-
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
43+
std::optional<bilingual_str> error{};
44+
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
45+
Assert(!error);
4446

4547
auto& chainstate{static_cast<DummyChainState&>(g_setup->m_node.chainman->ActiveChainstate())};
4648
chainstate.SetMempool(&pool);

src/test/miner_tests.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <test/util/setup_common.h>
2424

2525
#include <memory>
26+
#include <optional>
2627

2728
#include <boost/test/unit_test.hpp>
2829

@@ -47,7 +48,9 @@ struct MinerTestingSetup : public TestingSetup {
4748
// pointer is not accessed, when the new one should be accessed
4849
// instead.
4950
m_node.mempool.reset();
50-
m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node));
51+
std::optional<bilingual_str> error{};
52+
m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node), error);
53+
Assert(!error);
5154
return *m_node.mempool;
5255
}
5356
BlockAssembler AssemblerForTest(CTxMemPool& tx_mempool);

src/test/util/setup_common.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
175175
GetMainSignals().RegisterBackgroundSignalScheduler(*m_node.scheduler);
176176

177177
m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(*m_node.args), DEFAULT_ACCEPT_STALE_FEE_ESTIMATES);
178-
m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node));
178+
std::optional<bilingual_str> error{};
179+
m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node), error);
180+
Assert(!error);
179181

180182
m_cache_sizes = CalculateCacheSizes(m_args);
181183

src/txmempool.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ void CTxMemPoolEntry::UpdateAncestorState(int32_t modifySize, CAmount modifyFee,
400400
assert(int(nSigOpCostWithAncestors) >= 0);
401401
}
402402

403-
CTxMemPool::CTxMemPool(const Options& opts)
404-
: m_check_ratio{opts.check_ratio},
403+
CTxMemPool::CTxMemPool(const Options& opts, std::optional<bilingual_str>& error)
404+
: m_check_ratio{std::clamp<int>(opts.check_ratio, 0, 1'000'000)},
405405
m_max_size_bytes{opts.max_size_bytes},
406406
m_expiry{opts.expiry},
407407
m_incremental_relay_feerate{opts.incremental_relay_feerate},
@@ -414,6 +414,10 @@ CTxMemPool::CTxMemPool(const Options& opts)
414414
m_persist_v1_dat{opts.persist_v1_dat},
415415
m_limits{opts.limits}
416416
{
417+
int64_t descendant_limit_bytes = opts.limits.descendant_size_vbytes * 40;
418+
if (opts.max_size_bytes < 0 || opts.max_size_bytes < descendant_limit_bytes) {
419+
error = strprintf(_("-maxmempool must be at least %d MB"), std::ceil(descendant_limit_bytes / 1'000'000.0));
420+
}
417421
}
418422

419423
bool CTxMemPool::isSpent(const COutPoint& outpoint) const

0 commit comments

Comments
 (0)