xrpld
Loading...
Searching...
No Matches
PeerGroup.h
1#pragma once
2
3#include <test/csf/Peer.h>
4#include <test/csf/random.h>
5
6#include <algorithm>
7#include <vector>
8
9namespace xrpl::test::csf {
10
20{
23
24public:
25 using iterator = peers_type::iterator;
26 using const_iterator = peers_type::const_iterator;
27 using reference = peers_type::reference;
28 using const_reference = peers_type::const_reference;
29
30 PeerGroup() = default;
31 PeerGroup(Peer* peer) : peers_{1, peer}
32 {
33 }
34 PeerGroup(std::vector<Peer*>&& peers) : peers_{std::move(peers)}
35 {
37 }
38 PeerGroup(std::vector<Peer*> const& peers) : peers_{peers}
39 {
41 }
42
43 PeerGroup(std::set<Peer*> const& peers) : peers_{peers.begin(), peers.end()}
44 {
45 }
46
49 {
50 return peers_.begin();
51 }
52
55 {
56 return peers_.end();
57 }
58
59 [[nodiscard]] const_iterator
60 begin() const
61 {
62 return peers_.begin();
63 }
64
65 [[nodiscard]] const_iterator
66 end() const
67 {
68 return peers_.end();
69 }
70
73 {
74 return peers_[i];
75 }
76
77 bool
78 contains(Peer const* p)
79 {
80 return std::ranges::find(peers_, p) != peers_.end();
81 }
82
83 bool
85 {
86 return std::ranges::find_if(peers_, [id](Peer const* p) { return p->id == id; }) !=
87 peers_.end();
88 }
89
90 [[nodiscard]] std::size_t
91 size() const
92 {
93 return peers_.size();
94 }
95
102 void
103 trust(PeerGroup const& o)
104 {
105 for (Peer* p : peers_)
106 {
107 for (Peer* target : o.peers_)
108 {
109 p->trust(*target);
110 }
111 }
112 }
113
120 void
122 {
123 for (Peer* p : peers_)
124 {
125 for (Peer* target : o.peers_)
126 {
127 p->untrust(*target);
128 }
129 }
130 }
131
142 void
144 {
145 for (Peer* p : peers_)
146 {
147 for (Peer* target : o.peers_)
148 {
149 // cannot send messages to self over network
150 if (p != target)
151 p->connect(*target, delay);
152 }
153 }
154 }
155
162 void
164 {
165 for (Peer* p : peers_)
166 {
167 for (Peer* target : o.peers_)
168 {
169 p->disconnect(*target);
170 }
171 }
172 }
173
182 void
184 {
185 trust(o);
186 connect(o, delay);
187 }
188
198 void
200 {
201 for (Peer* peer : peers_)
202 {
203 for (Peer* to : peer->trustGraph.trustedPeers(peer))
204 {
205 peer->connect(*to, delay);
206 }
207 }
208 }
209
210 // Union of PeerGroups
211 friend PeerGroup
212 operator+(PeerGroup const& a, PeerGroup const& b)
213 {
214 PeerGroup res;
216 return res;
217 }
218
219 // Set difference of PeerGroups
220 friend PeerGroup
221 operator-(PeerGroup const& a, PeerGroup const& b)
222 {
223 PeerGroup res;
224
226
227 return res;
228 }
229
231 operator<<(std::ostream& o, PeerGroup const& t)
232 {
233 o << "{";
234 bool first = true;
235 for (Peer const* p : t)
236 {
237 if (!first)
238 o << ", ";
239 first = false;
240 o << p->id;
241 }
242 o << "}";
243 return o;
244 }
245};
246
266template <class RandomNumberDistribution, class Generator>
269 PeerGroup& peers,
270 std::vector<double> const& ranks,
271 int numGroups,
272 RandomNumberDistribution sizeDist,
273 Generator& g)
274{
275 assert(peers.size() == ranks.size());
276
278 groups.reserve(numGroups);
279 std::vector<Peer*> rawPeers(peers.begin(), peers.end());
280 std::generate_n(std::back_inserter(groups), numGroups, [&]() {
281 std::vector<Peer*> res = randomWeightedShuffle(rawPeers, ranks, g);
282 res.resize(sizeDist(g));
283 return PeerGroup(std::move(res));
284 });
285
286 return groups;
287}
288
293template <class RandomNumberDistribution, class Generator>
294void
296 PeerGroup& peers,
297 std::vector<double> const& ranks,
298 int numGroups,
299 RandomNumberDistribution sizeDist,
300 Generator& g)
301{
302 std::vector<PeerGroup> const groups = randomRankedGroups(peers, ranks, numGroups, sizeDist, g);
303 std::uniform_int_distribution<int> u(0, groups.size() - 1); // NOLINT(misc-const-correctness)
304
305 for (auto& peer : peers)
306 {
307 for (auto& target : groups[u(g)])
308 peer->trust(*target);
309 }
310}
311
316template <class RandomNumberDistribution, class Generator>
317void
319 PeerGroup& peers,
320 std::vector<double> const& ranks,
321 int numGroups,
322 RandomNumberDistribution sizeDist,
323 Generator& g,
324 SimDuration delay)
325{
326 std::vector<PeerGroup> const groups = randomRankedGroups(peers, ranks, numGroups, sizeDist, g);
327 std::uniform_int_distribution<int> u(0, groups.size() - 1); // NOLINT(misc-const-correctness)
328
329 for (auto& peer : peers)
330 {
331 for (auto& target : groups[u(g)])
332 peer->connect(*target, delay);
333 }
334}
335
336} // namespace xrpl::test::csf
T back_inserter(T... args)
A group of simulation Peers.
Definition PeerGroup.h:20
peers_type::reference reference
Definition PeerGroup.h:27
peers_type::const_reference const_reference
Definition PeerGroup.h:28
friend PeerGroup operator+(PeerGroup const &a, PeerGroup const &b)
Definition PeerGroup.h:212
const_reference operator[](std::size_t i) const
Definition PeerGroup.h:72
bool contains(PeerID id)
Definition PeerGroup.h:84
void connectFromTrust(SimDuration delay)
Establish network connections based on trust relations.
Definition PeerGroup.h:199
void trust(PeerGroup const &o)
Establish trust.
Definition PeerGroup.h:103
void disconnect(PeerGroup const &o)
Destroy network connection.
Definition PeerGroup.h:163
void connect(PeerGroup const &o, SimDuration delay)
Establish network connection.
Definition PeerGroup.h:143
std::size_t size() const
Definition PeerGroup.h:91
friend std::ostream & operator<<(std::ostream &o, PeerGroup const &t)
Definition PeerGroup.h:231
std::vector< Peer * > peers_type
Definition PeerGroup.h:21
void trustAndConnect(PeerGroup const &o, SimDuration delay)
Establish trust and network connection.
Definition PeerGroup.h:183
peers_type::iterator iterator
Definition PeerGroup.h:25
PeerGroup(std::vector< Peer * > &&peers)
Definition PeerGroup.h:34
const_iterator end() const
Definition PeerGroup.h:66
bool contains(Peer const *p)
Definition PeerGroup.h:78
void untrust(PeerGroup const &o)
Revoke trust.
Definition PeerGroup.h:121
friend PeerGroup operator-(PeerGroup const &a, PeerGroup const &b)
Definition PeerGroup.h:221
const_iterator begin() const
Definition PeerGroup.h:60
PeerGroup(std::set< Peer * > const &peers)
Definition PeerGroup.h:43
peers_type::const_iterator const_iterator
Definition PeerGroup.h:26
PeerGroup(std::vector< Peer * > const &peers)
Definition PeerGroup.h:38
T find(T... args)
T generate_n(T... args)
STL namespace.
SimClock::duration SimDuration
Definition SimTime.h:14
TaggedInteger< std::uint32_t, PeerIDTag > PeerID
Definition Validation.h:15
std::vector< PeerGroup > randomRankedGroups(PeerGroup &peers, std::vector< double > const &ranks, int numGroups, RandomNumberDistribution sizeDist, Generator &g)
Randomly generate peer groups according to ranks.
Definition PeerGroup.h:268
void randomRankedConnect(PeerGroup &peers, std::vector< double > const &ranks, int numGroups, RandomNumberDistribution sizeDist, Generator &g, SimDuration delay)
Generate random network groups based on peer rankings.
Definition PeerGroup.h:318
void randomRankedTrust(PeerGroup &peers, std::vector< double > const &ranks, int numGroups, RandomNumberDistribution sizeDist, Generator &g)
Generate random trust groups based on peer rankings.
Definition PeerGroup.h:295
std::vector< T > randomWeightedShuffle(std::vector< T > v, std::vector< double > w, G &g)
Return a randomly shuffled copy of vector based on weights w.
Dir::ConstIterator const_iterator
Definition Dir.cpp:16
T reserve(T... args)
T resize(T... args)
T set_difference(T... args)
T set_union(T... args)
T size(T... args)
T sort(T... args)
A single peer in the simulation.
TrustGraph< Peer * > & trustGraph
Handle to Trust graph of network.
PeerID id
Our unique ID.