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)[
to_string(unsupportedID)];
292 BEAST_EXPECT(unsupp.
size() == 0);
296 table->veto(unsupportedID);
299 table->getJson(unsupportedID)[
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());
489 for (
int i = 0; i < num; ++i)
525 auto const roundTime =
weekTime(week);
529 validations.
reserve(validators.size());
532 for (
auto const& [pub, sec] : validators)
537 for (
auto const& [hash, nVotes] : votes)
542 field.push_back(hash);
546 auto v = std::make_shared<STValidation>(
564 Rules({feat}), roundTime, enabled, majority, validations);
565 for (
auto const& [hash, action] : actions)
573 if (enabled.
find(hash) != enabled.
end())
574 Throw<std::runtime_error>(
"enabling already enabled");
575 if (majority.
find(hash) == majority.
end())
576 Throw<std::runtime_error>(
"enabling without majority");
578 majority.
erase(hash);
582 if (majority.
find(hash) != majority.
end())
583 Throw<std::runtime_error>(
584 "got majority while having majority");
585 majority[hash] = roundTime;
589 if (majority.
find(hash) == majority.
end())
590 Throw<std::runtime_error>(
591 "lost majority without majority");
592 majority.
erase(hash);
596 Throw<std::runtime_error>(
"unknown action");
605 testcase(
"Vote NO on unknown");
607 auto const testAmendment =
amendmentId(
"TestAmendment");
628 BEAST_EXPECT(ourVotes.empty());
629 BEAST_EXPECT(enabled.empty());
630 BEAST_EXPECT(majority.
empty());
632 votes.emplace_back(testAmendment, validators.size());
643 BEAST_EXPECT(ourVotes.empty());
644 BEAST_EXPECT(enabled.empty());
659 BEAST_EXPECT(ourVotes.empty());
660 BEAST_EXPECT(enabled.empty());
667 testcase(
"Vote NO on vetoed");
669 auto const testAmendment =
amendmentId(
"vetoedAmendment");
695 BEAST_EXPECT(ourVotes.empty());
696 BEAST_EXPECT(enabled.empty());
697 BEAST_EXPECT(majority.
empty());
699 votes.emplace_back(testAmendment, validators.size());
710 BEAST_EXPECT(ourVotes.empty());
711 BEAST_EXPECT(enabled.empty());
724 BEAST_EXPECT(ourVotes.empty());
725 BEAST_EXPECT(enabled.empty());
732 testcase(
"voteEnable");
754 BEAST_EXPECT(ourVotes.size() ==
yes_.
size());
755 BEAST_EXPECT(enabled.empty());
756 for (
auto const& i :
yes_)
760 for (
auto const& i :
yes_)
773 BEAST_EXPECT(ourVotes.size() ==
yes_.
size());
774 BEAST_EXPECT(enabled.empty());
776 for (
auto const& i :
yes_)
789 BEAST_EXPECT(enabled.size() ==
yes_.
size());
801 BEAST_EXPECT(enabled.size() ==
yes_.
size());
802 BEAST_EXPECT(ourVotes.empty());
803 for (
auto const& i :
yes_)
811 testcase(
"detectMajority");
813 auto const testAmendment =
amendmentId(
"detectMajority");
827 for (
int i = 0; i <= 17; ++i)
832 if ((i > 0) && (i < 17))
848 BEAST_EXPECT(!ourVotes.empty());
849 BEAST_EXPECT(enabled.empty());
850 BEAST_EXPECT(majority.
empty());
855 BEAST_EXPECT(!ourVotes.empty());
856 BEAST_EXPECT(!majority.
empty());
857 BEAST_EXPECT(enabled.empty());
862 BEAST_EXPECT(!ourVotes.empty());
863 BEAST_EXPECT(majority.
empty());
864 BEAST_EXPECT(!enabled.empty());
869 BEAST_EXPECT(ourVotes.empty());
870 BEAST_EXPECT(majority.
empty());
871 BEAST_EXPECT(!enabled.empty());
880 testcase(
"lostMajority");
882 auto const testAmendment =
amendmentId(
"lostMajority");
913 BEAST_EXPECT(enabled.empty());
914 BEAST_EXPECT(!majority.
empty());
917 for (
int i = 1; i < 8; ++i)
923 votes.
emplace_back(testAmendment, validators.size() - i);
938 BEAST_EXPECT(!ourVotes.empty());
939 BEAST_EXPECT(enabled.empty());
940 BEAST_EXPECT(!majority.
empty());
945 BEAST_EXPECT(!ourVotes.empty());
946 BEAST_EXPECT(majority.
empty());
947 BEAST_EXPECT(enabled.empty());
955 testcase(
"hasUnsupportedEnabled");
957 using namespace std::chrono_literals;
958 weeks constexpr w(1);
961 BEAST_EXPECT(!table->hasUnsupportedEnabled());
962 BEAST_EXPECT(!table->firstUnsupportedExpected());
963 BEAST_EXPECT(table->needValidatedLedger(1));
969 [&enabled](
auto const& s) { enabled.insert(amendmentId(s)); });
972 table->doValidatedLedger(1, enabled, majority);
973 BEAST_EXPECT(table->hasUnsupportedEnabled());
974 BEAST_EXPECT(!table->firstUnsupportedExpected());
980 [&majority, &t](
auto const& s) {
981 majority[amendmentId(s)] = NetClock::time_point{--t};
984 table->doValidatedLedger(1, enabled, majority);
985 BEAST_EXPECT(table->hasUnsupportedEnabled());
987 table->firstUnsupportedExpected() &&
991 BEAST_EXPECT(!table->needValidatedLedger(256));
992 BEAST_EXPECT(table->needValidatedLedger(257));