Skip to content

Commit 566357f

Browse files
jonatackvasild
andcommitted
refactor: move GetRandomNodeEvictionCandidates() to test utilities
Co-authored-by: Vasil Dimov <[email protected]>
1 parent 088b348 commit 566357f

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

src/test/net_peer_eviction_tests.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,6 @@
1717

1818
BOOST_FIXTURE_TEST_SUITE(net_peer_eviction_tests, BasicTestingSetup)
1919

20-
std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(const int n_candidates, FastRandomContext& random_context)
21-
{
22-
std::vector<NodeEvictionCandidate> candidates;
23-
for (int id = 0; id < n_candidates; ++id) {
24-
candidates.push_back({
25-
/* id */ id,
26-
/* nTimeConnected */ static_cast<int64_t>(random_context.randrange(100)),
27-
/* m_min_ping_time */ std::chrono::microseconds{random_context.randrange(100)},
28-
/* nLastBlockTime */ static_cast<int64_t>(random_context.randrange(100)),
29-
/* nLastTXTime */ static_cast<int64_t>(random_context.randrange(100)),
30-
/* fRelevantServices */ random_context.randbool(),
31-
/* fRelayTxes */ random_context.randbool(),
32-
/* fBloomFilter */ random_context.randbool(),
33-
/* nKeyedNetGroup */ random_context.randrange(100),
34-
/* prefer_evict */ random_context.randbool(),
35-
/* m_is_local */ random_context.randbool(),
36-
/* m_network */ ALL_NETWORKS[random_context.randrange(ALL_NETWORKS.size())],
37-
});
38-
}
39-
return candidates;
40-
}
41-
4220
// Create `num_peers` random nodes, apply setup function `candidate_setup_fn`,
4321
// call ProtectEvictionCandidatesByRatio() to apply protection logic, and then
4422
// return true if all of `protected_peer_ids` and none of `unprotected_peer_ids`

src/test/util/net.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
#include <chainparams.h>
88
#include <net.h>
9+
#include <span.h>
10+
11+
#include <vector>
912

1013
void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span<const uint8_t> msg_bytes, bool& complete) const
1114
{
@@ -37,3 +40,25 @@ bool ConnmanTestMsg::ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) con
3740
NodeReceiveMsgBytes(node, ser_msg.data, complete);
3841
return complete;
3942
}
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+
}

src/test/util/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,6 @@ class StaticContentsSock : public Sock
141141
mutable size_t m_consumed;
142142
};
143143

144+
std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context);
145+
144146
#endif // BITCOIN_TEST_UTIL_NET_H

0 commit comments

Comments
 (0)