xrpld
Loading...
Searching...
No Matches
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
24namespace xrpl {
25
29
31Cluster::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
41bool
42Cluster::isMember(PublicKey const& identity) const
43{
44 std::scoped_lock const lock(mutex_);
45
46 return nodes_.contains(identity);
47}
48
51{
52 std::scoped_lock const lock(mutex_);
53
54 return nodes_.size();
55}
56
57bool
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
83void
85{
86 std::scoped_lock const lock(mutex_);
87 for (auto const& ni : nodes_)
88 func(ni);
89}
90
91bool
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
A generic endpoint for log messages.
Definition Journal.h:44
Cluster(beast::Journal j)
Definition Cluster.cpp:26
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
std::mutex mutex_
Definition Cluster.h:51
void forEach(std::function< void(ClusterNode const &)> func) const
Invokes the callback once for every cluster node.
Definition Cluster.cpp:84
std::optional< std::string > member(PublicKey const &node) const
Determines whether a node belongs in the cluster.
Definition Cluster.cpp:31
bool isMember(PublicKey const &node) const
Determines whether a node belongs in the cluster.
Definition Cluster.cpp:42
beast::Journal j_
Definition Cluster.h:52
std::set< ClusterNode, Comparator > nodes_
Definition Cluster.h:50
bool load(Section const &nodes)
Load the list of cluster nodes.
Definition Cluster.cpp:92
std::size_t size() const
The number of nodes in the cluster list.
Definition Cluster.cpp:50
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
A public key.
Definition PublicKey.h:53
Holds a collection of configuration values.
Definition BasicConfig.h:28
std::vector< std::string > const & values() const
Returns all the values in the section.
Definition BasicConfig.h:68
T empty(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string trimWhitespace(std::string str)
Remove leading and trailing ASCII whitespace.