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
25#include <boost/container/flat_set.hpp>
26
27#include <chrono>
28#include <numeric>
29#include <random>
30#include <vector>
31
32namespace ripple {
33namespace test {
34namespace csf {
35
43template <class Peer>
45{
47
49
50public:
53 TrustGraph() = default;
54
55 Graph const&
57 {
58 return graph_;
59 }
60
70 void
71 trust(Peer const& from, Peer const& to)
72 {
73 graph_.connect(from, to);
74 }
75
84 void
85 untrust(Peer const& from, Peer const& to)
86 {
87 graph_.disconnect(from, to);
88 }
89
90 //< Whether from trusts to
91 bool
92 trusts(Peer const& from, Peer const& to) const
93 {
94 return graph_.connected(from, to);
95 }
96
103 auto
104 trustedPeers(Peer const& a) const
105 {
106 return graph_.outVertices(a);
107 }
108
118
119 //< Return nodes that fail the white-paper no-forking condition
121 forkablePairs(double quorum) const
122 {
123 // Check the forking condition by looking at intersection
124 // of UNL between all pairs of nodes.
125
126 // TODO: Use the improved bound instead of the whitepaper bound.
127
128 using UNL = std::set<Peer>;
129 std::set<UNL> unique;
130 for (Peer const peer : graph_.outVertices())
131 {
132 unique.emplace(
134 }
135
136 std::vector<UNL> uniqueUNLs(unique.begin(), unique.end());
138
139 // Loop over all pairs of uniqueUNLs
140 for (int i = 0; i < uniqueUNLs.size(); ++i)
141 {
142 for (int j = (i + 1); j < uniqueUNLs.size(); ++j)
143 {
144 auto const& unlA = uniqueUNLs[i];
145 auto const& unlB = uniqueUNLs[j];
146 double rhs =
147 2.0 * (1. - quorum) * std::max(unlA.size(), unlB.size());
148
149 int intersectionSize =
150 std::count_if(unlA.begin(), unlA.end(), [&](Peer p) {
151 return unlB.find(p) != unlB.end();
152 });
153
154 if (intersectionSize < rhs)
155 {
156 res.emplace_back(
157 ForkInfo{unlA, unlB, intersectionSize, rhs});
158 }
159 }
160 }
161 return res;
162 }
163
167 bool
168 canFork(double quorum) const
169 {
170 return !forkablePairs(quorum).empty();
171 }
172};
173
174} // namespace csf
175} // namespace test
176} // namespace ripple
177
178#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:71
bool canFork(double quorum) const
Check whether this trust graph satisfies the whitepaper no-forking condition.
Definition TrustGraph.h:168
void untrust(Peer const &from, Peer const &to)
Remove trust.
Definition TrustGraph.h:85
std::vector< ForkInfo > forkablePairs(double quorum) const
Definition TrustGraph.h:121
auto trustedPeers(Peer const &a) const
Range over trusted peers.
Definition TrustGraph.h:104
bool trusts(Peer const &from, Peer const &to) const
Definition TrustGraph.h:92
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:25
T size(T... args)
A single peer in the simulation.
An example of nodes that fail the whitepaper no-forking condition.
Definition TrustGraph.h:112