20 #include <ripple/app/misc/AmendmentTable.h>
21 #include <ripple/basics/BasicConfig.h>
22 #include <ripple/basics/Log.h>
23 #include <ripple/basics/chrono.h>
24 #include <ripple/beast/unit_test.h>
25 #include <ripple/core/ConfigSections.h>
26 #include <ripple/protocol/Feature.h>
27 #include <ripple/protocol/PublicKey.h>
28 #include <ripple/protocol/STValidation.h>
29 #include <ripple/protocol/SecretKey.h>
30 #include <ripple/protocol/TxFlags.h>
31 #include <ripple/protocol/digest.h>
32 #include <ripple/protocol/jss.h>
33 #include <test/jtx/Env.h>
34 #include <test/unit_test/SuiteJournal.h>
60 for (
auto const& a : amendments)
78 "a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
79 "l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u"};
81 enabled_{
"b",
"d",
"f",
"h",
"j",
"l",
"n",
"p"};
124 testcase(
"Construction");
149 testcase(
"Name to ID mapping");
161 BEAST_EXPECT(!table->find(a));
163 BEAST_EXPECT(!table->find(a));
170 table->getJson(unsupportedID)[
to_string(unsupportedID)];
171 BEAST_EXPECT(unsupp.
size() == 0);
175 table->veto(unsupportedID);
178 table->getJson(unsupportedID)[
to_string(unsupportedID)];
179 BEAST_EXPECT(unsupp[jss::vetoed].asBool());
189 testcase(
"Bad Config");
198 fail(
"Accepted only amendment ID");
208 test.
append(
id +
" Test Name");
213 fail(
"Accepted extra arguments");
223 sid.resize(sid.length() - 1);
226 test.
append(sid +
" Name");
231 fail(
"Accepted short amendment ID");
241 sid.resize(sid.length() + 1,
'0');
244 test.
append(sid +
" Name");
249 fail(
"Accepted long amendment ID");
259 sid.resize(sid.length() - 1);
263 test.
append(sid +
" Name");
268 fail(
"Accepted non-hex amendment ID");
280 testcase(
"enable and veto");
296 for (
uint256 const& a : lateEnabled)
300 BEAST_EXPECT(!table->hasUnsupportedEnabled());
303 allEnabled.
insert(lateEnabled.begin(), lateEnabled.end());
308 table->isEnabled(supportedID) ==
309 (allEnabled.
find(supportedID) != allEnabled.
end()));
319 for (
uint256 const& a : desired)
320 BEAST_EXPECT(vetoed.
count(a) == 0);
326 BEAST_EXPECT(desired == table->getDesired());
332 table->unVeto(unvetoedID);
345 BEAST_EXPECT(table->getDesired().empty());
349 BEAST_EXPECT(!table->hasUnsupportedEnabled());
351 BEAST_EXPECT(table->hasUnsupportedEnabled());
360 for (
int i = 0; i < num; ++i)
395 auto const roundTime =
weekTime(week);
399 validations.
reserve(validators.size());
402 for (
auto const& val : validators)
407 for (
auto const& amendment : votes)
409 if ((256 * i) < (validators.size() * amendment.second))
412 field.push_back(amendment.first);
416 auto v = std::make_shared<STValidation>(
434 table.
doVoting(roundTime, enabled, majority, validations);
435 for (
auto const& action : actions)
439 auto const& hash = action.first;
440 switch (action.second)
444 if (enabled.
find(hash) != enabled.
end())
445 Throw<std::runtime_error>(
"enabling already enabled");
446 if (majority.
find(hash) == majority.
end())
447 Throw<std::runtime_error>(
"enabling without majority");
449 majority.
erase(hash);
453 if (majority.
find(hash) != majority.
end())
454 Throw<std::runtime_error>(
455 "got majority while having majority");
456 majority[hash] = roundTime;
460 if (majority.
find(hash) == majority.
end())
461 Throw<std::runtime_error>(
462 "lost majority without majority");
463 majority.
erase(hash);
467 Throw<std::runtime_error>(
"unknown action");
476 testcase(
"Vote NO on unknown");
478 auto const testAmendment =
amendmentId(
"TestAmendment");
490 *table,
weeks{1}, validators, votes, ourVotes, enabled, majority);
491 BEAST_EXPECT(ourVotes.empty());
492 BEAST_EXPECT(enabled.empty());
493 BEAST_EXPECT(majority.
empty());
495 votes.emplace_back(testAmendment, 256);
498 *table,
weeks{2}, validators, votes, ourVotes, enabled, majority);
499 BEAST_EXPECT(ourVotes.empty());
500 BEAST_EXPECT(enabled.empty());
507 *table,
weeks{5}, validators, votes, ourVotes, enabled, majority);
508 BEAST_EXPECT(ourVotes.empty());
509 BEAST_EXPECT(enabled.empty());
516 testcase(
"Vote NO on vetoed");
518 auto const testAmendment =
amendmentId(
"vetoedAmendment");
531 *table,
weeks{1}, validators, votes, ourVotes, enabled, majority);
532 BEAST_EXPECT(ourVotes.empty());
533 BEAST_EXPECT(enabled.empty());
534 BEAST_EXPECT(majority.
empty());
536 votes.emplace_back(testAmendment, 256);
539 *table,
weeks{2}, validators, votes, ourVotes, enabled, majority);
540 BEAST_EXPECT(ourVotes.empty());
541 BEAST_EXPECT(enabled.empty());
546 *table,
weeks{5}, validators, votes, ourVotes, enabled, majority);
547 BEAST_EXPECT(ourVotes.empty());
548 BEAST_EXPECT(enabled.empty());
555 testcase(
"voteEnable");
568 *table,
weeks{1}, validators, votes, ourVotes, enabled, majority);
570 BEAST_EXPECT(enabled.empty());
580 *table,
weeks{2}, validators, votes, ourVotes, enabled, majority);
582 BEAST_EXPECT(enabled.empty());
589 *table,
weeks{5}, validators, votes, ourVotes, enabled, majority);
594 *table,
weeks{6}, validators, votes, ourVotes, enabled, majority);
596 BEAST_EXPECT(ourVotes.empty());
605 testcase(
"detectMajority");
607 auto const testAmendment =
amendmentId(
"detectMajority");
616 for (
int i = 0; i <= 17; ++i)
621 if ((i > 0) && (i < 17))
636 BEAST_EXPECT(!ourVotes.empty());
637 BEAST_EXPECT(enabled.empty());
638 BEAST_EXPECT(majority.
empty());
643 BEAST_EXPECT(!ourVotes.empty());
644 BEAST_EXPECT(!majority.
empty());
645 BEAST_EXPECT(enabled.empty());
650 BEAST_EXPECT(!ourVotes.empty());
651 BEAST_EXPECT(majority.
empty());
652 BEAST_EXPECT(!enabled.empty());
657 BEAST_EXPECT(ourVotes.empty());
658 BEAST_EXPECT(majority.
empty());
659 BEAST_EXPECT(!enabled.empty());
668 testcase(
"lostMajority");
670 auto const testAmendment =
amendmentId(
"lostMajority");
695 BEAST_EXPECT(enabled.empty());
696 BEAST_EXPECT(!majority.
empty());
699 for (
int i = 1; i < 16; ++i)
719 BEAST_EXPECT(!ourVotes.empty());
720 BEAST_EXPECT(enabled.empty());
721 BEAST_EXPECT(!majority.
empty());
726 BEAST_EXPECT(!ourVotes.empty());
727 BEAST_EXPECT(majority.
empty());
728 BEAST_EXPECT(enabled.empty());
736 testcase(
"hasUnsupportedEnabled");
741 using namespace std::chrono_literals;
742 weeks constexpr w(1);
744 BEAST_EXPECT(!table->hasUnsupportedEnabled());
745 BEAST_EXPECT(!table->firstUnsupportedExpected());
746 BEAST_EXPECT(table->needValidatedLedger(1));
752 [&enabled](
auto const& s) { enabled.insert(amendmentId(s)); });
755 table->doValidatedLedger(1, enabled, majority);
756 BEAST_EXPECT(table->hasUnsupportedEnabled());
757 BEAST_EXPECT(!table->firstUnsupportedExpected());
763 [&majority, &t](
auto const& s) {
764 majority[amendmentId(s)] = NetClock::time_point{--t};
767 table->doValidatedLedger(1, enabled, majority);
768 BEAST_EXPECT(table->hasUnsupportedEnabled());
770 table->firstUnsupportedExpected() &&
774 BEAST_EXPECT(!table->needValidatedLedger(256));
775 BEAST_EXPECT(table->needValidatedLedger(257));