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
43{
44 std::scoped_lock const lock(mutex_);
45
46 return nodes_.size();
47}
48
49bool
51 PublicKey const& identity,
52 std::string name,
53 std::uint32_t loadFee,
54 NetClock::time_point reportTime)
55{
56 std::scoped_lock const lock(mutex_);
57
58 auto iter = nodes_.find(identity);
59
60 if (iter != nodes_.end())
61 {
62 if (reportTime <= iter->getReportTime())
63 return false;
64
65 if (name.empty())
66 name = iter->name();
67
68 iter = nodes_.erase(iter);
69 }
70
71 nodes_.emplace_hint(iter, identity, std::move(name), loadFee, reportTime);
72 return true;
73}
74
75void
77{
78 std::scoped_lock const lock(mutex_);
79 for (auto const& ni : nodes_)
80 func(ni);
81}
82
83bool
85{
86 static boost::regex const kRE(
87 "[[:space:]]*" // skip leading whitespace
88 "([[:alnum:]]+)" // node identity
89 "(?:" // begin optional comment block
90 "[[:space:]]+" // (skip all leading whitespace)
91 "(?:" // begin optional comment
92 "(.*[^[:space:]]+)" // the comment
93 "[[:space:]]*" // (skip all trailing whitespace)
94 ")?" // end optional comment
95 ")?" // end optional comment block
96 );
97
98 for (auto const& n : nodes.values())
99 {
100 boost::smatch match;
101
102 if (!boost::regex_match(n, match, kRE))
103 {
104 JLOG(j_.error()) << "Malformed entry: '" << n << "'";
105 return false;
106 }
107
108 auto const id = parseBase58<PublicKey>(TokenType::NodePublic, match[1].str());
109
110 if (!id)
111 {
112 JLOG(j_.error()) << "Invalid node identity: " << match[1];
113 return false;
114 }
115
116 if (member(*id))
117 {
118 JLOG(j_.warn()) << "Duplicate node identity: " << match[1];
119 continue;
120 }
121
122 update(*id, trimWhitespace(match[2]));
123 }
124
125 return true;
126}
127
128} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
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:50
std::mutex mutex_
Definition Cluster.h:45
void forEach(std::function< void(ClusterNode const &)> func) const
Invokes the callback once for every cluster node.
Definition Cluster.cpp:76
std::optional< std::string > member(PublicKey const &node) const
Determines whether a node belongs in the cluster.
Definition Cluster.cpp:31
beast::Journal j_
Definition Cluster.h:46
std::set< ClusterNode, Comparator > nodes_
Definition Cluster.h:44
bool load(Section const &nodes)
Load the list of cluster nodes.
Definition Cluster.cpp:84
std::size_t size() const
The number of nodes in the cluster list.
Definition Cluster.cpp:42
std::chrono::time_point< NetClock > time_point
Definition chrono.h:46
A public key.
Definition PublicKey.h:42
Holds a collection of configuration values.
Definition BasicConfig.h:24
std::vector< std::string > const & values() const
Returns all the values in the section.
Definition BasicConfig.h:58
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)