Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2024-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or https://www.opensource.org/licenses/mit-license.php.5#include <chain.h>6#include <chainparams.h>7#include <consensus/params.h>8#include <interfaces/mining.h>9#include <net_processing.h>10#include <pow.h>11#include <primitives/block.h>12#include <protocol.h>13#include <sync.h>14#include <test/util/setup_common.h>15#include <test/util/time.h>16#include <util/check.h>17#include <validation.h>18#include <validationinterface.h>20#include <boost/test/unit_test.hpp>22#include <cstdint>23#include <memory>25BOOST_FIXTURE_TEST_SUITE(peerman_tests, RegTestingSetup)27/** Window, in blocks, for connecting to NODE_NETWORK_LIMITED peers */28static constexpr int64_t NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS = 144;30static void mineBlock(node::NodeContext& node, FakeNodeClock& clock, std::chrono::seconds block_time)31{32auto curr_time = GetTime<std::chrono::seconds>();33clock.set(block_time); // update time so the block is created with it34auto mining{interfaces::MakeMining(node)};35auto block_template{mining->createNewBlock({}, /*cooldown=*/false)};36BOOST_REQUIRE(block_template);37CBlock block{block_template->getBlock()};38while (!CheckProofOfWork(block.GetHash(), block.nBits, node.chainman->GetConsensus())) ++block.nNonce;39block.fChecked = true; // little speedup40clock.set(curr_time); // process block at current time41Assert(node.chainman->ProcessNewBlock(std::make_shared<const CBlock>(block), /*force_processing=*/true, /*min_pow_checked=*/true, nullptr));42node.validation_signals->SyncWithValidationInterfaceQueue(); // drain events queue43}45// Verifying when network-limited peer connections are desirable based on the node's proximity to the tip46BOOST_AUTO_TEST_CASE(connections_desirable_service_flags)47{48FakeNodeClock clock{};49std::unique_ptr<PeerManager> peerman = PeerManager::make(*m_node.connman, *m_node.addrman, nullptr, *m_node.chainman, *m_node.mempool, *m_node.warnings, {});50auto consensus = m_node.chainman->GetParams().GetConsensus();52// Check we start connecting to full nodes53ServiceFlags peer_flags{NODE_WITNESS | NODE_NETWORK_LIMITED};54BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK | NODE_WITNESS));56// Make peerman aware of the initial best block and verify we accept limited peers when we start close to the tip time.57auto tip = WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Tip());58uint64_t tip_block_time = tip->GetBlockTime();59int tip_block_height = tip->nHeight;60peerman->SetBestBlock(tip_block_height, std::chrono::seconds{tip_block_time});62clock.set(std::chrono::seconds{tip_block_time + 1}); // Set node time to tip time63BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS));65// Check we don't disallow limited peers connections when we are behind but still recoverable (below the connection safety window)66clock += std::chrono::seconds{consensus.nPowTargetSpacing * (NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS - 1)};67BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS));69// Check we disallow limited peers connections when we are further than the limited peers safety window70clock += std::chrono::seconds{consensus.nPowTargetSpacing * 2};71BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK | NODE_WITNESS));73// By now, we tested that the connections desirable services flags change based on the node's time proximity to the tip.74// Now, perform the same tests for when the node receives a block.75m_node.validation_signals->RegisterValidationInterface(peerman.get());77// First, verify a block in the past doesn't enable limited peers connections78// At this point, our time is (NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS + 1) * 10 minutes ahead the tip's time.79mineBlock(m_node, clock, /*block_time=*/std::chrono::seconds{tip_block_time + 1});80BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK | NODE_WITNESS));82// Verify a block close to the tip enables limited peers connections83mineBlock(m_node, clock, /*block_time=*/GetTime<std::chrono::seconds>());84BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS));86// Lastly, verify the stale tip checks can disallow limited peers connections after not receiving blocks for a prolonged period.87clock += std::chrono::seconds{consensus.nPowTargetSpacing * NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS + 1};88BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK | NODE_WITNESS));89}91BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.