8#include <chainparams.h>
21#include <validation.h>
26#include <boost/test/unit_test.hpp>
57 CNode dummyNode1{
id++,
86 LOCK(dummyNode1.cs_vSend);
87 const auto& [to_send, _more, _msg_type] = dummyNode1.m_transport->GetBytesToSend(
false);
97 LOCK(dummyNode1.cs_vSend);
98 const auto& [to_send, _more, _msg_type] = dummyNode1.m_transport->GetBytesToSend(
false);
123 vNodes.emplace_back(
new CNode{
id++,
136 node.fSuccessfullyConnected =
true;
152 const auto time_init{GetTime<std::chrono::seconds>()};
154 const auto time_later{time_init + 3 * std::chrono::seconds{
m_node.
chainman->GetConsensus().nPowTargetSpacing} + 1
s};
155 connman->Init(options);
156 std::vector<CNode *> vNodes;
159 for (
int i = 0; i < max_outbound_full_relay; ++i) {
163 peerLogic->CheckForStaleTipAndEvictPeers();
174 peerLogic->CheckForStaleTipAndEvictPeers();
189 peerLogic->CheckForStaleTipAndEvictPeers();
190 for (
int i = 0; i < max_outbound_full_relay; ++i) {
196 vNodes.back()->fDisconnect =
false;
200 peerLogic->UpdateLastBlockAnnounceTime(vNodes.back()->GetId(),
GetTime());
202 peerLogic->CheckForStaleTipAndEvictPeers();
203 for (
int i = 0; i < max_outbound_full_relay - 1; ++i) {
206 BOOST_CHECK(vNodes[max_outbound_full_relay-1]->fDisconnect ==
true);
209 vNodes[max_outbound_full_relay - 1]->fDisconnect =
false;
216 peerLogic->CheckForStaleTipAndEvictPeers();
218 for (
int i = 0; i < max_outbound_full_relay - 2; ++i) {
221 BOOST_CHECK(vNodes[max_outbound_full_relay - 2]->fDisconnect ==
false);
222 BOOST_CHECK(vNodes[max_outbound_full_relay - 1]->fDisconnect ==
true);
223 BOOST_CHECK(vNodes[max_outbound_full_relay]->fDisconnect ==
false);
229 peerLogic->CheckForStaleTipAndEvictPeers();
234 peerLogic->FinalizeNode(*
node);
237 connman->ClearTestNodes();
251 connman->Init(options);
252 std::vector<CNode*> vNodes;
255 for (
int i = 0; i < max_outbound_block_relay; ++i) {
258 peerLogic->CheckForStaleTipAndEvictPeers();
260 for (
int i = 0; i < max_outbound_block_relay; ++i) {
266 peerLogic->CheckForStaleTipAndEvictPeers();
269 for (
int i = 0; i < max_outbound_block_relay; ++i) {
275 peerLogic->CheckForStaleTipAndEvictPeers();
276 for (
int i = 0; i < max_outbound_block_relay; ++i) {
283 vNodes.back()->fDisconnect =
false;
284 vNodes.back()->m_last_block_time = GetTime<std::chrono::seconds>();
286 peerLogic->CheckForStaleTipAndEvictPeers();
287 for (
int i = 0; i < max_outbound_block_relay - 1; ++i) {
290 BOOST_CHECK(vNodes[max_outbound_block_relay - 1]->fDisconnect ==
true);
294 peerLogic->FinalizeNode(*
node);
296 connman->ClearTestNodes();
309 tor_netaddr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
318 std::array<CNode*, 3> nodes;
320 banman->ClearBanned();
322 nodes[0] =
new CNode{
id++,
333 nodes[0]->fSuccessfullyConnected =
true;
334 connman->AddTestNode(*nodes[0]);
335 peerLogic->UnitTestMisbehaving(nodes[0]->GetId());
342 nodes[1] =
new CNode{
id++,
353 nodes[1]->fSuccessfullyConnected =
true;
354 connman->AddTestNode(*nodes[1]);
362 peerLogic->UnitTestMisbehaving(nodes[1]->GetId());
372 nodes[2] =
new CNode{
id++,
383 nodes[2]->fSuccessfullyConnected =
true;
384 connman->AddTestNode(*nodes[2]);
385 peerLogic->UnitTestMisbehaving(nodes[2]->GetId());
395 peerLogic->FinalizeNode(*
node);
397 connman->ClearTestNodes();
408 banman->ClearBanned();
409 int64_t nStartTime =
GetTime();
414 CNode dummyNode{
id++,
425 dummyNode.fSuccessfullyConnected =
true;
427 peerLogic->UnitTestMisbehaving(dummyNode.GetId());
431 peerLogic->FinalizeNode(dummyNode);
static constexpr unsigned int DEFAULT_MISBEHAVING_BANTIME
const CChainParams & Params()
Return the currently selected parameters.
A CService with information about it as peer.
uint16_t GetDefaultPort() const
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
Information about a peer.
A combination of a network address (CNetAddr) and a (TCP) port.
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
virtual bool SendMessages(CNode *pnode) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex)=0
Send queued protocol messages to a given node.
virtual void FinalizeNode(const CNode &node)=0
Handle removal of a peer (clear state)
virtual void InitializeNode(const CNode &node, ServiceFlags our_services)=0
Initialize a peer (setup state)
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
std::vector< B > randbytes(size_t len) noexcept
Generate random bytes.
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
ConnectionType
Different types of connections to a peer.
@ BLOCK_RELAY
We use block-relay-only connections to help prevent against partition attacks.
@ OUTBOUND_FULL_RELAY
These are the default connections that we use to connect with the network.
@ INBOUND
Inbound connections are those initiated by a peer.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
BOOST_FIXTURE_TEST_CASE(stale_tip_peer_management, OutboundTest)
BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
static CService ip(uint32_t i)
static const std::string addr1
static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS
The maximum number of peer connections to maintain.
static const int MAX_OUTBOUND_FULL_RELAY_CONNECTIONS
Maximum number of automatic outgoing nodes over which we'll relay everything (blocks,...
static const int MAX_BLOCK_RELAY_ONLY_CONNECTIONS
Maximum number of block-relay-only outgoing connections.
static constexpr auto MINIMUM_CONNECT_TIME
Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict.
std::string OnionToString(Span< const uint8_t > addr)
static constexpr size_t ADDR_TORV3_SIZE
Size of TORv3 address (in bytes).
#define BOOST_CHECK(expr)
ServiceFlags
nServices flags
static const int PROTOCOL_VERSION
network protocol versioning
int m_max_automatic_connections
void AddTestNode(CNode &node)
void FlushSendBuffer(CNode &node) const
void SetPeerConnectTimeout(std::chrono::seconds timeout)
void AddRandomOutboundPeer(NodeId &id, std::vector< CNode * > &vNodes, PeerManager &peerLogic, ConnmanTestMsg &connman, ConnectionType connType, bool onion_peer=false)
Testing setup that configures a complete environment.
std::unique_ptr< CConnman > connman
std::unique_ptr< CTxMemPool > mempool
std::unique_ptr< AddrMan > addrman
std::unique_ptr< ChainstateManager > chainman
std::unique_ptr< node::Warnings > warnings
Manages all the node warnings.
std::unique_ptr< PeerManager > peerman
std::unique_ptr< const NetGroupManager > netgroupman
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.