rippled
TrustGraph.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_UNL_H_INCLUDED
21 #define RIPPLE_TEST_CSF_UNL_H_INCLUDED
22 
23 #include <boost/container/flat_set.hpp>
24 #include <test/csf/random.h>
25 
26 #include <chrono>
27 #include <numeric>
28 #include <random>
29 #include <vector>
30 
31 namespace ripple {
32 namespace test {
33 namespace csf {
34 
42 template <class Peer>
44 {
46 
48 
49 public:
52  TrustGraph() = default;
53 
54  Graph const&
56  {
57  return graph_;
58  }
59 
69  void
70  trust(Peer const& from, Peer const& to)
71  {
72  graph_.connect(from, to);
73  }
74 
83  void
84  untrust(Peer const& from, Peer const& to)
85  {
86  graph_.disconnect(from, to);
87  }
88 
89  //< Whether from trusts to
90  bool
91  trusts(Peer const& from, Peer const& to) const
92  {
93  return graph_.connected(from, to);
94  }
95 
102  auto
103  trustedPeers(Peer const& a) const
104  {
105  return graph_.outVertices(a);
106  }
107 
110  struct ForkInfo
111  {
114  int overlap;
115  double required;
116  };
117 
118  //< Return nodes that fail the white-paper no-forking condition
120  forkablePairs(double quorum) const
121  {
122  // Check the forking condition by looking at intersection
123  // of UNL between all pairs of nodes.
124 
125  // TODO: Use the improved bound instead of the whitepaper bound.
126 
127  using UNL = std::set<Peer>;
128  std::set<UNL> unique;
129  for (Peer const peer : graph_.outVertices())
130  {
131  unique.emplace(
133  }
134 
135  std::vector<UNL> uniqueUNLs(unique.begin(), unique.end());
137 
138  // Loop over all pairs of uniqueUNLs
139  for (int i = 0; i < uniqueUNLs.size(); ++i)
140  {
141  for (int j = (i + 1); j < uniqueUNLs.size(); ++j)
142  {
143  auto const& unlA = uniqueUNLs[i];
144  auto const& unlB = uniqueUNLs[j];
145  double rhs =
146  2.0 * (1. - quorum) * std::max(unlA.size(), unlB.size());
147 
148  int intersectionSize =
149  std::count_if(unlA.begin(), unlA.end(), [&](Peer p) {
150  return unlB.find(p) != unlB.end();
151  });
152 
153  if (intersectionSize < rhs)
154  {
155  res.emplace_back(
156  ForkInfo{unlA, unlB, intersectionSize, rhs});
157  }
158  }
159  }
160  return res;
161  }
162 
166  bool
167  canFork(double quorum) const
168  {
169  return !forkablePairs(quorum).empty();
170  }
171 };
172 
173 } // namespace csf
174 } // namespace test
175 } // namespace ripple
176 
177 #endif
ripple::test::csf::TrustGraph
Trust graph.
Definition: TrustGraph.h:43
ripple::test::csf::Digraph::outVertices
auto outVertices() const
Range over vertices in the graph.
Definition: Digraph.h:149
ripple::test::csf::TrustGraph::ForkInfo::unlB
std::set< Peer > unlB
Definition: TrustGraph.h:113
vector
std::vector::size
T size(T... args)
ripple::test::csf::TrustGraph::TrustGraph
TrustGraph()=default
Create an empty trust graph.
ripple::test::csf::TrustGraph::trust
void trust(Peer const &from, Peer const &to)
Create trust.
Definition: TrustGraph.h:70
random
ripple::test::csf::Digraph< ripple::test::csf::Peer * >
ripple::test::csf::TrustGraph::untrust
void untrust(Peer const &from, Peer const &to)
Remove trust.
Definition: TrustGraph.h:84
ripple::test::csf::TrustGraph::ForkInfo::unlA
std::set< Peer > unlA
Definition: TrustGraph.h:112
ripple::test::csf::TrustGraph::ForkInfo
An example of nodes that fail the whitepaper no-forking condition.
Definition: TrustGraph.h:110
ripple::test::csf::Digraph::disconnect
bool disconnect(Vertex source, Vertex target)
Disconnect two vertices.
Definition: Digraph.h:101
chrono
ripple::test::csf::Peer
A single peer in the simulation.
Definition: test/csf/Peer.h:54
std::vector::emplace_back
T emplace_back(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::csf::TrustGraph::trustedPeers
auto trustedPeers(Peer const &a) const
Range over trusted peers.
Definition: TrustGraph.h:103
ripple::test::csf::TrustGraph::graph_
Graph graph_
Definition: TrustGraph.h:47
std::begin
T begin(T... args)
std::count_if
T count_if(T... args)
std::end
T end(T... args)
ripple::test::csf::TrustGraph::canFork
bool canFork(double quorum) const
Check whether this trust graph satisfies the whitepaper no-forking condition.
Definition: TrustGraph.h:167
ripple::test::csf::Digraph::connect
bool connect(Vertex source, Vertex target, EdgeData e)
Connect two vertices.
Definition: Digraph.h:74
numeric
std::max
T max(T... args)
ripple::test::csf::TrustGraph::trusts
bool trusts(Peer const &from, Peer const &to) const
Definition: TrustGraph.h:91
ripple::test::csf::TrustGraph::ForkInfo::required
double required
Definition: TrustGraph.h:115
ripple::test::csf::Digraph::connected
bool connected(Vertex source, Vertex target) const
Check if two vertices are connected.
Definition: Digraph.h:138
ripple::test::csf::TrustGraph::ForkInfo::overlap
int overlap
Definition: TrustGraph.h:114
std::set
STL class.
ripple::test::csf::TrustGraph::forkablePairs
std::vector< ForkInfo > forkablePairs(double quorum) const
Definition: TrustGraph.h:120
ripple::test::csf::TrustGraph::graph
Graph const & graph()
Definition: TrustGraph.h:55