xrpld
Loading...
Searching...
No Matches
AmendmentTable_test.cpp
1#include <test/jtx/Env.h>
2#include <test/jtx/envconfig.h>
3#include <test/unit_test/SuiteJournal.h>
4
5#include <xrpld/app/main/Application.h>
6#include <xrpld/core/Config.h>
7
8#include <xrpl/basics/UnorderedContainers.h>
9#include <xrpl/basics/base_uint.h>
10#include <xrpl/basics/chrono.h>
11#include <xrpl/basics/contract.h>
12#include <xrpl/beast/unit_test/suite.h>
13#include <xrpl/config/BasicConfig.h>
14#include <xrpl/config/Constants.h>
15#include <xrpl/json/json_value.h>
16#include <xrpl/ledger/AmendmentTable.h>
17#include <xrpl/ledger/View.h>
18#include <xrpl/protocol/Feature.h>
19#include <xrpl/protocol/KeyType.h>
20#include <xrpl/protocol/PublicKey.h>
21#include <xrpl/protocol/Rules.h>
22#include <xrpl/protocol/SField.h>
23#include <xrpl/protocol/STValidation.h>
24#include <xrpl/protocol/SecretKey.h>
25#include <xrpl/protocol/TxFlags.h>
26#include <xrpl/protocol/digest.h>
27#include <xrpl/protocol/jss.h>
28
29#include <algorithm>
30#include <cassert>
31#include <chrono>
32#include <cstddef>
33#include <cstring>
34#include <exception>
35#include <memory>
36#include <set>
37#include <stdexcept>
38#include <string>
39#include <utility>
40#include <vector>
41
42namespace xrpl {
43
45{
46private:
47 static UInt256
49 {
52 hash_append(h, in);
53 auto const d = static_cast<Sha256Hasher::result_type>(h);
54 UInt256 result;
55 std::memcpy(result.data(), d.data(), d.size());
56 return result;
57 }
58
59 static Section
60 makeSection(std::string const& name, std::vector<std::string> const& amendments)
61 {
62 Section section(name);
63 for (auto const& a : amendments)
64 section.append(to_string(amendmentId(a)) + " " + a);
65 return section;
66 }
67
68 static Section
70 {
71 return makeSection("Test", amendments);
72 }
73
74 static Section
75 makeSection(UInt256 const& amendment)
76 {
77 Section section("Test");
78 section.append(to_string(amendment) + " " + to_string(amendment));
79 return section;
80 }
81
90
92 makeFeatureInfo(std::vector<std::string> const& amendments, VoteBehavior voteBehavior)
93 {
95 result.reserve(amendments.size());
96 for (auto const& a : amendments)
97 {
98 result.emplace_back(a, amendmentId(a), voteBehavior);
99 }
100 return result;
101 }
102
105 {
106 return makeFeatureInfo(amendments, VoteBehavior::DefaultYes);
107 }
108
110 makeDefaultYes(UInt256 const amendment)
111 {
113 {to_string(amendment), amendment, VoteBehavior::DefaultYes},
114 };
115 return result;
116 }
117
120 {
121 return makeFeatureInfo(amendments, VoteBehavior::DefaultNo);
122 }
123
126 {
127 return makeFeatureInfo(amendments, VoteBehavior::Obsolete);
128 }
129
130 template <class Arg, class... Args>
131 static size_t
132 totalsize(std::vector<Arg> const& src, Args const&... args)
133 {
134 if constexpr (sizeof...(args) > 0)
135 return src.size() + totalsize(args...);
136 return src.size();
137 }
138
139 template <class Arg, class... Args>
140 static void
141 combineArg(std::vector<Arg>& dest, std::vector<Arg> const& src, Args const&... args)
142 {
143 assert(dest.capacity() >= dest.size() + src.size());
145 if constexpr (sizeof...(args) > 0)
146 combineArg(dest, args...);
147 }
148
149 template <class Arg, class... Args>
150 static std::vector<Arg>
152 // Pass "left" by value. The values will need to be copied one way or
153 // another, so just reuse it.
154 std::vector<Arg> left,
155 std::vector<Arg> const& right,
156 Args const&... args)
157 {
158 left.reserve(totalsize(left, right, args...));
159
160 combineArg(left, right, args...);
161
162 return left;
163 }
164
165 // All useful amendments are supported amendments.
166 // Enabled amendments are typically a subset of supported amendments.
167 // Vetoed amendments should be supported but not enabled.
168 // Unsupported amendments may be added to the AmendmentTable.
169 std::vector<std::string> const yes_{"g", "i", "k", "m", "o", "q", "r", "s", "t", "u"};
170 std::vector<std::string> const enabled_{"b", "d", "f", "h", "j", "l", "n", "p"};
171 std::vector<std::string> const vetoed_{"a", "c", "e"};
176
179
181
182public:
183 AmendmentTable_test() : journal_("AmendmentTable_test", *this)
184 {
185 }
186
189 Application& app,
190 std::chrono::seconds majorityTime,
192 Section const& enabled,
193 Section const& vetoed)
194 {
195 return makeAmendmentTable(app, majorityTime, supported, enabled, vetoed, journal_);
196 }
197
200 test::jtx::Env& env,
201 std::chrono::seconds majorityTime,
203 Section const& enabled,
204 Section const& vetoed)
205 {
206 return makeTable(env.app(), majorityTime, supported, enabled, vetoed);
207 }
208
211 {
212 static std::vector<AmendmentTable::FeatureInfo> const kSupported = combine(
214 // Use non-intuitive default votes for "enabled_" and "vetoed_"
215 // so that when the tests later explicitly enable or veto them,
216 // we can be certain that they are not simply going by their
217 // default vote setting.
221 return makeTable(
222 env.app(), majorityTime, kSupported, makeSection(enabled_), makeSection(vetoed_));
223 }
224
225 void
227 {
228 testcase("Construction");
229 test::jtx::Env env{*this, makeConfig()};
230 auto table = makeTable(env, Weeks(1));
231
232 for (auto const& a : allSupported_)
233 BEAST_EXPECT(table->isSupported(amendmentId(a)));
234
235 for (auto const& a : yes_)
236 BEAST_EXPECT(table->isSupported(amendmentId(a)));
237
238 for (auto const& a : enabled_)
239 BEAST_EXPECT(table->isSupported(amendmentId(a)));
240
241 for (auto const& a : vetoed_)
242 {
243 BEAST_EXPECT(table->isSupported(amendmentId(a)));
244 BEAST_EXPECT(!table->isEnabled(amendmentId(a)));
245 }
246
247 for (auto const& a : obsolete_)
248 {
249 BEAST_EXPECT(table->isSupported(amendmentId(a)));
250 BEAST_EXPECT(!table->isEnabled(amendmentId(a)));
251 }
252 }
253
254 void
256 {
257 testcase("Name to ID mapping");
258
259 test::jtx::Env env{*this, makeConfig()};
260 auto table = makeTable(env, Weeks(1));
261
262 for (auto const& a : yes_)
263 BEAST_EXPECT(table->find(a) == amendmentId(a));
264 for (auto const& a : enabled_)
265 BEAST_EXPECT(table->find(a) == amendmentId(a));
266 for (auto const& a : vetoed_)
267 BEAST_EXPECT(table->find(a) == amendmentId(a));
268 for (auto const& a : obsolete_)
269 BEAST_EXPECT(table->find(a) == amendmentId(a));
270 for (auto const& a : unsupported_)
271 BEAST_EXPECT(!table->find(a));
272 for (auto const& a : unsupportedMajority_)
273 BEAST_EXPECT(!table->find(a));
274
275 // Vetoing an unsupported amendment should add the amendment to table.
276 // Verify that unsupportedID is not in table.
277 UInt256 const unsupportedID = amendmentId(unsupported_[0]);
278 {
279 json::Value const unsupp =
280 table->getJson(unsupportedID, true)[to_string(unsupportedID)];
281 BEAST_EXPECT(unsupp.size() == 0);
282 }
283
284 // After vetoing unsupportedID verify that it is in table.
285 table->veto(unsupportedID);
286 {
287 json::Value const unsupp =
288 table->getJson(unsupportedID, true)[to_string(unsupportedID)];
289 BEAST_EXPECT(unsupp[jss::vetoed].asBool());
290 }
291 }
292
293 void
295 {
296 auto const yesVotes = makeDefaultYes(yes_);
297 auto const section = makeSection(vetoed_);
298 auto const id = to_string(amendmentId(enabled_[0]));
299
300 testcase("Bad Config");
301
302 { // Two arguments are required - we pass one
303 Section test = section;
304 test.append(id);
305
306 try
307 {
308 test::jtx::Env env{*this, makeConfig()};
309 if (makeTable(env, Weeks(2), yesVotes, test, emptySection_))
310 fail("Accepted only amendment ID");
311 }
312 catch (std::exception const& e)
313 {
314 BEAST_EXPECT(e.what() == "Invalid entry '" + id + "' in [Test]");
315 }
316 }
317
318 { // Two arguments are required - we pass three
319 Section test = section;
320 test.append(id + " Test Name");
321
322 try
323 {
324 test::jtx::Env env{*this, makeConfig()};
325 if (makeTable(env, Weeks(2), yesVotes, test, emptySection_))
326 fail("Accepted extra arguments");
327 }
328 catch (std::exception const& e)
329 {
330 BEAST_EXPECT(e.what() == "Invalid entry '" + id + " Test Name' in [Test]");
331 }
332 }
333
334 {
335 auto sid = id;
336 sid.resize(sid.length() - 1);
337
338 Section test = section;
339 test.append(sid + " Name");
340
341 try
342 {
343 test::jtx::Env env{*this, makeConfig()};
344 if (makeTable(env, Weeks(2), yesVotes, test, emptySection_))
345 fail("Accepted short amendment ID");
346 }
347 catch (std::exception const& e)
348 {
349 BEAST_EXPECT(e.what() == "Invalid entry '" + sid + " Name' in [Test]");
350 }
351 }
352
353 {
354 auto sid = id;
355 sid.resize(sid.length() + 1, '0');
356
357 Section test = section;
358 test.append(sid + " Name");
359
360 try
361 {
362 test::jtx::Env env{*this, makeConfig()};
363 if (makeTable(env, Weeks(2), yesVotes, test, emptySection_))
364 fail("Accepted long amendment ID");
365 }
366 catch (std::exception const& e)
367 {
368 BEAST_EXPECT(e.what() == "Invalid entry '" + sid + " Name' in [Test]");
369 }
370 }
371
372 {
373 auto sid = id;
374 sid.resize(sid.length() - 1);
375 sid.push_back('Q');
376
377 Section test = section;
378 test.append(sid + " Name");
379
380 try
381 {
382 test::jtx::Env env{*this, makeConfig()};
383 if (makeTable(env, Weeks(2), yesVotes, test, emptySection_))
384 fail("Accepted non-hex amendment ID");
385 }
386 catch (std::exception const& e)
387 {
388 BEAST_EXPECT(e.what() == "Invalid entry '" + sid + " Name' in [Test]");
389 }
390 }
391 }
392
393 void
395 {
396 testcase("enable and veto");
397
398 test::jtx::Env env{*this, makeConfig()};
400
401 // Note which entries are enabled (convert the amendment names to IDs)
402 std::set<UInt256> allEnabled;
403 for (auto const& a : enabled_)
404 allEnabled.insert(amendmentId(a));
405
406 for (UInt256 const& a : allEnabled)
407 BEAST_EXPECT(table->enable(a));
408
409 // So far all enabled amendments are supported.
410 BEAST_EXPECT(!table->hasUnsupportedEnabled());
411
412 // Verify all enables are enabled and nothing else.
413 for (std::string const& a : yes_)
414 {
415 UInt256 const supportedID = amendmentId(a);
416 bool const enabled = table->isEnabled(supportedID);
417 bool const found = allEnabled.contains(supportedID);
418 BEAST_EXPECTS(
419 enabled == found,
420 a + (enabled ? " enabled " : " disabled ") + (found ? " found" : " not found"));
421 }
422
423 // All supported and unVetoed amendments should be returned as desired.
424 {
425 std::set<UInt256> vetoed;
426 for (std::string const& a : vetoed_)
427 vetoed.insert(amendmentId(a));
428
429 std::vector<UInt256> const desired = table->getDesired();
430 for (UInt256 const& a : desired)
431 BEAST_EXPECT(not vetoed.contains(a));
432
433 // Unveto an amendment that is already not vetoed. Shouldn't
434 // hurt anything, but the values returned by getDesired()
435 // shouldn't change.
436 BEAST_EXPECT(!table->unVeto(amendmentId(yes_[1])));
437 BEAST_EXPECT(desired == table->getDesired());
438 }
439
440 // UnVeto one of the vetoed amendments. It should now be desired.
441 {
442 UInt256 const unvetoedID = amendmentId(vetoed_[0]);
443 BEAST_EXPECT(table->unVeto(unvetoedID));
444
445 std::vector<UInt256> const desired = table->getDesired();
446 BEAST_EXPECT(std::ranges::find(desired, unvetoedID) != desired.end());
447 }
448
449 // Veto all supported amendments. Now desired should be empty.
450 for (std::string const& a : allSupported_)
451 {
452 table->veto(amendmentId(a));
453 }
454 BEAST_EXPECT(table->getDesired().empty());
455
456 // Enable an unsupported amendment.
457 {
458 BEAST_EXPECT(!table->hasUnsupportedEnabled());
459 table->enable(amendmentId(unsupported_[0]));
460 BEAST_EXPECT(table->hasUnsupportedEnabled());
461 }
462 }
463
464 // Make a list of trusted validators.
465 // Register the validators with AmendmentTable and return the list.
468 {
470 ret.reserve(num);
471 HashSet<PublicKey> trustedValidators;
472 trustedValidators.reserve(num);
473 for (int i = 0; i < num; ++i)
474 {
475 auto const& back = ret.emplace_back(randomKeyPair(KeyType::Secp256k1));
476 trustedValidators.insert(back.first);
477 }
478 table->trustChanged(trustedValidators);
479 return ret;
480 }
481
484 {
485 return NetClock::time_point{h};
486 }
487
488 // Execute a pretend consensus round for a flag ledger
489 static void
491 Rules const& rules,
492 AmendmentTable& table,
496 std::vector<UInt256>& ourVotes,
497 std::set<UInt256>& enabled,
498 MajorityAmendmentsT& majority)
499 {
500 // Do a round at the specified time
501 // Returns the amendments we voted for
502
503 // Parameters:
504 // table: Our table of known and vetoed amendments
505 // validators: The addresses of validators we trust
506 // votes: Amendments and the number of validators who vote for them
507 // ourVotes: The amendments we vote for in our validation
508 // enabled: In/out enabled amendments
509 // majority: In/our majority amendments (and when they got a majority)
510
511 auto const roundTime = hourTime(hour);
512
513 // Build validations
515 validations.reserve(validators.size());
516
517 int i = 0;
518 for (auto const& [pub, sec] : validators)
519 {
520 ++i;
522
523 for (auto const& [hash, nVotes] : votes)
524 {
525 if (nVotes >= i)
526 {
527 // We vote yes on this amendment
528 field.push_back(hash);
529 }
530 }
531
532 auto const v = std::make_shared<STValidation>(
533 xrpl::NetClock::time_point{}, pub, sec, calcNodeID(pub), [&field](STValidation& v) {
534 if (!field.empty())
535 v.setFieldV256(sfAmendments, STVector256(sfAmendments, field));
536 v.setFieldU32(sfLedgerSequence, 6180339);
537 });
538
539 validations.emplace_back(v);
540 }
541
542 ourVotes = table.doValidation(enabled);
543
544 auto const actions = table.doVoting(rules, roundTime, enabled, majority, validations);
545 for (auto const& [hash, action] : actions)
546 {
547 // This code assumes other validators do as we do
548
549 switch (action)
550 {
551 case 0:
552 // amendment goes from majority to enabled
553 if (enabled.contains(hash))
554 Throw<std::runtime_error>("enabling already enabled");
555 if (!majority.contains(hash))
556 Throw<std::runtime_error>("enabling without majority");
557 enabled.insert(hash);
558 majority.erase(hash);
559 break;
560
561 case tfGotMajority:
562 if (majority.contains(hash))
563 Throw<std::runtime_error>("got majority while having majority");
564 majority[hash] = roundTime;
565 break;
566
567 case tfLostMajority:
568 if (!majority.contains(hash))
569 Throw<std::runtime_error>("lost majority without majority");
570 majority.erase(hash);
571 break;
572
573 default:
574 Throw<std::runtime_error>("unknown action");
575 }
576 }
577 }
578
579 // No vote on unknown amendment
580 void
582 {
583 testcase("Vote NO on unknown");
584
585 auto const testAmendment = amendmentId("TestAmendment");
586
587 test::jtx::Env env{*this, feat};
588 auto table = makeTable(env, Weeks(2), emptyYes_, emptySection_, emptySection_);
589
590 auto const validators = makeValidators(10, table);
591
593 std::vector<UInt256> ourVotes;
594 std::set<UInt256> enabled;
595 MajorityAmendmentsT majority;
596
597 doRound(
598 env.current()->rules(),
599 *table,
600 Weeks{1},
601 validators,
602 votes,
603 ourVotes,
604 enabled,
605 majority);
606 BEAST_EXPECT(ourVotes.empty());
607 BEAST_EXPECT(enabled.empty());
608 BEAST_EXPECT(majority.empty());
609
610 UInt256 const unsupportedID = amendmentId(unsupported_[0]);
611 {
612 json::Value const unsupp =
613 table->getJson(unsupportedID, false)[to_string(unsupportedID)];
614 BEAST_EXPECT(unsupp.size() == 0);
615 }
616
617 table->veto(unsupportedID);
618 {
619 json::Value const unsupp =
620 table->getJson(unsupportedID, false)[to_string(unsupportedID)];
621 BEAST_EXPECT(!unsupp[jss::vetoed].asBool());
622 }
623
624 votes.emplace_back(testAmendment, validators.size());
625
626 votes.emplace_back(testAmendment, validators.size());
627
628 doRound(
629 env.current()->rules(),
630 *table,
631 Weeks{2},
632 validators,
633 votes,
634 ourVotes,
635 enabled,
636 majority);
637 BEAST_EXPECT(ourVotes.empty());
638 BEAST_EXPECT(enabled.empty());
639
640 majority[testAmendment] = hourTime(Weeks{1});
641
642 // Note that the simulation code assumes others behave as we do,
643 // so the amendment won't get enabled
644 doRound(
645 env.current()->rules(),
646 *table,
647 Weeks{5},
648 validators,
649 votes,
650 ourVotes,
651 enabled,
652 majority);
653 BEAST_EXPECT(ourVotes.empty());
654 BEAST_EXPECT(enabled.empty());
655 }
656
657 // No vote on vetoed amendment
658 void
660 {
661 testcase("Vote NO on vetoed");
662
663 auto const testAmendment = amendmentId("vetoedAmendment");
664
665 test::jtx::Env env{*this, feat};
666 auto table = makeTable(env, Weeks(2), emptyYes_, emptySection_, makeSection(testAmendment));
667
668 auto const validators = makeValidators(10, table);
669
671 std::vector<UInt256> ourVotes;
672 std::set<UInt256> enabled;
673 MajorityAmendmentsT majority;
674
675 doRound(
676 env.current()->rules(),
677 *table,
678 Weeks{1},
679 validators,
680 votes,
681 ourVotes,
682 enabled,
683 majority);
684 BEAST_EXPECT(ourVotes.empty());
685 BEAST_EXPECT(enabled.empty());
686 BEAST_EXPECT(majority.empty());
687
688 votes.emplace_back(testAmendment, validators.size());
689
690 doRound(
691 env.current()->rules(),
692 *table,
693 Weeks{2},
694 validators,
695 votes,
696 ourVotes,
697 enabled,
698 majority);
699 BEAST_EXPECT(ourVotes.empty());
700 BEAST_EXPECT(enabled.empty());
701
702 majority[testAmendment] = hourTime(Weeks{1});
703
704 doRound(
705 env.current()->rules(),
706 *table,
707 Weeks{5},
708 validators,
709 votes,
710 ourVotes,
711 enabled,
712 majority);
713 BEAST_EXPECT(ourVotes.empty());
714 BEAST_EXPECT(enabled.empty());
715 }
716
717 // Vote on and enable known, not-enabled amendment
718 void
720 {
721 testcase("voteEnable");
722
723 test::jtx::Env env{*this, feat};
725
726 auto const validators = makeValidators(10, table);
727
729 std::vector<UInt256> ourVotes;
730 std::set<UInt256> enabled;
731 MajorityAmendmentsT majority;
732
733 // Week 1: We should vote for all known amendments not enabled
734 doRound(
735 env.current()->rules(),
736 *table,
737 Weeks{1},
738 validators,
739 votes,
740 ourVotes,
741 enabled,
742 majority);
743 BEAST_EXPECT(ourVotes.size() == yes_.size());
744 BEAST_EXPECT(enabled.empty());
745 for (auto const& i : yes_)
746 BEAST_EXPECT(not majority.contains(amendmentId(i)));
747
748 // Now, everyone votes for this feature
749 for (auto const& i : yes_)
750 votes.emplace_back(amendmentId(i), validators.size());
751
752 // Week 2: We should recognize a majority
753 doRound(
754 env.current()->rules(),
755 *table,
756 Weeks{2},
757 validators,
758 votes,
759 ourVotes,
760 enabled,
761 majority);
762 BEAST_EXPECT(ourVotes.size() == yes_.size());
763 BEAST_EXPECT(enabled.empty());
764
765 for (auto const& i : yes_)
766 BEAST_EXPECT(majority[amendmentId(i)] == hourTime(Weeks{2}));
767
768 // Week 5: We should enable the amendment
769 doRound(
770 env.current()->rules(),
771 *table,
772 Weeks{5},
773 validators,
774 votes,
775 ourVotes,
776 enabled,
777 majority);
778 BEAST_EXPECT(enabled.size() == yes_.size());
779
780 // Week 6: We should remove it from our votes and from having a majority
781 doRound(
782 env.current()->rules(),
783 *table,
784 Weeks{6},
785 validators,
786 votes,
787 ourVotes,
788 enabled,
789 majority);
790 BEAST_EXPECT(enabled.size() == yes_.size());
791 BEAST_EXPECT(ourVotes.empty());
792 for (auto const& i : yes_)
793 BEAST_EXPECT(not majority.contains(amendmentId(i)));
794 }
795
796 // Detect majority at 80%, enable later
797 void
799 {
800 testcase("detectMajority");
801
802 auto const testAmendment = amendmentId("detectMajority");
803 test::jtx::Env env{*this, feat};
804 auto table =
805 makeTable(env, Weeks(2), makeDefaultYes(testAmendment), emptySection_, emptySection_);
806
807 auto const validators = makeValidators(16, table);
808
809 std::set<UInt256> enabled;
810 MajorityAmendmentsT majority;
811
812 for (int i = 0; i <= 17; ++i)
813 {
815 std::vector<UInt256> ourVotes;
816
817 if ((i > 0) && (i < 17))
818 votes.emplace_back(testAmendment, i);
819
820 doRound(
821 env.current()->rules(),
822 *table,
823 Weeks{i},
824 validators,
825 votes,
826 ourVotes,
827 enabled,
828 majority);
829
830 if (i < 13) // 13 => 13/16 = 0.8125 => > 80%
831 {
832 // We are voting yes, not enabled, no majority
833 BEAST_EXPECT(!ourVotes.empty());
834 BEAST_EXPECT(enabled.empty());
835 BEAST_EXPECT(majority.empty());
836 }
837 else if (i < 15)
838 {
839 // We have a majority, not enabled, keep voting
840 BEAST_EXPECT(!ourVotes.empty());
841 BEAST_EXPECT(!majority.empty());
842 BEAST_EXPECT(enabled.empty());
843 }
844 else if (i == 15)
845 {
846 // enable, keep voting, remove from majority
847 BEAST_EXPECT(!ourVotes.empty());
848 BEAST_EXPECT(majority.empty());
849 BEAST_EXPECT(!enabled.empty());
850 }
851 else
852 {
853 // Done, we should be enabled and not voting
854 BEAST_EXPECT(ourVotes.empty());
855 BEAST_EXPECT(majority.empty());
856 BEAST_EXPECT(!enabled.empty());
857 }
858 }
859 }
860
861 // Detect loss of majority
862 void
864 {
865 testcase("lostMajority");
866
867 auto const testAmendment = amendmentId("lostMajority");
868
869 test::jtx::Env env{*this, feat};
870 auto table =
871 makeTable(env, Weeks(8), makeDefaultYes(testAmendment), emptySection_, emptySection_);
872
873 auto const validators = makeValidators(16, table);
874
875 std::set<UInt256> enabled;
876 MajorityAmendmentsT majority;
877
878 {
879 // establish majority
881 std::vector<UInt256> ourVotes;
882
883 votes.emplace_back(testAmendment, validators.size());
884
885 doRound(
886 env.current()->rules(),
887 *table,
888 Weeks{1},
889 validators,
890 votes,
891 ourVotes,
892 enabled,
893 majority);
894
895 BEAST_EXPECT(enabled.empty());
896 BEAST_EXPECT(!majority.empty());
897 }
898
899 for (int i = 1; i < 8; ++i)
900 {
902 std::vector<UInt256> ourVotes;
903
904 // Gradually reduce support
905 votes.emplace_back(testAmendment, validators.size() - i);
906
907 doRound(
908 env.current()->rules(),
909 *table,
910 Weeks{i + 1},
911 validators,
912 votes,
913 ourVotes,
914 enabled,
915 majority);
916
917 if (i < 4) // 16 - 3 = 13 => 13/16 = 0.8125 => > 80%
918 { // 16 - 4 = 12 => 12/16 = 0.75 => < 80%
919 // We are voting yes, not enabled, majority
920 BEAST_EXPECT(!ourVotes.empty());
921 BEAST_EXPECT(enabled.empty());
922 BEAST_EXPECT(!majority.empty());
923 }
924 else
925 {
926 // No majority, not enabled, keep voting
927 BEAST_EXPECT(!ourVotes.empty());
928 BEAST_EXPECT(majority.empty());
929 BEAST_EXPECT(enabled.empty());
930 }
931 }
932 }
933
934 // Exercise the UNL changing while voting is in progress.
935 void
937 {
938 testcase("changedUNL");
939
940 auto const testAmendment = amendmentId("changedUNL");
941 test::jtx::Env env{*this, feat};
942 auto table =
943 makeTable(env, Weeks(8), makeDefaultYes(testAmendment), emptySection_, emptySection_);
944
946
947 std::set<UInt256> enabled;
948 MajorityAmendmentsT majority;
949
950 {
951 // 10 validators with 2 voting against won't get majority.
953 std::vector<UInt256> ourVotes;
954
955 votes.emplace_back(testAmendment, validators.size() - 2);
956
957 doRound(
958 env.current()->rules(),
959 *table,
960 Weeks{1},
961 validators,
962 votes,
963 ourVotes,
964 enabled,
965 majority);
966
967 BEAST_EXPECT(enabled.empty());
968 BEAST_EXPECT(majority.empty());
969 }
970
971 // Add one new validator to the UNL.
973
974 // A lambda that updates the AmendmentTable with the latest
975 // trusted validators.
976 auto const callTrustChanged =
978 std::unique_ptr<AmendmentTable> const& table) {
979 // We need a HashSet to pass to trustChanged.
980 HashSet<PublicKey> trustedValidators;
981 trustedValidators.reserve(validators.size());
982 std::ranges::for_each(validators, [&trustedValidators](auto const& val) {
983 trustedValidators.insert(val.first);
984 });
985
986 // Tell the AmendmentTable that the UNL changed.
987 table->trustChanged(trustedValidators);
988 };
989
990 // Tell the table that there's been a change in trusted validators.
991 callTrustChanged(validators, table);
992
993 {
994 // 11 validators with 2 voting against gains majority.
996 std::vector<UInt256> ourVotes;
997
998 votes.emplace_back(testAmendment, validators.size() - 2);
999
1000 doRound(
1001 env.current()->rules(),
1002 *table,
1003 Weeks{2},
1004 validators,
1005 votes,
1006 ourVotes,
1007 enabled,
1008 majority);
1009
1010 BEAST_EXPECT(enabled.empty());
1011 BEAST_EXPECT(!majority.empty());
1012 }
1013 {
1014 // One of the validators goes flaky and doesn't send validations
1015 // (without the UNL changing) so the amendment loses majority.
1016 std::pair<PublicKey, SecretKey> const savedValidator = validators.front();
1017 validators.erase(validators.begin());
1018
1020 std::vector<UInt256> ourVotes;
1021
1022 votes.emplace_back(testAmendment, validators.size() - 2);
1023
1024 doRound(
1025 env.current()->rules(),
1026 *table,
1027 Weeks{3},
1028 validators,
1029 votes,
1030 ourVotes,
1031 enabled,
1032 majority);
1033
1034 BEAST_EXPECT(enabled.empty());
1035 BEAST_EXPECT(majority.empty());
1036
1037 // Simulate the validator re-syncing to the network by adding it
1038 // back to the validators vector
1039 validators.insert(validators.begin(), savedValidator);
1040
1041 votes.front().second = validators.size() - 2;
1042
1043 doRound(
1044 env.current()->rules(),
1045 *table,
1046 Weeks{4},
1047 validators,
1048 votes,
1049 ourVotes,
1050 enabled,
1051 majority);
1052
1053 BEAST_EXPECT(enabled.empty());
1054 BEAST_EXPECT(!majority.empty());
1055
1056 // Finally, remove one validator from the UNL and see that majority
1057 // is lost.
1058 validators.erase(validators.begin());
1059
1060 // Tell the table that there's been a change in trusted validators.
1061 callTrustChanged(validators, table);
1062
1063 votes.front().second = validators.size() - 2;
1064
1065 doRound(
1066 env.current()->rules(),
1067 *table,
1068 Weeks{5},
1069 validators,
1070 votes,
1071 ourVotes,
1072 enabled,
1073 majority);
1074
1075 BEAST_EXPECT(enabled.empty());
1076 BEAST_EXPECT(majority.empty());
1077 }
1078 }
1079
1080 // Exercise a validator losing connectivity and then regaining it after
1081 // extended delays. Depending on how long that delay is an amendment
1082 // either will or will not go live.
1083 void
1085 {
1086 testcase("validatorFlapping");
1087
1088 // We run a test where a validator flaps on and off every 23 hours
1089 // and another one one where it flaps on and off every 25 hours.
1090 //
1091 // Since the local validator vote record expires after 24 hours,
1092 // with 23 hour flapping the amendment will go live. But with 25
1093 // hour flapping the amendment will not go live.
1094 for (int const flapRateHours : {23, 25})
1095 {
1096 test::jtx::Env env{*this, feat};
1097 auto const testAmendment = amendmentId("validatorFlapping");
1098 auto table = makeTable(
1099 env, Weeks(1), makeDefaultYes(testAmendment), emptySection_, emptySection_);
1100
1101 // Make two lists of validators, one with a missing validator, to
1102 // make it easy to simulate validator flapping.
1103 auto const allValidators = makeValidators(11, table);
1104 decltype(allValidators)
1105 const mostValidators(allValidators.begin() + 1, allValidators.end());
1106 BEAST_EXPECT(allValidators.size() == mostValidators.size() + 1);
1107
1108 std::set<UInt256> enabled;
1109 MajorityAmendmentsT majority;
1110
1112 std::vector<UInt256> ourVotes;
1113
1114 votes.emplace_back(testAmendment, allValidators.size() - 2);
1115
1116 int delay = flapRateHours;
1117 // Loop for 1 week plus a day.
1118 for (int hour = 1; hour < (24 * 8); ++hour)
1119 {
1120 decltype(allValidators) const& thisHoursValidators =
1121 (delay < flapRateHours) ? mostValidators : allValidators;
1122 delay = delay == flapRateHours ? 0 : delay + 1;
1123
1124 votes.front().second = thisHoursValidators.size() - 2;
1125
1126 using namespace std::chrono;
1127 doRound(
1128 env.current()->rules(),
1129 *table,
1130 hours(hour),
1131 thisHoursValidators,
1132 votes,
1133 ourVotes,
1134 enabled,
1135 majority);
1136
1137 if (hour <= (24 * 7) || flapRateHours > 24)
1138 {
1139 // The amendment should not be enabled under any
1140 // circumstance until one week has elapsed.
1141 BEAST_EXPECT(enabled.empty());
1142
1143 // If flapping is less than 24 hours, there should be
1144 // no flapping. Otherwise we should only have majority
1145 // if allValidators vote -- which means there are no
1146 // missing validators.
1147 bool const expectMajority =
1148 (delay <= 24) ? true : &thisHoursValidators == &allValidators;
1149 BEAST_EXPECT(majority.empty() != expectMajority);
1150 }
1151 else
1152 {
1153 // We're...
1154 // o Past one week, and
1155 // o AmendmentFlapping was less than 24 hours.
1156 // The amendment should be enabled.
1157 BEAST_EXPECT(!enabled.empty());
1158 BEAST_EXPECT(majority.empty());
1159 }
1160 }
1161 }
1162 }
1163
1164 void
1166 {
1167 testcase("hasUnsupportedEnabled");
1168
1169 using namespace std::chrono_literals;
1170 constexpr Weeks kW(1);
1171 test::jtx::Env env{*this, makeConfig()};
1172 auto table = makeTable(env, kW);
1173 BEAST_EXPECT(!table->hasUnsupportedEnabled());
1174 BEAST_EXPECT(!table->firstUnsupportedExpected());
1175 BEAST_EXPECT(table->needValidatedLedger(1));
1176
1177 std::set<UInt256> enabled;
1179 unsupported_, [&enabled](auto const& s) { enabled.insert(amendmentId(s)); });
1180
1181 MajorityAmendmentsT majority;
1182 table->doValidatedLedger(1, enabled, majority);
1183 BEAST_EXPECT(table->hasUnsupportedEnabled());
1184 BEAST_EXPECT(!table->firstUnsupportedExpected());
1185
1186 NetClock::duration t{1000s};
1187 std::ranges::for_each(unsupportedMajority_, [&majority, &t](auto const& s) {
1188 majority[amendmentId(s)] = NetClock::time_point{--t};
1189 });
1190
1191 table->doValidatedLedger(1, enabled, majority);
1192 BEAST_EXPECT(table->hasUnsupportedEnabled());
1193 BEAST_EXPECT(
1194 table->firstUnsupportedExpected() &&
1195 *table->firstUnsupportedExpected() == NetClock::time_point{t} + kW);
1196
1197 // Make sure the table knows when it needs an update.
1198 BEAST_EXPECT(!table->needValidatedLedger(256));
1199 BEAST_EXPECT(table->needValidatedLedger(257));
1200 }
1201
1202 void
1204 {
1205 testNoOnUnknown(feat);
1206 testNoOnVetoed(feat);
1207 testVoteEnable(feat);
1208 testDetectMajority(feat);
1209 testLostMajority(feat);
1210 testChangedUNL(feat);
1212 }
1213
1214 void
1215 run() override
1216 {
1218
1219 testConstruct();
1220 testGet();
1221 testBadConfig();
1224 testFeature(all);
1225 }
1226};
1227
1229
1230} // namespace xrpl
T back_inserter(T... args)
T begin(T... args)
T capacity(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
Represents a JSON value.
Definition json_value.h:121
UInt size() const
Number of values in array or object.
static Section makeSection(std::string const &name, std::vector< std::string > const &amendments)
void run() override
Runs the suite.
static NetClock::time_point hourTime(std::chrono::hours h)
void testLostMajority(FeatureBitset const &feat)
static std::vector< AmendmentTable::FeatureInfo > makeFeatureInfo(std::vector< std::string > const &amendments, VoteBehavior voteBehavior)
std::unique_ptr< AmendmentTable > makeTable(test::jtx::Env &env, std::chrono::seconds majorityTime, std::vector< AmendmentTable::FeatureInfo > const &supported, Section const &enabled, Section const &vetoed)
static std::vector< AmendmentTable::FeatureInfo > makeDefaultYes(std::vector< std::string > const &amendments)
void testDetectMajority(FeatureBitset const &feat)
void testValidatorFlapping(FeatureBitset const &feat)
std::vector< AmendmentTable::FeatureInfo > const emptyYes_
void testChangedUNL(FeatureBitset const &feat)
static Section makeSection(std::vector< std::string > const &amendments)
std::vector< std::string > const unsupportedMajority_
void testNoOnVetoed(FeatureBitset const &feat)
std::unique_ptr< AmendmentTable > makeTable(Application &app, std::chrono::seconds majorityTime, std::vector< AmendmentTable::FeatureInfo > const &supported, Section const &enabled, Section const &vetoed)
static void doRound(Rules const &rules, AmendmentTable &table, std::chrono::hours hour, std::vector< std::pair< PublicKey, SecretKey > > const &validators, std::vector< std::pair< UInt256, int > > const &votes, std::vector< UInt256 > &ourVotes, std::set< UInt256 > &enabled, MajorityAmendmentsT &majority)
static void combineArg(std::vector< Arg > &dest, std::vector< Arg > const &src, Args const &... args)
static UInt256 amendmentId(std::string in)
static std::vector< AmendmentTable::FeatureInfo > makeObsolete(std::vector< std::string > const &amendments)
static std::vector< Arg > combine(std::vector< Arg > left, std::vector< Arg > const &right, Args const &... args)
std::vector< std::string > const vetoed_
std::unique_ptr< AmendmentTable > makeTable(test::jtx::Env &env, std::chrono::seconds majorityTime)
std::vector< std::string > const enabled_
static std::vector< AmendmentTable::FeatureInfo > makeDefaultYes(UInt256 const amendment)
static size_t totalsize(std::vector< Arg > const &src, Args const &... args)
static std::vector< std::pair< PublicKey, SecretKey > > makeValidators(int num, std::unique_ptr< AmendmentTable > const &table)
std::vector< std::string > const allSupported_
static Section makeSection(UInt256 const &amendment)
std::vector< std::string > const unsupported_
std::vector< std::string > const obsolete_
void testVoteEnable(FeatureBitset const &feat)
static std::vector< AmendmentTable::FeatureInfo > makeDefaultNo(std::vector< std::string > const &amendments)
std::unique_ptr< Config > makeConfig()
std::vector< std::string > const yes_
void testFeature(FeatureBitset const &feat)
void testNoOnUnknown(FeatureBitset const &feat)
The amendment table stores the list of enabled and potential amendments.
virtual std::vector< UInt256 > doValidation(std::set< UInt256 > const &enabled) const =0
virtual std::map< UInt256, std::uint32_t > doVoting(Rules const &rules, NetClock::time_point closeTime, std::set< UInt256 > const &enabledAmendments, MajorityAmendmentsT const &majorityAmendments, std::vector< std::shared_ptr< STValidation > > const &valSet)=0
pointer data()
Definition base_uint.h:117
static constexpr std::size_t size()
Definition base_uint.h:549
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
std::chrono::duration< rep, period > duration
Definition chrono.h:47
Rules controlling protocol behavior.
Definition Rules.h:40
Holds a collection of configuration values.
Definition BasicConfig.h:28
void append(std::vector< std::string > const &lines)
Append a set of lines to this section.
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
T contains(T... args)
T copy(T... args)
T emplace_back(T... args)
T empty(T... args)
T end(T... args)
T erase(T... args)
T find(T... args)
T for_each(T... args)
T front(T... args)
T insert(T... args)
T make_shared(T... args)
T memcpy(T... args)
void hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
FeatureBitset testableAmendments()
Definition Env.h:92
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:38
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
std::unique_ptr< AmendmentTable > makeAmendmentTable(ServiceRegistry &registry, std::chrono::seconds majorityTime, std::vector< AmendmentTable::FeatureInfo > const &supported, Section const &enabled, Section const &vetoed, beast::Journal journal)
VoteBehavior
Definition Feature.h:112
OpensslSha256Hasher Sha256Hasher
Definition digest.h:102
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
BaseUInt< 256 > UInt256
Definition base_uint.h:581
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
std::map< UInt256, NetClock::time_point > MajorityAmendmentsT
Definition View.h:121
std::unordered_set< Value, Hash, Pred, Allocator > HashSet
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:194
std::chrono::duration< int, std::ratio_multiply< Days::period, std::ratio< 7 > > > Weeks
Definition chrono.h:22
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T push_back(T... args)
T reserve(T... args)
T size(T... args)
std::array< std::uint8_t, 32 > result_type
Definition digest.h:85
static constexpr auto kAmendments
Definition Constants.h:7
static constexpr auto kVetoAmendments
Definition Constants.h:76
T what(T... args)