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) { return p->id == id; }) != peers_.end();
89 }
90
92 size() const
93 {
94 return peers_.size();
95 }
96
103 void
104 trust(PeerGroup const& o)
105 {
106 for (Peer* p : peers_)
107 {
108 for (Peer* target : o.peers_)
109 {
110 p->trust(*target);
111 }
112 }
113 }
114
121 void
123 {
124 for (Peer* p : peers_)
125 {
126 for (Peer* target : o.peers_)
127 {
128 p->untrust(*target);
129 }
130 }
131 }
132
143 void
145 {
146 for (Peer* p : peers_)
147 {
148 for (Peer* target : o.peers_)
149 {
150 // cannot send messages to self over network
151 if (p != target)
152 p->connect(*target, delay);
153 }
154 }
155 }
156
163 void
165 {
166 for (Peer* p : peers_)
167 {
168 for (Peer* target : o.peers_)
169 {
170 p->disconnect(*target);
171 }
172 }
173 }
174
183 void
185 {
186 trust(o);
187 connect(o, delay);
188 }
189
199 void
201 {
202 for (Peer* peer : peers_)
203 {
204 for (Peer* to : peer->trustGraph.trustedPeers(peer))
205 {
206 peer->connect(*to, delay);
207 }
208 }
209 }
210
211 // Union of PeerGroups
212 friend PeerGroup
213 operator+(PeerGroup const& a, PeerGroup const& b)
214 {
215 PeerGroup res;
218 return res;
219 }
220
221 // Set difference of PeerGroups
222 friend PeerGroup
223 operator-(PeerGroup const& a, PeerGroup const& b)
224 {
225 PeerGroup res;
226
229
230 return res;
231 }
232
234 operator<<(std::ostream& o, PeerGroup const& t)
235 {
236 o << "{";
237 bool first = true;
238 for (Peer const* p : t)
239 {
240 if (!first)
241 o << ", ";
242 first = false;
243 o << p->id;
244 }
245 o << "}";
246 return o;
247 }
248};
249
269template <class RandomNumberDistribution, class Generator>
272 PeerGroup& peers,
273 std::vector<double> const& ranks,
274 int numGroups,
275 RandomNumberDistribution sizeDist,
276 Generator& g)
277{
278 assert(peers.size() == ranks.size());
279
281 groups.reserve(numGroups);
282 std::vector<Peer*> rawPeers(peers.begin(), peers.end());
283 std::generate_n(std::back_inserter(groups), numGroups, [&]() {
284 std::vector<Peer*> res = random_weighted_shuffle(rawPeers, ranks, g);
285 res.resize(sizeDist(g));
286 return PeerGroup(std::move(res));
287 });
288
289 return groups;
290}
291
296template <class RandomNumberDistribution, class Generator>
297void
299 PeerGroup& peers,
300 std::vector<double> const& ranks,
301 int numGroups,
302 RandomNumberDistribution sizeDist,
303 Generator& g)
304{
305 std::vector<PeerGroup> const groups = randomRankedGroups(peers, ranks, numGroups, sizeDist, g);
306
307 std::uniform_int_distribution<int> u(0, groups.size() - 1);
308 for (auto& peer : peers)
309 {
310 for (auto& target : groups[u(g)])
311 peer->trust(*target);
312 }
313}
314
319template <class RandomNumberDistribution, class Generator>
320void
322 PeerGroup& peers,
323 std::vector<double> const& ranks,
324 int numGroups,
325 RandomNumberDistribution sizeDist,
326 Generator& g,
327 SimDuration delay)
328{
329 std::vector<PeerGroup> const groups = randomRankedGroups(peers, ranks, numGroups, sizeDist, g);
330
331 std::uniform_int_distribution<int> u(0, groups.size() - 1);
332 for (auto& peer : peers)
333 {
334 for (auto& target : groups[u(g)])
335 peer->connect(*target, delay);
336 }
337}
338
339} // namespace csf
340} // namespace test
341} // 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:213
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:200
void trust(PeerGroup const &o)
Establish trust.
Definition PeerGroup.h:104
void disconnect(PeerGroup const &o)
Destroy network connection.
Definition PeerGroup.h:164
void connect(PeerGroup const &o, SimDuration delay)
Establish network connection.
Definition PeerGroup.h:144
std::size_t size() const
Definition PeerGroup.h:92
friend std::ostream & operator<<(std::ostream &o, PeerGroup const &t)
Definition PeerGroup.h:234
void trustAndConnect(PeerGroup const &o, SimDuration delay)
Establish trust and network connection.
Definition PeerGroup.h:184
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:122
friend PeerGroup operator-(PeerGroup const &a, PeerGroup const &b)
Definition PeerGroup.h:223
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:271
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:321
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:298
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.