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 
NodeReceiveMsgBytes(CNode & node,Span<const uint8_t> msg_bytes,bool & complete) const13 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 
ReceiveMsgFrom(CNode & node,CSerializedNetMsg & ser_msg) const33 bool ConnmanTestMsg::ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) const
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 
GetRandomNodeEvictionCandidates(int n_candidates,FastRandomContext & random_context)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 }
65