Bitcoin Core  24.1.0
P2P Digital Currency
net.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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 
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  {
48  LOCK(node.cs_sendProcessing);
49  peerman.SendMessages(&node);
50  }
51  if (node.fDisconnect) return;
52  assert(node.nVersion == version);
53  assert(node.GetCommonVersion() == std::min(version, PROTOCOL_VERSION));
54  CNodeStateStats statestats;
55  assert(peerman.GetNodeStateStats(node.GetId(), statestats));
56  assert(statestats.m_relay_txs == (relay_txs && !node.IsBlockOnlyConn()));
57  assert(statestats.their_services == remote_services);
58  if (successfully_connected) {
59  CSerializedNetMsg msg_verack{mm.Make(NetMsgType::VERACK)};
60  (void)connman.ReceiveMsgFrom(node, msg_verack);
61  node.fPauseSend = false;
62  connman.ProcessMessagesOnce(node);
63  {
64  LOCK(node.cs_sendProcessing);
65  peerman.SendMessages(&node);
66  }
67  assert(node.fSuccessfullyConnected == true);
68  }
69 }
70 
71 void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const
72 {
73  assert(node.ReceiveMsgBytes(msg_bytes, complete));
74  if (complete) {
75  size_t nSizeAdded = 0;
76  auto it(node.vRecvMsg.begin());
77  for (; it != node.vRecvMsg.end(); ++it) {
78  // vRecvMsg contains only completed CNetMessage
79  // the single possible partially deserialized message are held by TransportDeserializer
80  nSizeAdded += it->m_raw_message_size;
81  }
82  {
83  LOCK(node.cs_vProcessMsg);
84  node.vProcessMsg.splice(node.vProcessMsg.end(), node.vRecvMsg, node.vRecvMsg.begin(), it);
85  node.nProcessQueueSize += nSizeAdded;
86  node.fPauseRecv = node.nProcessQueueSize > nReceiveFloodSize;
87  }
88  }
89 }
90 
92 {
93  std::vector<uint8_t> ser_msg_header;
94  node.m_serializer->prepareForTransport(ser_msg, ser_msg_header);
95 
96  bool complete;
97  NodeReceiveMsgBytes(node, ser_msg_header, complete);
98  NodeReceiveMsgBytes(node, ser_msg.data, complete);
99  return complete;
100 }
101 
102 std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context)
103 {
104  std::vector<NodeEvictionCandidate> candidates;
105  for (int id = 0; id < n_candidates; ++id) {
106  candidates.push_back({
107  /*id=*/id,
108  /*m_connected=*/std::chrono::seconds{random_context.randrange(100)},
109  /*m_min_ping_time=*/std::chrono::microseconds{random_context.randrange(100)},
110  /*m_last_block_time=*/std::chrono::seconds{random_context.randrange(100)},
111  /*m_last_tx_time=*/std::chrono::seconds{random_context.randrange(100)},
112  /*fRelevantServices=*/random_context.randbool(),
113  /*m_relay_txs=*/random_context.randbool(),
114  /*fBloomFilter=*/random_context.randbool(),
115  /*nKeyedNetGroup=*/random_context.randrange(100),
116  /*prefer_evict=*/random_context.randbool(),
117  /*m_is_local=*/random_context.randbool(),
118  /*m_network=*/ALL_NETWORKS[random_context.randrange(ALL_NETWORKS.size())],
119  /*m_noban=*/false,
120  /*m_conn_type=*/ConnectionType::INBOUND,
121  });
122  }
123  return candidates;
124 }
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &ser_msg) const
Definition: net.cpp:91
ServiceFlags
nServices flags
Definition: protocol.h:267
Inbound connections are those initiated by a peer.
assert(!tx.IsCoinBase())
std::vector< unsigned char > data
Definition: net.h:122
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
#define LOCK(cs)
Definition: sync.h:261
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:520
Fast randomness source.
Definition: random.h:142
void NodeReceiveMsgBytes(CNode &node, Span< const uint8_t > msg_bytes, bool &complete) const
Definition: net.cpp:71
Definition: init.h:25
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:14
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:13
void Handshake(CNode &node, bool successfully_connected, ServiceFlags remote_services, ServiceFlags local_services, int32_t version, bool relay_txs)
Definition: net.cpp:16
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:465
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:234
constexpr auto ALL_NETWORKS
Definition: net.h:87
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
Definition: net.cpp:102
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:96
Information about a peer.
Definition: net.h:347
nReceiveFloodSize
Definition: net.h:715
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:213