Bitcoin Core  24.99.0
P2P Digital Currency
net.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2022 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 <node/eviction.h>
9 #include <net.h>
10 #include <net_processing.h>
11 #include <netmessagemaker.h>
12 #include <span.h>
13 
14 #include <vector>
15 
16 void ConnmanTestMsg::Handshake(CNode& node,
17  bool successfully_connected,
18  ServiceFlags remote_services,
19  ServiceFlags local_services,
20  int32_t version,
21  bool relay_txs)
22 {
23  auto& peerman{static_cast<PeerManager&>(*m_msgproc)};
24  auto& connman{*this};
25  const CNetMsgMaker mm{0};
26 
27  peerman.InitializeNode(node, local_services);
28 
29  CSerializedNetMsg msg_version{
30  mm.Make(NetMsgType::VERSION,
31  version, //
32  Using<CustomUintFormatter<8>>(remote_services), //
33  int64_t{}, // dummy time
34  int64_t{}, // ignored service bits
35  CService{}, // dummy
36  int64_t{}, // ignored service bits
37  CService{}, // ignored
38  uint64_t{1}, // dummy nonce
39  std::string{}, // dummy subver
40  int32_t{}, // dummy starting_height
41  relay_txs),
42  };
43 
44  (void)connman.ReceiveMsgFrom(node, msg_version);
45  node.fPauseSend = false;
46  connman.ProcessMessagesOnce(node);
47  peerman.SendMessages(&node);
48  if (node.fDisconnect) return;
49  assert(node.nVersion == version);
50  assert(node.GetCommonVersion() == std::min(version, PROTOCOL_VERSION));
51  CNodeStateStats statestats;
52  assert(peerman.GetNodeStateStats(node.GetId(), statestats));
53  assert(statestats.m_relay_txs == (relay_txs && !node.IsBlockOnlyConn()));
54  assert(statestats.their_services == remote_services);
55  if (successfully_connected) {
56  CSerializedNetMsg msg_verack{mm.Make(NetMsgType::VERACK)};
57  (void)connman.ReceiveMsgFrom(node, msg_verack);
58  node.fPauseSend = false;
59  connman.ProcessMessagesOnce(node);
60  peerman.SendMessages(&node);
61  assert(node.fSuccessfullyConnected == true);
62  }
63 }
64 
65 void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const
66 {
67  assert(node.ReceiveMsgBytes(msg_bytes, complete));
68  if (complete) {
69  size_t nSizeAdded = 0;
70  for (const auto& msg : node.vRecvMsg) {
71  // vRecvMsg contains only completed CNetMessage
72  // the single possible partially deserialized message are held by TransportDeserializer
73  nSizeAdded += msg.m_raw_message_size;
74  }
75  {
76  LOCK(node.cs_vProcessMsg);
77  node.vProcessMsg.splice(node.vProcessMsg.end(), node.vRecvMsg);
78  node.nProcessQueueSize += nSizeAdded;
79  node.fPauseRecv = node.nProcessQueueSize > nReceiveFloodSize;
80  }
81  }
82 }
83 
85 {
86  std::vector<uint8_t> ser_msg_header;
87  node.m_serializer->prepareForTransport(ser_msg, ser_msg_header);
88 
89  bool complete;
90  NodeReceiveMsgBytes(node, ser_msg_header, complete);
91  NodeReceiveMsgBytes(node, ser_msg.data, complete);
92  return complete;
93 }
94 
95 std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context)
96 {
97  std::vector<NodeEvictionCandidate> candidates;
98  for (int id = 0; id < n_candidates; ++id) {
99  candidates.push_back({
100  /*id=*/id,
101  /*m_connected=*/std::chrono::seconds{random_context.randrange(100)},
102  /*m_min_ping_time=*/std::chrono::microseconds{random_context.randrange(100)},
103  /*m_last_block_time=*/std::chrono::seconds{random_context.randrange(100)},
104  /*m_last_tx_time=*/std::chrono::seconds{random_context.randrange(100)},
105  /*fRelevantServices=*/random_context.randbool(),
106  /*m_relay_txs=*/random_context.randbool(),
107  /*fBloomFilter=*/random_context.randbool(),
108  /*nKeyedNetGroup=*/random_context.randrange(100),
109  /*prefer_evict=*/random_context.randbool(),
110  /*m_is_local=*/random_context.randbool(),
111  /*m_network=*/ALL_NETWORKS[random_context.randrange(ALL_NETWORKS.size())],
112  /*m_noban=*/false,
113  /*m_conn_type=*/ConnectionType::INBOUND,
114  });
115  }
116  return candidates;
117 }
nReceiveFloodSize
Definition: net.h:714
Information about a peer.
Definition: net.h:348
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
Fast randomness source.
Definition: random.h:143
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:234
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:213
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:97
@ INBOUND
Inbound connections are those initiated by a peer.
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:13
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:14
Definition: init.h:25
ServiceFlags
nServices flags
Definition: protocol.h:273
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:433
ServiceFlags their_services
std::vector< unsigned char > data
Definition: net.h:122
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &ser_msg) const
Definition: net.cpp:84
void NodeReceiveMsgBytes(CNode &node, Span< const uint8_t > msg_bytes, bool &complete) const
Definition: net.cpp:65
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:466
#define LOCK(cs)
Definition: sync.h:258
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
Definition: net.cpp:95
constexpr auto ALL_NETWORKS
Definition: net.h:88
assert(!tx.IsCoinBase())
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12