22 #include <validation.h>
27 #include <boost/test/unit_test.hpp>
36 BOOST_FIXTURE_TEST_SUITE(denialofservice_tests,
TestingSetup)
58 CNode dummyNode1{
id++,
88 LOCK(dummyNode1.cs_vSend);
90 dummyNode1.vSendMsg.clear();
98 LOCK(dummyNode1.cs_vSend);
112 vNodes.emplace_back(
new CNode{
id++,
125 node.fSuccessfullyConnected =
true;
143 const auto time_init{GetTime<std::chrono::seconds>()};
145 const auto time_later{time_init + 3 * std::chrono::seconds{
m_node.
chainman->GetConsensus().nPowTargetSpacing} + 1s};
146 connman->Init(options);
147 std::vector<CNode *> vNodes;
150 for (
int i = 0; i < max_outbound_full_relay; ++i) {
154 peerLogic->CheckForStaleTipAndEvictPeers();
165 peerLogic->CheckForStaleTipAndEvictPeers();
180 peerLogic->CheckForStaleTipAndEvictPeers();
181 for (
int i = 0; i < max_outbound_full_relay; ++i) {
187 vNodes.back()->fDisconnect =
false;
191 peerLogic->UpdateLastBlockAnnounceTime(vNodes.back()->GetId(),
GetTime());
193 peerLogic->CheckForStaleTipAndEvictPeers();
194 for (
int i = 0; i < max_outbound_full_relay - 1; ++i) {
197 BOOST_CHECK(vNodes[max_outbound_full_relay-1]->fDisconnect ==
true);
201 peerLogic->FinalizeNode(*
node);
204 connman->ClearTestNodes();
221 connman->Init(options);
222 std::vector<CNode*> vNodes;
225 for (
int i = 0; i < max_outbound_block_relay; ++i) {
228 peerLogic->CheckForStaleTipAndEvictPeers();
230 for (
int i = 0; i < max_outbound_block_relay; ++i) {
236 peerLogic->CheckForStaleTipAndEvictPeers();
239 for (
int i = 0; i < max_outbound_block_relay; ++i) {
245 peerLogic->CheckForStaleTipAndEvictPeers();
246 for (
int i = 0; i < max_outbound_block_relay; ++i) {
253 vNodes.back()->fDisconnect =
false;
254 vNodes.back()->m_last_block_time = GetTime<std::chrono::seconds>();
256 peerLogic->CheckForStaleTipAndEvictPeers();
257 for (
int i = 0; i < max_outbound_block_relay - 1; ++i) {
260 BOOST_CHECK(vNodes[max_outbound_block_relay - 1]->fDisconnect ==
true);
264 peerLogic->FinalizeNode(*
node);
266 connman->ClearTestNodes();
280 tor_netaddr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
289 std::array<CNode*, 3> nodes;
291 banman->ClearBanned();
293 nodes[0] =
new CNode{
id++,
304 nodes[0]->fSuccessfullyConnected =
true;
305 connman->AddTestNode(*nodes[0]);
313 nodes[1] =
new CNode{
id++,
324 nodes[1]->fSuccessfullyConnected =
true;
325 connman->AddTestNode(*nodes[1]);
334 peerLogic->UnitTestMisbehaving(nodes[1]->GetId(), 1);
344 nodes[2] =
new CNode{
id++,
355 nodes[2]->fSuccessfullyConnected =
true;
356 connman->AddTestNode(*nodes[2]);
367 peerLogic->FinalizeNode(*
node);
369 connman->ClearTestNodes();
381 banman->ClearBanned();
382 int64_t nStartTime =
GetTime();
387 CNode dummyNode{
id++,
398 dummyNode.fSuccessfullyConnected =
true;
404 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.
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
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(CNode &node, ServiceFlags our_services)=0
Initialize a peer (setup state, queue any initial messages)
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, bool ignore_incoming_txs)
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_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
static void AddRandomOutboundPeer(NodeId &id, std::vector< CNode * > &vNodes, PeerManager &peerLogic, ConnmanTestMsg &connman, ConnectionType connType)
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_FEELER_CONNECTIONS
Maximum number of feeler connections.
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.
static const int DISCOURAGEMENT_THRESHOLD
Threshold for marking a node to be discouraged, e.g.
#define BOOST_CHECK(expr)
ServiceFlags
nServices flags
FastRandomContext g_insecure_rand_ctx
This global and the helpers that use it are not thread-safe.
int m_max_outbound_block_relay
int m_max_outbound_full_relay
void AddTestNode(CNode &node)
void SetPeerConnectTimeout(std::chrono::seconds timeout)
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< PeerManager > peerman
std::unique_ptr< const NetGroupManager > netgroupman
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
void TestOnlyResetTimeData()
Reset the internal state of GetTimeOffset(), GetAdjustedTime() and AddTimeData().
static const int PROTOCOL_VERSION
network protocol versioning