20#include <test/jtx/Env.h>
21#include <test/unit_test/SuiteJournal.h>
22#include <xrpld/app/misc/AmendmentTable.h>
23#include <xrpld/core/ConfigSections.h>
24#include <xrpl/basics/BasicConfig.h>
25#include <xrpl/basics/Log.h>
26#include <xrpl/basics/chrono.h>
27#include <xrpl/beast/unit_test.h>
28#include <xrpl/protocol/Feature.h>
29#include <xrpl/protocol/PublicKey.h>
30#include <xrpl/protocol/STValidation.h>
31#include <xrpl/protocol/SecretKey.h>
32#include <xrpl/protocol/TxFlags.h>
33#include <xrpl/protocol/digest.h>
34#include <xrpl/protocol/jss.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);
246 for (
auto const& a :
yes_)
273 for (
auto const& a :
yes_)
282 BEAST_EXPECT(!table->find(a));
284 BEAST_EXPECT(!table->find(a));
291 table->getJson(unsupportedID,
true)[
to_string(unsupportedID)];
292 BEAST_EXPECT(unsupp.
size() == 0);
296 table->veto(unsupportedID);
299 table->getJson(unsupportedID,
true)[
to_string(unsupportedID)];
300 BEAST_EXPECT(unsupp[jss::vetoed].asBool());
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]");
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());
492 trustedValidators.
reserve(num);
493 for (
int i = 0; i < num; ++i)
497 trustedValidators.
insert(back.first);
499 table->trustChanged(trustedValidators);
532 auto const roundTime =
hourTime(hour);
536 validations.
reserve(validators.size());
539 for (
auto const& [pub, sec] : validators)
544 for (
auto const& [hash, nVotes] : votes)
546 if (rules.
enabled(fixAmendmentMajorityCalc) ? nVotes >= i
550 field.push_back(hash);
554 auto v = std::make_shared<STValidation>(
563 v.setFieldU32(sfLedgerSequence, 6180339);
572 table.
doVoting(rules, roundTime, enabled, majority, validations);
573 for (
auto const& [hash, action] : actions)
581 if (enabled.
find(hash) != enabled.
end())
582 Throw<std::runtime_error>(
"enabling already enabled");
583 if (majority.
find(hash) == majority.
end())
584 Throw<std::runtime_error>(
"enabling without majority");
586 majority.
erase(hash);
590 if (majority.
find(hash) != majority.
end())
591 Throw<std::runtime_error>(
592 "got majority while having majority");
593 majority[hash] = roundTime;
597 if (majority.
find(hash) == majority.
end())
598 Throw<std::runtime_error>(
599 "lost majority without majority");
600 majority.
erase(hash);
604 Throw<std::runtime_error>(
"unknown action");
615 auto const testAmendment =
amendmentId(
"TestAmendment");
629 env.current()->rules(),
637 BEAST_EXPECT(ourVotes.
empty());
638 BEAST_EXPECT(enabled.
empty());
639 BEAST_EXPECT(majority.
empty());
644 table->getJson(unsupportedID,
false)[
to_string(unsupportedID)];
645 BEAST_EXPECT(unsupp.
size() == 0);
648 table->veto(unsupportedID);
651 table->getJson(unsupportedID,
false)[
to_string(unsupportedID)];
652 BEAST_EXPECT(!unsupp[jss::vetoed].asBool());
660 env.current()->rules(),
668 BEAST_EXPECT(ourVotes.
empty());
669 BEAST_EXPECT(enabled.
empty());
676 env.current()->rules(),
684 BEAST_EXPECT(ourVotes.
empty());
685 BEAST_EXPECT(enabled.
empty());
694 auto const testAmendment =
amendmentId(
"vetoedAmendment");
712 env.current()->rules(),
720 BEAST_EXPECT(ourVotes.
empty());
721 BEAST_EXPECT(enabled.
empty());
722 BEAST_EXPECT(majority.
empty());
727 env.current()->rules(),
735 BEAST_EXPECT(ourVotes.
empty());
736 BEAST_EXPECT(enabled.
empty());
741 env.current()->rules(),
749 BEAST_EXPECT(ourVotes.
empty());
750 BEAST_EXPECT(enabled.
empty());
772 env.current()->rules(),
781 BEAST_EXPECT(enabled.
empty());
782 for (
auto const& i :
yes_)
786 for (
auto const& i :
yes_)
791 env.current()->rules(),
800 BEAST_EXPECT(enabled.
empty());
802 for (
auto const& i :
yes_)
807 env.current()->rules(),
819 env.current()->rules(),
828 BEAST_EXPECT(ourVotes.
empty());
829 for (
auto const& i :
yes_)
839 auto const testAmendment =
amendmentId(
"detectMajority");
853 for (
int i = 0; i <= 17; ++i)
858 if ((i > 0) && (i < 17))
862 env.current()->rules(),
874 BEAST_EXPECT(!ourVotes.
empty());
875 BEAST_EXPECT(enabled.
empty());
876 BEAST_EXPECT(majority.
empty());
881 BEAST_EXPECT(!ourVotes.
empty());
882 BEAST_EXPECT(!majority.
empty());
883 BEAST_EXPECT(enabled.
empty());
888 BEAST_EXPECT(!ourVotes.
empty());
889 BEAST_EXPECT(majority.
empty());
890 BEAST_EXPECT(!enabled.
empty());
895 BEAST_EXPECT(ourVotes.
empty());
896 BEAST_EXPECT(majority.
empty());
897 BEAST_EXPECT(!enabled.
empty());
908 auto const testAmendment =
amendmentId(
"lostMajority");
931 env.current()->rules(),
940 BEAST_EXPECT(enabled.
empty());
941 BEAST_EXPECT(!majority.
empty());
944 for (
int i = 1; i < 8; ++i)
950 votes.
emplace_back(testAmendment, validators.size() - i);
953 env.current()->rules(),
965 BEAST_EXPECT(!ourVotes.
empty());
966 BEAST_EXPECT(enabled.
empty());
967 BEAST_EXPECT(!majority.
empty());
972 BEAST_EXPECT(!ourVotes.
empty());
973 BEAST_EXPECT(majority.
empty());
974 BEAST_EXPECT(enabled.
empty());
984 if (!feat[fixAmendmentMajorityCalc])
989 auto const testAmendment =
amendmentId(
"changedUNL");
1012 env.current()->rules(),
1021 BEAST_EXPECT(enabled.
empty());
1022 BEAST_EXPECT(majority.
empty());
1030 auto callTrustChanged =
1039 [&trustedValidators](
auto const& val) {
1040 trustedValidators.insert(val.first);
1044 table->trustChanged(trustedValidators);
1048 callTrustChanged(validators, table);
1058 env.current()->rules(),
1067 BEAST_EXPECT(enabled.
empty());
1068 BEAST_EXPECT(!majority.
empty());
1083 env.current()->rules(),
1092 BEAST_EXPECT(enabled.
empty());
1093 BEAST_EXPECT(majority.
empty());
1097 validators.
insert(validators.
begin(), savedValidator);
1099 votes.
front().second = validators.
size() - 2;
1102 env.current()->rules(),
1111 BEAST_EXPECT(enabled.
empty());
1112 BEAST_EXPECT(!majority.
empty());
1119 callTrustChanged(validators, table);
1121 votes.
front().second = validators.
size() - 2;
1124 env.current()->rules(),
1133 BEAST_EXPECT(enabled.
empty());
1134 BEAST_EXPECT(majority.
empty());
1145 if (!feat[fixAmendmentMajorityCalc])
1156 for (
int flapRateHours : {23, 25})
1159 auto const testAmendment =
amendmentId(
"validatorFlapping");
1170 decltype(allValidators)
const mostValidators(
1171 allValidators.begin() + 1, allValidators.end());
1172 BEAST_EXPECT(allValidators.size() == mostValidators.size() + 1);
1180 votes.
emplace_back(testAmendment, allValidators.size() - 2);
1182 int delay = flapRateHours;
1184 for (
int hour = 1; hour < (24 * 8); ++hour)
1186 decltype(allValidators)
const& thisHoursValidators =
1187 (delay < flapRateHours) ? mostValidators : allValidators;
1188 delay = delay == flapRateHours ? 0 : delay + 1;
1190 votes.
front().second = thisHoursValidators.size() - 2;
1194 env.current()->rules(),
1197 thisHoursValidators,
1203 if (hour <= (24 * 7) || flapRateHours > 24)
1207 BEAST_EXPECT(enabled.
empty());
1213 bool const expectMajority = (delay <= 24)
1215 : &thisHoursValidators == &allValidators;
1216 BEAST_EXPECT(majority.
empty() != expectMajority);
1224 BEAST_EXPECT(!enabled.
empty());
1225 BEAST_EXPECT(majority.
empty());
1236 using namespace std::chrono_literals;
1237 weeks constexpr w(1);
1240 BEAST_EXPECT(!table->hasUnsupportedEnabled());
1241 BEAST_EXPECT(!table->firstUnsupportedExpected());
1242 BEAST_EXPECT(table->needValidatedLedger(1));
1248 [&enabled](
auto const& s) { enabled.insert(amendmentId(s)); });
1251 table->doValidatedLedger(1, enabled, majority);
1252 BEAST_EXPECT(table->hasUnsupportedEnabled());
1253 BEAST_EXPECT(!table->firstUnsupportedExpected());
1259 [&majority, &t](
auto const& s) {
1260 majority[amendmentId(s)] = NetClock::time_point{--t};
1263 table->doValidatedLedger(1, enabled, majority);
1264 BEAST_EXPECT(table->hasUnsupportedEnabled());
1266 table->firstUnsupportedExpected() &&
1270 BEAST_EXPECT(!table->needValidatedLedger(256));
1271 BEAST_EXPECT(table->needValidatedLedger(257));
1277 testNoOnUnknown(feat);
1278 testNoOnVetoed(feat);
1279 testVoteEnable(feat);
1280 testDetectMajority(feat);
1281 testLostMajority(feat);
1282 testChangedUNL(feat);
1283 testValidatorFlapping(feat);
1290 FeatureBitset const fixMajorityCalc{fixAmendmentMajorityCalc};
1296 testHasUnsupported();
1297 testFeature(
all - fixMajorityCalc);
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.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
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 supported_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