Skip to content

Commit eb15569

Browse files
committed
fuzz: add util/mempool/h.cpp
Moving the mempool code (Boost) out of util.h, results in a ~10% speedup (for me) when compiling the fuzz tests.
1 parent d919e8d commit eb15569

10 files changed

+43
-25
lines changed

src/Makefile.test_fuzz.include

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ EXTRA_LIBRARIES += \
1010
TEST_FUZZ_H = \
1111
test/fuzz/fuzz.h \
1212
test/fuzz/FuzzedDataProvider.h \
13-
test/fuzz/mempool_utils.h \
14-
test/fuzz/util.h
13+
test/fuzz/util.h \
14+
test/fuzz/util/mempool.h
1515

1616
libtest_fuzz_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BOOST_CPPFLAGS)
1717
libtest_fuzz_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
1818
libtest_fuzz_a_SOURCES = \
1919
test/fuzz/fuzz.cpp \
2020
test/fuzz/util.cpp \
21+
test/fuzz/util/mempool.cpp \
2122
$(TEST_FUZZ_H)

src/test/fuzz/integer.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <key_io.h>
1313
#include <memusage.h>
1414
#include <netbase.h>
15+
#include <policy/policy.h>
1516
#include <policy/settings.h>
1617
#include <pow.h>
1718
#include <protocol.h>

src/test/fuzz/policy_estimator.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <test/fuzz/FuzzedDataProvider.h>
99
#include <test/fuzz/fuzz.h>
1010
#include <test/fuzz/util.h>
11+
#include <test/fuzz/util/mempool.h>
1112
#include <test/util/setup_common.h>
1213
#include <txmempool.h>
1314

src/test/fuzz/rbf.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <test/fuzz/FuzzedDataProvider.h>
1010
#include <test/fuzz/fuzz.h>
1111
#include <test/fuzz/util.h>
12+
#include <test/fuzz/util/mempool.h>
1213
#include <test/util/setup_common.h>
1314
#include <txmempool.h>
1415

src/test/fuzz/tx_pool.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#include <node/miner.h>
99
#include <test/fuzz/FuzzedDataProvider.h>
1010
#include <test/fuzz/fuzz.h>
11-
#include <test/fuzz/mempool_utils.h>
1211
#include <test/fuzz/util.h>
12+
#include <test/fuzz/util/mempool.h>
1313
#include <test/util/mining.h>
1414
#include <test/util/script.h>
1515
#include <test/util/setup_common.h>

src/test/fuzz/util.cpp

-15
Original file line numberDiff line numberDiff line change
@@ -478,21 +478,6 @@ CTxDestination ConsumeTxDestination(FuzzedDataProvider& fuzzed_data_provider) no
478478
return tx_destination;
479479
}
480480

481-
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider& fuzzed_data_provider, const CTransaction& tx) noexcept
482-
{
483-
// Avoid:
484-
// policy/feerate.cpp:28:34: runtime error: signed integer overflow: 34873208148477500 * 1000 cannot be represented in type 'long'
485-
//
486-
// Reproduce using CFeeRate(348732081484775, 10).GetFeePerK()
487-
const CAmount fee = std::min<CAmount>(ConsumeMoney(fuzzed_data_provider), std::numeric_limits<CAmount>::max() / static_cast<CAmount>(100000));
488-
assert(MoneyRange(fee));
489-
const int64_t time = fuzzed_data_provider.ConsumeIntegral<int64_t>();
490-
const unsigned int entry_height = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
491-
const bool spends_coinbase = fuzzed_data_provider.ConsumeBool();
492-
const unsigned int sig_op_cost = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, MAX_BLOCK_SIGOPS_COST);
493-
return CTxMemPoolEntry{MakeTransactionRef(tx), fee, time, entry_height, spends_coinbase, sig_op_cost, {}};
494-
}
495-
496481
bool ContainsSpentInput(const CTransaction& tx, const CCoinsViewCache& inputs) noexcept
497482
{
498483
for (const CTxIn& tx_in : tx.vin) {

src/test/fuzz/util.h

-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <test/fuzz/FuzzedDataProvider.h>
2424
#include <test/fuzz/fuzz.h>
2525
#include <test/util/net.h>
26-
#include <txmempool.h>
2726
#include <uint256.h>
2827
#include <version.h>
2928

@@ -213,8 +212,6 @@ template <typename WeakEnumType, size_t size>
213212
return UintToArith256(ConsumeUInt256(fuzzed_data_provider));
214213
}
215214

216-
[[nodiscard]] CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider& fuzzed_data_provider, const CTransaction& tx) noexcept;
217-
218215
[[nodiscard]] CTxDestination ConsumeTxDestination(FuzzedDataProvider& fuzzed_data_provider) noexcept;
219216

