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
20#ifndef RIPPLE_TEST_CSF_PEERGROUP_H_INCLUDED
21#define RIPPLE_TEST_CSF_PEERGROUP_H_INCLUDED
22
23#include <test/csf/Peer.h>
24#include <test/csf/random.h>
25
26#include <algorithm>
27#include <vector>
28
29namespace ripple {
30namespace test {
31namespace csf {
32
42{
45
46public:
47 using iterator = peers_type::iterator;
48 using const_iterator = peers_type::const_iterator;
49 using reference = peers_type::reference;
50 using const_reference = peers_type::const_reference;
51
52 PeerGroup() = default;
53 PeerGroup(Peer* peer) : peers_{1, peer}
54 {
55 }
56 PeerGroup(std::vector<Peer*>&& peers) : peers_{std::move(peers)}
57 {
59 }
60 PeerGroup(std::vector<Peer*> const& peers) : peers_{peers}
61 {
63 }
64
65 PeerGroup(std::set<Peer*> const& peers) : peers_{peers.begin(), peers.end()}
66 {
67 }
68
71 {
72 return peers_.begin();
73 }
74
77 {
78 return peers_.end();
79 }
80
82 begin() const
83 {
84 return peers_.begin();
85 }
86
88 end() const
89 {
90 return peers_.end();
91 }
92
95 {
96 return peers_[i];
97 }
98
99 bool
100 contains(Peer const* p)
101 {
102 return std::find(peers_.begin(), peers_.end(), p) != peers_.end();
103 }
104
105 bool
107 {
108 return std::find_if(peers_.begin(), peers_.end(), [id](Peer const* p) {
109 return p->id == id;
110 }) != peers_.end();
111 }
112
114 size() const
115 {
116 return peers_.size();
117 }
118
125 void
126 trust(PeerGroup const& o)
127 {
128 for (Peer* p : peers_)
129 {
130 for (Peer* target : o.peers_)
131 {
132 p->trust(*target);
133 }
134 }
135 }
136
143 void
145 {
146 for (Peer* p : peers_)
147 {
148 for (Peer* target : o.peers_)
149 {
150 p->untrust(*target);
151 }
152 }
153 }
154
165 void
167 {
168 for (Peer* p : peers_)
169 {
170 for (Peer* target : o.peers_)
171 {
172 // cannot send messages to self over network
173 if (p != target)
174 p->connect(*target, delay);
175 }
176 }
177 }
178
185 void
187 {
188 for (Peer* p : peers_)
189 {
190 for (Peer* target : o.peers_)
191 {
192 p->disconnect(*target);
193 }
194 }
195 }
196
205 void
207 {
208 trust(o);
209 connect(o, delay);
210 }
211
221 void
223 {
224 for (Peer* peer : peers_)
225 {
226 for (Peer* to : peer->trustGraph.trustedPeers(peer))
227 {
228 peer->connect(*to, delay);
229 }
230 }
231 }
232
233 // Union of PeerGroups
234 friend PeerGroup
235 operator+(PeerGroup const& a, PeerGroup const& b)
236 {
237 PeerGroup res;
239 a.peers_.begin(),
240 a.peers_.end(),
241 b.peers_.begin(),
242 b.peers_.end(),
244 return res;
245 }
246
247 // Set difference of PeerGroups
248 friend PeerGroup
249 operator-(PeerGroup const& a, PeerGroup const& b)
250 {
251 PeerGroup res;
252
254 a.peers_.begin(),
255 a.peers_.end(),
256 b.peers_.begin(),
257 b.peers_.end(),
259
260 return res;
261 }
262
264 operator<<(std::ostream& o, PeerGroup const& t)
265 {
266 o << "{";
267 bool first = true;
268 for (Peer const* p : t)
269 {
270 if (!first)
271 o << ", ";
272 first = false;
273 o << p->id;
274 }
275 o << "}";
276 return o;
277 }
278};
279
299template <class RandomNumberDistribution, class Generator>
302 PeerGroup& peers,
303 std::vector<double> const& ranks,
304 int numGroups,
305 RandomNumberDistribution sizeDist,
306 Generator& g)
307{
308 assert(peers.size() == ranks.size());
309
311 groups.reserve(numGroups);
312 std::vector<Peer*> rawPeers(peers.begin(), peers.end());
313 std::generate_n(std::back_inserter(groups), numGroups, [&]() {
314 std::vector<Peer*> res = random_weighted_shuffle(rawPeers, ranks, g);
315 res.resize(sizeDist(g));
316 return PeerGroup(std::move(res));
317 });
318
319 return groups;
320}
321
326template <class RandomNumberDistribution, class Generator>
327void
329 PeerGroup& peers,
330 std::vector<double> const& ranks,
331 int numGroups,
332 RandomNumberDistribution sizeDist,
333 Generator& g)
334{
335 std::vector<PeerGroup> const groups =
336 randomRankedGroups(peers, ranks, numGroups, sizeDist, g);
337
338 std::uniform_int_distribution<int> u(0, groups.size() - 1);
339 for (auto& peer : peers)
340 {
341 for (auto& target : groups[u(g)])
342 peer->trust(*target);
343 }
344}
345
350template <class RandomNumberDistribution, class Generator>
351void
353 PeerGroup& peers,
354 std::vector<double> const& ranks,
355 int numGroups,
356 RandomNumberDistribution sizeDist,
357 Generator& g,
358 SimDuration delay)
359{
360 std::vector<PeerGroup> const groups =
361 randomRankedGroups(peers, ranks, numGroups, sizeDist, g);
362
363 std::uniform_int_distribution<int> u(0, groups.size() - 1);
364 for (auto& peer : peers)
365 {
366 for (auto& target : groups[u(g)])
367 peer->connect(*target, delay);
368 }
369}
370
371} // namespace csf
372} // namespace test
373} // namespace ripple
374#endif
T back_inserter(T... args)
T begin(T... args)
A group of simulation Peers.
Definition PeerGroup.h:42
bool contains(Peer const *p)
Definition PeerGroup.h:100
PeerGroup(std::vector< Peer * > &&peers)
Definition PeerGroup.h:56
std::size_t size() const
Definition PeerGroup.h:114
friend PeerGroup operator+(PeerGroup const &a, PeerGroup const &b)
Definition PeerGroup.h:235
peers_type::const_iterator const_iterator
Definition PeerGroup.h:48
peers_type::reference reference
Definition PeerGroup.h:49
PeerGroup(std::set< Peer * > const &peers)
Definition PeerGroup.h:65
const_iterator begin() const
Definition PeerGroup.h:82
PeerGroup(std::vector< Peer * > const &peers)
Definition PeerGroup.h:60
void trust(PeerGroup const &o)
Establish trust.
Definition PeerGroup.h:126
void trustAndConnect(PeerGroup const &o, SimDuration delay)
Establish trust and network connection.
Definition PeerGroup.h:206
void disconnect(PeerGroup const &o)
Destroy network connection.
Definition PeerGroup.h:186
friend std::ostream & operator<<(std::ostream &o, PeerGroup const &t)
Definition PeerGroup.h:264
peers_type::const_reference const_reference
Definition PeerGroup.h:50
void connectFromTrust(SimDuration delay)
Establish network connections based on trust relations.
Definition PeerGroup.h:222
const_iterator end() const
Definition PeerGroup.h:88
peers_type::iterator iterator
Definition PeerGroup.h:47
void untrust(PeerGroup const &o)
Revoke trust.
Definition PeerGroup.h:144
const_reference operator[](std::size_t i) const
Definition PeerGroup.h:94
friend PeerGroup operator-(PeerGroup const &a, PeerGroup const &b)
Definition PeerGroup.h:249
void connect(PeerGroup const &o, SimDuration delay)
Establish network connection.
Definition PeerGroup.h:166
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:352
typename SimClock::duration SimDuration
Definition SimTime.h:36
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:328
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:301
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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.