21 #include <ripple/app/misc/AmendmentTable.h>
22 #include <ripple/basics/BasicConfig.h>
23 #include <ripple/basics/chrono.h>
24 #include <ripple/basics/Log.h>
25 #include <ripple/beast/unit_test.h>
26 #include <ripple/core/ConfigSections.h>
27 #include <ripple/protocol/Feature.h>
28 #include <ripple/protocol/PublicKey.h>
29 #include <ripple/protocol/SecretKey.h>
30 #include <ripple/protocol/digest.h>
31 #include <ripple/protocol/TxFlags.h>
32 #include <test/unit_test/SuiteJournal.h>
61 for (
int i = 0; i < count; i++)
72 for (
auto const& a : amendments)
103 ,
journal (
"AmendmentTable_test", *this)
135 testcase (
"Construction");
139 for (
auto const& a :
m_set1)
141 BEAST_EXPECT(table->isSupported (
amendmentId (a)));
145 for (
auto const& a :
m_set2)
147 BEAST_EXPECT(table->isSupported (
amendmentId (a)));
151 for (
auto const& a :
m_set3)
153 BEAST_EXPECT(!table->isSupported (
amendmentId (a)));
160 testcase (
"Name to ID mapping");
164 for (
auto const& a :
m_set1)
166 for (
auto const& a :
m_set2)
169 for (
auto const& a :
m_set3)
170 BEAST_EXPECT(!table->find (a));
171 for (
auto const& a :
m_set4)
172 BEAST_EXPECT(!table->find (a));
173 for (
auto const& a :
m_set5)
174 BEAST_EXPECT(!table->find (a));
182 testcase (
"Bad Config");
191 fail (
"Accepted only amendment ID");
201 test.
append (
id +
" Test Name");
206 fail (
"Accepted extra arguments");
216 sid.resize (sid.length() - 1);
219 test.
append (sid +
" Name");
224 fail (
"Accepted short amendment ID");
234 sid.resize (sid.length() + 1,
'0');
237 test.
append (sid +
" Name");
242 fail (
"Accepted long amendment ID");
252 sid.resize (sid.length() - 1);
256 test.
append (sid +
" Name");
261 fail (
"Accepted non-hex amendment ID");
279 for (
auto const& a : v)
292 for (
auto const& a : exclude)
300 testcase (
"enable & disable");
302 auto const testAmendment =
amendmentId(
"TestAmendment");
307 enabled.
insert (testAmendment);
315 auto const pre_state =
getState (table.get(), enabled);
318 for (
auto const& a : enabled)
321 for (
auto const& a : enabled)
322 BEAST_EXPECT(table->isEnabled (a));
325 for (
auto const& a : enabled)
328 for (
auto const& a : enabled)
329 BEAST_EXPECT(!table->isEnabled (a));
332 auto const post_state =
getState (table.get(), enabled);
336 pre_state.begin(), pre_state.end(),
337 post_state.begin(), post_state.end());
339 BEAST_EXPECT(ret.first == pre_state.end());
340 BEAST_EXPECT(ret.second == post_state.end());
347 for (
int i = 0; i < num; ++i)
380 auto const roundTime =
weekTime (week);
384 validations.
reserve (validators.size ());
387 for (
auto const& val : validators)
392 for (
auto const& amendment : votes)
394 if ((256 * i) < (validators.size() * amendment.second))
397 field.push_back (amendment.first);
401 auto v = std::make_shared<STValidation>(
419 roundTime, enabled, majority, validations);
420 for (
auto const& action : actions)
424 auto const& hash = action.first;
425 switch (action.second)
429 if (enabled.
find (hash) != enabled.
end ())
430 Throw<std::runtime_error> (
"enabling already enabled");
431 if (majority.
find (hash) == majority.
end ())
432 Throw<std::runtime_error> (
"enabling without majority");
434 majority.
erase (hash);
438 if (majority.
find (hash) != majority.
end ())
439 Throw<std::runtime_error> (
"got majority while having majority");
440 majority[hash] = roundTime;
444 if (majority.
find (hash) == majority.
end ())
445 Throw<std::runtime_error> (
"lost majority without majority");
446 majority.
erase (hash);
450 Throw<std::runtime_error> (
"unknown action");
458 testcase (
"Vote NO on unknown");
460 auto const testAmendment =
amendmentId(
"TestAmendment");
479 BEAST_EXPECT(ourVotes.empty());
480 BEAST_EXPECT(enabled.empty());
481 BEAST_EXPECT(majority.
empty());
483 votes.emplace_back (testAmendment, 256);
491 BEAST_EXPECT(ourVotes.empty());
492 BEAST_EXPECT(enabled.empty());
504 BEAST_EXPECT(ourVotes.empty());
505 BEAST_EXPECT(enabled.empty());
511 testcase (
"Vote NO on vetoed");
513 auto const testAmendment =
amendmentId (
"vetoedAmendment");
533 BEAST_EXPECT(ourVotes.empty());
534 BEAST_EXPECT(enabled.empty());
535 BEAST_EXPECT(majority.
empty());
537 votes.emplace_back (testAmendment, 256);
545 BEAST_EXPECT(ourVotes.empty());
546 BEAST_EXPECT(enabled.empty());
556 BEAST_EXPECT(ourVotes.empty());
557 BEAST_EXPECT(enabled.empty());
563 testcase (
"voteEnable");
584 BEAST_EXPECT(ourVotes.size() ==
m_set1.
size());
585 BEAST_EXPECT(enabled.empty());
586 for (
auto const& i :
m_set1)
590 for (
auto const& i :
m_set1)
600 BEAST_EXPECT(ourVotes.size() ==
m_set1.
size());
601 BEAST_EXPECT(enabled.empty());
603 for (
auto const& i :
m_set1)
613 BEAST_EXPECT(enabled.size() ==
m_set1.
size());
622 BEAST_EXPECT(enabled.size() ==
m_set1.
size());
623 BEAST_EXPECT(ourVotes.empty());
624 for (
auto const& i :
m_set1)
631 testcase (
"detectMajority");
633 auto const testAmendment =
amendmentId (
"detectMajority");
645 for (
int i = 0; i <= 17; ++i)
650 if ((i > 0) && (i < 17))
654 validators, votes, ourVotes, enabled, majority);
659 BEAST_EXPECT(!ourVotes.empty());
660 BEAST_EXPECT(enabled.empty());
661 BEAST_EXPECT(majority.
empty());
666 BEAST_EXPECT(!ourVotes.empty());
667 BEAST_EXPECT(!majority.
empty());
668 BEAST_EXPECT(enabled.empty());
673 BEAST_EXPECT(!ourVotes.empty());
674 BEAST_EXPECT(majority.
empty());
675 BEAST_EXPECT(!enabled.empty());
680 BEAST_EXPECT(ourVotes.empty());
681 BEAST_EXPECT(majority.
empty());
682 BEAST_EXPECT(!enabled.empty());
690 testcase (
"lostMajority");
692 auto const testAmendment =
amendmentId (
"lostMajority");
712 validators, votes, ourVotes, enabled, majority);
714 BEAST_EXPECT(enabled.empty());
715 BEAST_EXPECT(!majority.
empty());
718 for (
int i = 1; i < 16; ++i)
727 validators, votes, ourVotes, enabled, majority);
732 BEAST_EXPECT(!ourVotes.empty());
733 BEAST_EXPECT(enabled.empty());
734 BEAST_EXPECT(!majority.
empty());
739 BEAST_EXPECT(!ourVotes.empty());
740 BEAST_EXPECT(majority.
empty());
741 BEAST_EXPECT(enabled.empty());
748 testcase (
"hasUnsupportedEnabled");
750 using namespace std::chrono_literals;
754 BEAST_EXPECT(! table->hasUnsupportedEnabled());
755 BEAST_EXPECT(! table->firstUnsupportedExpected());
760 [&enabled](
auto const &s){ enabled.insert(amendmentId(s)); });
761 table->doValidatedLedger(1, enabled, majority);
762 BEAST_EXPECT(table->hasUnsupportedEnabled());
763 BEAST_EXPECT(!table->firstUnsupportedExpected());
767 majority[amendmentId(s)] = NetClock::time_point{--t};
769 table->doValidatedLedger(1, enabled, majority);
770 BEAST_EXPECT(table->hasUnsupportedEnabled());
772 table->firstUnsupportedExpected() &&
773 *table->firstUnsupportedExpected() ==