Bitcoin Core  28.1.0
P2P Digital Currency
peerman_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2024-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
4 
5 #include <chainparams.h>
6 #include <node/miner.h>
7 #include <net_processing.h>
8 #include <pow.h>
10 #include <validation.h>
11 
12 #include <boost/test/unit_test.hpp>
13 
14 BOOST_FIXTURE_TEST_SUITE(peerman_tests, RegTestingSetup)
15 
16 
17 static constexpr int64_t NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS = 144;
18 
19 static void mineBlock(const node::NodeContext& node, std::chrono::seconds block_time)
20 {
21  auto curr_time = GetTime<std::chrono::seconds>();
22  SetMockTime(block_time); // update time so the block is created with it
24  CBlock block = node::BlockAssembler{node.chainman->ActiveChainstate(), nullptr, options}.CreateNewBlock(CScript() << OP_TRUE)->block;
25  while (!CheckProofOfWork(block.GetHash(), block.nBits, node.chainman->GetConsensus())) ++block.nNonce;
26  block.fChecked = true; // little speedup
27  SetMockTime(curr_time); // process block at current time
28  Assert(node.chainman->ProcessNewBlock(std::make_shared<const CBlock>(block), /*force_processing=*/true, /*min_pow_checked=*/true, nullptr));
29  node.validation_signals->SyncWithValidationInterfaceQueue(); // drain events queue
30 }
31 
32 // Verifying when network-limited peer connections are desirable based on the node's proximity to the tip
33 BOOST_AUTO_TEST_CASE(connections_desirable_service_flags)
34 {
35  std::unique_ptr<PeerManager> peerman = PeerManager::make(*m_node.connman, *m_node.addrman, nullptr, *m_node.chainman, *m_node.mempool, *m_node.warnings, {});
36  auto consensus = m_node.chainman->GetParams().GetConsensus();
37 
38  // Check we start connecting to full nodes
40  BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK | NODE_WITNESS));
41 
42  // Make peerman aware of the initial best block and verify we accept limited peers when we start close to the tip time.
43  auto tip = WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Tip());
44  uint64_t tip_block_time = tip->GetBlockTime();
45  int tip_block_height = tip->nHeight;
46  peerman->SetBestBlock(tip_block_height, std::chrono::seconds{tip_block_time});
47 
48  SetMockTime(tip_block_time + 1); // Set node time to tip time
49  BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS));
50 
51  // Check we don't disallow limited peers connections when we are behind but still recoverable (below the connection safety window)
52  SetMockTime(GetTime<std::chrono::seconds>() + std::chrono::seconds{consensus.nPowTargetSpacing * (NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS - 1)});
53  BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS));
54 
55  // Check we disallow limited peers connections when we are further than the limited peers safety window
56  SetMockTime(GetTime<std::chrono::seconds>() + std::chrono::seconds{consensus.nPowTargetSpacing * 2});
57  BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK | NODE_WITNESS));
58 
59  // By now, we tested that the connections desirable services flags change based on the node's time proximity to the tip.
60  // Now, perform the same tests for when the node receives a block.
61  m_node.validation_signals->RegisterValidationInterface(peerman.get());
62 
63  // First, verify a block in the past doesn't enable limited peers connections
64  // At this point, our time is (NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS + 1) * 10 minutes ahead the tip's time.
65  mineBlock(m_node, /*block_time=*/std::chrono::seconds{tip_block_time + 1});
66  BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK | NODE_WITNESS));
67 
68  // Verify a block close to the tip enables limited peers connections
69  mineBlock(m_node, /*block_time=*/GetTime<std::chrono::seconds>());
70  BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS));
71 
72  // Lastly, verify the stale tip checks can disallow limited peers connections after not receiving blocks for a prolonged period.
73  SetMockTime(GetTime<std::chrono::seconds>() + std::chrono::seconds{consensus.nPowTargetSpacing * NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS + 1});
74  BOOST_CHECK(peerman->GetDesirableServiceFlags(peer_flags) == ServiceFlags(NODE_NETWORK | NODE_WITNESS));
75 }
76 
uint32_t nNonce
Definition: block.h:30
std::unique_ptr< node::Warnings > warnings
Manages all the node warnings.
Definition: context.h:88
ServiceFlags
nServices flags
Definition: protocol.h:309
Generate a new block, without valid proof-of-work.
Definition: miner.h:139
Definition: block.h:68
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
Definition: context.h:85
std::unique_ptr< AddrMan > addrman
Definition: context.h:63
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:65
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:32
Definition: script.h:83
BOOST_AUTO_TEST_CASE(connections_desirable_service_flags)
BOOST_AUTO_TEST_SUITE_END()
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:137
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:301
Definition: messages.h:20
uint256 GetHash() const
Definition: block.cpp:11
static constexpr int64_t NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS
Window, in blocks, for connecting to NODE_NETWORK_LIMITED peers.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:413
std::unique_ptr< CConnman > connman
Definition: context.h:64
static void mineBlock(const node::NodeContext &node, std::chrono::seconds block_time)
bool fChecked
Definition: block.h:75
Identical to TestingSetup, but chain set to regtest.
Definition: setup_common.h:103
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:69
#define Assert(val)
Identity function.
Definition: check.h:77
uint32_t nBits
Definition: block.h:29
#define BOOST_CHECK(expr)
Definition: object.cpp:17