Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuzz: Speed up *_package_eval fuzz targets a bit #31457

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/test/fuzz/package_eval.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 The Bitcoin Core developers
// Copyright (c) 2023-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand Down Expand Up @@ -53,9 +53,9 @@ void initialize_tx_pool()
}

struct OutpointsUpdater final : public CValidationInterface {
std::set<COutPoint>& m_mempool_outpoints;
std::unordered_set<COutPoint, SaltedOutpointHasher>& m_mempool_outpoints;

explicit OutpointsUpdater(std::set<COutPoint>& r)
explicit OutpointsUpdater(std::unordered_set<COutPoint, SaltedOutpointHasher>& r)
: m_mempool_outpoints{r} {}

void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /* mempool_sequence */) override
Expand Down Expand Up @@ -195,8 +195,8 @@ FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool)
MockTime(fuzzed_data_provider, chainstate);

// All RBF-spendable outpoints outside of the unsubmitted package
std::set<COutPoint> mempool_outpoints;
std::map<COutPoint, CAmount> outpoints_value;
std::unordered_set<COutPoint, SaltedOutpointHasher> mempool_outpoints;
std::unordered_map<COutPoint, CAmount, SaltedOutpointHasher> outpoints_value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a SaltedOutpointHasher probably requires to call SeedRandomStateForTest(SeedRand::ZEROS) each iteration. Otherwise non-determinism might happen due to e.g. different order when iterating over the sets.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. This means:

  • Other tests may also need a re-seed on every iteration, if they interact with data, sorted by a salted hasher
  • The speed-up may actually come from invalidating the fuzz inputs, making a change like this harder to test

for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
Assert(mempool_outpoints.insert(outpoint).second);
outpoints_value[outpoint] = 50 * COIN;
Expand All @@ -220,9 +220,9 @@ FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool)
std::optional<COutPoint> outpoint_to_rbf{fuzzed_data_provider.ConsumeBool() ? GetChildEvictingPrevout(tx_pool) : std::nullopt};

// Make small packages
const auto num_txs = outpoint_to_rbf ? 1 : (size_t) fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 4);
const auto num_txs = outpoint_to_rbf ? 1 : fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4);

std::set<COutPoint> package_outpoints;
std::unordered_set<COutPoint, SaltedOutpointHasher> package_outpoints;
while (txs.size() < num_txs) {
// Create transaction to add to the mempool
txs.emplace_back([&] {
Expand Down Expand Up @@ -349,8 +349,8 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
MockTime(fuzzed_data_provider, chainstate);

// All RBF-spendable outpoints outside of the unsubmitted package
std::set<COutPoint> mempool_outpoints;
std::map<COutPoint, CAmount> outpoints_value;
std::unordered_set<COutPoint, SaltedOutpointHasher> mempool_outpoints;
std::unordered_map<COutPoint, CAmount, SaltedOutpointHasher> outpoints_value;
for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
Assert(mempool_outpoints.insert(outpoint).second);
outpoints_value[outpoint] = 50 * COIN;
Expand All @@ -371,8 +371,8 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
std::vector<CTransactionRef> txs;

// Make packages of 1-to-26 transactions
const auto num_txs = (size_t) fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 26);
std::set<COutPoint> package_outpoints;
const auto num_txs = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 26);
std::unordered_set<COutPoint, SaltedOutpointHasher> package_outpoints;
while (txs.size() < num_txs) {
// Create transaction to add to the mempool
txs.emplace_back([&] {
Expand Down
Loading