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>
59 for (
auto const& a : amendments)
82 cfg->section(SECTION_AMENDMENTS) =
84 cfg->section(SECTION_VETO_AMENDMENTS) =
95 result.
reserve(amendments.size());
96 for (
auto const& a : amendments)
129 template <
class Arg,
class... Args>
133 if constexpr (
sizeof...(args) > 0)
138 template <
class Arg,
class... Args>
147 if constexpr (
sizeof...(args) > 0)
151 template <
class Arg,
class... Args>
160 left.reserve(
totalsize(left, right, args...));
172 yes_{
"g",
"i",
"k",
"m",
"o",
"q",
"r",
"s",
"t",
"u"};
201 app, majorityTime, supported, enabled, vetoed,
journal_);
212 return makeTable(env.
app(), majorityTime, supported, enabled, vetoed);
239 testcase(
"Construction");
246 for (
auto const& a :
yes_)
268 testcase(
"Name to ID mapping");
273 for (
auto const& a :
yes_)
282 BEAST_EXPECT(!table->find(a));
284 BEAST_EXPECT(!table->find(a));
291 table->getJson(unsupportedID,
true)[
to_string(unsupportedID)];
292 BEAST_EXPECT(unsupp.
size() == 0);
296 table->veto(unsupportedID);
299 table->getJson(unsupportedID,
true)[
to_string(unsupportedID)];
300 BEAST_EXPECT(unsupp[jss::vetoed].asBool());
311 testcase(
"Bad Config");
321 fail(
"Accepted only amendment ID");
326 e.
what() ==
"Invalid entry '" +
id +
"' in [Test]");
332 test.
append(
id +
" Test Name");
338 fail(
"Accepted extra arguments");
344 "Invalid entry '" +
id +
" Test Name' in [Test]");
350 sid.resize(sid.length() - 1);
353 test.
append(sid +
" Name");
359 fail(
"Accepted short amendment ID");
364 e.
what() ==
"Invalid entry '" + sid +
" Name' in [Test]");
370 sid.resize(sid.length() + 1,
'0');
373 test.
append(sid +
" Name");
379 fail(
"Accepted long amendment ID");
384 e.
what() ==
"Invalid entry '" + sid +
" Name' in [Test]");
390 sid.resize(sid.length() - 1);
394 test.
append(sid +
" Name");
400 fail(
"Accepted non-hex amendment ID");
405 e.
what() ==
"Invalid entry '" + sid +
" Name' in [Test]");
413 testcase(
"enable and veto");
423 for (
uint256 const& a : allEnabled)
424 BEAST_EXPECT(table->enable(a));
427 BEAST_EXPECT(!table->hasUnsupportedEnabled());
433 bool const enabled = table->isEnabled(supportedID);
434 bool const found = allEnabled.find(supportedID) != allEnabled.end();
437 a + (enabled ?
" enabled " :
" disabled ") +
438 (found ?
" found" :
" not found"));
448 for (
uint256 const& a : desired)
449 BEAST_EXPECT(vetoed.
count(a) == 0);
455 BEAST_EXPECT(desired == table->getDesired());
461 BEAST_EXPECT(table->unVeto(unvetoedID));
474 BEAST_EXPECT(table->getDesired().empty());
478 BEAST_EXPECT(!table->hasUnsupportedEnabled());
480 BEAST_EXPECT(table->hasUnsupportedEnabled());
492 trustedValidators.
reserve(num);
493 for (
int i = 0; i < num; ++i)
497 trustedValidators.
insert(back.first);
499 table->trustChanged(trustedValidators);
532 auto const roundTime =
hourTime(hour);
536 validations.
reserve(validators.size());
539 for (
auto const& [pub, sec] : validators)
544 for (
auto const& [hash, nVotes] : votes)
550 field.push_back(hash);
554 auto v = std::make_shared<STValidation>(
572 table.
doVoting(rules, roundTime, enabled, majority, validations);
573 for (
auto const& [hash, action] : actions)
581 if (enabled.
find(hash) != enabled.
end())
582 Throw<std::runtime_error>(
"enabling already enabled");
583 if (majority.
find(hash) == majority.
end())
584 Throw<std::runtime_error>(
"enabling without majority");
586 majority.
erase(hash);
590 if (majority.
find(hash) != majority.
end())
591 Throw<std::runtime_error>(
592 "got majority while having majority");
593 majority[hash] = roundTime;
597 if (majority.
find(hash) == majority.
end())
598 Throw<std::runtime_error>(
599 "lost majority without majority");
600 majority.
erase(hash);
604 Throw<std::runtime_error>(
"unknown action");
613 testcase(
"Vote NO on unknown");
615 auto const testAmendment =
amendmentId(
"TestAmendment");
629 env.current()->rules(),
637 BEAST_EXPECT(ourVotes.
empty());
638 BEAST_EXPECT(enabled.
empty());
639 BEAST_EXPECT(majority.
empty());
644 table->getJson(unsupportedID,
false)[
to_string(unsupportedID)];
645 BEAST_EXPECT(unsupp.
size() == 0);
648 table->veto(unsupportedID);
651 table->getJson(unsupportedID,
false)[
to_string(unsupportedID)];
652 BEAST_EXPECT(!unsupp[jss::vetoed].asBool());
660 env.current()->rules(),
668 BEAST_EXPECT(ourVotes.
empty());
669 BEAST_EXPECT(enabled.
empty());
676 env.current()->rules(),
684 BEAST_EXPECT(ourVotes.
empty());
685 BEAST_EXPECT(enabled.
empty());
692 testcase(
"Vote NO on vetoed");
694 auto const testAmendment =
amendmentId(
"vetoedAmendment");
712 env.current()->rules(),
720 BEAST_EXPECT(ourVotes.
empty());
721 BEAST_EXPECT(enabled.
empty());
722 BEAST_EXPECT(majority.
empty());
727 env.current()->rules(),
735 BEAST_EXPECT(ourVotes.
empty());
736 BEAST_EXPECT(enabled.
empty());
741 env.current()->rules(),
749 BEAST_EXPECT(ourVotes.
empty());
750 BEAST_EXPECT(enabled.
empty());
757 testcase(
"voteEnable");
772 env.current()->rules(),
781 BEAST_EXPECT(enabled.
empty());
782 for (
auto const& i :
yes_)
786 for (
auto const& i :
yes_)
791 env.current()->rules(),
800 BEAST_EXPECT(enabled.
empty());
802 for (
auto const& i :
yes_)
807 env.current()->rules(),
819 env.current()->rules(),
828 BEAST_EXPECT(ourVotes.
empty());
829 for (
auto const& i :
yes_)
837 testcase(
"detectMajority");
839 auto const testAmendment =
amendmentId(
"detectMajority");
853 for (
int i = 0; i <= 17; ++i)
858 if ((i > 0) && (i < 17))
862 env.current()->rules(),
874 BEAST_EXPECT(!ourVotes.
empty());
875 BEAST_EXPECT(enabled.
empty());
876 BEAST_EXPECT(majority.
empty());
881 BEAST_EXPECT(!ourVotes.
empty());
882 BEAST_EXPECT(!majority.
empty());
883 BEAST_EXPECT(enabled.
empty());
888 BEAST_EXPECT(!ourVotes.
empty());
889 BEAST_EXPECT(majority.
empty());
890 BEAST_EXPECT(!enabled.
empty());
895 BEAST_EXPECT(ourVotes.
empty());
896 BEAST_EXPECT(majority.
empty());
897 BEAST_EXPECT(!enabled.
empty());
906 testcase(
"lostMajority");
908 auto const testAmendment =
amendmentId(
"lostMajority");
931 env.current()->rules(),
940 BEAST_EXPECT(enabled.
empty());
941 BEAST_EXPECT(!majority.
empty());
944 for (
int i = 1; i < 8; ++i)
950 votes.
emplace_back(testAmendment, validators.size() - i);
953 env.current()->rules(),
965 BEAST_EXPECT(!ourVotes.
empty());
966 BEAST_EXPECT(enabled.
empty());
967 BEAST_EXPECT(!majority.
empty());
972 BEAST_EXPECT(!ourVotes.
empty());
973 BEAST_EXPECT(majority.
empty());
974 BEAST_EXPECT(enabled.
empty());
987 testcase(
"changedUNL");
989 auto const testAmendment =
amendmentId(
"changedUNL");
1012 env.current()->rules(),
1021 BEAST_EXPECT(enabled.
empty());
1022 BEAST_EXPECT(majority.
empty());
1030 auto callTrustChanged =
1039 [&trustedValidators](
auto const& val) {
1040 trustedValidators.insert(val.first);
1044 table->trustChanged(trustedValidators);
1048 callTrustChanged(validators, table);
1058 env.current()->rules(),
1067 BEAST_EXPECT(enabled.
empty());
1068 BEAST_EXPECT(!majority.
empty());
1083 env.current()->rules(),
1092 BEAST_EXPECT(enabled.
empty());
1093 BEAST_EXPECT(majority.
empty());
1097 validators.
insert(validators.
begin(), savedValidator);
1099 votes.
front().second = validators.
size() - 2;
1102 env.current()->rules(),
1111 BEAST_EXPECT(enabled.
empty());
1112 BEAST_EXPECT(!majority.
empty());
1119 callTrustChanged(validators, table);
1121 votes.
front().second = validators.
size() - 2;
1124 env.current()->rules(),
1133 BEAST_EXPECT(enabled.
empty());
1134 BEAST_EXPECT(majority.
empty());
1148 testcase(
"validatorFlapping");
1156 for (
int flapRateHours : {23, 25})
1159 auto const testAmendment =
amendmentId(
"validatorFlapping");
1170 decltype(allValidators)
const mostValidators(
1171 allValidators.begin() + 1, allValidators.end());
1172 BEAST_EXPECT(allValidators.size() == mostValidators.size() + 1);
1180 votes.
emplace_back(testAmendment, allValidators.size() - 2);
1182 int delay = flapRateHours;
1184 for (
int hour = 1; hour < (24 * 8); ++hour)
1186 decltype(allValidators)
const& thisHoursValidators =
1187 (delay < flapRateHours) ? mostValidators : allValidators;
1188 delay = delay == flapRateHours ? 0 : delay + 1;
1190 votes.
front().second = thisHoursValidators.size() - 2;
1194 env.current()->rules(),
1197 thisHoursValidators,
1203 if (hour <= (24 * 7) || flapRateHours > 24)
1207 BEAST_EXPECT(enabled.
empty());
1213 bool const expectMajority = (delay <= 24)
1215 : &thisHoursValidators == &allValidators;
1216 BEAST_EXPECT(majority.
empty() != expectMajority);
1224 BEAST_EXPECT(!enabled.
empty());
1225 BEAST_EXPECT(majority.
empty());
1234 testcase(
"hasUnsupportedEnabled");
1236 using namespace std::chrono_literals;
1237 weeks constexpr w(1);
1240 BEAST_EXPECT(!table->hasUnsupportedEnabled());
1241 BEAST_EXPECT(!table->firstUnsupportedExpected());
1242 BEAST_EXPECT(table->needValidatedLedger(1));
1248 [&enabled](
auto const& s) { enabled.insert(amendmentId(s)); });
1251 table->doValidatedLedger(1, enabled, majority);
1252 BEAST_EXPECT(table->hasUnsupportedEnabled());
1253 BEAST_EXPECT(!table->firstUnsupportedExpected());
1259 [&majority, &t](
auto const& s) {
1260 majority[amendmentId(s)] = NetClock::time_point{--t};
1263 table->doValidatedLedger(1, enabled, majority);
1264 BEAST_EXPECT(table->hasUnsupportedEnabled());
1266 table->firstUnsupportedExpected() &&
1270 BEAST_EXPECT(!table->needValidatedLedger(256));
1271 BEAST_EXPECT(table->needValidatedLedger(257));