rippled
Loading...
Searching...
No Matches
cluster_test.cpp
1#include <test/jtx/TestSuite.h>
2#include <test/unit_test/SuiteJournal.h>
3
4#include <xrpld/overlay/Cluster.h>
5
6#include <xrpl/basics/BasicConfig.h>
7#include <xrpl/protocol/SecretKey.h>
8
9namespace ripple {
10namespace tests {
11
13{
15
16public:
17 cluster_test() : journal_("cluster_test", *this)
18 {
19 }
20
23 {
24 auto cluster = std::make_unique<Cluster>(journal_);
25
26 for (auto const& n : nodes)
27 cluster->update(n, "Test");
28
29 return cluster;
30 }
31
37
38 void
40 {
41 // The servers on the network
43
44 while (network.size() != 128)
45 network.push_back(randomNode());
46
47 {
48 testcase("Membership: Empty cluster");
49
50 auto c = create({});
51
52 for (auto const& n : network)
53 BEAST_EXPECT(!c->member(n));
54 }
55
56 {
57 testcase("Membership: Non-empty cluster and none present");
58
60 while (cluster.size() != 32)
61 cluster.push_back(randomNode());
62
63 auto c = create(cluster);
64
65 for (auto const& n : network)
66 BEAST_EXPECT(!c->member(n));
67 }
68
69 {
70 testcase("Membership: Non-empty cluster and some present");
71
73 network.begin(), network.begin() + 16);
74
75 while (cluster.size() != 32)
76 cluster.push_back(randomNode());
77
78 auto c = create(cluster);
79
80 for (auto const& n : cluster)
81 BEAST_EXPECT(c->member(n));
82
83 for (auto const& n : network)
84 {
85 auto found = std::find(cluster.begin(), cluster.end(), n);
86 BEAST_EXPECT(
87 static_cast<bool>(c->member(n)) ==
88 (found != cluster.end()));
89 }
90 }
91
92 {
93 testcase("Membership: Non-empty cluster and all present");
94
96 network.begin(), network.begin() + 32);
97
98 auto c = create(cluster);
99
100 for (auto const& n : cluster)
101 BEAST_EXPECT(c->member(n));
102
103 for (auto const& n : network)
104 {
105 auto found = std::find(cluster.begin(), cluster.end(), n);
106 BEAST_EXPECT(
107 static_cast<bool>(c->member(n)) ==
108 (found != cluster.end()));
109 }
110 }
111 }
112
113 void
115 {
116 testcase("Updating");
117
118 auto c = create({});
119
120 auto const node = randomNode();
121 auto const name = toBase58(TokenType::NodePublic, node);
122 std::uint32_t load = 0;
123 NetClock::time_point tick = {};
124
125 // Initial update
126 BEAST_EXPECT(c->update(node, "", load, tick));
127 {
128 auto member = c->member(node);
129 BEAST_EXPECT(static_cast<bool>(member));
130 BEAST_EXPECT(member->empty());
131 }
132
133 // Updating too quickly: should fail
134 BEAST_EXPECT(!c->update(node, name, load, tick));
135 {
136 auto member = c->member(node);
137 BEAST_EXPECT(static_cast<bool>(member));
138 BEAST_EXPECT(member->empty());
139 }
140
141 using namespace std::chrono_literals;
142
143 // Updating the name (empty updates to non-empty)
144 tick += 1s;
145 BEAST_EXPECT(c->update(node, name, load, tick));
146 {
147 auto member = c->member(node);
148 BEAST_EXPECT(static_cast<bool>(member));
149 BEAST_EXPECT(member->compare(name) == 0);
150 }
151
152 // Updating the name (non-empty doesn't go to empty)
153 tick += 1s;
154 BEAST_EXPECT(c->update(node, "", load, tick));
155 {
156 auto member = c->member(node);
157 BEAST_EXPECT(static_cast<bool>(member));
158 BEAST_EXPECT(member->compare(name) == 0);
159 }
160
161 // Updating the name (non-empty updates to new non-empty)
162 tick += 1s;
163 BEAST_EXPECT(c->update(node, "test", load, tick));
164 {
165 auto member = c->member(node);
166 BEAST_EXPECT(static_cast<bool>(member));
167 BEAST_EXPECT(member->compare("test") == 0);
168 }
169 }
170
171 void
173 {
174 testcase("Config Load");
175
177
178 // The servers on the network
180
181 while (network.size() != 8)
182 network.push_back(randomNode());
183
184 auto format = [](PublicKey const& publicKey,
185 char const* comment = nullptr) {
186 auto ret = toBase58(TokenType::NodePublic, publicKey);
187
188 if (comment)
189 ret += comment;
190
191 return ret;
192 };
193
194 Section s1;
195
196 // Correct (empty) configuration
197 BEAST_EXPECT(c->load(s1));
198 BEAST_EXPECT(c->size() == 0);
199
200 // Correct configuration
201 s1.append(format(network[0]));
202 s1.append(format(network[1], " "));
203 s1.append(format(network[2], " Comment"));
204 s1.append(format(network[3], " Multi Word Comment"));
205 s1.append(format(network[4], " Leading Whitespace"));
206 s1.append(format(network[5], " Trailing Whitespace "));
207 s1.append(format(network[6], " Leading & Trailing Whitespace "));
208 s1.append(format(
209 network[7], " Leading, Trailing & Internal Whitespace "));
210
211 BEAST_EXPECT(c->load(s1));
212
213 for (auto const& n : network)
214 BEAST_EXPECT(c->member(n));
215
216 // Incorrect configurations
217 Section s2;
218 s2.append("NotAPublicKey");
219 BEAST_EXPECT(!c->load(s2));
220
221 Section s3;
222 s3.append(format(network[0], "!"));
223 BEAST_EXPECT(!c->load(s3));
224
225 Section s4;
226 s4.append(format(network[0], "! Comment"));
227 BEAST_EXPECT(!c->load(s4));
228
229 // Check if we properly terminate when we encounter
230 // a malformed or unparseable entry:
231 auto const node1 = randomNode();
232 auto const node2 = randomNode();
233
234 Section s5;
235 s5.append(format(node1, "XXX"));
236 s5.append(format(node2));
237 BEAST_EXPECT(!c->load(s5));
238 BEAST_EXPECT(!c->member(node1));
239 BEAST_EXPECT(!c->member(node2));
240 }
241
242 void
243 run() override
244 {
246 testUpdating();
248 }
249};
250
251BEAST_DEFINE_TESTSUITE(cluster, overlay, ripple);
252
253} // namespace tests
254} // namespace ripple
T begin(T... args)
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:152
A public key.
Definition PublicKey.h:43
Holds a collection of configuration values.
Definition BasicConfig.h:26
void append(std::vector< std::string > const &lines)
Append a set of lines to this section.
void run() override
Runs the suite.
test::SuiteJournal journal_
std::unique_ptr< Cluster > create(std::vector< PublicKey > const &nodes)
T end(T... args)
T find(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 toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
T push_back(T... args)
T size(T... args)