8 #include <chainparams.h> 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);
89 const auto& [to_send, _more, _msg_type] = dummyNode1.m_transport->GetBytesToSend(
false);
99 LOCK(dummyNode1.cs_vSend);
100 const auto& [to_send, _more, _msg_type] = dummyNode1.m_transport->GetBytesToSend(
false);
124 vNodes.emplace_back(
new CNode{
id++,
137 node.fSuccessfullyConnected =
true;
154 const auto time_init{GetTime<std::chrono::seconds>()};
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();
255 connman->Init(options);
256 std::vector<CNode*> vNodes;
259 for (
int i = 0; i < max_outbound_block_relay; ++i) {
262 peerLogic->CheckForStaleTipAndEvictPeers();
264 for (
int i = 0; i < max_outbound_block_relay; ++i) {
270 peerLogic->CheckForStaleTipAndEvictPeers();
273 for (
int i = 0; i < max_outbound_block_relay; ++i) {
279 peerLogic->CheckForStaleTipAndEvictPeers();
280 for (
int i = 0; i < max_outbound_block_relay; ++i) {
287 vNodes.back()->fDisconnect =
false;
288 vNodes.back()->m_last_block_time = GetTime<std::chrono::seconds>();
290 peerLogic->CheckForStaleTipAndEvictPeers();
291 for (
int i = 0; i < max_outbound_block_relay - 1; ++i) {
294 BOOST_CHECK(vNodes[max_outbound_block_relay - 1]->fDisconnect ==
true);
298 peerLogic->FinalizeNode(*
node);
300 connman->ClearTestNodes();
313 tor_netaddr.SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
322 std::array<CNode*, 3> nodes;
324 banman->ClearBanned();
326 nodes[0] =
new CNode{
id++,
337 nodes[0]->fSuccessfullyConnected =
true;
338 connman->AddTestNode(*nodes[0]);
346 nodes[1] =
new CNode{
id++,
357 nodes[1]->fSuccessfullyConnected =
true;
358 connman->AddTestNode(*nodes[1]);
367 peerLogic->UnitTestMisbehaving(nodes[1]->GetId(), 1);
377 nodes[2] =
new CNode{
id++,
388 nodes[2]->fSuccessfullyConnected =
true;
389 connman->AddTestNode(*nodes[2]);
400 peerLogic->FinalizeNode(*
node);
402 connman->ClearTestNodes();
413 banman->ClearBanned();
414 int64_t nStartTime =
GetTime();
419 CNode dummyNode{
id++,
430 dummyNode.fSuccessfullyConnected =
true;
436 peerLogic->FinalizeNode(dummyNode);
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, Options opts)
int m_max_outbound_full_relay
static const int MAX_BLOCK_RELAY_ONLY_CONNECTIONS
Maximum number of block-relay-only outgoing connections.
void SetPeerConnectTimeout(std::chrono::seconds timeout)
BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
ServiceFlags
nServices flags
Inbound connections are those initiated by a peer.
int m_max_outbound_block_relay
static constexpr size_t ADDR_TORV3_SIZE
Size of TORv3 address (in bytes).
void TestOnlyResetTimeData()
Reset the internal state of GetTimeOffset(), GetAdjustedTime() and AddTimeData(). ...
std::unique_ptr< const NetGroupManager > netgroupman
void FlushSendBuffer(CNode &node) const
std::unique_ptr< AddrMan > addrman
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
These are the default connections that we use to connect with the network.
static const int MAX_FEELER_CONNECTIONS
Maximum number of feeler connections.
uint16_t GetDefaultPort() const
std::unique_ptr< CTxMemPool > mempool
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
virtual void InitializeNode(CNode &node, ServiceFlags our_services)=0
Initialize a peer (setup state, queue any initial messages)
static const std::string addr1
A combination of a network address (CNetAddr) and a (TCP) port.
BOOST_AUTO_TEST_SUITE_END()
virtual void FinalizeNode(const CNode &node)=0
Handle removal of a peer (clear state)
A CService with information about it as peer.
static CService ip(uint32_t i)
static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS
The maximum number of peer connections to maintain.
static const int DISCOURAGEMENT_THRESHOLD
Threshold for marking a node to be discouraged, e.g.
static void AddRandomOutboundPeer(NodeId &id, std::vector< CNode *> &vNodes, PeerManager &peerLogic, ConnmanTestMsg &connman, ConnectionType connType, bool onion_peer=false)
std::vector< B > randbytes(size_t len)
Generate random bytes.
static constexpr unsigned int DEFAULT_MISBEHAVING_BANTIME
const CChainParams & Params()
Return the currently selected parameters.
static const int PROTOCOL_VERSION
network protocol versioning
FastRandomContext g_insecure_rand_ctx
This global and the helpers that use it are not thread-safe.
ConnectionType
Different types of connections to a peer.
std::string OnionToString(Span< const uint8_t > addr)
static const int MAX_OUTBOUND_FULL_RELAY_CONNECTIONS
Maximum number of automatic outgoing nodes over which we'll relay everything (blocks, tx, addrs, etc)
std::unique_ptr< CConnman > connman
Information about a peer.
static constexpr auto MINIMUM_CONNECT_TIME
Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict...
virtual bool SendMessages(CNode *pnode) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex)=0
Send queued protocol messages to a given node.
int64_t GetTime()
DEPRECATED, see GetTime.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
void AddTestNode(CNode &node)
std::unique_ptr< ChainstateManager > chainman
Testing setup that configures a complete environment.
We use block-relay-only connections to help prevent against partition attacks.
#define BOOST_CHECK(expr)
std::unique_ptr< PeerManager > peerman