rippled
Loading...
Searching...
No Matches
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 <test/csf/random.h>
24#include <boost/container/flat_set.hpp>
25
26#include <chrono>
27#include <numeric>
28#include <random>
29#include <vector>
30
31namespace ripple {
32namespace test {
33namespace csf {
34
42template <class Peer>
44{
46
48
49public:
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 {
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
T begin(T... args)
bool connected(Vertex source, Vertex target) const
Check if two vertices are connected.
Definition: Digraph.h:138
auto outVertices() const
Range over vertices in the graph.
Definition: Digraph.h:149
bool disconnect(Vertex source, Vertex target)
Disconnect two vertices.
Definition: Digraph.h:101
bool connect(Vertex source, Vertex target, EdgeData e)
Connect two vertices.
Definition: Digraph.h:74
void trust(Peer const &from, Peer const &to)
Create trust.
Definition: TrustGraph.h:70
bool canFork(double quorum) const
Check whether this trust graph satisfies the whitepaper no-forking condition.
Definition: TrustGraph.h:167
void untrust(Peer const &from, Peer const &to)
Remove trust.
Definition: TrustGraph.h:84
std::vector< ForkInfo > forkablePairs(double quorum) const
Definition: TrustGraph.h:120
auto trustedPeers(Peer const &a) const
Range over trusted peers.
Definition: TrustGraph.h:103
bool trusts(Peer const &from, Peer const &to) const
Definition: TrustGraph.h:91
TrustGraph()=default
Create an empty trust graph.
T count_if(T... args)
T emplace_back(T... args)
T end(T... args)
T max(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
T size(T... args)
A single peer in the simulation.
Definition: test/csf/Peer.h:55
An example of nodes that fail the whitepaper no-forking condition.
Definition: TrustGraph.h:111