Bitcoin Core  22.99.0
P2P Digital Currency
net.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <test/util/net.h>
6 
7 #include <chainparams.h>
8 #include <net.h>
9 #include <span.h>
10 
11 #include <vector>
12 
13 void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const
14 {
15  assert(node.ReceiveMsgBytes(msg_bytes, complete));
16  if (complete) {
17  size_t nSizeAdded = 0;
18  auto it(node.vRecvMsg.begin());
19  for (; it != node.vRecvMsg.end(); ++it) {
20  // vRecvMsg contains only completed CNetMessage
21  // the single possible partially deserialized message are held by TransportDeserializer
22  nSizeAdded += it->m_raw_message_size;
23  }
24  {
25  LOCK(node.cs_vProcessMsg);
26  node.vProcessMsg.splice(node.vProcessMsg.end(), node.vRecvMsg, node.vRecvMsg.begin(), it);
27  node.nProcessQueueSize += nSizeAdded;
28  node.fPauseRecv = node.nProcessQueueSize > nReceiveFloodSize;
29  }
30  }
31 }
32 
34 {
35  std::vector<uint8_t> ser_msg_header;
36  node.m_serializer->prepareForTransport(ser_msg, ser_msg_header);
37 
38  bool complete;
39  NodeReceiveMsgBytes(node, ser_msg_header, complete);
40  NodeReceiveMsgBytes(node, ser_msg.data, complete);
41  return complete;
42 }
43 
44 std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context)
45 {
46  std::vector<NodeEvictionCandidate> candidates;
47  for (int id = 0; id < n_candidates; ++id) {
48  candidates.push_back({
49  /* id */ id,
50  /* nTimeConnected */ static_cast<int64_t>(random_context.randrange(100)),
51  /* m_min_ping_time */ std::chrono::microseconds{random_context.randrange(100)},
52  /* nLastBlockTime */ static_cast<int64_t>(random_context.randrange(100)),
53  /* nLastTXTime */ static_cast<int64_t>(random_context.randrange(100)),
54  /* fRelevantServices */ random_context.randbool(),
55  /* fRelayTxes */ random_context.randbool(),
56  /* fBloomFilter */ random_context.randbool(),
57  /* nKeyedNetGroup */ random_context.randrange(100),
58  /* prefer_evict */ random_context.randbool(),
59  /* m_is_local */ random_context.randbool(),
60  /* m_network */ ALL_NETWORKS[random_context.randrange(ALL_NETWORKS.size())],
61  });
62  }
63  return candidates;
64 }
assert
assert(!tx.IsCoinBase())
CConnman::nReceiveFloodSize
unsigned int nReceiveFloodSize
Definition: net.h:1047
CSerializedNetMsg::data
std::vector< unsigned char > data
Definition: net.h:109
FastRandomContext::randbool
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:211
CNode
Information about a peer.
Definition: net.h:393
chainparams.h
ConnmanTestMsg::ReceiveMsgFrom
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &ser_msg) const
Definition: net.cpp:33
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
ConnmanTestMsg::NodeReceiveMsgBytes
void NodeReceiveMsgBytes(CNode &node, Span< const uint8_t > msg_bytes, bool &complete) const
Definition: net.cpp:13
net.h
span.h
CSerializedNetMsg
Definition: net.h:100
id
static NodeId id
Definition: denialofservice_tests.cpp:37
GetRandomNodeEvictionCandidates
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
Definition: net.cpp:44
LOCK
#define LOCK(cs)
Definition: sync.h:226
FastRandomContext::randrange
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:190
node
Definition: interfaces.cpp:68
FastRandomContext
Fast randomness source.
Definition: random.h:119
ALL_NETWORKS
constexpr auto ALL_NETWORKS
Definition: net.h:72