8#include <chainparams.h>
21#include <validation.h>
26#include <boost/test/unit_test.hpp>
57 CNode dummyNode1{
id++,
79 BOOST_CHECK(m_node.chainman->ActiveChain().Tip() !=
nullptr);
80 BOOST_CHECK(m_node.chainman->ActiveChain().Tip()->nChainWork > 0);
88 const auto& [to_send, _more, _msg_type] = dummyNode1.
m_transport->GetBytesToSend(
false);
99 const auto& [to_send, _more, _msg_type] = dummyNode1.
m_transport->GetBytesToSend(
false);
124 vNodes.emplace_back(
new CNode{
id++,
135 node.SetCommonVersion(PROTOCOL_VERSION);
138 node.fSuccessfullyConnected =
true;
147 auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman,
Params());
148 auto peerLogic =
PeerManager::make(*connman, *m_node.addrman,
nullptr, *m_node.chainman, *m_node.mempool, *m_node.warnings, {});
150 constexpr int max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS;
156 const auto time_later{time_init + 3 * std::chrono::seconds{m_node.chainman->GetConsensus().nPowTargetSpacing} + 1s};
157 connman->Init(options);
158 std::vector<CNode *> vNodes;
161 for (
int i = 0; i < max_outbound_full_relay; ++i) {
165 peerLogic->CheckForStaleTipAndEvictPeers();
176 peerLogic->CheckForStaleTipAndEvictPeers();
191 peerLogic->CheckForStaleTipAndEvictPeers();
192 for (
int i = 0; i < max_outbound_full_relay; ++i) {
198 vNodes.back()->fDisconnect =
false;
202 peerLogic->UpdateLastBlockAnnounceTime(vNodes.back()->GetId(),
GetTime());
204 peerLogic->CheckForStaleTipAndEvictPeers();
205 for (
int i = 0; i < max_outbound_full_relay - 1; ++i) {
208 BOOST_CHECK(vNodes[max_outbound_full_relay-1]->fDisconnect ==
true);
211 vNodes[max_outbound_full_relay - 1]->fDisconnect =
false;
218 peerLogic->CheckForStaleTipAndEvictPeers();
220 for (
int i = 0; i < max_outbound_full_relay - 2; ++i) {
223 BOOST_CHECK(vNodes[max_outbound_full_relay - 2]->fDisconnect ==
false);
224 BOOST_CHECK(vNodes[max_outbound_full_relay - 1]->fDisconnect ==
true);
225 BOOST_CHECK(vNodes[max_outbound_full_relay]->fDisconnect ==
false);
231 peerLogic->CheckForStaleTipAndEvictPeers();
236 peerLogic->FinalizeNode(*
node);
239 connman->ClearTestNodes();
245 auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman,
Params());
246 auto peerLogic =
PeerManager::make(*connman, *m_node.addrman,
nullptr, *m_node.chainman, *m_node.mempool, *m_node.warnings, {});
248 constexpr int max_outbound_block_relay{MAX_BLOCK_RELAY_ONLY_CONNECTIONS};
249 constexpr int64_t MINIMUM_CONNECT_TIME{30};
253 connman->Init(options);
254 std::vector<CNode*> vNodes;
257 for (
int i = 0; i < max_outbound_block_relay; ++i) {
260 peerLogic->CheckForStaleTipAndEvictPeers();
262 for (
int i = 0; i < max_outbound_block_relay; ++i) {
268 peerLogic->CheckForStaleTipAndEvictPeers();
271 for (
int i = 0; i < max_outbound_block_relay; ++i) {
277 peerLogic->CheckForStaleTipAndEvictPeers();
278 for (
int i = 0; i < max_outbound_block_relay; ++i) {
285 vNodes.back()->fDisconnect =
false;
288 peerLogic->CheckForStaleTipAndEvictPeers();
289 for (
int i = 0; i < max_outbound_block_relay - 1; ++i) {
292 BOOST_CHECK(vNodes[max_outbound_block_relay - 1]->fDisconnect ==
true);
296 peerLogic->FinalizeNode(*
node);
298 connman->ClearTestNodes();
305 auto banman = std::make_unique<BanMan>(m_args.GetDataDirBase() /
"banlist",
nullptr, DEFAULT_MISBEHAVING_BANTIME);
306 auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman,
Params());
307 auto peerLogic =
PeerManager::make(*connman, *m_node.addrman, banman.get(), *m_node.chainman, *m_node.mempool, *m_node.warnings, {});
311 tor_netaddr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
320 std::array<CNode*, 3> nodes;
322 banman->ClearBanned();
324 nodes[0] =
new CNode{
id++,
336 nodes[0]->fSuccessfullyConnected =
true;
337 connman->AddTestNode(*nodes[0]);
338 peerLogic->UnitTestMisbehaving(nodes[0]->GetId());
345 nodes[1] =
new CNode{
id++,
357 nodes[1]->fSuccessfullyConnected =
true;
358 connman->AddTestNode(*nodes[1]);
366 peerLogic->UnitTestMisbehaving(nodes[1]->GetId());
376 nodes[2] =
new CNode{
id++,
388 nodes[2]->fSuccessfullyConnected =
true;
389 connman->AddTestNode(*nodes[2]);
390 peerLogic->UnitTestMisbehaving(nodes[2]->GetId());
400 peerLogic->FinalizeNode(*
node);
402 connman->ClearTestNodes();
409 auto banman = std::make_unique<BanMan>(m_args.GetDataDirBase() /
"banlist",
nullptr, DEFAULT_MISBEHAVING_BANTIME);
410 auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman,
Params());
411 auto peerLogic =
PeerManager::make(*connman, *m_node.addrman, banman.get(), *m_node.chainman, *m_node.mempool, *m_node.warnings, {});
413 banman->ClearBanned();
414 int64_t nStartTime =
GetTime();
419 CNode dummyNode{
id++,
433 peerLogic->UnitTestMisbehaving(dummyNode.
GetId());
437 peerLogic->FinalizeNode(dummyNode);
BOOST_FIXTURE_TEST_CASE(util_CheckValue, CheckValueTest)
const CChainParams & Params()
Return the currently selected parameters.
A CService with information about it as peer.
uint16_t GetDefaultPort() const
bool SetSpecial(std::string_view addr)
Parse a Tor or I2P address and set this object to it.
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Information about a peer.
void SetCommonVersion(int greatest_common_version)
std::atomic_bool fSuccessfullyConnected
fSuccessfullyConnected is set to true on receiving VERACK from the peer.
const std::unique_ptr< Transport > m_transport
Transport serializer/deserializer.
std::atomic_bool fDisconnect
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 void FinalizeNode(const CNode &node)=0
Handle removal of a peer (clear state).
virtual bool SendMessages(CNode &node) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex)=0
Send queued protocol messages to a given node.
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()
static CService ip(uint32_t i)
BOOST_AUTO_TEST_CASE(ipc_tests)
std::string OnionToString(std::span< const uint8_t > addr)
#define BOOST_CHECK(expr)
ServiceFlags
nServices flags
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.
TestingSetup(ChainType chainType=ChainType::MAIN, TestOpts={})
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.