rippled
Loading...
Searching...
No Matches
Cluster.cpp
1#include <xrpld/core/Config.h>
2#include <xrpld/core/TimeKeeper.h>
3#include <xrpld/overlay/Cluster.h>
4#include <xrpld/overlay/ClusterNode.h>
5
6#include <xrpl/basics/Log.h>
7#include <xrpl/basics/StringUtilities.h>
8#include <xrpl/protocol/tokens.h>
9
10#include <boost/regex.hpp>
11
12namespace xrpl {
13
17
19Cluster::member(PublicKey const& identity) const
20{
22
23 auto iter = nodes_.find(identity);
24 if (iter == nodes_.end())
25 return std::nullopt;
26 return iter->name();
27}
28
31{
33
34 return nodes_.size();
35}
36
37bool
38Cluster::update(PublicKey const& identity, std::string name, std::uint32_t loadFee, NetClock::time_point reportTime)
39{
41
42 auto iter = nodes_.find(identity);
43
44 if (iter != nodes_.end())
45 {
46 if (reportTime <= iter->getReportTime())
47 return false;
48
49 if (name.empty())
50 name = iter->name();
51
52 iter = nodes_.erase(iter);
53 }
54
55 nodes_.emplace_hint(iter, identity, name, loadFee, reportTime);
56 return true;
57}
58
59void
61{
63 for (auto const& ni : nodes_)
64 func(ni);
65}
66
67bool
69{
70 static boost::regex const re(
71 "[[:space:]]*" // skip leading whitespace
72 "([[:alnum:]]+)" // node identity
73 "(?:" // begin optional comment block
74 "[[:space:]]+" // (skip all leading whitespace)
75 "(?:" // begin optional comment
76 "(.*[^[:space:]]+)" // the comment
77 "[[:space:]]*" // (skip all trailing whitespace)
78 ")?" // end optional comment
79 ")?" // end optional comment block
80 );
81
82 for (auto const& n : nodes.values())
83 {
84 boost::smatch match;
85
86 if (!boost::regex_match(n, match, re))
87 {
88 JLOG(j_.error()) << "Malformed entry: '" << n << "'";
89 return false;
90 }
91
92 auto const id = parseBase58<PublicKey>(TokenType::NodePublic, match[1].str());
93
94 if (!id)
95 {
96 JLOG(j_.error()) << "Invalid node identity: " << match[1];
97 return false;
98 }
99
100 if (member(*id))
101 {
102 JLOG(j_.warn()) << "Duplicate node identity: " << match[1];
103 continue;
104 }
105
106 update(*id, trim_whitespace(match[2]));
107 }
108
109 return true;
110}
111
112} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:41
Stream error() const
Definition Journal.h:319
Stream warn() const
Definition Journal.h:313
Cluster(beast::Journal j)
Definition Cluster.cpp:14
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:38
std::mutex mutex_
Definition Cluster.h:46
void for_each(std::function< void(ClusterNode const &)> func) const
Invokes the callback once for every cluster node.
Definition Cluster.cpp:60
std::optional< std::string > member(PublicKey const &node) const
Determines whether a node belongs in the cluster.
Definition Cluster.cpp:19
beast::Journal j_
Definition Cluster.h:47
std::set< ClusterNode, Comparator > nodes_
Definition Cluster.h:45
bool load(Section const &nodes)
Load the list of cluster nodes.
Definition Cluster.cpp:68
std::size_t size() const
The number of nodes in the cluster list.
Definition Cluster.cpp:30
A public key.
Definition PublicKey.h:43
Holds a collection of configuration values.
Definition BasicConfig.h:25
std::vector< std::string > const & values() const
Returns all the values in the section.
Definition BasicConfig.h:59
T empty(T... args)
T erase(T... args)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string trim_whitespace(std::string str)