forked from XRPLF/rippled
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrustGraph.h
176 lines (145 loc) · 4.74 KB
/
TrustGraph.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012-2017 Ripple Labs Inc
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_TEST_CSF_UNL_H_INCLUDED
#define RIPPLE_TEST_CSF_UNL_H_INCLUDED
#include <test/csf/random.h>
#include <boost/container/flat_set.hpp>
#include <boost/optional.hpp>
#include <chrono>
#include <numeric>
#include <random>
#include <vector>
namespace ripple {
namespace test {
namespace csf {
/** Trust graph
Trust is a directed relationship from a node i to node j.
If node i trusts node j, then node i has node j in its UNL.
This class wraps a digraph representing the trust relationships for all peers
in the simulation.
*/
template <class Peer>
class TrustGraph
{
using Graph = Digraph<Peer>;
Graph graph_;
public:
/** Create an empty trust graph
*/
TrustGraph() = default;
Graph const&
graph()
{
return graph_;
}
/** Create trust
Establish trust between Peer `from` and Peer `to`; as if `from` put `to`
in its UNL.
@param from The peer granting trust
@param to The peer receiving trust
*/
void
trust(Peer const& from, Peer const& to)
{
graph_.connect(from, to);
}
/** Remove trust
Revoke trust from Peer `from` to Peer `to`; as if `from` removed `to`
from its UNL.
@param from The peer revoking trust
@param to The peer being revoked
*/
void
untrust(Peer const& from, Peer const& to)
{
graph_.disconnect(from, to);
}
//< Whether from trusts to
bool
trusts(Peer const& from, Peer const& to) const
{
return graph_.connected(from, to);
}
/** Range over trusted peers
@param a The node granting trust
@return boost transformed range over nodes `a` trusts, i.e. the nodes
in its UNL
*/
auto
trustedPeers(Peer const & a) const
{
return graph_.outVertices(a);
}
/** An example of nodes that fail the whitepaper no-forking condition
*/
struct ForkInfo
{
std::set<Peer> unlA;
std::set<Peer> unlB;
int overlap;
double required;
};
//< Return nodes that fail the white-paper no-forking condition
std::vector<ForkInfo>
forkablePairs(double quorum) const
{
// Check the forking condition by looking at intersection
// of UNL between all pairs of nodes.
// TODO: Use the improved bound instead of the whitepaper bound.
using UNL = std::set<Peer>;
std::set<UNL> unique;
for (Peer const & peer : graph_.outVertices())
{
unique.emplace(
std::begin(trustedPeers(peer)), std::end(trustedPeers(peer)));
}
std::vector<UNL> uniqueUNLs(unique.begin(), unique.end());
std::vector<ForkInfo> res;
// Loop over all pairs of uniqueUNLs
for (int i = 0; i < uniqueUNLs.size(); ++i)
{
for (int j = (i + 1); j < uniqueUNLs.size(); ++j)
{
auto const& unlA = uniqueUNLs[i];
auto const& unlB = uniqueUNLs[j];
double rhs =
2.0 * (1. - quorum) * std::max(unlA.size(), unlB.size());
int intersectionSize = std::count_if(
unlA.begin(), unlA.end(), [&](Peer p) {
return unlB.find(p) != unlB.end();
});
if (intersectionSize < rhs)
{
res.emplace_back(ForkInfo{unlA, unlB, intersectionSize, rhs});
}
}
}
return res;
}
/** Check whether this trust graph satisfies the whitepaper no-forking
condition
*/
bool
canFork(double quorum) const
{
return !forkablePairs(quorum).empty();
}
};
} // csf
} // test
} // ripple
#endif