220217
template <typename T>

src/test/fuzz/util/mempool.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#include <consensus/amount.h>
6+
#include <primitives/transaction.h>
7+
#include <test/fuzz/FuzzedDataProvider.h>
8+
#include <test/fuzz/util.h>
9+
#include <test/fuzz/util/mempool.h>
10+
#include <txmempool.h>
11+
12+
#include <limits>
13+
14+
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider& fuzzed_data_provider, const CTransaction& tx) noexcept
15+
{
16+
// Avoid:
17+
// policy/feerate.cpp:28:34: runtime error: signed integer overflow: 34873208148477500 * 1000 cannot be represented in type 'long'
18+
//
19+
// Reproduce using CFeeRate(348732081484775, 10).GetFeePerK()
20+
const CAmount fee = std::min<CAmount>(ConsumeMoney(fuzzed_data_provider), std::numeric_limits<CAmount>::max() / static_cast<CAmount>(100000));
21+
assert(MoneyRange(fee));
22+
const int64_t time = fuzzed_data_provider.ConsumeIntegral<int64_t>();
23+
const unsigned int entry_height = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
24+
const bool spends_coinbase = fuzzed_data_provider.ConsumeBool();
25+
const unsigned int sig_op_cost = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, MAX_BLOCK_SIGOPS_COST);
26+
return CTxMemPoolEntry{MakeTransactionRef(tx), fee, time, entry_height, spends_coinbase, sig_op_cost, {}};
27+
}

src/test/fuzz/mempool_utils.h renamed to src/test/fuzz/util/mempool.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#ifndef BITCOIN_TEST_FUZZ_MEMPOOL_UTILS_H
6-
#define BITCOIN_TEST_FUZZ_MEMPOOL_UTILS_H
5+
#ifndef BITCOIN_TEST_FUZZ_UTIL_MEMPOOL_H
6+
#define BITCOIN_TEST_FUZZ_UTIL_MEMPOOL_H
77

8+
#include <primitives/transaction.h>
9+
#include <test/fuzz/FuzzedDataProvider.h>
10+
#include <txmempool.h>
811
#include <validation.h>
912

1013
class DummyChainState final : public Chainstate
@@ -16,4 +19,6 @@ class DummyChainState final : public Chainstate
1619
}
1720
};
1821

19-
#endif // BITCOIN_TEST_FUZZ_MEMPOOL_UTILS_H
22+
[[nodiscard]] CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider& fuzzed_data_provider, const CTransaction& tx) noexcept;
23+
24+
#endif // BITCOIN_TEST_FUZZ_UTIL_MEMPOOL_H

src/test/fuzz/validation_load_mempool.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <node/mempool_persist_args.h>
1010
#include <test/fuzz/FuzzedDataProvider.h>
1111
#include <test/fuzz/fuzz.h>
12-
#include <test/fuzz/mempool_utils.h>
1312
#include <test/fuzz/util.h>
13+
#include <test/fuzz/util/mempool.h>
1414
#include <test/util/setup_common.h>
1515
#include <txmempool.h>
1616
#include <util/time.h>

0 commit comments

Comments
 (0)