Merge bitcoin/bitcoin#35141: fuzz: apply node context reset pattern to p2p_handshake

dfe5d6a81d fuzz: apply node context reset pattern to p2p_handshake (frankomosh)

Pull request description:

  Follow-up to #34302. Applies the node context reset pattern from fabf8d1 to `p2p_handshake`.

  Previous code pattern created local `AddrMan` and `node::Warnings` objects, and passed them to `PeerManager::make`. `connman` was left holding a dangling `reference_wrapper<AddrMan>` across iterations, since the local objects destruct at iteration end while `connman` is global.

  Like in fabf8d1 , reset and reinstall `node.addrman` and `node.peerman` on each iteration. This PR also removes `includes` made unused by this or prior refactors.

ACKs for top commit:
  nervana21:
    tACK dfe5d6a81d
  maflcko:
    review ACK dfe5d6a81d 🦏
  sedited:
    ACK dfe5d6a81d

Tree-SHA512: 141ddec03c6d37f76a3b2d94701d18c851e85ea74e57716abb69ecc955d30371e342c6e267d2669ad853fe2d95fb77dd2fb506e4233ae3a88501d59ee1bbae30
This commit is contained in:
merge-script
2026-05-23 13:11:28 +02:00

View File

@@ -2,19 +2,15 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <addrman.h>
#include <consensus/consensus.h>
#include <banman.h>
#include <net.h>
#include <net_processing.h>
#include <node/warnings.h>
#include <protocol.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/fuzz/util/net.h>
#include <test/util/mining.h>
#include <test/util/net.h>
#include <test/util/setup_common.h>
#include <test/util/time.h>
@@ -23,16 +19,15 @@
#include <validationinterface.h>
#include <ios>
#include <string>
#include <utility>
#include <vector>
namespace {
const TestingSetup* g_setup;
TestingSetup* g_setup;
void initialize()
{
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(
static const auto testing_setup = MakeNoLogFileContext<TestingSetup>(
/*chain_type=*/ChainType::REGTEST);
g_setup = testing_setup.get();
}
@@ -43,22 +38,26 @@ FUZZ_TARGET(p2p_handshake, .init = ::initialize)
SeedRandomStateForTest(SeedRand::ZEROS);
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
auto& connman = static_cast<ConnmanTestMsg&>(*g_setup->m_node.connman);
auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman);
auto& node{g_setup->m_node};
auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
NodeClockContext clock_ctx{1610000000s}; // any time to successfully reset ibd
chainman.ResetIbd();
node::Warnings warnings{};
auto netgroupman{NetGroupManager::NoAsmap()};
AddrMan addrman{netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0};
auto peerman = PeerManager::make(connman, addrman,
node.banman.reset();
node.addrman.reset();
node.peerman.reset();
node.addrman = std::make_unique<AddrMan>(
*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
node.peerman = PeerManager::make(connman, *node.addrman,
/*banman=*/nullptr, chainman,
*g_setup->m_node.mempool, warnings,
*node.mempool, *node.warnings,
PeerManager::Options{
.reconcile_txs = true,
.deterministic_rng = true,
});
connman.SetMsgProc(peerman.get());
connman.SetMsgProc(node.peerman.get());
connman.SetAddrman(*node.addrman);
LOCK(NetEventsInterface::g_msgproc_mutex);
@@ -67,7 +66,7 @@ FUZZ_TARGET(p2p_handshake, .init = ::initialize)
for (int i = 0; i < num_peers_to_add; ++i) {
peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
connman.AddTestNode(*peers.back());
peerman->InitializeNode(
node.peerman->InitializeNode(
*peers.back(),
static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>()));
}
@@ -102,9 +101,9 @@ FUZZ_TARGET(p2p_handshake, .init = ::initialize)
more_work = connman.ProcessMessagesOnce(connection);
} catch (const std::ios_base::failure&) {
}
peerman->SendMessages(connection);
node.peerman->SendMessages(connection);
}
}
g_setup->m_node.connman->StopNodes();
node.connman->StopNodes();
}