Bitcoin Core  26.1.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  FlushSendBuffer(node); // Drop the version message added by InitializeNode.
29 
30  CSerializedNetMsg msg_version{
31  mm.Make(NetMsgType::VERSION,
32  version, //
33  Using<CustomUintFormatter<8>>(remote_services), //
34  int64_t{}, // dummy time
35  int64_t{}, // ignored service bits
36  CNetAddr::V1(CService{}), // dummy
37  int64_t{}, // ignored service bits
38  CNetAddr::V1(CService{}), // ignored
39  uint64_t{1}, // dummy nonce
40  std::string{}, // dummy subver
41  int32_t{}, // dummy starting_height
42  relay_txs),
43  };
44 
45  (void)connman.ReceiveMsgFrom(node, std::move(msg_version));
46  node.fPauseSend = false;
47  connman.ProcessMessagesOnce(node);
48  peerman.SendMessages(&node);
49  FlushSendBuffer(node); // Drop the verack message added by SendMessages.
50  if (node.fDisconnect) return;
51  assert(node.nVersion == version);
52  assert(node.GetCommonVersion() == std::min(version, PROTOCOL_VERSION));
53  CNodeStateStats statestats;
54  assert(peerman.GetNodeStateStats(node.GetId(), statestats));
55  assert(statestats.m_relay_txs == (relay_txs && !node.IsBlockOnlyConn()));
56  assert(statestats.their_services == remote_services);
57  if (successfully_connected) {
58  CSerializedNetMsg msg_verack{mm.Make(NetMsgType::VERACK)};
59  (void)connman.ReceiveMsgFrom(node, std::move(msg_verack));
60  node.fPauseSend = false;
61  connman.ProcessMessagesOnce(node);
62  peerman.SendMessages(&node);
63  assert(node.fSuccessfullyConnected == true);
64  }
65 }
66 
67 void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const
68 {
69  assert(node.ReceiveMsgBytes(msg_bytes, complete));
70  if (complete) {
71  node.MarkReceivedMsgsForProcessing();
72  }
73 }
74 
76 {
77  LOCK(node.cs_vSend);
78  node.vSendMsg.clear();
79  node.m_send_memusage = 0;
80  while (true) {
81  const auto& [to_send, _more, _msg_type] = node.m_transport->GetBytesToSend(false);
82  if (to_send.empty()) break;
83  node.m_transport->MarkBytesSent(to_send.size());
84  }
85 }
86 
88 {
89  bool queued = node.m_transport->SetMessageToSend(ser_msg);
90  assert(queued);
91  bool complete{false};
92  while (true) {
93  const auto& [to_send, _more, _msg_type] = node.m_transport->GetBytesToSend(false);
94  if (to_send.empty()) break;
95  NodeReceiveMsgBytes(node, to_send, complete);
96  node.m_transport->MarkBytesSent(to_send.size());
97  }
98  return complete;
99 }
100 
101 std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context)
102 {
103  std::vector<NodeEvictionCandidate> candidates;
104  candidates.reserve(n_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 }
ServiceFlags
nServices flags
Definition: protocol.h:274
Inbound connections are those initiated by a peer.
assert(!tx.IsCoinBase())
void FlushSendBuffer(CNode &node) const
Definition: net.cpp:75
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class...
Definition: serialize.h:519
#define LOCK(cs)
Definition: sync.h:258
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:534
Fast randomness source.
Definition: random.h:143
static constexpr SerParams V1
Definition: netaddress.h:235
void NodeReceiveMsgBytes(CNode &node, Span< const uint8_t > msg_bytes, bool &complete) const
Definition: net.cpp:67
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &&ser_msg) const
Definition: net.cpp:87
Definition: init.h:25
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:16
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:15
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
const NodeId id
Definition: net.h:969
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:551
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:227
constexpr auto ALL_NETWORKS
Definition: net.h:93
std::vector< NodeEvictionCandidate > GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext &random_context)
Definition: net.cpp:101
A Span is an object that can refer to a contiguous sequence of objects.
Definition: solver.h:20
Information about a peer.
Definition: net.h:683
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:202