xrpld
Loading...
Searching...
No Matches
ValidatorList_test.cpp
1
2#include <test/jtx/Env.h>
3#include <test/jtx/envconfig.h>
4
5#include <xrpld/app/misc/ValidatorList.h>
6#include <xrpld/overlay/Compression.h>
7#include <xrpld/overlay/detail/ProtocolMessage.h>
8
9#include <xrpl/basics/Slice.h>
10#include <xrpl/basics/UnorderedContainers.h>
11#include <xrpl/basics/base64.h>
12#include <xrpl/basics/chrono.h>
13#include <xrpl/basics/strHex.h>
14#include <xrpl/beast/unit_test/suite.h>
15#include <xrpl/beast/utility/Journal.h>
16#include <xrpl/config/Constants.h>
17#include <xrpl/protocol/HashPrefix.h>
18#include <xrpl/protocol/KeyType.h>
19#include <xrpl/protocol/PublicKey.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/STObject.h>
22#include <xrpl/protocol/SecretKey.h>
23#include <xrpl/protocol/Serializer.h>
24#include <xrpl/protocol/Sign.h>
25#include <xrpl/protocol/UintTypes.h>
26#include <xrpl/protocol/digest.h>
27#include <xrpl/protocol/jss.h>
28#include <xrpl/protocol/tokens.h>
29#include <xrpl/server/Manifest.h>
30
31#include <boost/asio/buffer.hpp>
32#include <boost/beast/core/multi_buffer.hpp>
33#include <boost/system/detail/error_code.hpp>
34
35#include <xrpl.pb.h>
36
37#include <algorithm>
38#include <array>
39#include <chrono>
40#include <cmath>
41#include <cstddef>
42#include <cstdint>
43#include <initializer_list>
44#include <limits>
45#include <map>
46#include <memory>
47#include <optional>
48#include <sstream>
49#include <string>
50#include <utility>
51#include <vector>
52
53namespace xrpl::test {
54
56{
57private:
64
65 static PublicKey
70
71 static PublicKey
76
77 static std::string
79 PublicKey const& pk,
80 SecretKey const& sk,
81 PublicKey const& spk,
82 SecretKey const& ssk,
83 int seq)
84 {
86 st[sfSequence] = seq;
87 st[sfPublicKey] = pk;
88
90 {
91 st[sfSigningPubKey] = spk;
92 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
94 }
95
96 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
97 sign(st, HashPrefix::Manifest, *publicKeyType(pk), sk, sfMasterSignature);
98
99 Serializer s;
100 st.add(s);
101
102 return std::string(static_cast<char const*>(s.data()), s.size());
103 }
104
105 static std::string
107 {
110 st[sfPublicKey] = pk;
111
112 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
113 sign(st, HashPrefix::Manifest, *publicKeyType(pk), sk, sfMasterSignature);
114
115 Serializer s;
116 st.add(s);
117
118 return std::string(static_cast<char const*>(s.data()), s.size());
119 }
120
121 static Validator
123 {
124 auto const secret = randomSecretKey();
125 auto const masterPublic = derivePublicKey(KeyType::Ed25519, secret);
126 auto const signingKeys = randomKeyPair(KeyType::Secp256k1);
127 return {
128 .masterPublic = masterPublic,
129 .signingPublic = signingKeys.first,
130 .manifest = base64Encode(
131 makeManifestString(masterPublic, secret, signingKeys.first, signingKeys.second, 1)),
132 };
133 }
134
135 static std::string
137 std::vector<Validator> const& validators,
138 std::size_t sequence,
139 std::size_t validUntil,
140 std::optional<std::size_t> validFrom = {})
141 {
142 std::string data = "{\"sequence\":" + std::to_string(sequence) +
143 ",\"expiration\":" + std::to_string(validUntil);
144 if (validFrom)
145 data += ",\"effective\":" + std::to_string(*validFrom);
146 data += ",\"validators\":[";
147
148 for (auto const& val : validators)
149 {
150 data += R"({"validation_public_key":")" + strHex(val.masterPublic) +
151 R"(","manifest":")" + val.manifest + "\"},";
152 }
153
154 data.pop_back();
155 data += "]}";
156 return base64Encode(data);
157 }
158
159 static std::string
161 {
162 auto const data = base64Decode(blob);
163 return strHex(sign(keys.first, keys.second, makeSlice(data)));
164 }
165
166 static HashSet<NodeID>
168 {
169 HashSet<NodeID> res;
170 res.reserve(pks.size());
171 for (auto const& pk : pks)
172 res.insert(calcNodeID(pk));
173 return res;
174 }
175
176 void
179 PublicKey pubKey,
180 ListDisposition expectedWorst,
181 ListDisposition expectedBest)
182 {
183 BEAST_EXPECT(
185 (result.publisherKey && *result.publisherKey == pubKey));
186 BEAST_EXPECT(result.bestDisposition() == expectedBest);
187 BEAST_EXPECT(result.worstDisposition() == expectedWorst);
188 }
189
190 void
192 {
193 testcase("Genesis Quorum");
194
195 ManifestCache manifests;
196 jtx::Env env(*this);
197 auto& app = env.app();
198 {
199 auto trustedKeys = std::make_unique<ValidatorList>(
200 manifests,
201 manifests,
202 env.timeKeeper(),
203 app.config().legacy(Sections::kDatabasePath),
204 env.journal);
205 BEAST_EXPECT(trustedKeys->quorum() == 1);
206 }
207 {
208 std::size_t minQuorum = 0;
209 auto trustedKeys = std::make_unique<ValidatorList>(
210 manifests,
211 manifests,
212 env.timeKeeper(),
213 app.config().legacy(Sections::kDatabasePath),
214 env.journal,
215 minQuorum);
216 BEAST_EXPECT(trustedKeys->quorum() == minQuorum);
217 }
218 }
219
220 void
222 {
223 testcase("Config Load");
224
225 jtx::Env env(*this, jtx::envconfig(), nullptr, beast::Severity::Disabled);
226 auto& app = env.app();
227 std::vector<std::string> const emptyCfgKeys;
228 std::vector<std::string> const emptyCfgPublishers;
229
230 auto const localSigningKeys = randomKeyPair(KeyType::Secp256k1);
231 auto const localSigningPublicOuter = localSigningKeys.first;
232 auto const localSigningSecret = localSigningKeys.second;
233 auto const localMasterSecret = randomSecretKey();
234 auto const localMasterPublic = derivePublicKey(KeyType::Ed25519, localMasterSecret);
235
236 std::string const cfgManifest(makeManifestString(
237 localMasterPublic, localMasterSecret, localSigningPublicOuter, localSigningSecret, 1));
238
239 auto const format = [](PublicKey const& publicKey, char const* comment = nullptr) {
240 auto ret = toBase58(TokenType::NodePublic, publicKey);
241
242 if (comment)
243 ret += comment;
244
245 return ret;
246 };
247
248 std::vector<PublicKey> configList;
249 configList.reserve(8);
250
251 while (configList.size() != 8)
252 configList.push_back(randomNode());
253
254 // Correct configuration
256 format(configList[0]),
257 format(configList[1], " Comment"),
258 format(configList[2], " Multi Word Comment"),
259 format(configList[3], " Leading Whitespace"),
260 format(configList[4], " Trailing Whitespace "),
261 format(configList[5], " Leading & Trailing Whitespace "),
262 format(configList[6], " Leading, Trailing & Internal Whitespace "),
263 format(configList[7], " "),
264 });
265
266 {
267 ManifestCache manifests;
268 auto trustedKeys = std::make_unique<ValidatorList>(
269 manifests,
270 manifests,
271 env.timeKeeper(),
272 app.config().legacy(Sections::kDatabasePath),
273 env.journal);
274
275 // Correct (empty) configuration
276 BEAST_EXPECT(trustedKeys->load({}, emptyCfgKeys, emptyCfgPublishers));
277
278 // load local validator key with or without manifest
279 BEAST_EXPECT(
280 trustedKeys->load(localSigningPublicOuter, emptyCfgKeys, emptyCfgPublishers));
281 BEAST_EXPECT(trustedKeys->listed(localSigningPublicOuter));
282
283 // NOLINTBEGIN(bugprone-unchecked-optional-access)
284 manifests.applyManifest(
286 // NOLINTEND(bugprone-unchecked-optional-access)
287 BEAST_EXPECT(
288 trustedKeys->load(localSigningPublicOuter, emptyCfgKeys, emptyCfgPublishers));
289
290 BEAST_EXPECT(trustedKeys->listed(localMasterPublic));
291 BEAST_EXPECT(trustedKeys->listed(localSigningPublicOuter));
292 }
293 {
294 // load should add validator keys from config
295 ManifestCache manifests;
296 auto trustedKeys = std::make_unique<ValidatorList>(
297 manifests,
298 manifests,
299 env.timeKeeper(),
300 app.config().legacy(Sections::kDatabasePath),
301 env.journal);
302
303 BEAST_EXPECT(trustedKeys->load({}, cfgKeys, emptyCfgPublishers));
304
305 for (auto const& n : configList)
306 BEAST_EXPECT(trustedKeys->listed(n));
307
308 // load should accept Ed25519 master public keys
309 auto const masterNode1 = randomMasterKey();
310 auto const masterNode2 = randomMasterKey();
311
312 std::vector<std::string> const cfgMasterKeys(
313 {format(masterNode1), format(masterNode2, " Comment")});
314 BEAST_EXPECT(trustedKeys->load({}, cfgMasterKeys, emptyCfgPublishers));
315 BEAST_EXPECT(trustedKeys->listed(masterNode1));
316 BEAST_EXPECT(trustedKeys->listed(masterNode2));
317
318 // load should reject invalid config keys
319 BEAST_EXPECT(!trustedKeys->load({}, {"NotAPublicKey"}, emptyCfgPublishers));
320 BEAST_EXPECT(!trustedKeys->load({}, {format(randomNode(), "!")}, emptyCfgPublishers));
321
322 // load terminates when encountering an invalid entry
323 auto const goodKey = randomNode();
324 BEAST_EXPECT(!trustedKeys->load(
325 {}, {format(randomNode(), "!"), format(goodKey)}, emptyCfgPublishers));
326 BEAST_EXPECT(!trustedKeys->listed(goodKey));
327 }
328 {
329 // local validator key on config list
330 ManifestCache manifests;
331 auto trustedKeys = std::make_unique<ValidatorList>(
332 manifests,
333 manifests,
334 env.timeKeeper(),
335 app.config().legacy(Sections::kDatabasePath),
336 env.journal);
337
338 auto const localSigningPublic =
340
341 BEAST_EXPECT(trustedKeys->load(localSigningPublic, cfgKeys, emptyCfgPublishers));
342 BEAST_EXPECT(trustedKeys->localPublicKey() == localSigningPublic);
343 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
344 BEAST_EXPECT(trustedKeys->listed(*localSigningPublic));
345 for (auto const& n : configList)
346 BEAST_EXPECT(trustedKeys->listed(n));
347 }
348 {
349 // local validator key not on config list
350 ManifestCache manifests;
351 auto trustedKeys = std::make_unique<ValidatorList>(
352 manifests,
353 manifests,
354 env.timeKeeper(),
355 app.config().legacy(Sections::kDatabasePath),
356 env.journal);
357
358 auto const localSigningPublic = randomNode();
359 BEAST_EXPECT(trustedKeys->load(localSigningPublic, cfgKeys, emptyCfgPublishers));
360
361 BEAST_EXPECT(trustedKeys->localPublicKey() == localSigningPublic);
362 BEAST_EXPECT(trustedKeys->listed(localSigningPublic));
363 for (auto const& n : configList)
364 BEAST_EXPECT(trustedKeys->listed(n));
365 }
366 {
367 // local validator key (with manifest) not on config list
368 ManifestCache manifests;
369 auto trustedKeys = std::make_unique<ValidatorList>(
370 manifests,
371 manifests,
372 env.timeKeeper(),
373 app.config().legacy(Sections::kDatabasePath),
374 env.journal);
375
376 // NOLINTBEGIN(bugprone-unchecked-optional-access)
377 manifests.applyManifest(
379 // NOLINTEND(bugprone-unchecked-optional-access)
380
381 BEAST_EXPECT(trustedKeys->load(localSigningPublicOuter, cfgKeys, emptyCfgPublishers));
382
383 BEAST_EXPECT(trustedKeys->localPublicKey() == localMasterPublic);
384 BEAST_EXPECT(trustedKeys->listed(localSigningPublicOuter));
385 BEAST_EXPECT(trustedKeys->listed(localMasterPublic));
386 for (auto const& n : configList)
387 BEAST_EXPECT(trustedKeys->listed(n));
388 }
389 {
390 ManifestCache manifests;
391 auto trustedKeys = std::make_unique<ValidatorList>(
392 manifests,
393 manifests,
394 env.timeKeeper(),
395 app.config().legacy(Sections::kDatabasePath),
396 env.journal);
397
398 // load should reject invalid validator list signing keys
399 std::vector<std::string> badPublishers({"NotASigningKey"});
400 BEAST_EXPECT(!trustedKeys->load({}, emptyCfgKeys, badPublishers));
401
402 // load should reject validator list signing keys with invalid
403 // encoding
404 std::vector<PublicKey> const keys(
406 badPublishers.clear();
407 for (auto const& key : keys)
408 badPublishers.push_back(toBase58(TokenType::NodePublic, key));
409
410 BEAST_EXPECT(!trustedKeys->load({}, emptyCfgKeys, badPublishers));
411 for (auto const& key : keys)
412 BEAST_EXPECT(!trustedKeys->trustedPublisher(key));
413
414 // load should accept valid validator list publisher keys
415 std::vector<std::string> cfgPublishers;
416 cfgPublishers.reserve(keys.size());
417 for (auto const& key : keys)
418 cfgPublishers.push_back(strHex(key));
419
420 BEAST_EXPECT(trustedKeys->load({}, emptyCfgKeys, cfgPublishers));
421 for (auto const& key : keys)
422 BEAST_EXPECT(trustedKeys->trustedPublisher(key));
423 BEAST_EXPECT(trustedKeys->getListThreshold() == (keys.size() / 2) + 1);
424 }
425 {
426 ManifestCache manifests;
427 auto trustedKeys = std::make_unique<ValidatorList>(
428 manifests,
429 manifests,
430 env.timeKeeper(),
431 app.config().legacy(Sections::kDatabasePath),
432 env.journal);
433
434 std::vector<PublicKey> const keys(
436 std::vector<std::string> cfgPublishers;
437 cfgPublishers.reserve(keys.size());
438 for (auto const& key : keys)
439 cfgPublishers.push_back(strHex(key));
440
441 // explicitly set the list threshold
442 BEAST_EXPECT(trustedKeys->load({}, emptyCfgKeys, cfgPublishers, std::size_t(2)));
443 for (auto const& key : keys)
444 BEAST_EXPECT(trustedKeys->trustedPublisher(key));
445 BEAST_EXPECT(trustedKeys->getListThreshold() == 2);
446 }
447 {
448 // Attempt to load a publisher key that has been revoked.
449 // Should fail
450 ManifestCache valManifests;
451 ManifestCache pubManifests;
452 auto trustedKeys = std::make_unique<ValidatorList>(
453 valManifests,
454 pubManifests,
455 env.timeKeeper(),
456 app.config().legacy(Sections::kDatabasePath),
457 env.journal);
458
459 auto const pubRevokedSecret = randomSecretKey();
460 auto const pubRevokedPublic = derivePublicKey(KeyType::Ed25519, pubRevokedSecret);
461 auto const pubRevokedSigning = randomKeyPair(KeyType::Secp256k1);
462 // make this manifest revoked (seq num = max)
463 // -- thus should not be loaded
464 // NOLINTBEGIN(bugprone-unchecked-optional-access)
465 pubManifests.applyManifest(
467 pubRevokedPublic,
468 pubRevokedSecret,
469 pubRevokedSigning.first,
470 pubRevokedSigning.second,
473 // NOLINTEND(bugprone-unchecked-optional-access)
474
475 // these two are not revoked (and not in the manifest cache at all.)
476 auto const legitKey1 = randomMasterKey();
477 auto const legitKey2 = randomMasterKey();
478
479 std::vector<std::string> const cfgPublishers = {
480 strHex(pubRevokedPublic),
481 strHex(legitKey1),
482 strHex(legitKey2),
483 };
484 BEAST_EXPECT(trustedKeys->load({}, emptyCfgKeys, cfgPublishers));
485
486 BEAST_EXPECT(!trustedKeys->trustedPublisher(pubRevokedPublic));
487 BEAST_EXPECT(trustedKeys->trustedPublisher(legitKey1));
488 BEAST_EXPECT(trustedKeys->trustedPublisher(legitKey2));
489 // 2 is the threshold for 3 publishers (even though 1 is revoked)
490 BEAST_EXPECT(trustedKeys->getListThreshold() == 2);
491 }
492 {
493 // One (of two) publisher keys has been revoked, the user had
494 // explicitly set validator list threshold to 2.
495 ManifestCache valManifests;
496 ManifestCache pubManifests;
497 auto trustedKeys = std::make_unique<ValidatorList>(
498 valManifests,
499 pubManifests,
500 env.timeKeeper(),
501 app.config().legacy(Sections::kDatabasePath),
502 env.journal);
503
504 auto const pubRevokedSecret = randomSecretKey();
505 auto const pubRevokedPublic = derivePublicKey(KeyType::Ed25519, pubRevokedSecret);
506 auto const pubRevokedSigning = randomKeyPair(KeyType::Secp256k1);
507 // make this manifest revoked (seq num = max)
508 // -- thus should not be loaded
509 // NOLINTBEGIN(bugprone-unchecked-optional-access)
510 pubManifests.applyManifest(
512 pubRevokedPublic,
513 pubRevokedSecret,
514 pubRevokedSigning.first,
515 pubRevokedSigning.second,
518 // NOLINTEND(bugprone-unchecked-optional-access)
519
520 // this one is not revoked (and not in the manifest cache at all.)
521 auto const legitKey = randomMasterKey();
522
523 std::vector<std::string> const cfgPublishers = {
524 strHex(pubRevokedPublic),
525 strHex(legitKey),
526 };
527 BEAST_EXPECT(trustedKeys->load({}, emptyCfgKeys, cfgPublishers, std::size_t(2)));
528
529 BEAST_EXPECT(!trustedKeys->trustedPublisher(pubRevokedPublic));
530 BEAST_EXPECT(trustedKeys->trustedPublisher(legitKey));
531 // 2 is the threshold, as requested in configuration
532 BEAST_EXPECT(trustedKeys->getListThreshold() == 2);
533 }
534 }
535
536 void
538 {
539 testcase("Apply list");
540 using namespace std::chrono_literals;
541
542 std::string const siteUri = "testApplyList.test";
543
544 auto const checkAvailable =
545 [this](
546 auto const& trustedKeys,
547 auto const& hexPublic,
548 auto const& manifest,
549 auto const version,
551 auto const available = trustedKeys->getAvailable(hexPublic);
552
553 BEAST_EXPECT(!version || available);
554 if (available)
555 {
556 auto const& a = *available;
557 BEAST_EXPECT(a[jss::public_key] == hexPublic);
558 BEAST_EXPECT(a[jss::manifest] == manifest);
559 // Because multiple lists were processed, the version was
560 // overridden
561 BEAST_EXPECT(a[jss::version] == version);
562 if (version == 1)
563 {
564 BEAST_EXPECT(expected.size() == 1);
565 BEAST_EXPECT(a[jss::blob] == expected[0].first);
566 BEAST_EXPECT(a[jss::signature] == expected[0].second);
567 BEAST_EXPECT(!a.isMember(jss::blobs_v2));
568 }
569 else if (BEAST_EXPECT(a.isMember(jss::blobs_v2)))
570 {
571 BEAST_EXPECT(!a.isMember(jss::blob));
572 BEAST_EXPECT(!a.isMember(jss::signature));
573 auto const& blobsV2 = a[jss::blobs_v2];
574 BEAST_EXPECT(blobsV2.isArray() && blobsV2.size() == expected.size());
575
576 for (unsigned int i = 0; i < expected.size(); ++i)
577 {
578 BEAST_EXPECT(blobsV2[i][jss::blob] == expected[i].first);
579 BEAST_EXPECT(blobsV2[i][jss::signature] == expected[i].second);
580 }
581 }
582 }
583 };
584
585 ManifestCache manifests;
586 jtx::Env env(*this);
587 auto& app = env.app();
588 auto trustedKeys = std::make_unique<ValidatorList>(
589 manifests,
590 manifests,
591 env.app().getTimeKeeper(),
592 app.config().legacy(Sections::kDatabasePath),
593 env.journal);
594
595 auto const expectTrusted = [this, &trustedKeys](std::vector<Validator> const& list) {
596 for (auto const& val : list)
597 {
598 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
599 BEAST_EXPECT(trustedKeys->listed(val.signingPublic));
600 }
601 };
602
603 auto const expectUntrusted = [this, &trustedKeys](std::vector<Validator> const& list) {
604 for (auto const& val : list)
605 {
606 BEAST_EXPECT(!trustedKeys->listed(val.masterPublic));
607 BEAST_EXPECT(!trustedKeys->listed(val.signingPublic));
608 }
609 };
610
611 auto const publisherSecret = randomSecretKey();
612 auto const publisherPublic = derivePublicKey(KeyType::Ed25519, publisherSecret);
613 auto const hexPublic = strHex(publisherPublic.begin(), publisherPublic.end());
614 auto const pubSigningKeys1 = randomKeyPair(KeyType::Secp256k1);
615 auto const manifest1 = base64Encode(makeManifestString(
616 publisherPublic, publisherSecret, pubSigningKeys1.first, pubSigningKeys1.second, 1));
617
618 std::vector<std::string> const cfgPublisherKeys({strHex(publisherPublic)});
619 std::vector<std::string> const emptyCfgKeys;
620
621 BEAST_EXPECT(trustedKeys->load({}, emptyCfgKeys, cfgPublisherKeys));
622
624 static constexpr auto kListSize = 20;
625 static constexpr auto kNumLists = 9;
627 // 1-based to correspond with the individually named blobs below.
628 for (auto i = 1; i <= kNumLists; ++i)
629 {
630 auto& list = lists[i];
631 list.reserve(kListSize);
632 while (list.size() < kListSize)
633 list.push_back(randomValidator());
634 }
635 return lists;
636 }();
637
638 // Attempt an expired list (fail) and a single list (succeed)
639 env.timeKeeper().set(env.timeKeeper().now() + 1s);
640 auto const version = 1;
641 auto const sequence1 = 1;
642 auto const expiredblob =
643 makeList(lists.at(1), sequence1, env.timeKeeper().now().time_since_epoch().count());
644 auto const expiredSig = signList(expiredblob, pubSigningKeys1);
645
646 NetClock::time_point const validUntil = env.timeKeeper().now() + 3600s;
647 auto const sequence2 = 2;
648 auto const blob2 = makeList(lists.at(2), sequence2, validUntil.time_since_epoch().count());
649 auto const sig2 = signList(blob2, pubSigningKeys1);
650
652 trustedKeys->applyLists(
653 manifest1,
654 version,
655 {
656 {.blob = expiredblob, .signature = expiredSig, .manifest = {}},
657 {.blob = blob2, .signature = sig2, .manifest = {}},
658 },
659 siteUri),
660 publisherPublic,
663
664 expectTrusted(lists.at(2));
665
666 checkAvailable(trustedKeys, hexPublic, manifest1, version, {{blob2, sig2}});
667
668 // Do not apply future lists, but process them
669 auto const version2 = 2;
670 auto const sequence7 = 7;
671 auto const effective7 = validUntil - 60s;
672 auto const expiration7 = effective7 + 3600s;
673 auto const blob7 = makeList(
674 lists.at(7),
675 sequence7,
676 expiration7.time_since_epoch().count(),
677 effective7.time_since_epoch().count());
678 auto const sig7 = signList(blob7, pubSigningKeys1);
679
680 auto const sequence8 = 8;
681 auto const effective8 = expiration7 - 60s;
682 auto const expiration8 = effective8 + 3600s;
683 auto const blob8 = makeList(
684 lists.at(8),
685 sequence8,
686 expiration8.time_since_epoch().count(),
687 effective8.time_since_epoch().count());
688 auto const sig8 = signList(blob8, pubSigningKeys1);
689
690 checkResult(
691 trustedKeys->applyLists(
692 manifest1,
693 version2,
694 {
695 {.blob = blob7, .signature = sig7, .manifest = {}},
696 {.blob = blob8, .signature = sig8, .manifest = {}},
697 },
698 siteUri),
699 publisherPublic,
700 ListDisposition::Pending,
701 ListDisposition::Pending);
702
703 expectUntrusted(lists.at(7));
704 expectUntrusted(lists.at(8));
705
706 // Do not apply out-of-order future list, but process it
707 auto const sequence6 = 6;
708 auto const effective6 = effective7 - 60s;
709 auto const expiration6 = effective6 + 3600s;
710 auto const blob6 = makeList(
711 lists.at(6),
712 sequence6,
713 expiration6.time_since_epoch().count(),
714 effective6.time_since_epoch().count());
715 auto const sig6 = signList(blob6, pubSigningKeys1);
716
717 // Process future list that is overridden by a later list
718 auto const sequence6a = 5;
719 auto const effective6a = effective6 + 60s;
720 auto const expiration6a = effective6a + 3600s;
721 auto const blob6a = makeList(
722 lists.at(5),
723 sequence6a,
724 expiration6a.time_since_epoch().count(),
725 effective6a.time_since_epoch().count());
726 auto const sig6a = signList(blob6a, pubSigningKeys1);
727
728 checkResult(
729 trustedKeys->applyLists(
730 manifest1,
731 version,
732 {
733 {.blob = blob6a, .signature = sig6a, .manifest = {}},
734 {.blob = blob6, .signature = sig6, .manifest = {}},
735 },
736 siteUri),
737 publisherPublic,
738 ListDisposition::Pending,
739 ListDisposition::Pending);
740
741 expectUntrusted(lists.at(6));
742 expectTrusted(lists.at(2));
743
744 // Do not apply re-process lists known future sequence numbers
745
746 checkResult(
747 trustedKeys->applyLists(
748 manifest1,
749 version,
750 {
751 {.blob = blob7, .signature = sig7, .manifest = {}},
752 {.blob = blob6, .signature = sig6, .manifest = {}},
753 },
754 siteUri),
755 publisherPublic,
756 ListDisposition::KnownSequence,
757 ListDisposition::KnownSequence);
758
759 expectUntrusted(lists.at(6));
760 expectUntrusted(lists.at(7));
761 expectTrusted(lists.at(2));
762
763 // try empty or mangled manifest
764 checkResult(
765 trustedKeys->applyLists(
766 "",
767 version,
768 {
769 {.blob = blob7, .signature = sig7, .manifest = {}},
770 {.blob = blob6, .signature = sig6, .manifest = {}},
771 },
772 siteUri),
773 publisherPublic,
774 ListDisposition::Invalid,
775 ListDisposition::Invalid);
776
777 checkResult(
778 trustedKeys->applyLists(
779 base64Encode("not a manifest"),
780 version,
781 {
782 {.blob = blob7, .signature = sig7, .manifest = {}},
783 {.blob = blob6, .signature = sig6, .manifest = {}},
784 },
785 siteUri),
786 publisherPublic,
787 ListDisposition::Invalid,
788 ListDisposition::Invalid);
789
790 // do not use list from untrusted publisher
791 auto const untrustedManifest = base64Encode(makeManifestString(
792 randomMasterKey(), publisherSecret, pubSigningKeys1.first, pubSigningKeys1.second, 1));
793
794 checkResult(
795 trustedKeys->applyLists(
796 untrustedManifest,
797 version,
798 {{.blob = blob2, .signature = sig2, .manifest = {}}},
799 siteUri),
800 publisherPublic,
801 ListDisposition::Untrusted,
802 ListDisposition::Untrusted);
803
804 // do not use list with unhandled version
805 auto const badVersion = 666;
806 checkResult(
807 trustedKeys->applyLists(
808 manifest1,
809 badVersion,
810 {{.blob = blob2, .signature = sig2, .manifest = {}}},
811 siteUri),
812 publisherPublic,
813 ListDisposition::UnsupportedVersion,
814 ListDisposition::UnsupportedVersion);
815
816 // apply list with highest sequence number
817 auto const sequence3 = 3;
818 auto const blob3 = makeList(lists.at(3), sequence3, validUntil.time_since_epoch().count());
819 auto const sig3 = signList(blob3, pubSigningKeys1);
820
821 checkResult(
822 trustedKeys->applyLists(
823 manifest1, version, {{.blob = blob3, .signature = sig3, .manifest = {}}}, siteUri),
824 publisherPublic,
825 ListDisposition::Accepted,
826 ListDisposition::Accepted);
827
828 expectUntrusted(lists.at(1));
829 expectUntrusted(lists.at(2));
830 expectTrusted(lists.at(3));
831
832 // Note that blob6a is not present, because it was dropped during
833 // processing
834 checkAvailable(
835 trustedKeys,
836 hexPublic,
837 manifest1,
838 2,
839 {{blob3, sig3}, {blob6, sig6}, {blob7, sig7}, {blob8, sig8}});
840
841 // do not re-apply lists with past or current sequence numbers
842 checkResult(
843 trustedKeys->applyLists(
844 manifest1,
845 version,
846 {
847 {.blob = blob2, .signature = sig2, .manifest = {}},
848 {.blob = blob3, .signature = sig3, .manifest = {}},
849 },
850 siteUri),
851 publisherPublic,
852 ListDisposition::Stale,
853 ListDisposition::SameSequence);
854
855 // apply list with new publisher key updated by manifest. Also send some
856 // old lists along with the old manifest
857 auto const pubSigningKeys2 = randomKeyPair(KeyType::Secp256k1);
858 auto const manifest2 = base64Encode(makeManifestString(
859 publisherPublic, publisherSecret, pubSigningKeys2.first, pubSigningKeys2.second, 2));
860
861 auto const sequence4 = 4;
862 auto const blob4 = makeList(lists.at(4), sequence4, validUntil.time_since_epoch().count());
863 auto const sig4 = signList(blob4, pubSigningKeys2);
864
865 checkResult(
866 trustedKeys->applyLists(
867 manifest2,
868 version,
869 {
870 {.blob = blob2, .signature = sig2, .manifest = manifest1},
871 {.blob = blob3, .signature = sig3, .manifest = manifest1},
872 {.blob = blob4, .signature = sig4, .manifest = {}},
873 },
874 siteUri),
875 publisherPublic,
876 ListDisposition::Stale,
877 ListDisposition::Accepted);
878
879 expectUntrusted(lists.at(2));
880 expectUntrusted(lists.at(3));
881 expectTrusted(lists.at(4));
882
883 checkAvailable(
884 trustedKeys,
885 hexPublic,
886 manifest2,
887 2,
888 {{blob4, sig4}, {blob6, sig6}, {blob7, sig7}, {blob8, sig8}});
889
890 auto const sequence5 = 5;
891 auto const blob5 = makeList(lists.at(5), sequence5, validUntil.time_since_epoch().count());
892 auto const badSig = signList(blob5, pubSigningKeys1);
893 checkResult(
894 trustedKeys->applyLists(
895 manifest1,
896 version,
897 {{.blob = blob5, .signature = badSig, .manifest = {}}},
898 siteUri),
899 publisherPublic,
900 ListDisposition::Invalid,
901 ListDisposition::Invalid);
902
903 expectUntrusted(lists.at(2));
904 expectUntrusted(lists.at(3));
905 expectTrusted(lists.at(4));
906 expectUntrusted(lists.at(5));
907
908 // Reprocess the pending list, but the signature is no longer valid
909 checkResult(
910 trustedKeys->applyLists(
911 manifest1,
912 version,
913 {
914 {.blob = blob7, .signature = sig7, .manifest = {}},
915 {.blob = blob8, .signature = sig8, .manifest = {}},
916 },
917 siteUri),
918 publisherPublic,
919 ListDisposition::Invalid,
920 ListDisposition::Invalid);
921
922 expectTrusted(lists.at(4));
923 expectUntrusted(lists.at(7));
924 expectUntrusted(lists.at(8));
925
926 // Automatically rotate the first pending already processed list using
927 // updateTrusted. Note that the timekeeper is NOT moved, so the close
928 // time will be ahead of the test's wall clock
929 trustedKeys->updateTrusted(
930 {},
931 effective6 + 1s,
932 env.app().getOPs(),
933 env.app().getOverlay(),
934 env.app().getHashRouter());
935
936 expectUntrusted(lists.at(3));
937 expectTrusted(lists.at(6));
938
939 checkAvailable(
940 trustedKeys, hexPublic, manifest2, 2, {{blob6, sig6}, {blob7, sig7}, {blob8, sig8}});
941
942 // Automatically rotate the LAST pending list using updateTrusted,
943 // bypassing blob7. Note that the timekeeper IS moved, so the provided
944 // close time will be behind the test's wall clock, and thus the wall
945 // clock is used.
946 env.timeKeeper().set(effective8);
947 trustedKeys->updateTrusted(
948 {},
949 effective8 + 1s,
950 env.app().getOPs(),
951 env.app().getOverlay(),
952 env.app().getHashRouter());
953
954 expectUntrusted(lists.at(6));
955 expectUntrusted(lists.at(7));
956 expectTrusted(lists.at(8));
957
958 checkAvailable(trustedKeys, hexPublic, manifest2, 2, {{blob8, sig8}});
959
960 // resign the pending list with new key and validate it, but it's
961 // already valid Also try reprocessing the pending list with an
962 // explicit manifest
963 // - it is still invalid
964 auto const sig82 = signList(blob8, pubSigningKeys2);
965
966 checkResult(
967 trustedKeys->applyLists(
968 manifest2,
969 version,
970 {
971 {.blob = blob8, .signature = sig8, .manifest = manifest1},
972 {.blob = blob8, .signature = sig82, .manifest = {}},
973 },
974 siteUri),
975 publisherPublic,
976 ListDisposition::Invalid,
977 ListDisposition::SameSequence);
978
979 expectTrusted(lists.at(8));
980
981 checkAvailable(trustedKeys, hexPublic, manifest2, 2, {{blob8, sig8}});
982
983 // do not apply list with revoked publisher key
984 // applied list is removed due to revoked publisher key
985 auto const signingKeysMax = randomKeyPair(KeyType::Secp256k1);
986 auto const maxManifest =
987 base64Encode(makeRevocationString(publisherPublic, publisherSecret));
988
989 auto const sequence9 = 9;
990 auto const blob9 = makeList(lists.at(9), sequence9, validUntil.time_since_epoch().count());
991 auto const sig9 = signList(blob9, signingKeysMax);
992
993 checkResult(
994 trustedKeys->applyLists(
995 maxManifest,
996 version,
997 {{.blob = blob9, .signature = sig9, .manifest = {}}},
998 siteUri),
999 publisherPublic,
1000 ListDisposition::Untrusted,
1001 ListDisposition::Untrusted);
1002
1003 BEAST_EXPECT(!trustedKeys->trustedPublisher(publisherPublic));
1004 for (auto const& [num, list] : lists)
1005 {
1006 (void)num;
1007 expectUntrusted(list);
1008 }
1009
1010 checkAvailable(trustedKeys, hexPublic, manifest2, 0, {});
1011 }
1012
1013 void
1015 {
1016 testcase("GetAvailable");
1017 using namespace std::chrono_literals;
1018
1019 std::string const siteUri = "testApplyList.test";
1020
1021 ManifestCache manifests;
1022 jtx::Env env(*this);
1023 auto& app = env.app();
1024 auto trustedKeys = std::make_unique<ValidatorList>(
1025 manifests,
1026 manifests,
1027 env.app().getTimeKeeper(),
1028 app.config().legacy(Sections::kDatabasePath),
1029 env.journal);
1030
1031 auto const publisherSecret = randomSecretKey();
1032 auto const publisherPublic = derivePublicKey(KeyType::Ed25519, publisherSecret);
1033 auto const hexPublic = strHex(publisherPublic.begin(), publisherPublic.end());
1034 auto const pubSigningKeys1 = randomKeyPair(KeyType::Secp256k1);
1035 auto const manifest = base64Encode(makeManifestString(
1036 publisherPublic, publisherSecret, pubSigningKeys1.first, pubSigningKeys1.second, 1));
1037
1038 std::vector<std::string> const cfgPublisherKeys({strHex(publisherPublic)});
1039 std::vector<std::string> const emptyCfgKeys;
1040
1041 BEAST_EXPECT(trustedKeys->load({}, emptyCfgKeys, cfgPublisherKeys));
1042
1043 std::vector<Validator> const list = [] {
1044 static constexpr auto kListSize = 20;
1046 list.reserve(kListSize);
1047 while (list.size() < kListSize)
1048 list.push_back(randomValidator());
1049 return list;
1050 }();
1051
1052 // Process a list
1053 env.timeKeeper().set(env.timeKeeper().now() + 1s);
1054 NetClock::time_point const validUntil = env.timeKeeper().now() + 3600s;
1055 auto const blob = makeList(list, 1, validUntil.time_since_epoch().count());
1056 auto const sig = signList(blob, pubSigningKeys1);
1057
1058 {
1059 // list unavailable
1060 auto const available = trustedKeys->getAvailable(hexPublic);
1061 BEAST_EXPECT(!available);
1062 }
1063
1064 BEAST_EXPECT(
1065 trustedKeys->applyLists(manifest, 1, {{blob, sig, {}}}, siteUri).bestDisposition() ==
1067
1068 {
1069 // invalid public key
1070 auto const available = trustedKeys->getAvailable(hexPublic + "invalid", 1);
1071 BEAST_EXPECT(!available);
1072 }
1073
1074 {
1075 // unknown public key
1076 auto const badSecret = randomSecretKey();
1077 auto const badPublic = derivePublicKey(KeyType::Ed25519, badSecret);
1078 auto const hexBad = strHex(badPublic.begin(), badPublic.end());
1079
1080 auto const available = trustedKeys->getAvailable(hexBad, 1);
1081 BEAST_EXPECT(!available);
1082 }
1083 {
1084 // bad version 0
1085 auto const available = trustedKeys->getAvailable(hexPublic, 0);
1086 if (BEAST_EXPECT(available))
1087 {
1088 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1089 auto const& a = *available;
1090 BEAST_EXPECT(!a);
1091 }
1092 }
1093 {
1094 // bad version 3
1095 auto const available = trustedKeys->getAvailable(hexPublic, 3);
1096 if (BEAST_EXPECT(available))
1097 {
1098 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1099 auto const& a = *available;
1100 BEAST_EXPECT(!a);
1101 }
1102 }
1103 {
1104 // version 1
1105 auto const available = trustedKeys->getAvailable(hexPublic, 1);
1106 if (BEAST_EXPECT(available))
1107 {
1108 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1109 auto const& a = *available;
1110 BEAST_EXPECT(a[jss::public_key] == hexPublic);
1111 BEAST_EXPECT(a[jss::manifest] == manifest);
1112 BEAST_EXPECT(a[jss::version] == 1);
1113
1114 BEAST_EXPECT(a[jss::blob] == blob);
1115 BEAST_EXPECT(a[jss::signature] == sig);
1116 BEAST_EXPECT(!a.isMember(jss::blobs_v2));
1117 }
1118 }
1119
1120 {
1121 // version 2
1122 auto const available = trustedKeys->getAvailable(hexPublic, 2);
1123 if (BEAST_EXPECT(available))
1124 {
1125 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1126 auto const& a = *available;
1127 BEAST_EXPECT(a[jss::public_key] == hexPublic);
1128 BEAST_EXPECT(a[jss::manifest] == manifest);
1129 BEAST_EXPECT(a[jss::version] == 2);
1130
1131 if (BEAST_EXPECT(a.isMember(jss::blobs_v2)))
1132 {
1133 BEAST_EXPECT(!a.isMember(jss::blob));
1134 BEAST_EXPECT(!a.isMember(jss::signature));
1135 auto const& blobsV2 = a[jss::blobs_v2];
1136 BEAST_EXPECT(blobsV2.isArray() && blobsV2.size() == 1);
1137
1138 BEAST_EXPECT(blobsV2[0u][jss::blob] == blob);
1139 BEAST_EXPECT(blobsV2[0u][jss::signature] == sig);
1140 }
1141 }
1142 }
1143 }
1144
1145 void
1147 {
1148 testcase("Update trusted");
1149
1150 std::string const siteUri = "testUpdateTrusted.test";
1151
1152 ManifestCache manifestsOuter;
1153 jtx::Env env(*this);
1154 auto& app = env.app();
1155 auto trustedKeysOuter = std::make_unique<ValidatorList>(
1156 manifestsOuter,
1157 manifestsOuter,
1158 env.timeKeeper(),
1159 app.config().legacy(Sections::kDatabasePath),
1160 env.journal);
1161
1162 std::vector<std::string> const cfgPublishersOuter;
1163 HashSet<NodeID> activeValidatorsOuter;
1164
1165 std::size_t const maxKeys = 40;
1166 {
1168 cfgKeys.reserve(maxKeys);
1169 HashSet<NodeID> unseenValidators;
1170
1171 while (cfgKeys.size() != maxKeys)
1172 {
1173 auto const valKey = randomNode();
1174 cfgKeys.push_back(toBase58(TokenType::NodePublic, valKey));
1175 if (cfgKeys.size() <= maxKeys - 5)
1176 {
1177 activeValidatorsOuter.emplace(calcNodeID(valKey));
1178 }
1179 else
1180 {
1181 unseenValidators.emplace(calcNodeID(valKey));
1182 }
1183 }
1184
1185 BEAST_EXPECT(trustedKeysOuter->load({}, cfgKeys, cfgPublishersOuter));
1186
1187 // updateTrusted should make all configured validators trusted
1188 // even if they are not active/seen
1189 TrustChanges changes = trustedKeysOuter->updateTrusted(
1190 activeValidatorsOuter,
1191 env.timeKeeper().now(),
1192 env.app().getOPs(),
1193 env.app().getOverlay(),
1194 env.app().getHashRouter());
1195
1196 for (auto const& val : unseenValidators)
1197 activeValidatorsOuter.emplace(val);
1198
1199 BEAST_EXPECT(changes.added == activeValidatorsOuter);
1200 BEAST_EXPECT(changes.removed.empty());
1201 BEAST_EXPECT(trustedKeysOuter->quorum() == std::ceil(cfgKeys.size() * 0.8f));
1202 for (auto const& val : cfgKeys)
1203 {
1204 if (auto const valKey = parseBase58<PublicKey>(TokenType::NodePublic, val))
1205 {
1206 BEAST_EXPECT(trustedKeysOuter->listed(*valKey));
1207 BEAST_EXPECT(trustedKeysOuter->trusted(*valKey));
1208 }
1209 else
1210 {
1211 fail();
1212 }
1213 }
1214
1215 changes = trustedKeysOuter->updateTrusted(
1216 activeValidatorsOuter,
1217 env.timeKeeper().now(),
1218 env.app().getOPs(),
1219 env.app().getOverlay(),
1220 env.app().getHashRouter());
1221 BEAST_EXPECT(changes.added.empty());
1222 BEAST_EXPECT(changes.removed.empty());
1223 BEAST_EXPECT(trustedKeysOuter->quorum() == std::ceil(cfgKeys.size() * 0.8f));
1224 }
1225 {
1226 // update with manifests
1227 auto const masterPrivate = randomSecretKey();
1228 auto const masterPublic = derivePublicKey(KeyType::Ed25519, masterPrivate);
1229
1230 std::vector<std::string> const cfgKeys({toBase58(TokenType::NodePublic, masterPublic)});
1231
1232 BEAST_EXPECT(trustedKeysOuter->load({}, cfgKeys, cfgPublishersOuter));
1233
1234 auto const signingKeys1 = randomKeyPair(KeyType::Secp256k1);
1235 auto const signingPublic1 = signingKeys1.first;
1236 activeValidatorsOuter.emplace(calcNodeID(masterPublic));
1237
1238 // Should not trust ephemeral signing key if there is no manifest
1239 TrustChanges changes = trustedKeysOuter->updateTrusted(
1240 activeValidatorsOuter,
1241 env.timeKeeper().now(),
1242 env.app().getOPs(),
1243 env.app().getOverlay(),
1244 env.app().getHashRouter());
1245 BEAST_EXPECT(changes.added == asNodeIDs({masterPublic}));
1246 BEAST_EXPECT(changes.removed.empty());
1247 BEAST_EXPECT(trustedKeysOuter->quorum() == std::ceil((maxKeys + 1) * 0.8f));
1248 BEAST_EXPECT(trustedKeysOuter->listed(masterPublic));
1249 BEAST_EXPECT(trustedKeysOuter->trusted(masterPublic));
1250 BEAST_EXPECT(!trustedKeysOuter->listed(signingPublic1));
1251 BEAST_EXPECT(!trustedKeysOuter->trusted(signingPublic1));
1252
1253 // Should trust the ephemeral signing key from the applied manifest
1255 masterPublic, masterPrivate, signingPublic1, signingKeys1.second, 1));
1256
1257 BEAST_EXPECT(
1258 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1259 manifestsOuter.applyManifest(std::move(*m1), ManifestRateLimitCapPolicy::Capped) ==
1261 BEAST_EXPECT(trustedKeysOuter->listed(masterPublic));
1262 BEAST_EXPECT(trustedKeysOuter->trusted(masterPublic));
1263 BEAST_EXPECT(trustedKeysOuter->listed(signingPublic1));
1264 BEAST_EXPECT(trustedKeysOuter->trusted(signingPublic1));
1265
1266 // Should only trust the ephemeral signing key
1267 // from the newest applied manifest
1268 auto const signingKeys2 = randomKeyPair(KeyType::Secp256k1);
1269 auto const signingPublic2 = signingKeys2.first;
1271 masterPublic, masterPrivate, signingPublic2, signingKeys2.second, 2));
1272 BEAST_EXPECT(
1273 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
1274 manifestsOuter.applyManifest(std::move(*m2), ManifestRateLimitCapPolicy::Capped) ==
1276 BEAST_EXPECT(trustedKeysOuter->listed(masterPublic));
1277 BEAST_EXPECT(trustedKeysOuter->trusted(masterPublic));
1278 BEAST_EXPECT(trustedKeysOuter->listed(signingPublic2));
1279 BEAST_EXPECT(trustedKeysOuter->trusted(signingPublic2));
1280 BEAST_EXPECT(!trustedKeysOuter->listed(signingPublic1));
1281 BEAST_EXPECT(!trustedKeysOuter->trusted(signingPublic1));
1282
1283 // Should not trust keys from revoked master public key
1284 auto const signingKeysMax = randomKeyPair(KeyType::Secp256k1);
1285 auto const signingPublicMax = signingKeysMax.first;
1286 activeValidatorsOuter.emplace(calcNodeID(signingPublicMax));
1287 auto max = deserializeManifest(makeRevocationString(masterPublic, masterPrivate));
1288
1289 // NOLINTBEGIN(bugprone-unchecked-optional-access)
1290 BEAST_EXPECT(max->revoked());
1291 BEAST_EXPECT(
1292 manifestsOuter.applyManifest(std::move(*max), ManifestRateLimitCapPolicy::Capped) ==
1294 // NOLINTEND(bugprone-unchecked-optional-access)
1295
1296 BEAST_EXPECT(manifestsOuter.getSigningKey(masterPublic) == masterPublic);
1297 BEAST_EXPECT(manifestsOuter.revoked(masterPublic));
1298
1299 // Revoked key remains trusted until list is updated
1300 BEAST_EXPECT(trustedKeysOuter->listed(masterPublic));
1301 BEAST_EXPECT(trustedKeysOuter->trusted(masterPublic));
1302
1303 changes = trustedKeysOuter->updateTrusted(
1304 activeValidatorsOuter,
1305 env.timeKeeper().now(),
1306 env.app().getOPs(),
1307 env.app().getOverlay(),
1308 env.app().getHashRouter());
1309 BEAST_EXPECT(changes.removed == asNodeIDs({masterPublic}));
1310 BEAST_EXPECT(changes.added.empty());
1311 BEAST_EXPECT(trustedKeysOuter->quorum() == std::ceil(maxKeys * 0.8f));
1312 BEAST_EXPECT(trustedKeysOuter->listed(masterPublic));
1313 BEAST_EXPECT(!trustedKeysOuter->trusted(masterPublic));
1314 BEAST_EXPECT(!trustedKeysOuter->listed(signingPublicMax));
1315 BEAST_EXPECT(!trustedKeysOuter->trusted(signingPublicMax));
1316 BEAST_EXPECT(!trustedKeysOuter->listed(signingPublic2));
1317 BEAST_EXPECT(!trustedKeysOuter->trusted(signingPublic2));
1318 BEAST_EXPECT(!trustedKeysOuter->listed(signingPublic1));
1319 BEAST_EXPECT(!trustedKeysOuter->trusted(signingPublic1));
1320 }
1321 {
1322 // Make quorum unattainable if lists from any publishers are
1323 // unavailable
1324 auto trustedKeys = std::make_unique<ValidatorList>(
1325 manifestsOuter,
1326 manifestsOuter,
1327 env.timeKeeper(),
1328 app.config().legacy(Sections::kDatabasePath),
1329 env.journal);
1330 auto const publisherSecret = randomSecretKey();
1331 auto const publisherPublic = derivePublicKey(KeyType::Ed25519, publisherSecret);
1332
1333 std::vector<std::string> const cfgPublishers({strHex(publisherPublic)});
1334 std::vector<std::string> const emptyCfgKeys;
1335
1336 BEAST_EXPECT(trustedKeys->load({}, emptyCfgKeys, cfgPublishers));
1337
1338 TrustChanges const changes = trustedKeys->updateTrusted(
1339 activeValidatorsOuter,
1340 env.timeKeeper().now(),
1341 env.app().getOPs(),
1342 env.app().getOverlay(),
1343 env.app().getHashRouter());
1344 BEAST_EXPECT(changes.removed.empty());
1345 BEAST_EXPECT(changes.added.empty());
1346 BEAST_EXPECT(trustedKeys->quorum() == std::numeric_limits<std::size_t>::max());
1347 }
1348 {
1349 // Trust explicitly listed validators also when list threshold is
1350 // higher than 1
1351 auto trustedKeys = std::make_unique<ValidatorList>(
1352 manifestsOuter,
1353 manifestsOuter,
1354 env.timeKeeper(),
1355 app.config().legacy(Sections::kDatabasePath),
1356 env.journal);
1357 auto const masterPrivate = randomSecretKey();
1358 auto const masterPublic = derivePublicKey(KeyType::Ed25519, masterPrivate);
1359 std::vector<std::string> const cfgKeys({toBase58(TokenType::NodePublic, masterPublic)});
1360
1361 auto const publisher1Secret = randomSecretKey();
1362 auto const publisher1Public = derivePublicKey(KeyType::Ed25519, publisher1Secret);
1363 auto const publisher2Secret = randomSecretKey();
1364 auto const publisher2Public = derivePublicKey(KeyType::Ed25519, publisher2Secret);
1365 std::vector<std::string> const cfgPublishers(
1366 {strHex(publisher1Public), strHex(publisher2Public)});
1367
1368 BEAST_EXPECT(trustedKeys->load({}, cfgKeys, cfgPublishers, std::size_t(2)));
1369
1370 TrustChanges const changes = trustedKeys->updateTrusted(
1371 activeValidatorsOuter,
1372 env.timeKeeper().now(),
1373 env.app().getOPs(),
1374 env.app().getOverlay(),
1375 env.app().getHashRouter());
1376 BEAST_EXPECT(changes.removed.empty());
1377 BEAST_EXPECT(changes.added.size() == 1);
1378 BEAST_EXPECT(trustedKeys->listed(masterPublic));
1379 BEAST_EXPECT(trustedKeys->trusted(masterPublic));
1380 }
1381 {
1382 // Should use custom minimum quorum
1383 std::size_t const minQuorum = 1;
1384 ManifestCache manifests;
1385 auto trustedKeys = std::make_unique<ValidatorList>(
1386 manifests,
1387 manifests,
1388 env.timeKeeper(),
1389 app.config().legacy(Sections::kDatabasePath),
1390 env.journal,
1391 minQuorum);
1392
1393 std::size_t const n = 10;
1395 cfgKeys.reserve(n);
1396 HashSet<NodeID> expectedTrusted;
1397 HashSet<NodeID> activeValidators;
1398 NodeID toBeSeen;
1399
1400 while (cfgKeys.size() < n)
1401 {
1402 auto const valKey = randomNode();
1403 cfgKeys.push_back(toBase58(TokenType::NodePublic, valKey));
1404 expectedTrusted.emplace(calcNodeID(valKey));
1405 if (cfgKeys.size() < std::ceil(n * 0.8f))
1406 {
1407 activeValidators.emplace(calcNodeID(valKey));
1408 }
1409 else if (cfgKeys.size() < std::ceil(n * 0.8f))
1410 {
1411 toBeSeen = calcNodeID(valKey);
1412 }
1413 }
1414
1415 BEAST_EXPECT(trustedKeys->load({}, cfgKeys, cfgPublishersOuter));
1416
1417 TrustChanges changes = trustedKeys->updateTrusted(
1418 activeValidators,
1419 env.timeKeeper().now(),
1420 env.app().getOPs(),
1421 env.app().getOverlay(),
1422 env.app().getHashRouter());
1423 BEAST_EXPECT(changes.removed.empty());
1424 BEAST_EXPECT(changes.added == expectedTrusted);
1425 BEAST_EXPECT(trustedKeys->quorum() == minQuorum);
1426
1427 // Use configured quorum even when seen validators >= quorum
1428 activeValidators.emplace(toBeSeen);
1429 changes = trustedKeys->updateTrusted(
1430 activeValidators,
1431 env.timeKeeper().now(),
1432 env.app().getOPs(),
1433 env.app().getOverlay(),
1434 env.app().getHashRouter());
1435 BEAST_EXPECT(changes.removed.empty());
1436 BEAST_EXPECT(changes.added.empty());
1437 BEAST_EXPECT(trustedKeys->quorum() == minQuorum);
1438 }
1439 {
1440 // Remove expired published list
1441 auto trustedKeys = std::make_unique<ValidatorList>(
1442 manifestsOuter,
1443 manifestsOuter,
1444 env.app().getTimeKeeper(),
1445 app.config().legacy(Sections::kDatabasePath),
1446 env.journal);
1447
1448 std::vector<std::string> const emptyCfgKeys;
1449 auto const publisherKeys = randomKeyPair(KeyType::Secp256k1);
1450 auto const pubSigningKeys = randomKeyPair(KeyType::Secp256k1);
1451 auto const manifest = base64Encode(makeManifestString(
1452 publisherKeys.first,
1453 publisherKeys.second,
1454 pubSigningKeys.first,
1455 pubSigningKeys.second,
1456 1));
1457
1458 std::vector<std::string> const cfgPublisherKeys({strHex(publisherKeys.first)});
1459
1460 BEAST_EXPECT(trustedKeys->load({}, emptyCfgKeys, cfgPublisherKeys));
1461
1463 HashSet<NodeID> activeValidators(
1464 asNodeIDs({list[0].masterPublic, list[1].masterPublic}));
1465
1466 // do not apply expired list
1467 auto const version = 1;
1468 auto const sequence = 1;
1469 using namespace std::chrono_literals;
1470 NetClock::time_point const validUntil = env.timeKeeper().now() + 60s;
1471 auto const blob = makeList(list, sequence, validUntil.time_since_epoch().count());
1472 auto const sig = signList(blob, pubSigningKeys);
1473
1474 BEAST_EXPECT(
1476 trustedKeys->applyLists(manifest, version, {{blob, sig, {}}}, siteUri)
1477 .bestDisposition());
1478
1479 TrustChanges changes = trustedKeys->updateTrusted(
1480 activeValidators,
1481 env.timeKeeper().now(),
1482 env.app().getOPs(),
1483 env.app().getOverlay(),
1484 env.app().getHashRouter());
1485 BEAST_EXPECT(changes.removed.empty());
1486 BEAST_EXPECT(changes.added == activeValidators);
1487 for (Validator const& val : list)
1488 {
1489 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
1490 BEAST_EXPECT(trustedKeys->trusted(val.signingPublic));
1491 }
1492 BEAST_EXPECT(trustedKeys->quorum() == 2);
1493
1494 env.timeKeeper().set(validUntil);
1495 changes = trustedKeys->updateTrusted(
1496 activeValidators,
1497 env.timeKeeper().now(),
1498 env.app().getOPs(),
1499 env.app().getOverlay(),
1500 env.app().getHashRouter());
1501 BEAST_EXPECT(changes.removed == activeValidators);
1502 BEAST_EXPECT(changes.added.empty());
1503 BEAST_EXPECT(!trustedKeys->trusted(list[0].masterPublic));
1504 BEAST_EXPECT(!trustedKeys->trusted(list[1].masterPublic));
1505 BEAST_EXPECT(trustedKeys->quorum() == std::numeric_limits<std::size_t>::max());
1506
1507 // (Re)trust validators from new valid list
1508 std::vector<Validator> list2({list[0], randomValidator()});
1509 activeValidators.insert(calcNodeID(list2[1].masterPublic));
1510 auto const sequence2 = 2;
1511 NetClock::time_point const expiration2 = env.timeKeeper().now() + 60s;
1512 auto const blob2 = makeList(list2, sequence2, expiration2.time_since_epoch().count());
1513 auto const sig2 = signList(blob2, pubSigningKeys);
1514
1515 BEAST_EXPECT(
1517 trustedKeys->applyLists(manifest, version, {{blob2, sig2, {}}}, siteUri)
1518 .bestDisposition());
1519
1520 changes = trustedKeys->updateTrusted(
1521 activeValidators,
1522 env.timeKeeper().now(),
1523 env.app().getOPs(),
1524 env.app().getOverlay(),
1525 env.app().getHashRouter());
1526 BEAST_EXPECT(changes.removed.empty());
1527 BEAST_EXPECT(
1528 changes.added == asNodeIDs({list2[0].masterPublic, list2[1].masterPublic}));
1529 for (Validator const& val : list2)
1530 {
1531 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
1532 BEAST_EXPECT(trustedKeys->trusted(val.signingPublic));
1533 }
1534 BEAST_EXPECT(!trustedKeys->trusted(list[1].masterPublic));
1535 BEAST_EXPECT(!trustedKeys->trusted(list[1].signingPublic));
1536 BEAST_EXPECT(trustedKeys->quorum() == 2);
1537 }
1538 {
1539 // Test 1-9 configured validators
1540 auto trustedKeys = std::make_unique<ValidatorList>(
1541 manifestsOuter,
1542 manifestsOuter,
1543 env.timeKeeper(),
1544 app.config().legacy(Sections::kDatabasePath),
1545 env.journal);
1546
1547 std::vector<std::string> const cfgPublishers;
1548 HashSet<NodeID> activeValidators;
1549 HashSet<PublicKey> activeKeys;
1550
1552 cfgKeys.reserve(9);
1553
1554 while (cfgKeys.size() < cfgKeys.capacity())
1555 {
1556 auto const valKey = randomNode();
1557 cfgKeys.push_back(toBase58(TokenType::NodePublic, valKey));
1558 activeValidators.emplace(calcNodeID(valKey));
1559 activeKeys.emplace(valKey);
1560 BEAST_EXPECT(trustedKeys->load({}, cfgKeys, cfgPublishers));
1561 TrustChanges const changes = trustedKeys->updateTrusted(
1562 activeValidators,
1563 env.timeKeeper().now(),
1564 env.app().getOPs(),
1565 env.app().getOverlay(),
1566 env.app().getHashRouter());
1567 BEAST_EXPECT(changes.removed.empty());
1568 BEAST_EXPECT(changes.added == asNodeIDs({valKey}));
1569 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(cfgKeys.size() * 0.8f));
1570 for (auto const& key : activeKeys)
1571 BEAST_EXPECT(trustedKeys->trusted(key));
1572 }
1573 }
1574 {
1575 // Test 2-9 configured validators as validator
1576 auto trustedKeys = std::make_unique<ValidatorList>(
1577 manifestsOuter,
1578 manifestsOuter,
1579 env.timeKeeper(),
1580 app.config().legacy(Sections::kDatabasePath),
1581 env.journal);
1582
1583 auto const localKey = randomNode();
1584 std::vector<std::string> const cfgPublishers;
1585 HashSet<NodeID> activeValidators;
1586 HashSet<PublicKey> activeKeys;
1587 std::vector<std::string> cfgKeys{toBase58(TokenType::NodePublic, localKey)};
1588 cfgKeys.reserve(9);
1589
1590 while (cfgKeys.size() < cfgKeys.capacity())
1591 {
1592 auto const valKey = randomNode();
1593 cfgKeys.push_back(toBase58(TokenType::NodePublic, valKey));
1594 activeValidators.emplace(calcNodeID(valKey));
1595 activeKeys.emplace(valKey);
1596
1597 BEAST_EXPECT(trustedKeys->load(localKey, cfgKeys, cfgPublishers));
1598 TrustChanges const changes = trustedKeys->updateTrusted(
1599 activeValidators,
1600 env.timeKeeper().now(),
1601 env.app().getOPs(),
1602 env.app().getOverlay(),
1603 env.app().getHashRouter());
1604 BEAST_EXPECT(changes.removed.empty());
1605 if (cfgKeys.size() > 2)
1606 {
1607 BEAST_EXPECT(changes.added == asNodeIDs({valKey}));
1608 }
1609 else
1610 {
1611 BEAST_EXPECT(changes.added == asNodeIDs({localKey, valKey}));
1612 }
1613
1614 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(cfgKeys.size() * 0.8f));
1615
1616 for (auto const& key : activeKeys)
1617 BEAST_EXPECT(trustedKeys->trusted(key));
1618 }
1619 }
1620 {
1621 // Trusted set should include all validators from multiple lists
1622 ManifestCache manifests;
1623 auto trustedKeys = std::make_unique<ValidatorList>(
1624 manifests,
1625 manifests,
1626 env.timeKeeper(),
1627 app.config().legacy(Sections::kDatabasePath),
1628 env.journal);
1629
1630 HashSet<NodeID> activeValidators;
1631 std::vector<Validator> valKeys;
1632 valKeys.reserve(maxKeys);
1633
1634 while (valKeys.size() != maxKeys)
1635 {
1636 valKeys.push_back(randomValidator());
1637 activeValidators.emplace(calcNodeID(valKeys.back().masterPublic));
1638 }
1639
1640 // locals[0]: from 0 to maxKeys - 4
1641 // locals[1]: from 1 to maxKeys - 2
1642 // locals[2]: from 2 to maxKeys
1643 static constexpr int kPublishers = 3;
1644 std::array<
1645 std::pair<decltype(valKeys)::const_iterator, decltype(valKeys)::const_iterator>,
1646 kPublishers>
1647 locals = {
1648 std::make_pair(valKeys.cbegin(), valKeys.cend() - 4),
1649 std::make_pair(valKeys.cbegin() + 1, valKeys.cend() - 2),
1650 std::make_pair(valKeys.cbegin() + 2, valKeys.cend()),
1651 };
1652
1653 auto const addPublishedList = [&, this](int i) {
1654 auto const publisherSecret = randomSecretKey();
1655 auto const publisherPublic = derivePublicKey(KeyType::Ed25519, publisherSecret);
1656 auto const pubSigningKeys = randomKeyPair(KeyType::Secp256k1);
1657 auto const manifest = base64Encode(makeManifestString(
1658 publisherPublic,
1659 publisherSecret,
1660 pubSigningKeys.first,
1661 pubSigningKeys.second,
1662 1));
1663
1664 std::vector<std::string> const cfgPublishers({strHex(publisherPublic)});
1665 std::vector<std::string> const emptyCfgKeys;
1666
1667 // Threshold of 1 will result in a union of all the lists
1668 BEAST_EXPECT(trustedKeys->load({}, emptyCfgKeys, cfgPublishers, std::size_t(1)));
1669
1670 auto const version = 1;
1671 auto const sequence = 1;
1672 using namespace std::chrono_literals;
1673 NetClock::time_point const validUntil = env.timeKeeper().now() + 3600s;
1674 std::vector<Validator> const localKeys{locals[i].first, locals[i].second};
1675 auto const blob =
1676 makeList(localKeys, sequence, validUntil.time_since_epoch().count());
1677 auto const sig = signList(blob, pubSigningKeys);
1678
1679 BEAST_EXPECT(
1681 trustedKeys->applyLists(manifest, version, {{blob, sig, {}}}, siteUri)
1682 .bestDisposition());
1683 };
1684
1685 // Apply multiple published lists
1686 for (auto i = 0; i < kPublishers; ++i)
1687 addPublishedList(i);
1688 BEAST_EXPECT(trustedKeys->getListThreshold() == 1);
1689
1690 TrustChanges const changes = trustedKeys->updateTrusted(
1691 activeValidators,
1692 env.timeKeeper().now(),
1693 env.app().getOPs(),
1694 env.app().getOverlay(),
1695 env.app().getHashRouter());
1696
1697 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(valKeys.size() * 0.8f));
1698
1699 HashSet<NodeID> added;
1700 for (auto const& val : valKeys)
1701 {
1702 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
1703 added.insert(calcNodeID(val.masterPublic));
1704 }
1705 BEAST_EXPECT(changes.added == added);
1706 BEAST_EXPECT(changes.removed.empty());
1707 }
1708 {
1709 // Trusted set should include validators from intersection of lists
1710 ManifestCache manifests;
1711 auto trustedKeys = std::make_unique<ValidatorList>(
1712 manifests,
1713 manifests,
1714 env.timeKeeper(),
1715 app.config().legacy(Sections::kDatabasePath),
1716 env.journal);
1717
1718 HashSet<NodeID> activeValidators;
1719 std::vector<Validator> valKeys;
1720 valKeys.reserve(maxKeys);
1721
1722 while (valKeys.size() != maxKeys)
1723 {
1724 valKeys.push_back(randomValidator());
1725 activeValidators.emplace(calcNodeID(valKeys.back().masterPublic));
1726 }
1727
1728 // locals[0]: from 0 to maxKeys - 4
1729 // locals[1]: from 1 to maxKeys - 2
1730 // locals[2]: from 2 to maxKeys
1731 // intersection of at least 2: same as locals[1]
1732 // intersection when 1 is dropped: from 2 to maxKeys - 4
1733 static constexpr int kPublishers = 3;
1734 std::array<
1735 std::pair<decltype(valKeys)::const_iterator, decltype(valKeys)::const_iterator>,
1736 kPublishers>
1737 locals = {
1738 std::make_pair(valKeys.cbegin(), valKeys.cend() - 4),
1739 std::make_pair(valKeys.cbegin() + 1, valKeys.cend() - 2),
1740 std::make_pair(valKeys.cbegin() + 2, valKeys.cend()),
1741 };
1742
1743 auto const addPublishedList =
1744 [&, this](
1745 int i, NetClock::time_point& validUntil1, NetClock::time_point& validUntil2) {
1746 auto const publisherSecret = randomSecretKey();
1747 auto const publisherPublic = derivePublicKey(KeyType::Ed25519, publisherSecret);
1748 auto const pubSigningKeys = randomKeyPair(KeyType::Secp256k1);
1749 auto const manifest = base64Encode(makeManifestString(
1750 publisherPublic,
1751 publisherSecret,
1752 pubSigningKeys.first,
1753 pubSigningKeys.second,
1754 1));
1755
1756 std::vector<std::string> const cfgPublishers({strHex(publisherPublic)});
1757 std::vector<std::string> const emptyCfgKeys;
1758
1759 BEAST_EXPECT(trustedKeys->load({}, emptyCfgKeys, cfgPublishers));
1760
1761 auto const version = 1;
1762 auto const sequence = 1;
1763 using namespace std::chrono_literals;
1764 // Want to drop 1 sooner
1765 std::chrono::seconds duration;
1766 if (i == 2)
1767 {
1768 duration = 120s;
1769 }
1770 else if (i == 1)
1771 {
1772 duration = 60s;
1773 }
1774 else
1775 {
1776 duration = 3600s;
1777 }
1778 NetClock::time_point const validUntil = env.timeKeeper().now() + duration;
1779 if (i == 1)
1780 {
1781 validUntil1 = validUntil;
1782 }
1783 else if (i == 2)
1784 {
1785 validUntil2 = validUntil;
1786 }
1787 std::vector<Validator> const localKeys{locals[i].first, locals[i].second};
1788 auto const blob =
1789 makeList(localKeys, sequence, validUntil.time_since_epoch().count());
1790 auto const sig = signList(blob, pubSigningKeys);
1791
1792 BEAST_EXPECT(
1794 trustedKeys->applyLists(manifest, version, {{blob, sig, {}}}, siteUri)
1795 .bestDisposition());
1796 };
1797
1798 // Apply multiple published lists
1799 // validUntil1 is expiration time for locals[1]
1800 NetClock::time_point validUntil1, validUntil2;
1801 for (auto i = 0; i < kPublishers; ++i)
1802 addPublishedList(i, validUntil1, validUntil2);
1803 BEAST_EXPECT(trustedKeys->getListThreshold() == 2);
1804
1805 TrustChanges changes = trustedKeys->updateTrusted(
1806 activeValidators,
1807 env.timeKeeper().now(),
1808 env.app().getOPs(),
1809 env.app().getOverlay(),
1810 env.app().getHashRouter());
1811
1812 BEAST_EXPECT(trustedKeys->quorum() == std::ceil((valKeys.size() - 3) * 0.8f));
1813
1814 for (auto const& val : valKeys)
1815 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
1816
1817 HashSet<NodeID> added;
1818 for (std::size_t i = 0; i < maxKeys; ++i)
1819 {
1820 auto const& val = valKeys[i];
1821 if (i >= 1 && i < maxKeys - 2)
1822 {
1823 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
1824 added.insert(calcNodeID(val.masterPublic));
1825 }
1826 else
1827 {
1828 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
1829 }
1830 }
1831 BEAST_EXPECT(changes.added == added);
1832 BEAST_EXPECT(changes.removed.empty());
1833
1834 // Expire locals[1]
1835 env.timeKeeper().set(validUntil1);
1836 changes = trustedKeys->updateTrusted(
1837 activeValidators,
1838 env.timeKeeper().now(),
1839 env.app().getOPs(),
1840 env.app().getOverlay(),
1841 env.app().getHashRouter());
1842
1843 BEAST_EXPECT(trustedKeys->quorum() == std::ceil((valKeys.size() - 6) * 0.8f));
1844
1845 for (auto const& val : valKeys)
1846 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
1847
1848 HashSet<NodeID> removed;
1849 for (std::size_t i = 0; i < maxKeys; ++i)
1850 {
1851 auto const& val = valKeys[i];
1852 if (i >= 2 && i < maxKeys - 4)
1853 {
1854 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
1855 }
1856 else
1857 {
1858 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
1859 if (i >= 1 && i < maxKeys - 2)
1860 removed.insert(calcNodeID(val.masterPublic));
1861 }
1862 }
1863
1864 BEAST_EXPECT(changes.added.empty());
1865 BEAST_EXPECT(changes.removed == removed);
1866
1867 // Expire locals[2], which removes all validators
1868 env.timeKeeper().set(validUntil2);
1869 changes = trustedKeys->updateTrusted(
1870 activeValidators,
1871 env.timeKeeper().now(),
1872 env.app().getOPs(),
1873 env.app().getOverlay(),
1874 env.app().getHashRouter());
1875
1876 BEAST_EXPECT(trustedKeys->quorum() == std::numeric_limits<std::size_t>::max());
1877
1878 removed.clear();
1879 for (std::size_t i = 0; i < maxKeys; ++i)
1880 {
1881 auto const& val = valKeys[i];
1882 if (i < maxKeys - 4)
1883 {
1884 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
1885 }
1886 else
1887 {
1888 BEAST_EXPECT(!trustedKeys->listed(val.masterPublic));
1889 }
1890
1891 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
1892 if (i >= 2 && i < maxKeys - 4)
1893 removed.insert(calcNodeID(val.masterPublic));
1894 }
1895
1896 BEAST_EXPECT(changes.added.empty());
1897 BEAST_EXPECT(changes.removed == removed);
1898 }
1899 }
1900
1901 void
1903 {
1904 testcase("Expires");
1905
1906 std::string const siteUri = "testExpires.test";
1907
1908 jtx::Env env(*this);
1909 auto& app = env.app();
1910
1911 auto const toStr = [](PublicKey const& publicKey) {
1912 return toBase58(TokenType::NodePublic, publicKey);
1913 };
1914
1915 // Config listed keys
1916 {
1917 ManifestCache manifests;
1918 auto trustedKeys = std::make_unique<ValidatorList>(
1919 manifests,
1920 manifests,
1921 env.timeKeeper(),
1922 app.config().legacy(Sections::kDatabasePath),
1923 env.journal);
1924
1925 // Empty list has no expiration
1926 BEAST_EXPECT(trustedKeys->expires() == std::nullopt);
1927
1928 // Config listed keys have maximum expiry
1929 PublicKey const localCfgListed = randomNode();
1930 trustedKeys->load({}, {toStr(localCfgListed)}, {});
1931 BEAST_EXPECT(
1932 trustedKeys->expires() &&
1933 trustedKeys->expires().value() == NetClock::time_point::max());
1934 BEAST_EXPECT(trustedKeys->listed(localCfgListed));
1935 }
1936
1937 // Published keys with expirations
1938 {
1939 ManifestCache manifests;
1940 auto trustedKeys = std::make_unique<ValidatorList>(
1941 manifests,
1942 manifests,
1943 env.app().getTimeKeeper(),
1944 app.config().legacy(Sections::kDatabasePath),
1945 env.journal);
1946
1947 std::vector<Validator> validators = {randomValidator()};
1948 HashSet<NodeID> activeValidators;
1949 for (Validator const& val : validators)
1950 activeValidators.insert(calcNodeID(val.masterPublic));
1951 // Store prepared list data to control when it is applied
1952 struct PreparedList
1953 {
1954 PublicKey publisherPublic;
1955 std::string manifest;
1957 int version;
1959 };
1960
1961 using namespace std::chrono_literals;
1962 auto const addPublishedList = [this, &env, &trustedKeys, &validators] {
1963 auto const publisherSecret = randomSecretKey();
1964 auto const publisherPublic = derivePublicKey(KeyType::Ed25519, publisherSecret);
1965 auto const pubSigningKeys = randomKeyPair(KeyType::Secp256k1);
1966 auto const manifest = base64Encode(makeManifestString(
1967 publisherPublic,
1968 publisherSecret,
1969 pubSigningKeys.first,
1970 pubSigningKeys.second,
1971 1));
1972
1973 std::vector<std::string> const cfgPublishers({strHex(publisherPublic)});
1974 std::vector<std::string> const emptyCfgKeys;
1975
1976 BEAST_EXPECT(trustedKeys->load({}, emptyCfgKeys, cfgPublishers));
1977
1978 auto const version = 2;
1979 auto const sequence1 = 1;
1980 NetClock::time_point const expiration1 = env.timeKeeper().now() + 1800s;
1981 auto const blob1 =
1982 makeList(validators, sequence1, expiration1.time_since_epoch().count());
1983 auto const sig1 = signList(blob1, pubSigningKeys);
1984
1985 NetClock::time_point const effective2 = expiration1 - 300s;
1986 NetClock::time_point const expiration2 = effective2 + 1800s;
1987 auto const sequence2 = 2;
1988 auto const blob2 = makeList(
1989 validators,
1990 sequence2,
1991 expiration2.time_since_epoch().count(),
1992 effective2.time_since_epoch().count());
1993 auto const sig2 = signList(blob2, pubSigningKeys);
1994
1995 return PreparedList{
1996 .publisherPublic = publisherPublic,
1997 .manifest = manifest,
1998 .blobs =
1999 {
2000 {.blob = blob1, .signature = sig1, .manifest = {}},
2001 {.blob = blob2, .signature = sig2, .manifest = {}},
2002 },
2003 .version = version,
2004 .expirations = {expiration1, expiration2},
2005 };
2006 };
2007
2008 // Configure two publishers and prepare 2 lists
2009 PreparedList prep1 = addPublishedList();
2010 env.timeKeeper().set(env.timeKeeper().now() + 200s);
2011 PreparedList const prep2 = addPublishedList();
2012
2013 // Initially, no list has been published, so no known expiration
2014 BEAST_EXPECT(trustedKeys->expires() == std::nullopt);
2015
2016 // Apply first list
2018 trustedKeys->applyLists(prep1.manifest, prep1.version, prep1.blobs, siteUri),
2019 prep1.publisherPublic,
2022
2023 // One list still hasn't published, so expiration is still
2024 // unknown
2025 BEAST_EXPECT(trustedKeys->expires() == std::nullopt);
2026
2027 // Apply second list
2029 trustedKeys->applyLists(prep2.manifest, prep2.version, prep2.blobs, siteUri),
2030 prep2.publisherPublic,
2033 // We now have loaded both lists, so expiration is known
2034 BEAST_EXPECT(
2035 trustedKeys->expires() &&
2036 trustedKeys->expires().value() == prep1.expirations.back());
2037
2038 // Advance past the first list's LAST validFrom date. It remains
2039 // the earliest validUntil, while rotating in the second list
2040 {
2041 env.timeKeeper().set(prep1.expirations.front() - 1s);
2042 auto const changes = trustedKeys->updateTrusted(
2043 activeValidators,
2044 env.timeKeeper().now(),
2045 env.app().getOPs(),
2046 env.app().getOverlay(),
2047 env.app().getHashRouter());
2048 BEAST_EXPECT(
2049 trustedKeys->expires() &&
2050 trustedKeys->expires().value() == prep1.expirations.back());
2051 BEAST_EXPECT(!changes.added.empty());
2052 BEAST_EXPECT(changes.removed.empty());
2053 }
2054
2055 // Advance past the first list's LAST validUntil, but it remains
2056 // the earliest validUntil, while being invalidated
2057 {
2058 env.timeKeeper().set(prep1.expirations.back() + 1s);
2059 auto const changes = trustedKeys->updateTrusted(
2060 activeValidators,
2061 env.timeKeeper().now(),
2062 env.app().getOPs(),
2063 env.app().getOverlay(),
2064 env.app().getHashRouter());
2065 BEAST_EXPECT(
2066 trustedKeys->expires() &&
2067 trustedKeys->expires().value() == prep1.expirations.back());
2068 BEAST_EXPECT(changes.added.empty());
2069 BEAST_EXPECT(changes.removed.empty());
2070 }
2071 }
2072 }
2073
2074 void
2076 {
2077 testcase("NegativeUNL");
2078 jtx::Env env(*this);
2079 ManifestCache manifests;
2080
2081 auto const createValidatorList =
2082 [&](std::uint32_t vlSize,
2084 auto trustedKeys = std::make_shared<ValidatorList>(
2085 manifests,
2086 manifests,
2087 env.timeKeeper(),
2089 env.journal,
2090 minimumQuorum);
2091
2092 std::vector<std::string> const cfgPublishers;
2094 HashSet<NodeID> activeValidators;
2095 cfgKeys.reserve(vlSize);
2096 while (cfgKeys.size() < cfgKeys.capacity())
2097 {
2098 auto const valKey = randomNode();
2099 cfgKeys.push_back(toBase58(TokenType::NodePublic, valKey));
2100 activeValidators.emplace(calcNodeID(valKey));
2101 }
2102 if (trustedKeys->load({}, cfgKeys, cfgPublishers))
2103 {
2104 trustedKeys->updateTrusted(
2105 activeValidators,
2106 env.timeKeeper().now(),
2107 env.app().getOPs(),
2108 env.app().getOverlay(),
2109 env.app().getHashRouter());
2110 if (minimumQuorum == trustedKeys->quorum() ||
2111 trustedKeys->quorum() == std::ceil(cfgKeys.size() * 0.8f))
2112 return trustedKeys;
2113 }
2114 return nullptr;
2115 };
2116
2117 /*
2118 * Test NegativeUNL
2119 * == Combinations ==
2120 * -- UNL size: 34, 35, 57
2121 * -- nUNL size: 0%, 20%, 30%, 50%
2122 *
2123 * == with UNL size 60
2124 * -- set == get,
2125 * -- check quorum, with nUNL size: 0, 12, 30, 18
2126 * -- nUNL overlap: |nUNL - UNL| = 5, with nUNL size: 18
2127 * -- with command line minimumQuorum = 50%,
2128 * seen_reliable affected by nUNL
2129 */
2130
2131 {
2132 HashSet<NodeID> const activeValidators;
2133 //== Combinations ==
2134 std::array<std::uint32_t, 4> const unlSizes = {34, 35, 39, 60};
2135 std::array<std::uint32_t, 4> const nUnlPercent = {0, 20, 30, 50};
2136 for (auto const us : unlSizes)
2137 {
2138 for (auto const np : nUnlPercent)
2139 {
2140 auto const validators = createValidatorList(us);
2141 BEAST_EXPECT(validators);
2142 if (validators)
2143 {
2144 std::uint32_t const nUnlSize = us * np / 100;
2145 auto unl = validators->getTrustedMasterKeys();
2146 HashSet<PublicKey> nUnl;
2147 auto it = unl.begin();
2148 for (std::uint32_t i = 0; i < nUnlSize; ++i)
2149 {
2150 nUnl.insert(*it);
2151 ++it;
2152 }
2153 validators->setNegativeUNL(nUnl);
2154 validators->updateTrusted(
2155 activeValidators,
2156 env.timeKeeper().now(),
2157 env.app().getOPs(),
2158 env.app().getOverlay(),
2159 env.app().getHashRouter());
2160 BEAST_EXPECT(
2161 validators->quorum() ==
2162 static_cast<std::size_t>(
2163 std::ceil(std::max((us - nUnlSize) * 0.8f, us * 0.6f))));
2164 }
2165 }
2166 }
2167 }
2168
2169 {
2170 //== with UNL size 60
2171 auto validators = createValidatorList(60);
2172 BEAST_EXPECT(validators);
2173 if (validators)
2174 {
2175 HashSet<NodeID> activeValidators;
2176 auto unl = validators->getTrustedMasterKeys();
2177 BEAST_EXPECT(unl.size() == 60);
2178 {
2179 //-- set == get,
2180 //-- check quorum, with nUNL size: 0, 30, 18, 12
2181 auto const nUnlChange = [&](std::uint32_t nUnlSize,
2182 std::uint32_t quorum) -> bool {
2183 HashSet<PublicKey> nUnl;
2184 auto it = unl.begin();
2185 for (std::uint32_t i = 0; i < nUnlSize; ++i)
2186 {
2187 nUnl.insert(*it);
2188 ++it;
2189 }
2190 validators->setNegativeUNL(nUnl);
2191 auto const nUnlTemp = validators->getNegativeUNL();
2192 if (nUnlTemp.size() == nUnl.size())
2193 {
2194 for (auto& n : nUnlTemp)
2195 {
2196 if (!nUnl.contains(n))
2197 return false;
2198 }
2199 validators->updateTrusted(
2200 activeValidators,
2201 env.timeKeeper().now(),
2202 env.app().getOPs(),
2203 env.app().getOverlay(),
2204 env.app().getHashRouter());
2205 return validators->quorum() == quorum;
2206 }
2207 return false;
2208 };
2209 BEAST_EXPECT(nUnlChange(0, 48));
2210 BEAST_EXPECT(nUnlChange(30, 36));
2211 BEAST_EXPECT(nUnlChange(18, 36));
2212 BEAST_EXPECT(nUnlChange(12, 39));
2213 }
2214
2215 {
2216 // nUNL overlap: |nUNL - UNL| = 5, with nUNL size:
2217 // 18
2218 auto nUnl = validators->getNegativeUNL();
2219 BEAST_EXPECT(nUnl.size() == 12);
2220 std::size_t const ss = 33;
2221 std::vector<uint8_t> data(ss, 0);
2222 data[0] = 0xED;
2223 for (int i = 0; i < 6; ++i)
2224 {
2225 Slice const s(data.data(), ss);
2226 data[1]++;
2227 nUnl.emplace(s);
2228 }
2229 validators->setNegativeUNL(nUnl);
2230 validators->updateTrusted(
2231 activeValidators,
2232 env.timeKeeper().now(),
2233 env.app().getOPs(),
2234 env.app().getOverlay(),
2235 env.app().getHashRouter());
2236 BEAST_EXPECT(validators->quorum() == 39);
2237 }
2238 }
2239 }
2240
2241 {
2242 //== with UNL size 60
2243 //-- with command line minimumQuorum = 50%,
2244 // seen_reliable affected by nUNL
2245 auto const validators = createValidatorList(60, 30);
2246 BEAST_EXPECT(validators);
2247 if (validators)
2248 {
2249 HashSet<NodeID> activeValidators;
2250 HashSet<PublicKey> unl = validators->getTrustedMasterKeys();
2251 auto it = unl.begin();
2252 for (std::uint32_t i = 0; i < 50; ++i)
2253 {
2254 activeValidators.insert(calcNodeID(*it));
2255 ++it;
2256 }
2257 validators->updateTrusted(
2258 activeValidators,
2259 env.timeKeeper().now(),
2260 env.app().getOPs(),
2261 env.app().getOverlay(),
2262 env.app().getHashRouter());
2263 BEAST_EXPECT(validators->quorum() == 30);
2264 HashSet<PublicKey> nUnl;
2265 it = unl.begin();
2266 for (std::uint32_t i = 0; i < 20; ++i)
2267 {
2268 nUnl.insert(*it);
2269 ++it;
2270 }
2271 validators->setNegativeUNL(nUnl);
2272 validators->updateTrusted(
2273 activeValidators,
2274 env.timeKeeper().now(),
2275 env.app().getOPs(),
2276 env.app().getOverlay(),
2277 env.app().getHashRouter());
2278 BEAST_EXPECT(validators->quorum() == 30);
2279 }
2280 }
2281 }
2282
2283 void
2285 {
2286 testcase("Sha512 hashing");
2287 // Tests that ValidatorList hash_append helpers with a single blob
2288 // return the same result as xrpl::Sha512Half
2289 std::string const manifest = "This is not really a manifest";
2290 std::string const blob = "This is not really a blob";
2291 std::string const signature = "This is not really a signature";
2292 std::uint32_t const version = 1;
2293
2294 auto const global = sha512Half(manifest, blob, signature, version);
2295 BEAST_EXPECT(!!global);
2296
2297 std::vector<ValidatorBlobInfo> blobVector(1);
2298 blobVector[0].blob = blob;
2299 blobVector[0].signature = signature;
2300 BEAST_EXPECT(global == sha512Half(manifest, blobVector, version));
2301 BEAST_EXPECT(global != sha512Half(signature, blobVector, version));
2302
2303 {
2304 std::map<std::size_t, ValidatorBlobInfo> const blobMap{{99, blobVector[0]}};
2305 BEAST_EXPECT(global == sha512Half(manifest, blobMap, version));
2306 BEAST_EXPECT(global != sha512Half(blob, blobMap, version));
2307 }
2308
2309 {
2310 protocol::TMValidatorListCollection msg2;
2311 msg2.set_manifest(manifest);
2312 msg2.set_version(version);
2313 auto& bi = *msg2.add_blobs();
2314 bi.set_blob(blob);
2315 bi.set_signature(signature);
2316 BEAST_EXPECT(global == sha512Half(msg2));
2317 bi.set_manifest(manifest);
2318 BEAST_EXPECT(global != sha512Half(msg2));
2319 }
2320 }
2321
2322 void
2324 {
2325 testcase("Build and split messages");
2326
2327 std::uint32_t const manifestCutoff = 7;
2328 auto extractHeader = [this](Message& message) {
2329 auto const& buffer = message.getBuffer(compression::Compressed::Off);
2330
2331 boost::beast::multi_buffer buffers;
2332
2333 // simulate multi-buffer
2334 auto const start = buffer.begin();
2335 auto const end = buffer.end();
2336 std::vector<std::uint8_t> slice(start, end);
2337 buffers.commit(
2338 boost::asio::buffer_copy(
2339 buffers.prepare(slice.size()), boost::asio::buffer(slice)));
2340
2341 boost::system::error_code ec;
2342 auto const header = detail::parseMessageHeader(ec, buffers.data(), buffers.size());
2343 BEAST_EXPECT(!ec);
2344 return std::make_pair(header, buffers);
2345 };
2346 auto extractProtocolMessage = [this, &extractHeader](Message& message) {
2347 auto [header, buffers] = extractHeader(message);
2348 if (BEAST_EXPECT(header) &&
2349 BEAST_EXPECT(header->messageType == protocol::mtVALIDATOR_LIST_COLLECTION))
2350 {
2352 *header, buffers.data());
2353 BEAST_EXPECT(msg);
2354 return msg;
2355 }
2357 };
2358 auto const verifyMessage = [this, manifestCutoff, &extractProtocolMessage](
2359 auto const version,
2360 auto const& manifest,
2361 auto const& blobInfos,
2362 auto const& messages,
2364 BEAST_EXPECT(messages.size() == expectedInfo.size());
2365 auto msgIter = expectedInfo.begin();
2366 for (auto const& messageWithHash : messages)
2367 {
2368 if (!BEAST_EXPECT(msgIter != expectedInfo.end()))
2369 break;
2370 if (!BEAST_EXPECT(messageWithHash.message))
2371 continue;
2372 auto const& expectedSeqs = *msgIter;
2373 auto seqIter = expectedSeqs.begin();
2374 {
2375 std::vector<ValidatorBlobInfo> hashingBlobs;
2376 hashingBlobs.reserve(expectedSeqs.size());
2377
2378 auto const msg = extractProtocolMessage(*messageWithHash.message);
2379 if (BEAST_EXPECT(msg))
2380 {
2381 BEAST_EXPECT(msg->version() == version);
2382 BEAST_EXPECT(msg->manifest() == manifest);
2383 for (auto const& blobInfo : msg->blobs())
2384 {
2385 if (!BEAST_EXPECT(seqIter != expectedSeqs.end()))
2386 break;
2387 auto const& expectedBlob = blobInfos.at(*seqIter);
2388 hashingBlobs.push_back(expectedBlob);
2389 BEAST_EXPECT(blobInfo.has_manifest() == !!expectedBlob.manifest);
2390 BEAST_EXPECT(blobInfo.has_manifest() == (*seqIter < manifestCutoff));
2391
2392 if (*seqIter < manifestCutoff)
2393 BEAST_EXPECT(blobInfo.manifest() == *expectedBlob.manifest);
2394 BEAST_EXPECT(blobInfo.blob() == expectedBlob.blob);
2395 BEAST_EXPECT(blobInfo.signature() == expectedBlob.signature);
2396 ++seqIter;
2397 }
2398 BEAST_EXPECT(seqIter == expectedSeqs.end());
2399 }
2400 BEAST_EXPECT(
2401 messageWithHash.hash == sha512Half(manifest, hashingBlobs, version));
2402 }
2403 ++msgIter;
2404 }
2405 BEAST_EXPECT(msgIter == expectedInfo.end());
2406 };
2407 auto const verifyBuildMessages = [this](
2409 std::size_t expectedSequence,
2410 std::size_t expectedSize) {
2411 BEAST_EXPECT(result.first == expectedSequence);
2412 BEAST_EXPECT(result.second == expectedSize);
2413 };
2414
2415 std::string const manifest = "This is not a manifest";
2416 std::uint32_t const version = 2;
2417 // Mutable so items can be removed in later tests.
2418 auto const blobInfos = [manifestCutoff = manifestCutoff] {
2420
2421 for (auto seq : {5, 6, 7, 10, 12})
2422 {
2423 auto& b = bis[seq];
2425 s << "This is not a blob with sequence " << seq;
2426 b.blob = s.str();
2427 s.str(std::string());
2428 s << "This is not a signature for sequence " << seq;
2429 b.signature = s.str();
2430 if (seq < manifestCutoff)
2431 {
2432 // add a manifest for the "early" blobs
2433 s.str(std::string());
2434 s << "This is not manifest " << seq;
2435 b.manifest = s.str();
2436 }
2437 }
2438 return bis;
2439 }();
2440 auto const maxSequence = blobInfos.rbegin()->first;
2441 BEAST_EXPECT(maxSequence == 12);
2442
2444
2445 // This peer has a VL ahead of us.
2446 verifyBuildMessages(
2448 maxSequence * 2, maxSequence, version, manifest, blobInfos, messages),
2449 0,
2450 0);
2451 BEAST_EXPECT(messages.empty());
2452
2453 // Don't repeat the work if messages is populated, even though the
2454 // peerSequence provided indicates it should. Note that this
2455 // situation is contrived for this test and should never happen in
2456 // real code.
2457 messages.emplace_back();
2458 verifyBuildMessages(
2460 3, maxSequence, version, manifest, blobInfos, messages),
2461 maxSequence,
2462 0);
2463 BEAST_EXPECT(messages.size() == 1 && !messages.front().message);
2464
2465 // Generate a message. Don't send the current
2466 messages.clear();
2467 verifyBuildMessages(
2469 5, maxSequence, version, manifest, blobInfos, messages),
2470 maxSequence,
2471 4);
2472 verifyMessage(version, manifest, blobInfos, messages, {{6, 7, 10, 12}});
2473
2474 // Test message splitting on size limits.
2475
2476 // Set a limit that should give two messages
2477 messages.clear();
2478 verifyBuildMessages(
2480 5, maxSequence, version, manifest, blobInfos, messages, 300),
2481 maxSequence,
2482 4);
2483 verifyMessage(version, manifest, blobInfos, messages, {{6, 7}, {10, 12}});
2484
2485 // Set a limit between the size of the two earlier messages so one
2486 // will split and the other won't
2487 messages.clear();
2488 verifyBuildMessages(
2490 5, maxSequence, version, manifest, blobInfos, messages, 200),
2491 maxSequence,
2492 4);
2493 verifyMessage(version, manifest, blobInfos, messages, {{6}, {7}, {10, 12}});
2494
2495 // Set a limit so that all the VLs are sent individually
2496 messages.clear();
2497 verifyBuildMessages(
2499 5, maxSequence, version, manifest, blobInfos, messages, 150),
2500 maxSequence,
2501 4);
2502 verifyMessage(version, manifest, blobInfos, messages, {{6}, {7}, {10}, {12}});
2503
2504 // Set a limit smaller than some of the messages. Because single
2505 // messages send regardless, they will all still be sent
2506 messages.clear();
2507 verifyBuildMessages(
2509 5, maxSequence, version, manifest, blobInfos, messages, 108),
2510 maxSequence,
2511 4);
2512 verifyMessage(version, manifest, blobInfos, messages, {{6}, {7}, {10}, {12}});
2513 }
2514
2515 void
2516 testQuorumDisabled() // NOLINT(readability-function-size)
2517 {
2518 testcase("Test quorum disabled");
2519
2520 std::string const siteUri = "testQuorumDisabled.test";
2521 jtx::Env env(*this);
2522 auto& app = env.app();
2523
2524 static constexpr std::size_t kMaxKeys = 20;
2525 HashSet<NodeID> activeValidators;
2526 std::vector<Validator> valKeys;
2527 while (valKeys.size() != kMaxKeys)
2528 {
2529 valKeys.push_back(randomValidator());
2530 activeValidators.emplace(calcNodeID(valKeys.back().masterPublic));
2531 }
2532
2533 struct Publisher
2534 {
2535 bool revoked;
2536 PublicKey pubKey;
2538 std::string manifest;
2539 NetClock::time_point expiry = {}; // NOLINT(readability-redundant-member-init)
2540 };
2541
2542 // Create ValidatorList with a set of countTotal publishers, of which
2543 // first countRevoked are revoked and the last one expires early
2544 auto const makeValidatorList = [&, this](
2545 std::size_t countTotal,
2546 std::size_t countRevoked,
2547 std::size_t listThreshold,
2548 ManifestCache& pubManifests,
2549 ManifestCache& valManifests,
2551 std::vector<Publisher>& publishers // out
2553 auto result = std::make_unique<ValidatorList>(
2554 valManifests,
2555 pubManifests,
2556 env.timeKeeper(),
2557 app.config().legacy(Sections::kDatabasePath),
2558 env.journal);
2559
2560 std::vector<std::string> cfgPublishers;
2561 for (std::size_t i = 0; i < countTotal; ++i)
2562 {
2563 auto const publisherSecret = randomSecretKey();
2564 auto const publisherPublic = derivePublicKey(KeyType::Ed25519, publisherSecret);
2565 auto const pubSigningKeys = randomKeyPair(KeyType::Secp256k1);
2566 cfgPublishers.push_back(strHex(publisherPublic));
2567
2568 constexpr auto kRevoked = std::numeric_limits<std::uint32_t>::max();
2569 auto const manifest = base64Encode(makeManifestString(
2570 publisherPublic,
2571 publisherSecret,
2572 pubSigningKeys.first,
2573 pubSigningKeys.second,
2574 i < countRevoked ? kRevoked : 1));
2575 publishers.push_back(
2576 Publisher{
2577 .revoked = i < countRevoked,
2578 .pubKey = publisherPublic,
2579 .signingKeys = pubSigningKeys,
2580 .manifest = manifest,
2581 });
2582 }
2583
2584 std::vector<std::string> const emptyCfgKeys;
2585 auto const threshold = listThreshold > 0 ? std::optional(listThreshold) : std::nullopt;
2586 if (self)
2587 {
2588 valManifests.applyManifest(
2589 *deserializeManifest(base64Decode(self->manifest)),
2591 BEAST_EXPECT(
2592 result->load(self->signingPublic, emptyCfgKeys, cfgPublishers, threshold));
2593 }
2594 else
2595 {
2596 BEAST_EXPECT(result->load({}, emptyCfgKeys, cfgPublishers, threshold));
2597 }
2598
2599 for (std::size_t i = 0; i < countTotal; ++i)
2600 {
2601 using namespace std::chrono_literals;
2602 publishers[i].expiry = env.timeKeeper().now() + (i == countTotal - 1 ? 60s : 3600s);
2603 auto const blob =
2604 makeList(valKeys, 1, publishers[i].expiry.time_since_epoch().count());
2605 auto const sig = signList(blob, publishers[i].signingKeys);
2606
2607 BEAST_EXPECT(
2608 result->applyLists(publishers[i].manifest, 1, {{blob, sig, {}}}, siteUri)
2609 .bestDisposition() ==
2610 (publishers[i].revoked ? ListDisposition::Untrusted
2612 }
2613
2614 return result;
2615 };
2616
2617 // Test cases use 5 publishers.
2618 constexpr auto kQuorumDisabled = std::numeric_limits<std::size_t>::max();
2619 {
2620 // List threshold = 5 (same as number of trusted publishers)
2621 ManifestCache pubManifests;
2622 ManifestCache valManifests;
2623 std::vector<Publisher> publishers;
2624 // Self is a random validator
2625 auto const self = randomValidator();
2626 auto const keysTotal = valKeys.size() + 1;
2627 auto trustedKeys = makeValidatorList(
2628 5, //
2629 0,
2630 5,
2631 pubManifests,
2632 valManifests,
2633 self,
2634 publishers);
2635 BEAST_EXPECT(trustedKeys->getListThreshold() == 5);
2636 for (auto const& p : publishers)
2637 BEAST_EXPECT(trustedKeys->trustedPublisher(p.pubKey));
2638
2639 TrustChanges changes = trustedKeys->updateTrusted(
2640 activeValidators,
2641 env.timeKeeper().now(),
2642 env.app().getOPs(),
2643 env.app().getOverlay(),
2644 env.app().getHashRouter());
2645 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
2646 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
2647
2648 HashSet<NodeID> added;
2649 added.insert(calcNodeID(self.masterPublic));
2650 for (auto const& val : valKeys)
2651 {
2652 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
2653 added.insert(calcNodeID(val.masterPublic));
2654 }
2655 BEAST_EXPECT(changes.added == added);
2656 BEAST_EXPECT(changes.removed.empty());
2657
2658 // Expire one publisher - only trusted validator is self
2659 env.timeKeeper().set(publishers.back().expiry);
2660 changes = trustedKeys->updateTrusted(
2661 activeValidators,
2662 env.timeKeeper().now(),
2663 env.app().getOPs(),
2664 env.app().getOverlay(),
2665 env.app().getHashRouter());
2666 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
2667 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == 1);
2668
2669 HashSet<NodeID> removed;
2670 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
2671 for (auto const& val : valKeys)
2672 {
2673 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
2674 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
2675 removed.insert(calcNodeID(val.masterPublic));
2676 }
2677 BEAST_EXPECT(changes.added.empty());
2678 BEAST_EXPECT(changes.removed == removed);
2679 }
2680 {
2681 // List threshold = 5 (same as number of trusted publishers)
2682 ManifestCache pubManifests;
2683 ManifestCache valManifests;
2684 std::vector<Publisher> publishers;
2685 auto const keysTotal = valKeys.size();
2686 auto trustedKeys = makeValidatorList(
2687 5, //
2688 0,
2689 5,
2690 pubManifests,
2691 valManifests,
2692 {},
2693 publishers);
2694 BEAST_EXPECT(trustedKeys->getListThreshold() == 5);
2695 for (auto const& p : publishers)
2696 BEAST_EXPECT(trustedKeys->trustedPublisher(p.pubKey));
2697
2698 TrustChanges changes = trustedKeys->updateTrusted(
2699 activeValidators,
2700 env.timeKeeper().now(),
2701 env.app().getOPs(),
2702 env.app().getOverlay(),
2703 env.app().getHashRouter());
2704 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
2705 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
2706
2707 HashSet<NodeID> added;
2708 for (auto const& val : valKeys)
2709 {
2710 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
2711 added.insert(calcNodeID(val.masterPublic));
2712 }
2713 BEAST_EXPECT(changes.added == added);
2714 BEAST_EXPECT(changes.removed.empty());
2715
2716 // Expire one publisher - no trusted validators
2717 env.timeKeeper().set(publishers.back().expiry);
2718 changes = trustedKeys->updateTrusted(
2719 activeValidators,
2720 env.timeKeeper().now(),
2721 env.app().getOPs(),
2722 env.app().getOverlay(),
2723 env.app().getHashRouter());
2724 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
2725 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().empty());
2726
2727 HashSet<NodeID> removed;
2728 for (auto const& val : valKeys)
2729 {
2730 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
2731 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
2732 removed.insert(calcNodeID(val.masterPublic));
2733 }
2734 BEAST_EXPECT(changes.added.empty());
2735 BEAST_EXPECT(changes.removed == removed);
2736 }
2737 {
2738 // List threshold = 4, 1 publisher is revoked
2739 ManifestCache pubManifests;
2740 ManifestCache valManifests;
2741 std::vector<Publisher> publishers;
2742 // Self is in UNL
2743 auto const self = valKeys[1];
2744 auto const keysTotal = valKeys.size();
2745 auto trustedKeys = makeValidatorList(
2746 5, //
2747 1,
2748 4,
2749 pubManifests,
2750 valManifests,
2751 self,
2752 publishers);
2753 BEAST_EXPECT(trustedKeys->getListThreshold() == 4);
2754 int untrustedCount = 0;
2755 for (auto const& p : publishers)
2756 {
2757 bool const trusted = trustedKeys->trustedPublisher(p.pubKey);
2758 BEAST_EXPECT(p.revoked ^ trusted);
2759 untrustedCount += trusted ? 0 : 1;
2760 }
2761 BEAST_EXPECT(untrustedCount == 1);
2762
2763 TrustChanges changes = trustedKeys->updateTrusted(
2764 activeValidators,
2765 env.timeKeeper().now(),
2766 env.app().getOPs(),
2767 env.app().getOverlay(),
2768 env.app().getHashRouter());
2769 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
2770 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
2771
2772 HashSet<NodeID> added;
2773 for (auto const& val : valKeys)
2774 {
2775 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
2776 added.insert(calcNodeID(val.masterPublic));
2777 }
2778 BEAST_EXPECT(changes.added == added);
2779 BEAST_EXPECT(changes.removed.empty());
2780
2781 // Expire one publisher - only trusted validator is self
2782 env.timeKeeper().set(publishers.back().expiry);
2783 changes = trustedKeys->updateTrusted(
2784 activeValidators,
2785 env.timeKeeper().now(),
2786 env.app().getOPs(),
2787 env.app().getOverlay(),
2788 env.app().getHashRouter());
2789 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
2790 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == 1);
2791
2792 HashSet<NodeID> removed;
2793 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
2794 for (auto const& val : valKeys)
2795 {
2796 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
2797 if (val.masterPublic != self.masterPublic)
2798 {
2799 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
2800 removed.insert(calcNodeID(val.masterPublic));
2801 }
2802 }
2803 BEAST_EXPECT(changes.added.empty());
2804 BEAST_EXPECT(changes.removed == removed);
2805 }
2806 {
2807 // List threshold = 3 (default), 2 publishers are revoked
2808 ManifestCache pubManifests;
2809 ManifestCache valManifests;
2810 std::vector<Publisher> publishers;
2811 // Self is a random validator
2812 auto const self = randomValidator();
2813 auto const keysTotal = valKeys.size() + 1;
2814 auto trustedKeys = makeValidatorList(
2815 5, //
2816 2,
2817 0,
2818 pubManifests,
2819 valManifests,
2820 self,
2821 publishers);
2822 BEAST_EXPECT(trustedKeys->getListThreshold() == 3);
2823 int untrustedCount = 0;
2824 for (auto const& p : publishers)
2825 {
2826 bool const trusted = trustedKeys->trustedPublisher(p.pubKey);
2827 BEAST_EXPECT(p.revoked ^ trusted);
2828 untrustedCount += trusted ? 0 : 1;
2829 }
2830 BEAST_EXPECT(untrustedCount == 2);
2831
2832 TrustChanges changes = trustedKeys->updateTrusted(
2833 activeValidators,
2834 env.timeKeeper().now(),
2835 env.app().getOPs(),
2836 env.app().getOverlay(),
2837 env.app().getHashRouter());
2838 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
2839 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
2840
2841 HashSet<NodeID> added;
2842 added.insert(calcNodeID(self.masterPublic));
2843 for (auto const& val : valKeys)
2844 {
2845 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
2846 added.insert(calcNodeID(val.masterPublic));
2847 }
2848 BEAST_EXPECT(changes.added == added);
2849 BEAST_EXPECT(changes.removed.empty());
2850
2851 // Expire one publisher - no quorum, only trusted validator is self
2852 env.timeKeeper().set(publishers.back().expiry);
2853 changes = trustedKeys->updateTrusted(
2854 activeValidators,
2855 env.timeKeeper().now(),
2856 env.app().getOPs(),
2857 env.app().getOverlay(),
2858 env.app().getHashRouter());
2859 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
2860 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == 1);
2861
2862 HashSet<NodeID> removed;
2863 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
2864 for (auto const& val : valKeys)
2865 {
2866 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
2867 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
2868 removed.insert(calcNodeID(val.masterPublic));
2869 }
2870 BEAST_EXPECT(changes.added.empty());
2871 BEAST_EXPECT(changes.removed == removed);
2872 }
2873 {
2874 // List threshold = 3 (default), 2 publishers are revoked
2875 ManifestCache pubManifests;
2876 ManifestCache valManifests;
2877 std::vector<Publisher> publishers;
2878 // Self is in UNL
2879 auto const self = valKeys[5];
2880 auto const keysTotal = valKeys.size();
2881 auto trustedKeys = makeValidatorList(
2882 5, //
2883 2,
2884 0,
2885 pubManifests,
2886 valManifests,
2887 self,
2888 publishers);
2889 BEAST_EXPECT(trustedKeys->getListThreshold() == 3);
2890 int untrustedCount = 0;
2891 for (auto const& p : publishers)
2892 {
2893 bool const trusted = trustedKeys->trustedPublisher(p.pubKey);
2894 BEAST_EXPECT(p.revoked ^ trusted);
2895 untrustedCount += trusted ? 0 : 1;
2896 }
2897 BEAST_EXPECT(untrustedCount == 2);
2898
2899 TrustChanges changes = trustedKeys->updateTrusted(
2900 activeValidators,
2901 env.timeKeeper().now(),
2902 env.app().getOPs(),
2903 env.app().getOverlay(),
2904 env.app().getHashRouter());
2905 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
2906 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
2907
2908 HashSet<NodeID> added;
2909 for (auto const& val : valKeys)
2910 {
2911 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
2912 added.insert(calcNodeID(val.masterPublic));
2913 }
2914 BEAST_EXPECT(changes.added == added);
2915 BEAST_EXPECT(changes.removed.empty());
2916
2917 // Expire one publisher - no quorum, only trusted validator is self
2918 env.timeKeeper().set(publishers.back().expiry);
2919 changes = trustedKeys->updateTrusted(
2920 activeValidators,
2921 env.timeKeeper().now(),
2922 env.app().getOPs(),
2923 env.app().getOverlay(),
2924 env.app().getHashRouter());
2925 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
2926 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == 1);
2927
2928 HashSet<NodeID> removed;
2929 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
2930 for (auto const& val : valKeys)
2931 {
2932 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
2933 if (val.masterPublic != self.masterPublic)
2934 {
2935 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
2936 removed.insert(calcNodeID(val.masterPublic));
2937 }
2938 }
2939 BEAST_EXPECT(changes.added.empty());
2940 BEAST_EXPECT(changes.removed == removed);
2941 }
2942 {
2943 // List threshold = 3 (default), 2 publishers are revoked
2944 ManifestCache pubManifests;
2945 ManifestCache valManifests;
2946 std::vector<Publisher> publishers;
2947 auto const keysTotal = valKeys.size();
2948 auto trustedKeys = makeValidatorList(
2949 5, //
2950 2,
2951 0,
2952 pubManifests,
2953 valManifests,
2954 {},
2955 publishers);
2956 BEAST_EXPECT(trustedKeys->getListThreshold() == 3);
2957 int untrustedCount = 0;
2958 for (auto const& p : publishers)
2959 {
2960 bool const trusted = trustedKeys->trustedPublisher(p.pubKey);
2961 BEAST_EXPECT(p.revoked ^ trusted);
2962 untrustedCount += trusted ? 0 : 1;
2963 }
2964 BEAST_EXPECT(untrustedCount == 2);
2965
2966 TrustChanges changes = trustedKeys->updateTrusted(
2967 activeValidators,
2968 env.timeKeeper().now(),
2969 env.app().getOPs(),
2970 env.app().getOverlay(),
2971 env.app().getHashRouter());
2972 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
2973 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
2974
2975 HashSet<NodeID> added;
2976 for (auto const& val : valKeys)
2977 {
2978 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
2979 added.insert(calcNodeID(val.masterPublic));
2980 }
2981 BEAST_EXPECT(changes.added == added);
2982 BEAST_EXPECT(changes.removed.empty());
2983
2984 // Expire one publisher - no quorum, no trusted validators
2985 env.timeKeeper().set(publishers.back().expiry);
2986 changes = trustedKeys->updateTrusted(
2987 activeValidators,
2988 env.timeKeeper().now(),
2989 env.app().getOPs(),
2990 env.app().getOverlay(),
2991 env.app().getHashRouter());
2992 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
2993 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().empty());
2994
2995 HashSet<NodeID> removed;
2996 for (auto const& val : valKeys)
2997 {
2998 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
2999 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
3000 removed.insert(calcNodeID(val.masterPublic));
3001 }
3002 BEAST_EXPECT(changes.added.empty());
3003 BEAST_EXPECT(changes.removed == removed);
3004 }
3005 {
3006 // List threshold = 2, 1 publisher is revoked
3007 ManifestCache pubManifests;
3008 ManifestCache valManifests;
3009 std::vector<Publisher> publishers;
3010 // Self is a random validator
3011 auto const self = randomValidator();
3012 auto const keysTotal = valKeys.size() + 1;
3013 auto trustedKeys = makeValidatorList(
3014 5, //
3015 1,
3016 2,
3017 pubManifests,
3018 valManifests,
3019 self,
3020 publishers);
3021 BEAST_EXPECT(trustedKeys->getListThreshold() == 2);
3022 int untrustedCount = 0;
3023 for (auto const& p : publishers)
3024 {
3025 bool const trusted = trustedKeys->trustedPublisher(p.pubKey);
3026 BEAST_EXPECT(p.revoked ^ trusted);
3027 untrustedCount += trusted ? 0 : 1;
3028 }
3029 BEAST_EXPECT(untrustedCount == 1);
3030
3031 TrustChanges changes = trustedKeys->updateTrusted(
3032 activeValidators,
3033 env.timeKeeper().now(),
3034 env.app().getOPs(),
3035 env.app().getOverlay(),
3036 env.app().getHashRouter());
3037 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
3038 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3039
3040 HashSet<NodeID> added;
3041 added.insert(calcNodeID(self.masterPublic));
3042 for (auto const& val : valKeys)
3043 {
3044 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3045 added.insert(calcNodeID(val.masterPublic));
3046 }
3047 BEAST_EXPECT(changes.added == added);
3048 BEAST_EXPECT(changes.removed.empty());
3049
3050 // Expire one publisher - no quorum
3051 env.timeKeeper().set(publishers.back().expiry);
3052 changes = trustedKeys->updateTrusted(
3053 activeValidators,
3054 env.timeKeeper().now(),
3055 env.app().getOPs(),
3056 env.app().getOverlay(),
3057 env.app().getHashRouter());
3058 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3059 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3060
3061 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
3062 for (auto const& val : valKeys)
3063 {
3064 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
3065 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3066 }
3067 BEAST_EXPECT(changes.added.empty());
3068 BEAST_EXPECT(changes.removed.empty());
3069 }
3070 {
3071 // List threshold = 1
3072 ManifestCache pubManifests;
3073 ManifestCache valManifests;
3074 std::vector<Publisher> publishers;
3075 // Self is a random validator
3076 auto const self = randomValidator();
3077 auto const keysTotal = valKeys.size() + 1;
3078 auto trustedKeys = makeValidatorList(
3079 5, //
3080 0,
3081 1,
3082 pubManifests,
3083 valManifests,
3084 self,
3085 publishers);
3086 BEAST_EXPECT(trustedKeys->getListThreshold() == 1);
3087 for (auto const& p : publishers)
3088 BEAST_EXPECT(trustedKeys->trustedPublisher(p.pubKey));
3089
3090 TrustChanges changes = trustedKeys->updateTrusted(
3091 activeValidators,
3092 env.timeKeeper().now(),
3093 env.app().getOPs(),
3094 env.app().getOverlay(),
3095 env.app().getHashRouter());
3096 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
3097 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3098
3099 HashSet<NodeID> added;
3100 added.insert(calcNodeID(self.masterPublic));
3101 for (auto const& val : valKeys)
3102 {
3103 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3104 added.insert(calcNodeID(val.masterPublic));
3105 }
3106 BEAST_EXPECT(changes.added == added);
3107 BEAST_EXPECT(changes.removed.empty());
3108
3109 // Expire one publisher - no quorum
3110 env.timeKeeper().set(publishers.back().expiry);
3111 changes = trustedKeys->updateTrusted(
3112 activeValidators,
3113 env.timeKeeper().now(),
3114 env.app().getOPs(),
3115 env.app().getOverlay(),
3116 env.app().getHashRouter());
3117 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3118 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3119
3120 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
3121 for (auto const& val : valKeys)
3122 {
3123 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
3124 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3125 }
3126 BEAST_EXPECT(changes.added.empty());
3127 BEAST_EXPECT(changes.removed.empty());
3128 }
3129 {
3130 // List threshold = 1
3131 ManifestCache pubManifests;
3132 ManifestCache valManifests;
3133 std::vector<Publisher> publishers;
3134 // Self is in UNL
3135 auto const self = valKeys[7];
3136 auto const keysTotal = valKeys.size();
3137 auto trustedKeys = makeValidatorList(
3138 5, //
3139 0,
3140 1,
3141 pubManifests,
3142 valManifests,
3143 self,
3144 publishers);
3145 BEAST_EXPECT(trustedKeys->getListThreshold() == 1);
3146 for (auto const& p : publishers)
3147 BEAST_EXPECT(trustedKeys->trustedPublisher(p.pubKey));
3148
3149 TrustChanges changes = trustedKeys->updateTrusted(
3150 activeValidators,
3151 env.timeKeeper().now(),
3152 env.app().getOPs(),
3153 env.app().getOverlay(),
3154 env.app().getHashRouter());
3155 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
3156 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3157
3158 HashSet<NodeID> added;
3159 for (auto const& val : valKeys)
3160 {
3161 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3162 added.insert(calcNodeID(val.masterPublic));
3163 }
3164 BEAST_EXPECT(changes.added == added);
3165 BEAST_EXPECT(changes.removed.empty());
3166
3167 // Expire one publisher - no quorum
3168 env.timeKeeper().set(publishers.back().expiry);
3169 changes = trustedKeys->updateTrusted(
3170 activeValidators,
3171 env.timeKeeper().now(),
3172 env.app().getOPs(),
3173 env.app().getOverlay(),
3174 env.app().getHashRouter());
3175 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3176 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3177
3178 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
3179 for (auto const& val : valKeys)
3180 {
3181 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
3182 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3183 }
3184 BEAST_EXPECT(changes.added.empty());
3185 BEAST_EXPECT(changes.removed.empty());
3186 }
3187 {
3188 // List threshold = 1
3189 ManifestCache pubManifests;
3190 ManifestCache valManifests;
3191 std::vector<Publisher> publishers;
3192 auto const keysTotal = valKeys.size();
3193 auto trustedKeys = makeValidatorList(
3194 5, //
3195 0,
3196 1,
3197 pubManifests,
3198 valManifests,
3199 {},
3200 publishers);
3201 BEAST_EXPECT(trustedKeys->getListThreshold() == 1);
3202 for (auto const& p : publishers)
3203 BEAST_EXPECT(trustedKeys->trustedPublisher(p.pubKey));
3204
3205 TrustChanges changes = trustedKeys->updateTrusted(
3206 activeValidators,
3207 env.timeKeeper().now(),
3208 env.app().getOPs(),
3209 env.app().getOverlay(),
3210 env.app().getHashRouter());
3211 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
3212 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3213
3214 HashSet<NodeID> added;
3215 for (auto const& val : valKeys)
3216 {
3217 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3218 added.insert(calcNodeID(val.masterPublic));
3219 }
3220 BEAST_EXPECT(changes.added == added);
3221 BEAST_EXPECT(changes.removed.empty());
3222
3223 // Expire one publisher - no quorum
3224 env.timeKeeper().set(publishers.back().expiry);
3225 changes = trustedKeys->updateTrusted(
3226 activeValidators,
3227 env.timeKeeper().now(),
3228 env.app().getOPs(),
3229 env.app().getOverlay(),
3230 env.app().getHashRouter());
3231 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3232 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3233
3234 for (auto const& val : valKeys)
3235 {
3236 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
3237 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3238 }
3239 BEAST_EXPECT(changes.added.empty());
3240 BEAST_EXPECT(changes.removed.empty());
3241 }
3242
3243 // Test cases use 2 publishers
3244 {
3245 // List threshold = 1, 1 publisher revoked
3246 ManifestCache pubManifests;
3247 ManifestCache valManifests;
3248 std::vector<Publisher> publishers;
3249 // Self is a random validator
3250 auto const self = randomValidator();
3251 auto const keysTotal = valKeys.size() + 1;
3252 auto trustedKeys = makeValidatorList(
3253 2, //
3254 1,
3255 1,
3256 pubManifests,
3257 valManifests,
3258 self,
3259 publishers);
3260 BEAST_EXPECT(trustedKeys->getListThreshold() == 1);
3261 int untrustedCount = 0;
3262 for (auto const& p : publishers)
3263 {
3264 bool const trusted = trustedKeys->trustedPublisher(p.pubKey);
3265 BEAST_EXPECT(p.revoked ^ trusted);
3266 untrustedCount += trusted ? 0 : 1;
3267 }
3268 BEAST_EXPECT(untrustedCount == 1);
3269
3270 TrustChanges changes = trustedKeys->updateTrusted(
3271 activeValidators,
3272 env.timeKeeper().now(),
3273 env.app().getOPs(),
3274 env.app().getOverlay(),
3275 env.app().getHashRouter());
3276 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3277 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3278
3279 HashSet<NodeID> added;
3280 added.insert(calcNodeID(self.masterPublic));
3281 for (auto const& val : valKeys)
3282 {
3283 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3284 added.insert(calcNodeID(val.masterPublic));
3285 }
3286 BEAST_EXPECT(changes.added == added);
3287 BEAST_EXPECT(changes.removed.empty());
3288
3289 // Expire one publisher - no quorum, only trusted validator is self
3290 env.timeKeeper().set(publishers.back().expiry);
3291 changes = trustedKeys->updateTrusted(
3292 activeValidators,
3293 env.timeKeeper().now(),
3294 env.app().getOPs(),
3295 env.app().getOverlay(),
3296 env.app().getHashRouter());
3297 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3298 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == 1);
3299
3300 HashSet<NodeID> removed;
3301 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
3302 for (auto const& val : valKeys)
3303 {
3304 BEAST_EXPECT(!trustedKeys->listed(val.masterPublic));
3305 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
3306 removed.insert(calcNodeID(val.masterPublic));
3307 }
3308 BEAST_EXPECT(changes.added.empty());
3309 BEAST_EXPECT(changes.removed == removed);
3310 }
3311 {
3312 // List threshold = 1, 1 publisher revoked
3313 ManifestCache pubManifests;
3314 ManifestCache valManifests;
3315 std::vector<Publisher> publishers;
3316 // Self is in UNL
3317 auto const self = valKeys[5];
3318 auto const keysTotal = valKeys.size();
3319 auto trustedKeys = makeValidatorList(
3320 2, //
3321 1,
3322 1,
3323 pubManifests,
3324 valManifests,
3325 self,
3326 publishers);
3327 BEAST_EXPECT(trustedKeys->getListThreshold() == 1);
3328 int untrustedCount = 0;
3329 for (auto const& p : publishers)
3330 {
3331 bool const trusted = trustedKeys->trustedPublisher(p.pubKey);
3332 BEAST_EXPECT(p.revoked ^ trusted);
3333 untrustedCount += trusted ? 0 : 1;
3334 }
3335 BEAST_EXPECT(untrustedCount == 1);
3336
3337 TrustChanges changes = trustedKeys->updateTrusted(
3338 activeValidators,
3339 env.timeKeeper().now(),
3340 env.app().getOPs(),
3341 env.app().getOverlay(),
3342 env.app().getHashRouter());
3343 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3344 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3345
3346 HashSet<NodeID> added;
3347 for (auto const& val : valKeys)
3348 {
3349 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3350 added.insert(calcNodeID(val.masterPublic));
3351 }
3352 BEAST_EXPECT(changes.added == added);
3353 BEAST_EXPECT(changes.removed.empty());
3354
3355 // Expire one publisher - no quorum, only trusted validator is self
3356 env.timeKeeper().set(publishers.back().expiry);
3357 changes = trustedKeys->updateTrusted(
3358 activeValidators,
3359 env.timeKeeper().now(),
3360 env.app().getOPs(),
3361 env.app().getOverlay(),
3362 env.app().getHashRouter());
3363 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3364 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == 1);
3365
3366 HashSet<NodeID> removed;
3367 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
3368 for (auto const& val : valKeys)
3369 {
3370 if (val.masterPublic != self.masterPublic)
3371 {
3372 BEAST_EXPECT(!trustedKeys->listed(val.masterPublic));
3373 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
3374 removed.insert(calcNodeID(val.masterPublic));
3375 }
3376 }
3377 BEAST_EXPECT(changes.added.empty());
3378 BEAST_EXPECT(changes.removed == removed);
3379 }
3380 {
3381 // List threshold = 1, 1 publisher revoked
3382 ManifestCache pubManifests;
3383 ManifestCache valManifests;
3384 std::vector<Publisher> publishers;
3385 auto const keysTotal = valKeys.size();
3386 auto trustedKeys = makeValidatorList(
3387 2, //
3388 1,
3389 1,
3390 pubManifests,
3391 valManifests,
3392 {},
3393 publishers);
3394 BEAST_EXPECT(trustedKeys->getListThreshold() == 1);
3395 int untrustedCount = 0;
3396 for (auto const& p : publishers)
3397 {
3398 bool const trusted = trustedKeys->trustedPublisher(p.pubKey);
3399 BEAST_EXPECT(p.revoked ^ trusted);
3400 untrustedCount += trusted ? 0 : 1;
3401 }
3402 BEAST_EXPECT(untrustedCount == 1);
3403
3404 TrustChanges changes = trustedKeys->updateTrusted(
3405 activeValidators,
3406 env.timeKeeper().now(),
3407 env.app().getOPs(),
3408 env.app().getOverlay(),
3409 env.app().getHashRouter());
3410 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3411 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3412
3413 HashSet<NodeID> added;
3414 for (auto const& val : valKeys)
3415 {
3416 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3417 added.insert(calcNodeID(val.masterPublic));
3418 }
3419 BEAST_EXPECT(changes.added == added);
3420 BEAST_EXPECT(changes.removed.empty());
3421
3422 // Expire one publisher - no quorum, no trusted validators
3423 env.timeKeeper().set(publishers.back().expiry);
3424 changes = trustedKeys->updateTrusted(
3425 activeValidators,
3426 env.timeKeeper().now(),
3427 env.app().getOPs(),
3428 env.app().getOverlay(),
3429 env.app().getHashRouter());
3430 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3431 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().empty());
3432
3433 HashSet<NodeID> removed;
3434 for (auto const& val : valKeys)
3435 {
3436 BEAST_EXPECT(!trustedKeys->listed(val.masterPublic));
3437 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
3438 removed.insert(calcNodeID(val.masterPublic));
3439 }
3440 BEAST_EXPECT(changes.added.empty());
3441 BEAST_EXPECT(changes.removed == removed);
3442 }
3443 {
3444 // List threshold = 2 (same as number of trusted publishers)
3445 ManifestCache pubManifests;
3446 ManifestCache valManifests;
3447 std::vector<Publisher> publishers;
3448 // Self is a random validator
3449 auto const self = randomValidator();
3450 auto const keysTotal = valKeys.size() + 1;
3451 auto trustedKeys = makeValidatorList(
3452 2, //
3453 0,
3454 2,
3455 pubManifests,
3456 valManifests,
3457 self,
3458 publishers);
3459 BEAST_EXPECT(trustedKeys->getListThreshold() == 2);
3460 for (auto const& p : publishers)
3461 BEAST_EXPECT(trustedKeys->trustedPublisher(p.pubKey));
3462
3463 TrustChanges changes = trustedKeys->updateTrusted(
3464 activeValidators,
3465 env.timeKeeper().now(),
3466 env.app().getOPs(),
3467 env.app().getOverlay(),
3468 env.app().getHashRouter());
3469 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
3470 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3471
3472 HashSet<NodeID> added;
3473 added.insert(calcNodeID(self.masterPublic));
3474 for (auto const& val : valKeys)
3475 {
3476 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3477 added.insert(calcNodeID(val.masterPublic));
3478 }
3479 BEAST_EXPECT(changes.added == added);
3480 BEAST_EXPECT(changes.removed.empty());
3481
3482 // Expire one publisher - only trusted validator is self
3483 env.timeKeeper().set(publishers.back().expiry);
3484 changes = trustedKeys->updateTrusted(
3485 activeValidators,
3486 env.timeKeeper().now(),
3487 env.app().getOPs(),
3488 env.app().getOverlay(),
3489 env.app().getHashRouter());
3490 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3491 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == 1);
3492
3493 HashSet<NodeID> removed;
3494 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
3495 for (auto const& val : valKeys)
3496 {
3497 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
3498 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
3499 removed.insert(calcNodeID(val.masterPublic));
3500 }
3501 BEAST_EXPECT(changes.added.empty());
3502 BEAST_EXPECT(changes.removed == removed);
3503 }
3504 {
3505 // List threshold = 2 (same as number of trusted publishers)
3506 ManifestCache pubManifests;
3507 ManifestCache valManifests;
3508 std::vector<Publisher> publishers;
3509 // Self is in UNL
3510 auto const self = valKeys[5];
3511 auto const keysTotal = valKeys.size();
3512 auto trustedKeys = makeValidatorList(
3513 2, //
3514 0,
3515 2,
3516 pubManifests,
3517 valManifests,
3518 self,
3519 publishers);
3520 BEAST_EXPECT(trustedKeys->getListThreshold() == 2);
3521 for (auto const& p : publishers)
3522 BEAST_EXPECT(trustedKeys->trustedPublisher(p.pubKey));
3523
3524 TrustChanges changes = trustedKeys->updateTrusted(
3525 activeValidators,
3526 env.timeKeeper().now(),
3527 env.app().getOPs(),
3528 env.app().getOverlay(),
3529 env.app().getHashRouter());
3530 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
3531 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3532
3533 HashSet<NodeID> added;
3534 added.insert(calcNodeID(self.masterPublic));
3535 for (auto const& val : valKeys)
3536 {
3537 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3538 added.insert(calcNodeID(val.masterPublic));
3539 }
3540 BEAST_EXPECT(changes.added == added);
3541 BEAST_EXPECT(changes.removed.empty());
3542
3543 // Expire one publisher - only trusted validator is self
3544 env.timeKeeper().set(publishers.back().expiry);
3545 changes = trustedKeys->updateTrusted(
3546 activeValidators,
3547 env.timeKeeper().now(),
3548 env.app().getOPs(),
3549 env.app().getOverlay(),
3550 env.app().getHashRouter());
3551 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3552 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == 1);
3553
3554 HashSet<NodeID> removed;
3555 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
3556 for (auto const& val : valKeys)
3557 {
3558 if (val.masterPublic != self.masterPublic)
3559 {
3560 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
3561 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
3562 removed.insert(calcNodeID(val.masterPublic));
3563 }
3564 }
3565 BEAST_EXPECT(changes.added.empty());
3566 BEAST_EXPECT(changes.removed == removed);
3567 }
3568 {
3569 // List threshold = 2 (same as number of trusted publishers)
3570 ManifestCache pubManifests;
3571 ManifestCache valManifests;
3572 std::vector<Publisher> publishers;
3573 auto const keysTotal = valKeys.size();
3574 auto trustedKeys = makeValidatorList(
3575 2, //
3576 0,
3577 2,
3578 pubManifests,
3579 valManifests,
3580 {},
3581 publishers);
3582 BEAST_EXPECT(trustedKeys->getListThreshold() == 2);
3583 for (auto const& p : publishers)
3584 BEAST_EXPECT(trustedKeys->trustedPublisher(p.pubKey));
3585
3586 TrustChanges changes = trustedKeys->updateTrusted(
3587 activeValidators,
3588 env.timeKeeper().now(),
3589 env.app().getOPs(),
3590 env.app().getOverlay(),
3591 env.app().getHashRouter());
3592 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
3593 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3594
3595 HashSet<NodeID> added;
3596 for (auto const& val : valKeys)
3597 {
3598 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3599 added.insert(calcNodeID(val.masterPublic));
3600 }
3601 BEAST_EXPECT(changes.added == added);
3602 BEAST_EXPECT(changes.removed.empty());
3603
3604 // Expire one publisher - no trusted validators
3605 env.timeKeeper().set(publishers.back().expiry);
3606 changes = trustedKeys->updateTrusted(
3607 activeValidators,
3608 env.timeKeeper().now(),
3609 env.app().getOPs(),
3610 env.app().getOverlay(),
3611 env.app().getHashRouter());
3612 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3613 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().empty());
3614
3615 HashSet<NodeID> removed;
3616 for (auto const& val : valKeys)
3617 {
3618 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
3619 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
3620 removed.insert(calcNodeID(val.masterPublic));
3621 }
3622 BEAST_EXPECT(changes.added.empty());
3623 BEAST_EXPECT(changes.removed == removed);
3624 }
3625
3626 // Test case for 1 publisher
3627 {
3628 // List threshold = 1 (default), no publisher revoked
3629 ManifestCache pubManifests;
3630 ManifestCache valManifests;
3631 std::vector<Publisher> publishers;
3632 // Self is a random validator
3633 auto const self = randomValidator();
3634 auto const keysTotal = valKeys.size() + 1;
3635 auto trustedKeys = makeValidatorList(
3636 1, //
3637 0,
3638 0,
3639 pubManifests,
3640 valManifests,
3641 self,
3642 publishers);
3643 BEAST_EXPECT(trustedKeys->getListThreshold() == 1);
3644 for (auto const& p : publishers)
3645 BEAST_EXPECT(trustedKeys->trustedPublisher(p.pubKey));
3646
3647 TrustChanges changes = trustedKeys->updateTrusted(
3648 activeValidators,
3649 env.timeKeeper().now(),
3650 env.app().getOPs(),
3651 env.app().getOverlay(),
3652 env.app().getHashRouter());
3653 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
3654 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3655
3656 HashSet<NodeID> added;
3657 added.insert(calcNodeID(self.masterPublic));
3658 for (auto const& val : valKeys)
3659 {
3660 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3661 added.insert(calcNodeID(val.masterPublic));
3662 }
3663 BEAST_EXPECT(changes.added == added);
3664 BEAST_EXPECT(changes.removed.empty());
3665
3666 // Expire one publisher - no quorum, only trusted validator is self
3667 env.timeKeeper().set(publishers.back().expiry);
3668 changes = trustedKeys->updateTrusted(
3669 activeValidators,
3670 env.timeKeeper().now(),
3671 env.app().getOPs(),
3672 env.app().getOverlay(),
3673 env.app().getHashRouter());
3674 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3675 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == 1);
3676
3677 HashSet<NodeID> removed;
3678 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
3679 for (auto const& val : valKeys)
3680 {
3681 BEAST_EXPECT(!trustedKeys->listed(val.masterPublic));
3682 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
3683 removed.insert(calcNodeID(val.masterPublic));
3684 }
3685 BEAST_EXPECT(changes.added.empty());
3686 BEAST_EXPECT(changes.removed == removed);
3687 }
3688
3689 // Test case for 3 publishers (for 2 is a block above)
3690 {
3691 // List threshold = 2 (default), no publisher revoked
3692 ManifestCache pubManifests;
3693 ManifestCache valManifests;
3694 std::vector<Publisher> publishers;
3695 // Self is in UNL
3696 auto const self = valKeys[2];
3697 auto const keysTotal = valKeys.size();
3698 auto trustedKeys = makeValidatorList(
3699 3, //
3700 0,
3701 0,
3702 pubManifests,
3703 valManifests,
3704 self,
3705 publishers);
3706 BEAST_EXPECT(trustedKeys->getListThreshold() == 2);
3707 for (auto const& p : publishers)
3708 BEAST_EXPECT(trustedKeys->trustedPublisher(p.pubKey));
3709
3710 TrustChanges const changes = trustedKeys->updateTrusted(
3711 activeValidators,
3712 env.timeKeeper().now(),
3713 env.app().getOPs(),
3714 env.app().getOverlay(),
3715 env.app().getHashRouter());
3716 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
3717 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3718
3719 HashSet<NodeID> added;
3720 for (auto const& val : valKeys)
3721 {
3722 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3723 added.insert(calcNodeID(val.masterPublic));
3724 }
3725 BEAST_EXPECT(changes.added == added);
3726 BEAST_EXPECT(changes.removed.empty());
3727 }
3728
3729 // Test case for 4 publishers
3730 {
3731 // List threshold = 3 (default), no publisher revoked
3732 ManifestCache pubManifests;
3733 ManifestCache valManifests;
3734 std::vector<Publisher> publishers;
3735 auto const keysTotal = valKeys.size();
3736 auto trustedKeys = makeValidatorList(
3737 4, //
3738 0,
3739 0,
3740 pubManifests,
3741 valManifests,
3742 {},
3743 publishers);
3744 BEAST_EXPECT(trustedKeys->getListThreshold() == 3);
3745 for (auto const& p : publishers)
3746 BEAST_EXPECT(trustedKeys->trustedPublisher(p.pubKey));
3747
3748 TrustChanges const changes = trustedKeys->updateTrusted(
3749 activeValidators,
3750 env.timeKeeper().now(),
3751 env.app().getOPs(),
3752 env.app().getOverlay(),
3753 env.app().getHashRouter());
3754 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
3755 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3756
3757 HashSet<NodeID> added;
3758 for (auto const& val : valKeys)
3759 {
3760 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3761 added.insert(calcNodeID(val.masterPublic));
3762 }
3763 BEAST_EXPECT(changes.added == added);
3764 BEAST_EXPECT(changes.removed.empty());
3765 }
3766
3767 // Test case for 6 publishers (for 5 is a large block above)
3768 {
3769 // List threshold = 4 (default), 2 publishers revoked
3770 ManifestCache pubManifests;
3771 ManifestCache valManifests;
3772 std::vector<Publisher> publishers;
3773 // Self is a random validator
3774 auto const self = randomValidator();
3775 auto const keysTotal = valKeys.size() + 1;
3776 auto trustedKeys = makeValidatorList(
3777 6, //
3778 2,
3779 0,
3780 pubManifests,
3781 valManifests,
3782 self,
3783 publishers);
3784 BEAST_EXPECT(trustedKeys->getListThreshold() == 4);
3785 int untrustedCount = 0;
3786 for (auto const& p : publishers)
3787 {
3788 bool const trusted = trustedKeys->trustedPublisher(p.pubKey);
3789 BEAST_EXPECT(p.revoked ^ trusted);
3790 untrustedCount += trusted ? 0 : 1;
3791 }
3792 BEAST_EXPECT(untrustedCount == 2);
3793
3794 TrustChanges changes = trustedKeys->updateTrusted(
3795 activeValidators,
3796 env.timeKeeper().now(),
3797 env.app().getOPs(),
3798 env.app().getOverlay(),
3799 env.app().getHashRouter());
3800 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
3801 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3802
3803 HashSet<NodeID> added;
3804 added.insert(calcNodeID(self.masterPublic));
3805 for (auto const& val : valKeys)
3806 {
3807 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3808 added.insert(calcNodeID(val.masterPublic));
3809 }
3810 BEAST_EXPECT(changes.added == added);
3811 BEAST_EXPECT(changes.removed.empty());
3812
3813 // Expire one publisher - no quorum, only trusted validator is self
3814 env.timeKeeper().set(publishers.back().expiry);
3815 changes = trustedKeys->updateTrusted(
3816 activeValidators,
3817 env.timeKeeper().now(),
3818 env.app().getOPs(),
3819 env.app().getOverlay(),
3820 env.app().getHashRouter());
3821 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3822 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == 1);
3823
3824 HashSet<NodeID> removed;
3825 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
3826 for (auto const& val : valKeys)
3827 {
3828 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
3829 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
3830 removed.insert(calcNodeID(val.masterPublic));
3831 }
3832 BEAST_EXPECT(changes.added.empty());
3833 BEAST_EXPECT(changes.removed == removed);
3834 }
3835
3836 // Test case for 7 publishers
3837 {
3838 // List threshold = 4 (default), 3 publishers revoked
3839 ManifestCache pubManifests;
3840 ManifestCache valManifests;
3841 std::vector<Publisher> publishers;
3842 // Self is in UNL
3843 auto const self = valKeys[2];
3844 auto const keysTotal = valKeys.size();
3845 auto trustedKeys = makeValidatorList(
3846 7, //
3847 3,
3848 0,
3849 pubManifests,
3850 valManifests,
3851 self,
3852 publishers);
3853 BEAST_EXPECT(trustedKeys->getListThreshold() == 4);
3854 int untrustedCount = 0;
3855 for (auto const& p : publishers)
3856 {
3857 bool const trusted = trustedKeys->trustedPublisher(p.pubKey);
3858 BEAST_EXPECT(p.revoked ^ trusted);
3859 untrustedCount += trusted ? 0 : 1;
3860 }
3861 BEAST_EXPECT(untrustedCount == 3);
3862
3863 TrustChanges changes = trustedKeys->updateTrusted(
3864 activeValidators,
3865 env.timeKeeper().now(),
3866 env.app().getOPs(),
3867 env.app().getOverlay(),
3868 env.app().getHashRouter());
3869 BEAST_EXPECT(trustedKeys->quorum() == std::ceil(keysTotal * 0.8f));
3870 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == keysTotal);
3871
3872 HashSet<NodeID> added;
3873 for (auto const& val : valKeys)
3874 {
3875 BEAST_EXPECT(trustedKeys->trusted(val.masterPublic));
3876 added.insert(calcNodeID(val.masterPublic));
3877 }
3878 BEAST_EXPECT(changes.added == added);
3879 BEAST_EXPECT(changes.removed.empty());
3880
3881 // Expire one publisher - only trusted validator is self
3882 env.timeKeeper().set(publishers.back().expiry);
3883 changes = trustedKeys->updateTrusted(
3884 activeValidators,
3885 env.timeKeeper().now(),
3886 env.app().getOPs(),
3887 env.app().getOverlay(),
3888 env.app().getHashRouter());
3889 BEAST_EXPECT(trustedKeys->quorum() == kQuorumDisabled);
3890 BEAST_EXPECT(trustedKeys->getTrustedMasterKeys().size() == 1);
3891
3892 HashSet<NodeID> removed;
3893 BEAST_EXPECT(trustedKeys->trusted(self.masterPublic));
3894 for (auto const& val : valKeys)
3895 {
3896 if (val.masterPublic != self.masterPublic)
3897 {
3898 BEAST_EXPECT(trustedKeys->listed(val.masterPublic));
3899 BEAST_EXPECT(!trustedKeys->trusted(val.masterPublic));
3900 removed.insert(calcNodeID(val.masterPublic));
3901 }
3902 }
3903 BEAST_EXPECT(changes.added.empty());
3904 BEAST_EXPECT(changes.removed == removed);
3905 }
3906 }
3907
3908public:
3909 void
3910 run() override
3911 {
3917 testExpires();
3922 }
3923}; // namespace test
3924
3926
3927} // namespace xrpl::test
T at(T... args)
T back(T... args)
T cbegin(T... args)
T capacity(T... args)
T ceil(T... args)
A testsuite class.
Definition suite.h:52
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
virtual Config & config()=0
void legacy(std::string const &section, std::string value)
Set a value that is not a key/value pair.
Remembers manifests with the highest sequence number.
Definition Manifest.h:374
std::optional< PublicKey > getSigningKey(PublicKey const &pk) const
Returns master key's current signing key.
ManifestDisposition applyManifest(Manifest m, ManifestRateLimitCapPolicy cap)
Add manifest to cache.
bool revoked(PublicKey const &pk) const
Returns true if master key has been revoked in a manifest.
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
A public key.
Definition PublicKey.h:53
void add(Serializer &s) const override
Definition STObject.cpp:123
A secret key.
Definition SecretKey.h:24
std::size_t size() const noexcept
Definition Serializer.h:147
void const * data() const noexcept
Definition Serializer.h:153
virtual NetworkOPs & getOPs()=0
virtual HashRouter & getHashRouter()=0
virtual Overlay & getOverlay()=0
virtual TimeKeeper & getTimeKeeper()=0
An immutable linear range of bytes.
Definition Slice.h:28
static std::pair< std::size_t, std::size_t > buildValidatorListMessages(std::uint64_t peerSequence, std::size_t maxSequence, std::uint32_t rawVersion, std::string const &rawManifest, std::map< std::size_t, ValidatorBlobInfo > const &blobInfos, std::vector< MessageWithHash > &messages, std::size_t maxSize=kMaximumMessageSize)
time_point now() const override
Returns the current time.
static HashSet< NodeID > asNodeIDs(std::initializer_list< PublicKey > const &pks)
static std::string signList(std::string const &blob, std::pair< PublicKey, SecretKey > const &keys)
static std::string makeRevocationString(PublicKey const &pk, SecretKey const &sk)
void checkResult(ValidatorList::PublisherListStats const &result, PublicKey pubKey, ListDisposition expectedWorst, ListDisposition expectedBest)
void run() override
Runs the suite.
static std::string makeList(std::vector< Validator > const &validators, std::size_t sequence, std::size_t validUntil, std::optional< std::size_t > validFrom={})
static std::string makeManifestString(PublicKey const &pk, SecretKey const &sk, PublicKey const &spk, SecretKey const &ssk, int seq)
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
ManualTimeKeeper & timeKeeper()
Definition Env.h:313
beast::Journal const journal
Definition Env.h:204
T clear(T... args)
T contains(T... args)
T emplace(T... args)
T cend(T... args)
T front(T... args)
T insert(T... args)
T make_pair(T... args)
T make_shared(T... args)
T make_unique(T... args)
T max(T... args)
boost::iterator_range< ListIterator > makeList(boost::string_ref const &field)
Returns a range representing the list.
Definition rfc2616.h:341
std::optional< MessageHeader > parseMessageHeader(boost::system::error_code &ec, BufferSequence const &bufs, std::size_t size)
Parse a message header.
std::shared_ptr< T > parseMessageContent(MessageHeader const &header, Buffers const &buffers)
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:38
void sign(json::Value const &jv, Account const &account, json::Value &sigObject, HashPrefix prefix)
Sign automatically into a specific Json field of the jv object.
Definition utility.cpp:52
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string base64Decode(std::string_view data)
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
ListDisposition
@ Expired
List is expired, but has the largest non-pending sequence seen so far.
@ SameSequence
Same sequence as current list.
@ Pending
List will be valid in the future.
@ Accepted
List is valid.
@ Untrusted
List signed by untrusted publisher key.
SField const sfGeneric
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
std::optional< Manifest > deserializeManifest(Slice s, beast::Journal journal)
Constructs Manifest from serialized string.
@ Capped
Subject to the untrusted cap (unlisted peer gossip).
Definition Manifest.h:364
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
std::string base64Encode(std::uint8_t const *data, std::size_t len)
std::unordered_set< Value, Hash, Pred, Allocator > HashSet
@ Manifest
Manifest.
Definition HashPrefix.h:84
BaseUInt< 160, detail::NodeIDTag > NodeID
NodeID is a 160-bit hash representing one node.
Definition UintTypes.h:47
Sha512HalfHasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:215
@ Accepted
Manifest is valid.
Definition Manifest.h:321
T push_back(T... args)
T reserve(T... args)
T str(T... args)
static constexpr auto kDatabasePath
Definition Constants.h:13
Changes in trusted nodes after updating validator list.
HashSet< NodeID > added
HashSet< NodeID > removed
Describes the result of processing a Validator List (UNL), including some of the information from the...
std::optional< PublicKey > publisherKey
T time_since_epoch(T... args)
T to_string(T... args)