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