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/SecretKey.h>
29 #include <ripple/protocol/TxFlags.h>
30 #include <ripple/protocol/digest.h>
31 #include <test/unit_test/SuiteJournal.h>
57 for (
int i = 0; i < count; i++)
67 for (
auto const& a : amendments)
97 ,
journal(
"AmendmentTable_test", *this)
122 testcase(
"Construction");
126 for (
auto const& a :
m_set1)
132 for (
auto const& a :
m_set2)
138 for (
auto const& a :
m_set3)
148 testcase(
"Name to ID mapping");
152 for (
auto const& a :
m_set1)
154 for (
auto const& a :
m_set2)
157 for (
auto const& a :
m_set3)
158 BEAST_EXPECT(!table->find(a));
159 for (
auto const& a :
m_set4)
160 BEAST_EXPECT(!table->find(a));
161 for (
auto const& a :
m_set5)
162 BEAST_EXPECT(!table->find(a));
171 testcase(
"Bad Config");
180 fail(
"Accepted only amendment ID");
190 test.
append(
id +
" Test Name");
195 fail(
"Accepted extra arguments");
205 sid.resize(sid.length() - 1);
208 test.
append(sid +
" Name");
213 fail(
"Accepted short amendment ID");
223 sid.resize(sid.length() + 1,
'0');
226 test.
append(sid +
" Name");
231 fail(
"Accepted long amendment ID");
241 sid.resize(sid.length() - 1);
245 test.
append(sid +
" Name");
250 fail(
"Accepted non-hex amendment ID");
265 for (
auto const& a : v)
278 for (
auto const& a : exclude)
287 testcase(
"enable & disable");
289 auto const testAmendment =
amendmentId(
"TestAmendment");
294 enabled.
insert(testAmendment);
302 auto const pre_state =
getState(table.get(), enabled);
305 for (
auto const& a : enabled)
308 for (
auto const& a : enabled)
309 BEAST_EXPECT(table->isEnabled(a));
312 for (
auto const& a : enabled)
315 for (
auto const& a : enabled)
316 BEAST_EXPECT(!table->isEnabled(a));
319 auto const post_state =
getState(table.get(), enabled);
328 BEAST_EXPECT(ret.first == pre_state.end());
329 BEAST_EXPECT(ret.second == post_state.end());
337 for (
int i = 0; i < num; ++i)
372 auto const roundTime =
weekTime(week);
376 validations.
reserve(validators.size());
379 for (
auto const& val : validators)
384 for (
auto const& amendment : votes)
386 if ((256 * i) < (validators.size() * amendment.second))
389 field.push_back(amendment.first);
393 auto v = std::make_shared<STValidation>(
411 table.
doVoting(roundTime, enabled, majority, validations);
412 for (
auto const& action : actions)
416 auto const& hash = action.first;
417 switch (action.second)
421 if (enabled.
find(hash) != enabled.
end())
422 Throw<std::runtime_error>(
"enabling already enabled");
423 if (majority.
find(hash) == majority.
end())
424 Throw<std::runtime_error>(
"enabling without majority");
426 majority.
erase(hash);
430 if (majority.
find(hash) != majority.
end())
431 Throw<std::runtime_error>(
432 "got majority while having majority");
433 majority[hash] = roundTime;
437 if (majority.
find(hash) == majority.
end())
438 Throw<std::runtime_error>(
439 "lost majority without majority");
440 majority.
erase(hash);
444 Throw<std::runtime_error>(
"unknown action");
453 testcase(
"Vote NO on unknown");
455 auto const testAmendment =
amendmentId(
"TestAmendment");
466 *table,
weeks{1}, validators, votes, ourVotes, enabled, majority);
467 BEAST_EXPECT(ourVotes.empty());
468 BEAST_EXPECT(enabled.empty());
469 BEAST_EXPECT(majority.
empty());
471 votes.emplace_back(testAmendment, 256);
474 *table,
weeks{2}, validators, votes, ourVotes, enabled, majority);
475 BEAST_EXPECT(ourVotes.empty());
476 BEAST_EXPECT(enabled.empty());
483 *table,
weeks{5}, validators, votes, ourVotes, enabled, majority);
484 BEAST_EXPECT(ourVotes.empty());
485 BEAST_EXPECT(enabled.empty());
492 testcase(
"Vote NO on vetoed");
494 auto const testAmendment =
amendmentId(
"vetoedAmendment");
507 *table,
weeks{1}, validators, votes, ourVotes, enabled, majority);
508 BEAST_EXPECT(ourVotes.empty());
509 BEAST_EXPECT(enabled.empty());
510 BEAST_EXPECT(majority.
empty());
512 votes.emplace_back(testAmendment, 256);
515 *table,
weeks{2}, validators, votes, ourVotes, enabled, majority);
516 BEAST_EXPECT(ourVotes.empty());
517 BEAST_EXPECT(enabled.empty());
522 *table,
weeks{5}, validators, votes, ourVotes, enabled, majority);
523 BEAST_EXPECT(ourVotes.empty());
524 BEAST_EXPECT(enabled.empty());
531 testcase(
"voteEnable");
544 *table,
weeks{1}, validators, votes, ourVotes, enabled, majority);
545 BEAST_EXPECT(ourVotes.size() ==
m_set1.
size());
546 BEAST_EXPECT(enabled.empty());
547 for (
auto const& i :
m_set1)
551 for (
auto const& i :
m_set1)
556 *table,
weeks{2}, validators, votes, ourVotes, enabled, majority);
557 BEAST_EXPECT(ourVotes.size() ==
m_set1.
size());
558 BEAST_EXPECT(enabled.empty());
560 for (
auto const& i :
m_set1)
565 *table,
weeks{5}, validators, votes, ourVotes, enabled, majority);
566 BEAST_EXPECT(enabled.size() ==
m_set1.
size());
570 *table,
weeks{6}, validators, votes, ourVotes, enabled, majority);
571 BEAST_EXPECT(enabled.size() ==
m_set1.
size());
572 BEAST_EXPECT(ourVotes.empty());
573 for (
auto const& i :
m_set1)
581 testcase(
"detectMajority");
583 auto const testAmendment =
amendmentId(
"detectMajority");
592 for (
int i = 0; i <= 17; ++i)
597 if ((i > 0) && (i < 17))
612 BEAST_EXPECT(!ourVotes.empty());
613 BEAST_EXPECT(enabled.empty());
614 BEAST_EXPECT(majority.
empty());
619 BEAST_EXPECT(!ourVotes.empty());
620 BEAST_EXPECT(!majority.
empty());
621 BEAST_EXPECT(enabled.empty());
626 BEAST_EXPECT(!ourVotes.empty());
627 BEAST_EXPECT(majority.
empty());
628 BEAST_EXPECT(!enabled.empty());
633 BEAST_EXPECT(ourVotes.empty());
634 BEAST_EXPECT(majority.
empty());
635 BEAST_EXPECT(!enabled.empty());
644 testcase(
"lostMajority");
646 auto const testAmendment =
amendmentId(
"lostMajority");
671 BEAST_EXPECT(enabled.empty());
672 BEAST_EXPECT(!majority.
empty());
675 for (
int i = 1; i < 16; ++i)
695 BEAST_EXPECT(!ourVotes.empty());
696 BEAST_EXPECT(enabled.empty());
697 BEAST_EXPECT(!majority.
empty());
702 BEAST_EXPECT(!ourVotes.empty());
703 BEAST_EXPECT(majority.
empty());
704 BEAST_EXPECT(enabled.empty());
712 testcase(
"hasUnsupportedEnabled");
714 using namespace std::chrono_literals;
718 BEAST_EXPECT(!table->hasUnsupportedEnabled());
719 BEAST_EXPECT(!table->firstUnsupportedExpected());
724 enabled.insert(amendmentId(s));
726 table->doValidatedLedger(1, enabled, majority);
727 BEAST_EXPECT(table->hasUnsupportedEnabled());
728 BEAST_EXPECT(!table->firstUnsupportedExpected());
732 majority[amendmentId(s)] = NetClock::time_point{--t};
734 table->doValidatedLedger(1, enabled, majority);
735 BEAST_EXPECT(table->hasUnsupportedEnabled());
737 table->firstUnsupportedExpected() &&
738 *table->firstUnsupportedExpected() ==