rippled
Loading...
Searching...
No Matches
PeerGroup.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2017 Ripple Labs Inc
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19#ifndef RIPPLE_TEST_CSF_PEERGROUP_H_INCLUDED
20#define RIPPLE_TEST_CSF_PEERGROUP_H_INCLUDED
21
22#include <test/csf/Peer.h>
23#include <test/csf/random.h>
24#include <algorithm>
25#include <vector>
26
27namespace ripple {
28namespace test {
29namespace csf {
30
40{
43
44public:
45 using iterator = peers_type::iterator;
46 using const_iterator = peers_type::const_iterator;
47 using reference = peers_type::reference;
48 using const_reference = peers_type::const_reference;
49
50 PeerGroup() = default;
51 PeerGroup(Peer* peer) : peers_{1, peer}
52 {
53 }
54 PeerGroup(std::vector<Peer*>&& peers) : peers_{std::move(peers)}
55 {
57 }
58 PeerGroup(std::vector<Peer*> const& peers) : peers_{peers}
59 {
61 }
62
63 PeerGroup(std::set<Peer*> const& peers) : peers_{peers.begin(), peers.end()}
64 {
65 }
66
69 {
70 return peers_.begin();
71 }
72
75 {
76 return peers_.end();
77 }
78
80 begin() const
81 {
82 return peers_.begin();
83 }
84
86 end() const
87 {
88 return peers_.end();
89 }
90
93 {
94 return peers_[i];
95 }
96
97 bool
98 contains(Peer const* p)
99 {
100 return std::find(peers_.begin(), peers_.end(), p) != peers_.end();
101 }
102
103 bool
105 {
106 return std::find_if(peers_.begin(), peers_.end(), [id](Peer const* p) {
107 return p->id == id;
108 }) != peers_.end();
109 }
110
112 size() const
113 {
114 return peers_.size();
115 }
116
123 void
124 trust(PeerGroup const& o)
125 {
126 for (Peer* p : peers_)
127 {
128 for (Peer* target : o.peers_)
129 {
130 p->trust(*target);
131 }
132 }
133 }
134
141 void
143 {
144 for (Peer* p : peers_)
145 {
146 for (Peer* target : o.peers_)
147 {
148 p->untrust(*target);
149 }
150 }
151 }
152
163 void
165 {
166 for (Peer* p : peers_)
167 {
168 for (Peer* target : o.peers_)
169 {
170 // cannot send messages to self over network
171 if (p != target)
172 p->connect(*target, delay);
173 }
174 }
175 }
176
183 void
185 {
186 for (Peer* p : peers_)
187 {
188 for (Peer* target : o.peers_)
189 {
190 p->disconnect(*target);
191 }
192 }
193 }
194
203 void
205 {
206 trust(o);
207 connect(o, delay);
208 }
209
219 void
221 {
222 for (Peer* peer : peers_)
223 {
224 for (Peer* to : peer->trustGraph.trustedPeers(peer))
225 {
226 peer->connect(*to, delay);
227 }
228 }
229 }
230
231 // Union of PeerGroups
232 friend PeerGroup
233 operator+(PeerGroup const& a, PeerGroup const& b)
234 {
235 PeerGroup res;
237 a.peers_.begin(),
238 a.peers_.end(),
239 b.peers_.begin(),
240 b.peers_.end(),
242 return res;
243 }
244
245 // Set difference of PeerGroups
246 friend PeerGroup
247 operator-(PeerGroup const& a, PeerGroup const& b)
248 {
249 PeerGroup res;
250
252 a.peers_.begin(),
253 a.peers_.end(),
254 b.peers_.begin(),
255 b.peers_.end(),
257
258 return res;
259 }
260
262 operator<<(std::ostream& o, PeerGroup const& t)
263 {
264 o << "{";
265 bool first = true;
266 for (Peer const* p : t)
267 {
268 if (!first)
269 o << ", ";
270 first = false;
271 o << p->id;
272 }
273 o << "}";
274 return o;
275 }
276};
277
297template <class RandomNumberDistribution, class Generator>
300 PeerGroup& peers,
301 std::vector<double> const& ranks,
302 int numGroups,
303 RandomNumberDistribution sizeDist,
304 Generator& g)
305{
306 assert(peers.size() == ranks.size());
307
309 groups.reserve(numGroups);
310 std::vector<Peer*> rawPeers(peers.begin(), peers.end());
311 std::generate_n(std::back_inserter(groups), numGroups, [&]() {
312 std::vector<Peer*> res = random_weighted_shuffle(rawPeers, ranks, g);
313 res.resize(sizeDist(g));
314 return PeerGroup(std::move(res));
315 });
316
317 return groups;
318}
319
324template <class RandomNumberDistribution, class Generator>
325void
327 PeerGroup& peers,
328 std::vector<double> const& ranks,
329 int numGroups,
330 RandomNumberDistribution sizeDist,
331 Generator& g)
332{
333 std::vector<PeerGroup> const groups =
334 randomRankedGroups(peers, ranks, numGroups, sizeDist, g);
335
336 std::uniform_int_distribution<int> u(0, groups.size() - 1);
337 for (auto& peer : peers)
338 {
339 for (auto& target : groups[u(g)])
340 peer->trust(*target);
341 }
342}
343
348template <class RandomNumberDistribution, class Generator>
349void
351 PeerGroup& peers,
352 std::vector<double> const& ranks,
353 int numGroups,
354 RandomNumberDistribution sizeDist,
355 Generator& g,
356 SimDuration delay)
357{
358 std::vector<PeerGroup> const groups =
359 randomRankedGroups(peers, ranks, numGroups, sizeDist, g);
360
361 std::uniform_int_distribution<int> u(0, groups.size() - 1);
362 for (auto& peer : peers)
363 {
364 for (auto& target : groups[u(g)])
365 peer->connect(*target, delay);
366 }
367}
368
369} // namespace csf
370} // namespace test
371} // namespace ripple
372#endif
T back_inserter(T... args)
T begin(T... args)
A group of simulation Peers.
Definition: PeerGroup.h:40
bool contains(Peer const *p)
Definition: PeerGroup.h:98
PeerGroup(std::vector< Peer * > &&peers)
Definition: PeerGroup.h:54
std::size_t size() const
Definition: PeerGroup.h:112
friend PeerGroup operator+(PeerGroup const &a, PeerGroup const &b)
Definition: PeerGroup.h:233
peers_type::const_iterator const_iterator
Definition: PeerGroup.h:46
peers_type::reference reference
Definition: PeerGroup.h:47
PeerGroup(std::set< Peer * > const &peers)
Definition: PeerGroup.h:63
const_iterator begin() const
Definition: PeerGroup.h:80
PeerGroup(std::vector< Peer * > const &peers)
Definition: PeerGroup.h:58
void trust(PeerGroup const &o)
Establish trust.
Definition: PeerGroup.h:124
void trustAndConnect(PeerGroup const &o, SimDuration delay)
Establish trust and network connection.
Definition: PeerGroup.h:204
void disconnect(PeerGroup const &o)
Destroy network connection.
Definition: PeerGroup.h:184
friend std::ostream & operator<<(std::ostream &o, PeerGroup const &t)
Definition: PeerGroup.h:262
peers_type::const_reference const_reference
Definition: PeerGroup.h:48
bool contains(PeerID id)
Definition: PeerGroup.h:104
void connectFromTrust(SimDuration delay)
Establish network connections based on trust relations.
Definition: PeerGroup.h:220
const_iterator end() const
Definition: PeerGroup.h:86
peers_type::iterator iterator
Definition: PeerGroup.h:45
void untrust(PeerGroup const &o)
Revoke trust.
Definition: PeerGroup.h:142
const_reference operator[](std::size_t i) const
Definition: PeerGroup.h:92
friend PeerGroup operator-(PeerGroup const &a, PeerGroup const &b)
Definition: PeerGroup.h:247
void connect(PeerGroup const &o, SimDuration delay)
Establish network connection.
Definition: PeerGroup.h:164
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:350
typename SimClock::duration SimDuration
Definition: SimTime.h:35
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:326
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:299
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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.
Definition: test/csf/Peer.h:55
TrustGraph< Peer * > & trustGraph
Handle to Trust graph of network.