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