[[refactor]] Check CTxMemPool options in constructor

This ensures that the tests run the same checks on the mempool options
that the init code also applies.
This commit is contained in:
TheCharlatan 2024-04-21 10:03:21 +02:00
parent 67c0d93982
commit d447bdcfb0
No known key found for this signature in database
GPG Key ID: 9B79B45691DB4173
11 changed files with 44 additions and 21 deletions

View File

@ -1523,16 +1523,14 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
if (!result) {
return InitError(util::ErrorString(result));
}
mempool_opts.check_ratio = std::clamp<int>(mempool_opts.check_ratio, 0, 1'000'000);
int64_t descendant_limit_bytes = mempool_opts.limits.descendant_size_vbytes * 40;
if (mempool_opts.max_size_bytes < 0 || mempool_opts.max_size_bytes < descendant_limit_bytes) {
return InitError(strprintf(_("-maxmempool must be at least %d MB"), std::ceil(descendant_limit_bytes / 1'000'000.0)));
}
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));
for (bool fLoaded = false; !fLoaded && !ShutdownRequested(node);) {
node.mempool = std::make_unique<CTxMemPool>(mempool_opts);
bilingual_str mempool_error;
node.mempool = std::make_unique<CTxMemPool>(mempool_opts, mempool_error);
if (!mempool_error.empty()) {
return InitError(mempool_error);
}
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));
node.chainman = std::make_unique<ChainstateManager>(*Assert(node.shutdown), chainman_opts, blockman_opts);
ChainstateManager& chainman = *node.chainman;

View File

@ -33,7 +33,9 @@ void initialize_miner()
FUZZ_TARGET(mini_miner, .init = initialize_miner)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
CTxMemPool pool{CTxMemPool::Options{}};
bilingual_str error;
CTxMemPool pool{CTxMemPool::Options{}, error};
Assert(error.empty());
std::vector<COutPoint> outpoints;
std::deque<COutPoint> available_coins = g_available_coins;
LOCK2(::cs_main, pool.cs);
@ -109,7 +111,9 @@ FUZZ_TARGET(mini_miner, .init = initialize_miner)
FUZZ_TARGET(mini_miner_selection, .init = initialize_miner)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
CTxMemPool pool{CTxMemPool::Options{}};
bilingual_str error;
CTxMemPool pool{CTxMemPool::Options{}, error};
Assert(error.empty());
// Make a copy to preserve determinism.
std::deque<COutPoint> available_coins = g_available_coins;
std::vector<CTransactionRef> transactions;

View File

@ -126,8 +126,11 @@ CTxMemPool MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeConte
mempool_opts.check_ratio = 1;
mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
// ... ignore the error since it might be beneficial to fuzz even when the
// mempool size is unreasonably small
bilingual_str error;
// ...and construct a CTxMemPool from it
return CTxMemPool{mempool_opts};
return CTxMemPool{mempool_opts, error};
}
FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)

View File

@ -52,7 +52,9 @@ FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
CBlockHeaderAndShortTxIDs cmpctblock{*block};
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
bilingual_str error;
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
Assert(error.empty());
PartiallyDownloadedBlock pdb{&pool};
// Set of available transactions (mempool or extra_txn)

View File

@ -56,7 +56,9 @@ FUZZ_TARGET(rbf, .init = initialize_rbf)
return;
}
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
bilingual_str error;
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
Assert(error.empty());
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), NUM_ITERS)
{
@ -101,7 +103,9 @@ FUZZ_TARGET(package_rbf, .init = initialize_package_rbf)
std::optional<CMutableTransaction> child = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
if (!child) return;
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
bilingual_str error;
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
Assert(error.empty());
// Add a bunch of parent-child pairs to the mempool, and remember them.
std::vector<CTransaction> mempool_txs;

View File

@ -126,7 +126,9 @@ CTxMemPool MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeConte
mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
// ...and construct a CTxMemPool from it
return CTxMemPool{mempool_opts};
bilingual_str error;
return CTxMemPool{mempool_opts, error};
Assert(error.empty());
}
void CheckATMPInvariants(const MempoolAcceptResult& res, bool txid_in_mempool, bool wtxid_in_mempool)

View File

@ -40,7 +40,9 @@ FUZZ_TARGET(validation_load_mempool, .init = initialize_validation_load_mempool)
SetMockTime(ConsumeTime(fuzzed_data_provider));
FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
bilingual_str error;
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
Assert(error.empty());
auto& chainstate{static_cast<DummyChainState&>(g_setup->m_node.chainman->ActiveChainstate())};
chainstate.SetMempool(&pool);

View File

@ -46,7 +46,9 @@ struct MinerTestingSetup : public TestingSetup {
// pointer is not accessed, when the new one should be accessed
// instead.
m_node.mempool.reset();
m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node));
bilingual_str error;
m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node), error);
Assert(error.empty());
return *m_node.mempool;
}
BlockAssembler AssemblerForTest(CTxMemPool& tx_mempool);

View File

@ -229,7 +229,9 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
m_node.validation_signals = std::make_unique<ValidationSignals>(std::make_unique<SerialTaskRunner>(*m_node.scheduler));
m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>(FeeestPath(*m_node.args), DEFAULT_ACCEPT_STALE_FEE_ESTIMATES);
m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node));
bilingual_str error{};
m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(m_node), error);
Assert(error.empty());
m_cache_sizes = CalculateCacheSizes(m_args);

View File

@ -395,8 +395,8 @@ void CTxMemPoolEntry::UpdateAncestorState(int32_t modifySize, CAmount modifyFee,
assert(int(nSigOpCostWithAncestors) >= 0);
}
CTxMemPool::CTxMemPool(const Options& opts)
: m_check_ratio{opts.check_ratio},
CTxMemPool::CTxMemPool(const Options& opts, bilingual_str& error)
: m_check_ratio{std::clamp<int>(opts.check_ratio, 0, 1'000'000)},
m_max_size_bytes{opts.max_size_bytes},
m_expiry{opts.expiry},
m_incremental_relay_feerate{opts.incremental_relay_feerate},
@ -410,6 +410,10 @@ CTxMemPool::CTxMemPool(const Options& opts)
m_limits{opts.limits},
m_signals{opts.signals}
{
int64_t descendant_limit_bytes = opts.limits.descendant_size_vbytes * 40;
if (opts.max_size_bytes < 0 || opts.max_size_bytes < descendant_limit_bytes) {
error = strprintf(_("-maxmempool must be at least %d MB"), std::ceil(descendant_limit_bytes / 1'000'000.0));
}
}
bool CTxMemPool::isSpent(const COutPoint& outpoint) const

View File

@ -456,7 +456,7 @@ public:
* accepting transactions becomes O(N^2) where N is the number of transactions
* in the pool.
*/
explicit CTxMemPool(const Options& opts);
explicit CTxMemPool(const Options& opts, bilingual_str& error);
/**
* If sanity-checking is turned on, check makes sure the pool is