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 <test/unit_test/SuiteJournal.h>
58 for (
int i = 0; i < count; i++)
68 for (
auto const& a : amendments)
98 ,
journal(
"AmendmentTable_test", *this)
123 testcase(
"Construction");
127 for (
auto const& a :
m_set1)
133 for (
auto const& a :
m_set2)
139 for (
auto const& a :
m_set3)
149 testcase(
"Name to ID mapping");
153 for (
auto const& a :
m_set1)
155 for (
auto const& a :
m_set2)
158 for (
auto const& a :
m_set3)
159 BEAST_EXPECT(!table->find(a));
160 for (
auto const& a :
m_set4)
161 BEAST_EXPECT(!table->find(a));
162 for (
auto const& a :
m_set5)
163 BEAST_EXPECT(!table->find(a));
172 testcase(
"Bad Config");
181 fail(
"Accepted only amendment ID");
191 test.
append(
id +
" Test Name");
196 fail(
"Accepted extra arguments");
206 sid.resize(sid.length() - 1);
209 test.
append(sid +
" Name");
214 fail(
"Accepted short amendment ID");
224 sid.resize(sid.length() + 1,
'0');
227 test.
append(sid +
" Name");
232 fail(
"Accepted long amendment ID");
242 sid.resize(sid.length() - 1);
246 test.
append(sid +
" Name");
251 fail(
"Accepted non-hex amendment ID");
266 for (
auto const& a : v)
279 for (
auto const& a : exclude)
288 testcase(
"enable & disable");
290 auto const testAmendment =
amendmentId(
"TestAmendment");
295 enabled.
insert(testAmendment);
303 auto const pre_state =
getState(table.get(), enabled);
306 for (
auto const& a : enabled)
309 for (
auto const& a : enabled)
310 BEAST_EXPECT(table->isEnabled(a));
313 for (
auto const& a : enabled)
316 for (
auto const& a : enabled)
317 BEAST_EXPECT(!table->isEnabled(a));
320 auto const post_state =
getState(table.get(), enabled);
329 BEAST_EXPECT(ret.first == pre_state.end());
330 BEAST_EXPECT(ret.second == post_state.end());
338 for (
int i = 0; i < num; ++i)
373 auto const roundTime =
weekTime(week);
377 validations.
reserve(validators.size());
380 for (
auto const& val : validators)
385 for (
auto const& amendment : votes)
387 if ((256 * i) < (validators.size() * amendment.second))
390 field.push_back(amendment.first);
394 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() ==