rippled
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 <algorithm>
23 #include <test/csf/Peer.h>
24 #include <test/csf/random.h>
25 #include <vector>
26 
27 namespace ripple {
28 namespace test {
29 namespace csf {
30 
39 class PeerGroup
40 {
43 
44 public:
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 
67  iterator
69  {
70  return peers_.begin();
71  }
72 
73  iterator
74  end()
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
142  untrust(PeerGroup const& o)
143  {
144  for (Peer* p : peers_)
145  {
146  for (Peer* target : o.peers_)
147  {
148  p->untrust(*target);
149  }
150  }
151  }
152 
163  void
164  connect(PeerGroup const& o, SimDuration delay)
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 
261  friend std::ostream&
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 
297 template <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 
308  std::vector<PeerGroup> groups;
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 
324 template <class RandomNumberDistribution, class Generator>
325 void
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 
348 template <class RandomNumberDistribution, class Generator>
349 void
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
ripple::test::csf::PeerGroup::connectFromTrust
void connectFromTrust(SimDuration delay)
Establish network connections based on trust relations.
Definition: PeerGroup.h:220
std::vector::resize
T resize(T... args)
std::generate_n
T generate_n(T... args)
ripple::Dir::const_iterator
Definition: Directory.h:49
std::uniform_int_distribution
ripple::test::csf::PeerGroup::contains
bool contains(Peer const *p)
Definition: PeerGroup.h:98
ripple::test::csf::PeerGroup::size
std::size_t size() const
Definition: PeerGroup.h:112
std::vector::reserve
T reserve(T... args)
vector
std::find
T find(T... args)
std::vector::size
T size(T... args)
std::back_inserter
T back_inserter(T... args)
ripple::test::csf::PeerGroup::PeerGroup
PeerGroup(std::set< Peer * > const &peers)
Definition: PeerGroup.h:63
ripple::test::csf::PeerGroup::operator-
friend PeerGroup operator-(PeerGroup const &a, PeerGroup const &b)
Definition: PeerGroup.h:247
ripple::test::csf::PeerGroup::const_reference
peers_type::const_reference const_reference
Definition: PeerGroup.h:48
ripple::test::csf::random_weighted_shuffle
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.
Definition: test/csf/random.h:41
ripple::test::csf::PeerGroup::begin
const_iterator begin() const
Definition: PeerGroup.h:80
std::sort
T sort(T... args)
algorithm
ripple::test::csf::PeerGroup::end
iterator end()
Definition: PeerGroup.h:74
ripple::test::csf::PeerGroup::connect
void connect(PeerGroup const &o, SimDuration delay)
Establish network connection.
Definition: PeerGroup.h:164
ripple::test::csf::PeerGroup::peers_
peers_type peers_
Definition: PeerGroup.h:42
ripple::test::csf::PeerGroup::begin
iterator begin()
Definition: PeerGroup.h:68
ripple::test::csf::PeerGroup::const_iterator
peers_type::const_iterator const_iterator
Definition: PeerGroup.h:46
std::ostream
STL class.
ripple::test::csf::PeerGroup::reference
peers_type::reference reference
Definition: PeerGroup.h:47
ripple::test::csf::PeerGroup::trustAndConnect
void trustAndConnect(PeerGroup const &o, SimDuration delay)
Establish trust and network connection.
Definition: PeerGroup.h:204
ripple::test::csf::randomRankedGroups
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
ripple::test::csf::PeerGroup::contains
bool contains(PeerID id)
Definition: PeerGroup.h:104
ripple::test::csf::PeerGroup::PeerGroup
PeerGroup()=default
std::set_union
T set_union(T... args)
ripple::test::csf::PeerGroup::PeerGroup
PeerGroup(Peer *peer)
Definition: PeerGroup.h:51
ripple::test::csf::randomRankedTrust
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
ripple::test::csf::PeerGroup::PeerGroup
PeerGroup(std::vector< Peer * > const &peers)
Definition: PeerGroup.h:58
ripple::test::csf::PeerGroup::trust
void trust(PeerGroup const &o)
Establish trust.
Definition: PeerGroup.h:124
ripple::test::csf::PeerGroup::end
const_iterator end() const
Definition: PeerGroup.h:86
ripple::test::csf::PeerGroup::PeerGroup
PeerGroup(std::vector< Peer * > &&peers)
Definition: PeerGroup.h:54
ripple::test::csf::PeerGroup::disconnect
void disconnect(PeerGroup const &o)
Destroy network connection.
Definition: PeerGroup.h:184
ripple::test::csf::PeerGroup
A group of simulation Peers.
Definition: PeerGroup.h:39
ripple::test::csf::Peer
A single peer in the simulation.
Definition: test/csf/Peer.h:54
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::csf::PeerGroup::operator[]
const_reference operator[](std::size_t i) const
Definition: PeerGroup.h:92
std::vector::begin
T begin(T... args)
std::set_difference
T set_difference(T... args)
ripple::test::csf::randomRankedConnect
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
ripple::test::csf::Peer::trustGraph
TrustGraph< Peer * > & trustGraph
Handle to Trust graph of network.
Definition: test/csf/Peer.h:188
std::size_t
std::vector::end
T end(T... args)
ripple::tagged_integer< std::uint32_t, PeerIDTag >
ripple::test::csf::SimDuration
typename SimClock::duration SimDuration
Definition: SimTime.h:35
ripple::test::csf::PeerGroup::iterator
peers_type::iterator iterator
Definition: PeerGroup.h:45
std::set
STL class.
ripple::test::csf::PeerGroup::operator+
friend PeerGroup operator+(PeerGroup const &a, PeerGroup const &b)
Definition: PeerGroup.h:233
ripple::test::csf::PeerGroup::untrust
void untrust(PeerGroup const &o)
Revoke trust.
Definition: PeerGroup.h:142
ripple::test::csf::PeerGroup::operator<<
friend std::ostream & operator<<(std::ostream &o, PeerGroup const &t)
Definition: PeerGroup.h:262