rippled
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 {
10namespace test {
11namespace csf {
12
22{
25
26public:
27 using iterator = peers_type::iterator;
28 using const_iterator = peers_type::const_iterator;
29 using reference = peers_type::reference;
30 using const_reference = peers_type::const_reference;
31
32 PeerGroup() = default;
33 PeerGroup(Peer* peer) : peers_{1, peer}
34 {
35 }
36 PeerGroup(std::vector<Peer*>&& peers) : peers_{std::move(peers)}
37 {
39 }
40 PeerGroup(std::vector<Peer*> const& peers) : peers_{peers}
41 {
43 }
44
45 PeerGroup(std::set<Peer*> const& peers) : peers_{peers.begin(), peers.end()}
46 {
47 }
48
51 {
52 return peers_.begin();
53 }
54
57 {
58 return peers_.end();
59 }
60
62 begin() const
63 {
64 return peers_.begin();
65 }
66
68 end() const
69 {
70 return peers_.end();
71 }
72
75 {
76 return peers_[i];
77 }
78
79 bool
80 contains(Peer const* p)
81 {
82 return std::find(peers_.begin(), peers_.end(), p) != peers_.end();
83 }
84
85 bool
87 {
88 return std::find_if(peers_.begin(), peers_.end(), [id](Peer const* p) {
89 return p->id == id;
90 }) != peers_.end();
91 }
92
94 size() const
95 {
96 return peers_.size();
97 }
98
105 void
106 trust(PeerGroup const& o)
107 {
108 for (Peer* p : peers_)
109 {
110 for (Peer* target : o.peers_)
111 {
112 p->trust(*target);
113 }
114 }
115 }
116
123 void
125 {
126 for (Peer* p : peers_)
127 {
128 for (Peer* target : o.peers_)
129 {
130 p->untrust(*target);
131 }
132 }
133 }
134
145 void
147 {
148 for (Peer* p : peers_)
149 {
150 for (Peer* target : o.peers_)
151 {
152 // cannot send messages to self over network
153 if (p != target)
154 p->connect(*target, delay);
155 }
156 }
157 }
158
165 void
167 {
168 for (Peer* p : peers_)
169 {
170 for (Peer* target : o.peers_)
171 {
172 p->disconnect(*target);
173 }
174 }
175 }
176
185 void
187 {
188 trust(o);
189 connect(o, delay);
190 }
191
201 void
203 {
204 for (Peer* peer : peers_)
205 {
206 for (Peer* to : peer->trustGraph.trustedPeers(peer))
207 {
208 peer->connect(*to, delay);
209 }
210 }
211 }
212
213 // Union of PeerGroups
214 friend PeerGroup
215 operator+(PeerGroup const& a, PeerGroup const& b)
216 {
217 PeerGroup res;
219 a.peers_.begin(),
220 a.peers_.end(),
221 b.peers_.begin(),
222 b.peers_.end(),
224 return res;
225 }
226
227 // Set difference of PeerGroups
228 friend PeerGroup
229 operator-(PeerGroup const& a, PeerGroup const& b)
230 {
231 PeerGroup res;
232
234 a.peers_.begin(),
235 a.peers_.end(),
236 b.peers_.begin(),
237 b.peers_.end(),
239
240 return res;
241 }
242
244 operator<<(std::ostream& o, PeerGroup const& t)
245 {
246 o << "{";
247 bool first = true;
248 for (Peer const* p : t)
249 {
250 if (!first)
251 o << ", ";
252 first = false;
253 o << p->id;
254 }
255 o << "}";
256 return o;
257 }
258};
259
279template <class RandomNumberDistribution, class Generator>
282 PeerGroup& peers,
283 std::vector<double> const& ranks,
284 int numGroups,
285 RandomNumberDistribution sizeDist,
286 Generator& g)
287{
288 assert(peers.size() == ranks.size());
289
291 groups.reserve(numGroups);
292 std::vector<Peer*> rawPeers(peers.begin(), peers.end());
293 std::generate_n(std::back_inserter(groups), numGroups, [&]() {
294 std::vector<Peer*> res = random_weighted_shuffle(rawPeers, ranks, g);
295 res.resize(sizeDist(g));
296 return PeerGroup(std::move(res));
297 });
298
299 return groups;
300}
301
306template <class RandomNumberDistribution, class Generator>
307void
309 PeerGroup& peers,
310 std::vector<double> const& ranks,
311 int numGroups,
312 RandomNumberDistribution sizeDist,
313 Generator& g)
314{
315 std::vector<PeerGroup> const groups = randomRankedGroups(peers, ranks, numGroups, sizeDist, g);
316 std::uniform_int_distribution<int> u(0, groups.size() - 1); // NOLINT(misc-const-correctness)
317
318 for (auto& peer : peers)
319 {
320 for (auto& target : groups[u(g)])
321 peer->trust(*target);
322 }
323}
324
329template <class RandomNumberDistribution, class Generator>
330void
332 PeerGroup& peers,
333 std::vector<double> const& ranks,
334 int numGroups,
335 RandomNumberDistribution sizeDist,
336 Generator& g,
337 SimDuration delay)
338{
339 std::vector<PeerGroup> const groups = randomRankedGroups(peers, ranks, numGroups, sizeDist, g);
340 std::uniform_int_distribution<int> u(0, groups.size() - 1); // NOLINT(misc-const-correctness)
341
342 for (auto& peer : peers)
343 {
344 for (auto& target : groups[u(g)])
345 peer->connect(*target, delay);
346 }
347}
348
349} // namespace csf
350} // namespace test
351} // namespace xrpl
T back_inserter(T... args)
T begin(T... args)
A group of simulation Peers.
Definition PeerGroup.h:22
peers_type::reference reference
Definition PeerGroup.h:29
peers_type::const_reference const_reference
Definition PeerGroup.h:30
friend PeerGroup operator+(PeerGroup const &a, PeerGroup const &b)
Definition PeerGroup.h:215
const_reference operator[](std::size_t i) const
Definition PeerGroup.h:74
bool contains(PeerID id)
Definition PeerGroup.h:86
void connectFromTrust(SimDuration delay)
Establish network connections based on trust relations.
Definition PeerGroup.h:202
void trust(PeerGroup const &o)
Establish trust.
Definition PeerGroup.h:106
void disconnect(PeerGroup const &o)
Destroy network connection.
Definition PeerGroup.h:166
void connect(PeerGroup const &o, SimDuration delay)
Establish network connection.
Definition PeerGroup.h:146
std::size_t size() const
Definition PeerGroup.h:94
friend std::ostream & operator<<(std::ostream &o, PeerGroup const &t)
Definition PeerGroup.h:244
void trustAndConnect(PeerGroup const &o, SimDuration delay)
Establish trust and network connection.
Definition PeerGroup.h:186
peers_type::iterator iterator
Definition PeerGroup.h:27
PeerGroup(std::vector< Peer * > &&peers)
Definition PeerGroup.h:36
const_iterator end() const
Definition PeerGroup.h:68
bool contains(Peer const *p)
Definition PeerGroup.h:80
void untrust(PeerGroup const &o)
Revoke trust.
Definition PeerGroup.h:124
friend PeerGroup operator-(PeerGroup const &a, PeerGroup const &b)
Definition PeerGroup.h:229
const_iterator begin() const
Definition PeerGroup.h:62
PeerGroup(std::set< Peer * > const &peers)
Definition PeerGroup.h:45
peers_type::const_iterator const_iterator
Definition PeerGroup.h:28
PeerGroup(std::vector< Peer * > const &peers)
Definition PeerGroup.h:40
T end(T... args)
T find(T... args)
T generate_n(T... args)
STL namespace.
typename SimClock::duration SimDuration
Definition SimTime.h:16
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:281
std::vector< T > random_weighted_shuffle(std::vector< T > v, std::vector< double > w, G &g)
Return a randomly shuffled copy of vector based on weights w.
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:331
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:308
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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.