xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
xrpld
overlay
detail
Cluster.cpp
1
#include <xrpld/overlay/Cluster.h>
2
3
#include <xrpld/overlay/ClusterNode.h>
4
5
#include <xrpl/basics/Log.h>
6
#include <xrpl/basics/StringUtilities.h>
7
#include <xrpl/basics/chrono.h>
8
#include <xrpl/beast/utility/Journal.h>
9
#include <xrpl/config/BasicConfig.h>
10
#include <xrpl/protocol/PublicKey.h>
11
#include <xrpl/protocol/tokens.h>
12
13
#include <boost/regex/v5/regex.hpp>
14
#include <boost/regex/v5/regex_match.hpp>
15
16
#include <
cstddef
>
17
#include <
cstdint
>
18
#include <
functional
>
19
#include <
mutex
>
20
#include <
optional
>
21
#include <
string
>
22
#include <
utility
>
23
24
namespace
xrpl
{
25
26
Cluster::Cluster
(
beast::Journal
j) :
j_
(j)
27
{
28
}
29
30
std::optional<std::string>
31
Cluster::member
(
PublicKey
const
& identity)
const
32
{
33
std::scoped_lock
const
lock(
mutex_
);
34
35
auto
iter =
nodes_
.find(identity);
36
if
(iter ==
nodes_
.end())
37
return
std::nullopt;
38
return
iter->name();
39
}
40
41
bool
42
Cluster::isMember
(
PublicKey
const
& identity)
const
43
{
44
std::scoped_lock
const
lock(
mutex_
);
45
46
return
nodes_
.contains(identity);
47
}
48
49
std::size_t
50
Cluster::size
()
const
51
{
52
std::scoped_lock
const
lock(
mutex_
);
53
54
return
nodes_
.size();
55
}
56
57
bool
58
Cluster::update
(
59
PublicKey
const
& identity,
60
std::string
name,
61
std::uint32_t
loadFee,
62
NetClock::time_point
reportTime)
63
{
64
std::scoped_lock
const
lock(
mutex_
);
65
66
auto
iter =
nodes_
.find(identity);
67
68
if
(iter !=
nodes_
.end())
69
{
70
if
(reportTime <= iter->getReportTime())
71
return
false
;
72
73
if
(name.
empty
())
74
name = iter->name();
75
76
iter =
nodes_
.erase(iter);
77
}
78
79
nodes_
.emplace_hint(iter, identity, std::move(name), loadFee, reportTime);
80
return
true
;
81
}
82
83
void
84
Cluster::forEach
(
std::function
<
void
(
ClusterNode
const
&)> func)
const
85
{
86
std::scoped_lock
const
lock(
mutex_
);
87
for
(
auto
const
& ni :
nodes_
)
88
func(ni);
89
}
90
91
bool
92
Cluster::load
(
Section
const
& nodes)
93
{
94
static
boost::regex
const
kRE(
95
"[[:space:]]*"
// skip leading whitespace
96
"([[:alnum:]]+)"
// node identity
97
"(?:"
// begin optional comment block
98
"[[:space:]]+"
// (skip all leading whitespace)
99
"(?:"
// begin optional comment
100
"(.*[^[:space:]]+)"
// the comment
101
"[[:space:]]*"
// (skip all trailing whitespace)
102
")?"
// end optional comment
103
")?"
// end optional comment block
104
);
105
106
for
(
auto
const
& n : nodes.
values
())
107
{
108
boost::smatch match;
109
110
if
(!boost::regex_match(n, match, kRE))
111
{
112
JLOG(
j_
.error()) <<
"Malformed entry: '"
<< n <<
"'"
;
113
return
false
;
114
}
115
116
auto
const
id
=
parseBase58<PublicKey>
(
TokenType::NodePublic
, match[1].str());
117
118
if
(!
id
)
119
{
120
JLOG(
j_
.error()) <<
"Invalid node identity: "
<< match[1];
121
return
false
;
122
}
123
124
if
(
member
(*
id
))
125
{
126
JLOG(
j_
.warn()) <<
"Duplicate node identity: "
<< match[1];
127
continue
;
128
}
129
130
update
(*
id
,
trimWhitespace
(match[2]));
131
}
132
133
return
true
;
134
}
135
136
}
// namespace xrpl
std::string
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
xrpl::ClusterNode
Definition
ClusterNode.h:13
xrpl::Cluster::Cluster
Cluster(beast::Journal j)
Definition
Cluster.cpp:26
xrpl::Cluster::update
bool update(PublicKey const &identity, std::string name, std::uint32_t loadFee=0, NetClock::time_point reportTime=NetClock::time_point{})
Store information about the state of a cluster node.
Definition
Cluster.cpp:58
xrpl::Cluster::mutex_
std::mutex mutex_
Definition
Cluster.h:51
xrpl::Cluster::forEach
void forEach(std::function< void(ClusterNode const &)> func) const
Invokes the callback once for every cluster node.
Definition
Cluster.cpp:84
xrpl::Cluster::member
std::optional< std::string > member(PublicKey const &node) const
Determines whether a node belongs in the cluster.
Definition
Cluster.cpp:31
xrpl::Cluster::isMember
bool isMember(PublicKey const &node) const
Determines whether a node belongs in the cluster.
Definition
Cluster.cpp:42
xrpl::Cluster::j_
beast::Journal j_
Definition
Cluster.h:52
xrpl::Cluster::nodes_
std::set< ClusterNode, Comparator > nodes_
Definition
Cluster.h:50
xrpl::Cluster::load
bool load(Section const &nodes)
Load the list of cluster nodes.
Definition
Cluster.cpp:92
xrpl::Cluster::size
std::size_t size() const
The number of nodes in the cluster list.
Definition
Cluster.cpp:50
xrpl::NetClock::time_point
std::chrono::time_point< NetClock > time_point
Definition
chrono.h:48
xrpl::PublicKey
A public key.
Definition
PublicKey.h:53
xrpl::Section
Holds a collection of configuration values.
Definition
BasicConfig.h:28
xrpl::Section::values
std::vector< std::string > const & values() const
Returns all the values in the section.
Definition
BasicConfig.h:68
cstddef
cstdint
std::string::empty
T empty(T... args)
std::function
functional
std::uint32_t
mutex
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::parseBase58
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition
AccountID.cpp:105
xrpl::trimWhitespace
std::string trimWhitespace(std::string str)
Remove leading and trailing ASCII whitespace.
Definition
libxrpl/basics/StringUtilities.cpp:119
xrpl::TokenType::NodePublic
@ NodePublic
Definition
tokens.h:21
optional
std::scoped_lock
std::size_t
string
utility
Generated by
1.17.0