20#include <test/jtx/Env.h>
21#include <test/unit_test/SuiteJournal.h>
23#include <xrpld/app/misc/AmendmentTable.h>
24#include <xrpld/core/ConfigSections.h>
26#include <xrpl/basics/BasicConfig.h>
27#include <xrpl/basics/Log.h>
28#include <xrpl/basics/chrono.h>
29#include <xrpl/beast/unit_test.h>
30#include <xrpl/protocol/Feature.h>
31#include <xrpl/protocol/PublicKey.h>
32#include <xrpl/protocol/STValidation.h>
33#include <xrpl/protocol/SecretKey.h>
34#include <xrpl/protocol/TxFlags.h>
35#include <xrpl/protocol/digest.h>
36#include <xrpl/protocol/jss.h>
61 for (
auto const& a : amendments)
84 cfg->section(SECTION_AMENDMENTS) =
86 cfg->section(SECTION_VETO_AMENDMENTS) =
97 result.
reserve(amendments.size());
98 for (
auto const& a : amendments)
131 template <
class Arg,
class... Args>
135 if constexpr (
sizeof...(args) > 0)
140 template <
class Arg,
class... Args>
149 if constexpr (
sizeof...(args) > 0)
153 template <
class Arg,
class... Args>
162 left.reserve(
totalsize(left, right, args...));
174 yes_{
"g",
"i",
"k",
"m",
"o",
"q",
"r",
"s",
"t",
"u"};
203 app, majorityTime, supported, enabled, vetoed,
journal_);
214 return makeTable(env.
app(), majorityTime, supported, enabled, vetoed);
248 for (
auto const& a :
yes_)
275 for (
auto const& a :
yes_)
284 BEAST_EXPECT(!table->find(a));
286 BEAST_EXPECT(!table->find(a));
293 table->getJson(unsupportedID,
true)[
to_string(unsupportedID)];
294 BEAST_EXPECT(unsupp.
size() == 0);
298 table->veto(unsupportedID);
301 table->getJson(unsupportedID,
true)[
to_string(unsupportedID)];
302 BEAST_EXPECT(unsupp[jss::vetoed].asBool());
323 fail(
"Accepted only amendment ID");
328 e.
what() ==
"Invalid entry '" +
id +
"' in [Test]");
334 test.
append(
id +
" Test Name");
340 fail(
"Accepted extra arguments");
346 "Invalid entry '" +
id +
" Test Name' in [Test]");
352 sid.resize(sid.length() - 1);
355 test.
append(sid +
" Name");
361 fail(
"Accepted short amendment ID");
366 e.
what() ==
"Invalid entry '" + sid +
" Name' in [Test]");
372 sid.resize(sid.length() + 1,
'0');
375 test.
append(sid +
" Name");
381 fail(
"Accepted long amendment ID");
386 e.
what() ==
"Invalid entry '" + sid +
" Name' in [Test]");
392 sid.resize(sid.length() - 1);
396 test.
append(sid +
" Name");
402 fail(
"Accepted non-hex amendment ID");
407 e.
what() ==
"Invalid entry '" + sid +
" Name' in [Test]");
425 for (
uint256 const& a : allEnabled)
426 BEAST_EXPECT(table->enable(a));
429 BEAST_EXPECT(!table->hasUnsupportedEnabled());
435 bool const enabled = table->isEnabled(supportedID);
436 bool const found = allEnabled.
find(supportedID) != allEnabled.
end();
439 a + (enabled ?
" enabled " :
" disabled ") +
440 (found ?
" found" :
" not found"));
450 for (
uint256 const& a : desired)
451 BEAST_EXPECT(vetoed.
count(a) == 0);
457 BEAST_EXPECT(desired == table->getDesired());
463 BEAST_EXPECT(table->unVeto(unvetoedID));
476 BEAST_EXPECT(table->getDesired().empty());
480 BEAST_EXPECT(!table->hasUnsupportedEnabled());
482 BEAST_EXPECT(table->hasUnsupportedEnabled());
494 trustedValidators.
reserve(num);
495 for (
int i = 0; i < num; ++i)
499 trustedValidators.
insert(back.first);
501 table->trustChanged(trustedValidators);
534 auto const roundTime =
hourTime(hour);
538 validations.
reserve(validators.size());
541 for (
auto const& [pub, sec] : validators)
546 for (
auto const& [hash, nVotes] : votes)
551 field.push_back(hash);
564 v.setFieldU32(sfLedgerSequence, 6180339);
573 table.
doVoting(rules, roundTime, enabled, majority, validations);
574 for (
auto const& [hash, action] : actions)
582 if (enabled.
find(hash) != enabled.
end())
583 Throw<std::runtime_error>(
"enabling already enabled");
584 if (majority.
find(hash) == majority.
end())
585 Throw<std::runtime_error>(
"enabling without majority");
587 majority.
erase(hash);
591 if (majority.
find(hash) != majority.
end())
592 Throw<std::runtime_error>(
593 "got majority while having majority");
594 majority[hash] = roundTime;
598 if (majority.
find(hash) == majority.
end())
599 Throw<std::runtime_error>(
600 "lost majority without majority");
601 majority.
erase(hash);
605 Throw<std::runtime_error>(
"unknown action");
616 auto const testAmendment =
amendmentId(
"TestAmendment");
630 env.current()->rules(),
638 BEAST_EXPECT(ourVotes.
empty());
639 BEAST_EXPECT(enabled.
empty());
640 BEAST_EXPECT(majority.
empty());
645 table->getJson(unsupportedID,
false)[
to_string(unsupportedID)];
646 BEAST_EXPECT(unsupp.
size() == 0);
649 table->veto(unsupportedID);
652 table->getJson(unsupportedID,
false)[
to_string(unsupportedID)];
653 BEAST_EXPECT(!unsupp[jss::vetoed].asBool());
661 env.current()->rules(),
669 BEAST_EXPECT(ourVotes.
empty());
670 BEAST_EXPECT(enabled.
empty());
677 env.current()->rules(),
685 BEAST_EXPECT(ourVotes.
empty());
686 BEAST_EXPECT(enabled.
empty());
695 auto const testAmendment =
amendmentId(
"vetoedAmendment");
713 env.current()->rules(),
721 BEAST_EXPECT(ourVotes.
empty());
722 BEAST_EXPECT(enabled.
empty());
723 BEAST_EXPECT(majority.
empty());
728 env.current()->rules(),
736 BEAST_EXPECT(ourVotes.
empty());
737 BEAST_EXPECT(enabled.
empty());
742 env.current()->rules(),
750 BEAST_EXPECT(ourVotes.
empty());
751 BEAST_EXPECT(enabled.
empty());
773 env.current()->rules(),
782 BEAST_EXPECT(enabled.
empty());
783 for (
auto const& i :
yes_)
787 for (
auto const& i :
yes_)
792 env.current()->rules(),
801 BEAST_EXPECT(enabled.
empty());
803 for (
auto const& i :
yes_)
808 env.current()->rules(),
820 env.current()->rules(),
829 BEAST_EXPECT(ourVotes.
empty());
830 for (
auto const& i :
yes_)
840 auto const testAmendment =
amendmentId(
"detectMajority");
854 for (
int i = 0; i <= 17; ++i)
859 if ((i > 0) && (i < 17))
863 env.current()->rules(),
875 BEAST_EXPECT(!ourVotes.
empty());
876 BEAST_EXPECT(enabled.
empty());
877 BEAST_EXPECT(majority.
empty());
882 BEAST_EXPECT(!ourVotes.
empty());
883 BEAST_EXPECT(!majority.
empty());
884 BEAST_EXPECT(enabled.
empty());
889 BEAST_EXPECT(!ourVotes.
empty());
890 BEAST_EXPECT(majority.
empty());
891 BEAST_EXPECT(!enabled.
empty());
896 BEAST_EXPECT(ourVotes.
empty());
897 BEAST_EXPECT(majority.
empty());
898 BEAST_EXPECT(!enabled.
empty());
909 auto const testAmendment =
amendmentId(
"lostMajority");
932 env.current()->rules(),
941 BEAST_EXPECT(enabled.
empty());
942 BEAST_EXPECT(!majority.
empty());
945 for (
int i = 1; i < 8; ++i)
951 votes.
emplace_back(testAmendment, validators.size() - i);
954 env.current()->rules(),
966 BEAST_EXPECT(!ourVotes.
empty());
967 BEAST_EXPECT(enabled.
empty());
968 BEAST_EXPECT(!majority.
empty());
973 BEAST_EXPECT(!ourVotes.
empty());
974 BEAST_EXPECT(majority.
empty());
975 BEAST_EXPECT(enabled.
empty());
986 auto const testAmendment =
amendmentId(
"changedUNL");
1009 env.current()->rules(),
1018 BEAST_EXPECT(enabled.
empty());
1019 BEAST_EXPECT(majority.
empty());
1027 auto callTrustChanged =
1036 [&trustedValidators](
auto const& val) {
1037 trustedValidators.insert(val.first);
1041 table->trustChanged(trustedValidators);
1045 callTrustChanged(validators, table);
1055 env.current()->rules(),
1064 BEAST_EXPECT(enabled.
empty());
1065 BEAST_EXPECT(!majority.
empty());
1080 env.current()->rules(),
1089 BEAST_EXPECT(enabled.
empty());
1090 BEAST_EXPECT(majority.
empty());
1094 validators.
insert(validators.
begin(), savedValidator);
1096 votes.
front().second = validators.
size() - 2;
1099 env.current()->rules(),
1108 BEAST_EXPECT(enabled.
empty());
1109 BEAST_EXPECT(!majority.
empty());
1116 callTrustChanged(validators, table);
1118 votes.
front().second = validators.
size() - 2;
1121 env.current()->rules(),
1130 BEAST_EXPECT(enabled.
empty());
1131 BEAST_EXPECT(majority.
empty());
1149 for (
int flapRateHours : {23, 25})
1152 auto const testAmendment =
amendmentId(
"validatorFlapping");
1163 decltype(allValidators)
const mostValidators(
1164 allValidators.begin() + 1, allValidators.end());
1165 BEAST_EXPECT(allValidators.size() == mostValidators.size() + 1);
1173 votes.
emplace_back(testAmendment, allValidators.size() - 2);
1175 int delay = flapRateHours;
1177 for (
int hour = 1; hour < (24 * 8); ++hour)
1179 decltype(allValidators)
const& thisHoursValidators =
1180 (delay < flapRateHours) ? mostValidators : allValidators;
1181 delay = delay == flapRateHours ? 0 : delay + 1;
1183 votes.
front().second = thisHoursValidators.size() - 2;
1187 env.current()->rules(),
1190 thisHoursValidators,
1196 if (hour <= (24 * 7) || flapRateHours > 24)
1200 BEAST_EXPECT(enabled.
empty());
1206 bool const expectMajority = (delay <= 24)
1208 : &thisHoursValidators == &allValidators;
1209 BEAST_EXPECT(majority.
empty() != expectMajority);
1217 BEAST_EXPECT(!enabled.
empty());
1218 BEAST_EXPECT(majority.
empty());
1229 using namespace std::chrono_literals;
1230 weeks constexpr w(1);
1233 BEAST_EXPECT(!table->hasUnsupportedEnabled());
1234 BEAST_EXPECT(!table->firstUnsupportedExpected());
1235 BEAST_EXPECT(table->needValidatedLedger(1));
1241 [&enabled](
auto const& s) { enabled.insert(amendmentId(s)); });
1244 table->doValidatedLedger(1, enabled, majority);
1245 BEAST_EXPECT(table->hasUnsupportedEnabled());
1246 BEAST_EXPECT(!table->firstUnsupportedExpected());
1252 [&majority, &t](
auto const& s) {
1253 majority[amendmentId(s)] = NetClock::time_point{--t};
1256 table->doValidatedLedger(1, enabled, majority);
1257 BEAST_EXPECT(table->hasUnsupportedEnabled());
1259 table->firstUnsupportedExpected() &&
1263 BEAST_EXPECT(!table->needValidatedLedger(256));
1264 BEAST_EXPECT(table->needValidatedLedger(257));
1270 testNoOnUnknown(feat);
1271 testNoOnVetoed(feat);
1272 testVoteEnable(feat);
1273 testDetectMajority(feat);
1274 testLostMajority(feat);
1275 testChangedUNL(feat);
1276 testValidatorFlapping(feat);
1288 testHasUnsupported();
T back_inserter(T... args)
UInt size() const
Number of values in array or object.
testcase_t testcase
Memberspace for declaring test cases.
void fail(String const &reason, char const *file, int line)
Record a failure.
void testNoOnVetoed(FeatureBitset const &feat)
static std::vector< AmendmentTable::FeatureInfo > makeObsolete(std::vector< std::string > const &amendments)
static Section makeSection(std::vector< std::string > const &amendments)
static uint256 amendmentId(std::string in)
void testDetectMajority(FeatureBitset const &feat)
std::unique_ptr< AmendmentTable > makeTable(test::jtx::Env &env, std::chrono::seconds majorityTime, std::vector< AmendmentTable::FeatureInfo > const &supported, Section const &enabled, Section const &vetoed)
static std::vector< AmendmentTable::FeatureInfo > makeDefaultYes(std::vector< std::string > const &amendments)
std::vector< std::string > const obsolete_
std::vector< std::string > const unsupportedMajority_
static void combine_arg(std::vector< Arg > &dest, std::vector< Arg > const &src, Args const &... args)
void testChangedUNL(FeatureBitset const &feat)
std::unique_ptr< AmendmentTable > makeTable(test::jtx::Env &env, std::chrono::seconds majorityTime)
std::vector< std::string > const enabled_
test::SuiteJournal journal_
void testValidatorFlapping(FeatureBitset const &feat)
std::unique_ptr< Config > makeConfig()
std::vector< std::string > const vetoed_
void testFeature(FeatureBitset const &feat)
std::vector< std::string > const unsupported_
std::unique_ptr< AmendmentTable > makeTable(Application &app, std::chrono::seconds majorityTime, std::vector< AmendmentTable::FeatureInfo > const &supported, Section const &enabled, Section const &vetoed)
static NetClock::time_point hourTime(std::chrono::hours h)
static Section makeSection(std::string const &name, std::vector< std::string > const &amendments)
static std::vector< Arg > combine(std::vector< Arg > left, std::vector< Arg > const &right, Args const &... args)
void testNoOnUnknown(FeatureBitset const &feat)
Section const emptySection_
void doRound(Rules const &rules, AmendmentTable &table, std::chrono::hours hour, std::vector< std::pair< PublicKey, SecretKey > > const &validators, std::vector< std::pair< uint256, int > > const &votes, std::vector< uint256 > &ourVotes, std::set< uint256 > &enabled, majorityAmendments_t &majority)
static size_t totalsize(std::vector< Arg > const &src, Args const &... args)
static std::vector< AmendmentTable::FeatureInfo > makeFeatureInfo(std::vector< std::string > const &amendments, VoteBehavior voteBehavior)
std::vector< std::string > const allSupported_
static std::vector< AmendmentTable::FeatureInfo > makeDefaultNo(std::vector< std::string > const &amendments)
static std::vector< AmendmentTable::FeatureInfo > makeDefaultYes(uint256 const amendment)
static Section makeSection(uint256 const &amendment)
std::vector< std::string > const yes_
void testLostMajority(FeatureBitset const &feat)
std::vector< std::pair< PublicKey, SecretKey > > makeValidators(int num, std::unique_ptr< AmendmentTable > const &table)
std::vector< AmendmentTable::FeatureInfo > const emptyYes_
void testHasUnsupported()
void run() override
Runs the suite.
void testVoteEnable(FeatureBitset const &feat)
The amendment table stores the list of enabled and potential amendments.
virtual std::vector< uint256 > doValidation(std::set< uint256 > const &enabled) const =0
virtual std::map< uint256, std::uint32_t > doVoting(Rules const &rules, NetClock::time_point closeTime, std::set< uint256 > const &enabledAmendments, majorityAmendments_t const &majorityAmendments, std::vector< std::shared_ptr< STValidation > > const &valSet)=0
Rules controlling protocol behavior.
Holds a collection of configuration values.
void append(std::vector< std::string > const &lines)
Append a set of lines to this section.
static constexpr std::size_t size()
A transaction testing environment.
T emplace_back(T... args)
std::enable_if_t< is_contiguously_hashable< T, Hasher >::value > hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
FeatureBitset testable_amendments()
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
void hash_append(Hasher &h, Slice const &v)
constexpr std::uint32_t tfGotMajority
std::chrono::duration< int, std::ratio_multiply< days::period, std::ratio< 7 > > > weeks
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
std::string to_string(base_uint< Bits, Tag > const &a)
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
std::unique_ptr< AmendmentTable > make_AmendmentTable(Application &app, std::chrono::seconds majorityTime, std::vector< AmendmentTable::FeatureInfo > const &supported, Section const &enabled, Section const &vetoed, beast::Journal journal)
constexpr std::uint32_t tfLostMajority