mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-12 23:13:25 +02:00
fuzz: initial compact block fuzz harness
Adds a fuzz harness cmpctblock to test BIP152 compact block relay. Currently just sets mock time in a loop.
This commit is contained in:
@@ -26,6 +26,7 @@ add_executable(fuzz
|
||||
chain.cpp
|
||||
checkqueue.cpp
|
||||
cluster_linearize.cpp
|
||||
cmpctblock.cpp
|
||||
coins_view.cpp
|
||||
coinscache_sim.cpp
|
||||
connman.cpp
|
||||
|
||||
95
src/test/fuzz/cmpctblock.cpp
Normal file
95
src/test/fuzz/cmpctblock.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
// Copyright (c) 2026 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <chain.h>
|
||||
#include <chainparams.h>
|
||||
#include <consensus/consensus.h>
|
||||
#include <node/miner.h>
|
||||
#include <primitives/block.h>
|
||||
#include <primitives/transaction.h>
|
||||
#include <script/script.h>
|
||||
#include <sync.h>
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
#include <test/fuzz/fuzz.h>
|
||||
#include <test/fuzz/util.h>
|
||||
#include <test/util/mining.h>
|
||||
#include <test/util/random.h>
|
||||
#include <test/util/script.h>
|
||||
#include <test/util/setup_common.h>
|
||||
#include <test/util/time.h>
|
||||
#include <test/util/txmempool.h>
|
||||
#include <test/util/validation.h>
|
||||
#include <txmempool.h>
|
||||
#include <util/check.h>
|
||||
#include <util/time.h>
|
||||
#include <util/translation.h>
|
||||
#include <validation.h>
|
||||
#include <validationinterface.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace {
|
||||
|
||||
TestingSetup* g_setup;
|
||||
|
||||
void ResetChainmanAndMempool(TestingSetup& setup)
|
||||
{
|
||||
SetMockTime(Params().GenesisBlock().Time());
|
||||
|
||||
bilingual_str error{};
|
||||
setup.m_node.mempool.reset();
|
||||
setup.m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(setup.m_node), error);
|
||||
Assert(error.empty());
|
||||
|
||||
setup.m_node.chainman.reset();
|
||||
setup.m_make_chainman();
|
||||
setup.LoadVerifyActivateChainstate();
|
||||
|
||||
node::BlockAssembler::Options options;
|
||||
options.coinbase_output_script = P2WSH_OP_TRUE;
|
||||
options.include_dummy_extranonce = true;
|
||||
|
||||
for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
|
||||
MineBlock(setup.m_node, options);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void initialize_cmpctblock()
|
||||
{
|
||||
static const auto testing_setup = MakeNoLogFileContext<TestingSetup>();
|
||||
g_setup = testing_setup.get();
|
||||
ResetChainmanAndMempool(*g_setup);
|
||||
}
|
||||
|
||||
FUZZ_TARGET(cmpctblock, .init = initialize_cmpctblock)
|
||||
{
|
||||
SeedRandomStateForTest(SeedRand::ZEROS);
|
||||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
||||
|
||||
NodeClockContext clock_ctx{1610000000s};
|
||||
|
||||
auto setup = g_setup;
|
||||
auto& chainman = static_cast<TestChainstateManager&>(*setup->m_node.chainman);
|
||||
chainman.ResetIbd();
|
||||
chainman.DisableNextWrite();
|
||||
|
||||
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 1000)
|
||||
{
|
||||
CallOneOf(
|
||||
fuzzed_data_provider,
|
||||
[&]() {
|
||||
// Set mock time randomly or to tip's time.
|
||||
if (fuzzed_data_provider.ConsumeBool()) {
|
||||
clock_ctx.set(ConsumeTime(fuzzed_data_provider));
|
||||
} else {
|
||||
const NodeSeconds tip_time = WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip()->Time());
|
||||
clock_ctx.set(tip_time);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user