1#include <test/jtx/Env.h>
2#include <test/jtx/envconfig.h>
3#include <test/unit_test/SuiteJournal.h>
5#include <xrpld/app/main/Application.h>
6#include <xrpld/core/Config.h>
8#include <xrpl/basics/UnorderedContainers.h>
9#include <xrpl/basics/base_uint.h>
10#include <xrpl/basics/chrono.h>
11#include <xrpl/basics/contract.h>
12#include <xrpl/beast/unit_test/suite.h>
13#include <xrpl/config/BasicConfig.h>
14#include <xrpl/config/Constants.h>
15#include <xrpl/json/json_value.h>
16#include <xrpl/ledger/AmendmentTable.h>
17#include <xrpl/ledger/View.h>
18#include <xrpl/protocol/Feature.h>
19#include <xrpl/protocol/KeyType.h>
20#include <xrpl/protocol/PublicKey.h>
21#include <xrpl/protocol/Rules.h>
22#include <xrpl/protocol/SField.h>
23#include <xrpl/protocol/STValidation.h>
24#include <xrpl/protocol/SecretKey.h>
25#include <xrpl/protocol/TxFlags.h>
26#include <xrpl/protocol/digest.h>
27#include <xrpl/protocol/jss.h>
63 for (
auto const& a : amendments)
95 result.
reserve(amendments.size());
96 for (
auto const& a : amendments)
130 template <
class Arg,
class... Args>
134 if constexpr (
sizeof...(args) > 0)
139 template <
class Arg,
class... Args>
145 if constexpr (
sizeof...(args) > 0)
149 template <
class Arg,
class... Args>
158 left.reserve(
totalsize(left, right, args...));
206 return makeTable(env.
app(), majorityTime, supported, enabled, vetoed);
235 for (
auto const& a :
yes_)
262 for (
auto const& a :
yes_)
271 BEAST_EXPECT(!table->find(a));
273 BEAST_EXPECT(!table->find(a));
280 table->getJson(unsupportedID,
true)[
to_string(unsupportedID)];
281 BEAST_EXPECT(unsupp.
size() == 0);
285 table->veto(unsupportedID);
288 table->getJson(unsupportedID,
true)[
to_string(unsupportedID)];
289 BEAST_EXPECT(unsupp[jss::vetoed].asBool());
310 fail(
"Accepted only amendment ID");
314 BEAST_EXPECT(e.
what() ==
"Invalid entry '" +
id +
"' in [Test]");
320 test.append(
id +
" Test Name");
326 fail(
"Accepted extra arguments");
330 BEAST_EXPECT(e.
what() ==
"Invalid entry '" +
id +
" Test Name' in [Test]");
336 sid.resize(sid.length() - 1);
339 test.append(sid +
" Name");
345 fail(
"Accepted short amendment ID");
349 BEAST_EXPECT(e.
what() ==
"Invalid entry '" + sid +
" Name' in [Test]");
355 sid.resize(sid.length() + 1,
'0');
358 test.append(sid +
" Name");
364 fail(
"Accepted long amendment ID");
368 BEAST_EXPECT(e.
what() ==
"Invalid entry '" + sid +
" Name' in [Test]");
374 sid.resize(sid.length() - 1);
378 test.append(sid +
" Name");
384 fail(
"Accepted non-hex amendment ID");
388 BEAST_EXPECT(e.
what() ==
"Invalid entry '" + sid +
" Name' in [Test]");
406 for (
UInt256 const& a : allEnabled)
407 BEAST_EXPECT(table->enable(a));
410 BEAST_EXPECT(!table->hasUnsupportedEnabled());
416 bool const enabled = table->isEnabled(supportedID);
417 bool const found = allEnabled.
contains(supportedID);
420 a + (enabled ?
" enabled " :
" disabled ") + (found ?
" found" :
" not found"));
430 for (
UInt256 const& a : desired)
431 BEAST_EXPECT(not vetoed.
contains(a));
437 BEAST_EXPECT(desired == table->getDesired());
443 BEAST_EXPECT(table->unVeto(unvetoedID));
454 BEAST_EXPECT(table->getDesired().empty());
458 BEAST_EXPECT(!table->hasUnsupportedEnabled());
460 BEAST_EXPECT(table->hasUnsupportedEnabled());
472 trustedValidators.
reserve(num);
473 for (
int i = 0; i < num; ++i)
476 trustedValidators.
insert(back.first);
478 table->trustChanged(trustedValidators);
511 auto const roundTime =
hourTime(hour);
515 validations.
reserve(validators.size());
518 for (
auto const& [pub, sec] : validators)
523 for (
auto const& [hash, nVotes] : votes)
535 v.setFieldV256(sfAmendments,
STVector256(sfAmendments, field));
536 v.setFieldU32(sfLedgerSequence, 6180339);
544 auto const actions = table.
doVoting(rules, roundTime, enabled, majority, validations);
545 for (
auto const& [hash, action] : actions)
558 majority.
erase(hash);
564 majority[hash] = roundTime;
570 majority.
erase(hash);
585 auto const testAmendment =
amendmentId(
"TestAmendment");
606 BEAST_EXPECT(ourVotes.
empty());
607 BEAST_EXPECT(enabled.
empty());
608 BEAST_EXPECT(majority.
empty());
613 table->getJson(unsupportedID,
false)[
to_string(unsupportedID)];
614 BEAST_EXPECT(unsupp.
size() == 0);
617 table->veto(unsupportedID);
620 table->getJson(unsupportedID,
false)[
to_string(unsupportedID)];
621 BEAST_EXPECT(!unsupp[jss::vetoed].asBool());
637 BEAST_EXPECT(ourVotes.
empty());
638 BEAST_EXPECT(enabled.
empty());
653 BEAST_EXPECT(ourVotes.
empty());
654 BEAST_EXPECT(enabled.
empty());
663 auto const testAmendment =
amendmentId(
"vetoedAmendment");
684 BEAST_EXPECT(ourVotes.
empty());
685 BEAST_EXPECT(enabled.
empty());
686 BEAST_EXPECT(majority.
empty());
699 BEAST_EXPECT(ourVotes.
empty());
700 BEAST_EXPECT(enabled.
empty());
713 BEAST_EXPECT(ourVotes.
empty());
714 BEAST_EXPECT(enabled.
empty());
743 BEAST_EXPECT(ourVotes.
size() ==
yes_.size());
744 BEAST_EXPECT(enabled.
empty());
745 for (
auto const& i :
yes_)
749 for (
auto const& i :
yes_)
762 BEAST_EXPECT(ourVotes.
size() ==
yes_.size());
763 BEAST_EXPECT(enabled.
empty());
765 for (
auto const& i :
yes_)
778 BEAST_EXPECT(enabled.
size() ==
yes_.size());
790 BEAST_EXPECT(enabled.
size() ==
yes_.size());
791 BEAST_EXPECT(ourVotes.
empty());
792 for (
auto const& i :
yes_)
802 auto const testAmendment =
amendmentId(
"detectMajority");
812 for (
int i = 0; i <= 17; ++i)
817 if ((i > 0) && (i < 17))
833 BEAST_EXPECT(!ourVotes.
empty());
834 BEAST_EXPECT(enabled.
empty());
835 BEAST_EXPECT(majority.
empty());
840 BEAST_EXPECT(!ourVotes.
empty());
841 BEAST_EXPECT(!majority.
empty());
842 BEAST_EXPECT(enabled.
empty());
847 BEAST_EXPECT(!ourVotes.
empty());
848 BEAST_EXPECT(majority.
empty());
849 BEAST_EXPECT(!enabled.
empty());
854 BEAST_EXPECT(ourVotes.
empty());
855 BEAST_EXPECT(majority.
empty());
856 BEAST_EXPECT(!enabled.
empty());
867 auto const testAmendment =
amendmentId(
"lostMajority");
895 BEAST_EXPECT(enabled.
empty());
896 BEAST_EXPECT(!majority.
empty());
899 for (
int i = 1; i < 8; ++i)
905 votes.
emplace_back(testAmendment, validators.size() - i);
920 BEAST_EXPECT(!ourVotes.
empty());
921 BEAST_EXPECT(enabled.
empty());
922 BEAST_EXPECT(!majority.
empty());
927 BEAST_EXPECT(!ourVotes.
empty());
928 BEAST_EXPECT(majority.
empty());
929 BEAST_EXPECT(enabled.
empty());
940 auto const testAmendment =
amendmentId(
"changedUNL");
967 BEAST_EXPECT(enabled.
empty());
968 BEAST_EXPECT(majority.
empty());
976 auto const callTrustChanged =
983 trustedValidators.
insert(val.first);
987 table->trustChanged(trustedValidators);
991 callTrustChanged(validators, table);
1010 BEAST_EXPECT(enabled.
empty());
1011 BEAST_EXPECT(!majority.
empty());
1034 BEAST_EXPECT(enabled.
empty());
1035 BEAST_EXPECT(majority.
empty());
1039 validators.
insert(validators.
begin(), savedValidator);
1041 votes.
front().second = validators.
size() - 2;
1053 BEAST_EXPECT(enabled.
empty());
1054 BEAST_EXPECT(!majority.
empty());
1061 callTrustChanged(validators, table);
1063 votes.
front().second = validators.
size() - 2;
1075 BEAST_EXPECT(enabled.
empty());
1076 BEAST_EXPECT(majority.
empty());
1094 for (
int const flapRateHours : {23, 25})
1097 auto const testAmendment =
amendmentId(
"validatorFlapping");
1104 decltype(allValidators)
1105 const mostValidators(allValidators.begin() + 1, allValidators.end());
1106 BEAST_EXPECT(allValidators.size() == mostValidators.size() + 1);
1114 votes.
emplace_back(testAmendment, allValidators.size() - 2);
1116 int delay = flapRateHours;
1118 for (
int hour = 1; hour < (24 * 8); ++hour)
1120 decltype(allValidators)
const& thisHoursValidators =
1121 (delay < flapRateHours) ? mostValidators : allValidators;
1122 delay = delay == flapRateHours ? 0 : delay + 1;
1124 votes.
front().second = thisHoursValidators.size() - 2;
1131 thisHoursValidators,
1137 if (hour <= (24 * 7) || flapRateHours > 24)
1141 BEAST_EXPECT(enabled.
empty());
1147 bool const expectMajority =
1148 (delay <= 24) ?
true : &thisHoursValidators == &allValidators;
1149 BEAST_EXPECT(majority.
empty() != expectMajority);
1157 BEAST_EXPECT(!enabled.
empty());
1158 BEAST_EXPECT(majority.
empty());
1169 using namespace std::chrono_literals;
1170 constexpr Weeks kW(1);
1173 BEAST_EXPECT(!table->hasUnsupportedEnabled());
1174 BEAST_EXPECT(!table->firstUnsupportedExpected());
1175 BEAST_EXPECT(table->needValidatedLedger(1));
1182 table->doValidatedLedger(1, enabled, majority);
1183 BEAST_EXPECT(table->hasUnsupportedEnabled());
1184 BEAST_EXPECT(!table->firstUnsupportedExpected());
1191 table->doValidatedLedger(1, enabled, majority);
1192 BEAST_EXPECT(table->hasUnsupportedEnabled());
1194 table->firstUnsupportedExpected() &&
1198 BEAST_EXPECT(!table->needValidatedLedger(256));
1199 BEAST_EXPECT(table->needValidatedLedger(257));
T back_inserter(T... args)
void fail(String const &reason, char const *file, int line)
Record a failure.
TestcaseT testcase
Memberspace for declaring test cases.
UInt size() const
Number of values in array or object.
static Section makeSection(std::string const &name, std::vector< std::string > const &amendments)
void run() override
Runs the suite.
static NetClock::time_point hourTime(std::chrono::hours h)
void testLostMajority(FeatureBitset const &feat)
test::SuiteJournal journal_
static std::vector< AmendmentTable::FeatureInfo > makeFeatureInfo(std::vector< std::string > const &amendments, VoteBehavior voteBehavior)
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)
void testDetectMajority(FeatureBitset const &feat)
void testHasUnsupported()
void testValidatorFlapping(FeatureBitset const &feat)
std::vector< AmendmentTable::FeatureInfo > const emptyYes_
void testChangedUNL(FeatureBitset const &feat)
static Section makeSection(std::vector< std::string > const &amendments)
std::vector< std::string > const unsupportedMajority_
void testNoOnVetoed(FeatureBitset const &feat)
std::unique_ptr< AmendmentTable > makeTable(Application &app, std::chrono::seconds majorityTime, std::vector< AmendmentTable::FeatureInfo > const &supported, Section const &enabled, Section const &vetoed)
static 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, MajorityAmendmentsT &majority)
static void combineArg(std::vector< Arg > &dest, std::vector< Arg > const &src, Args const &... args)
static UInt256 amendmentId(std::string in)
static std::vector< AmendmentTable::FeatureInfo > makeObsolete(std::vector< std::string > const &amendments)
static std::vector< Arg > combine(std::vector< Arg > left, std::vector< Arg > const &right, Args const &... args)
std::vector< std::string > const vetoed_
std::unique_ptr< AmendmentTable > makeTable(test::jtx::Env &env, std::chrono::seconds majorityTime)
std::vector< std::string > const enabled_
static std::vector< AmendmentTable::FeatureInfo > makeDefaultYes(UInt256 const amendment)
static size_t totalsize(std::vector< Arg > const &src, Args const &... args)
static std::vector< std::pair< PublicKey, SecretKey > > makeValidators(int num, std::unique_ptr< AmendmentTable > const &table)
std::vector< std::string > const allSupported_
static Section makeSection(UInt256 const &amendment)
std::vector< std::string > const unsupported_
std::vector< std::string > const obsolete_
void testVoteEnable(FeatureBitset const &feat)
Section const emptySection_
static std::vector< AmendmentTable::FeatureInfo > makeDefaultNo(std::vector< std::string > const &amendments)
std::unique_ptr< Config > makeConfig()
std::vector< std::string > const yes_
void testFeature(FeatureBitset const &feat)
void testNoOnUnknown(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, MajorityAmendmentsT const &majorityAmendments, std::vector< std::shared_ptr< STValidation > > const &valSet)=0
static constexpr std::size_t size()
std::chrono::time_point< NetClock > time_point
std::chrono::duration< rep, period > duration
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.
A transaction testing environment.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
T emplace_back(T... args)
void hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
FeatureBitset testableAmendments()
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
std::unique_ptr< AmendmentTable > makeAmendmentTable(ServiceRegistry ®istry, std::chrono::seconds majorityTime, std::vector< AmendmentTable::FeatureInfo > const &supported, Section const &enabled, Section const &vetoed, beast::Journal journal)
OpensslSha256Hasher Sha256Hasher
std::string to_string(BaseUInt< Bits, Tag > const &a)
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
std::map< UInt256, NetClock::time_point > MajorityAmendmentsT
std::unordered_set< Value, Hash, Pred, Allocator > HashSet
void hash_append(Hasher &h, Slice const &v)
std::chrono::duration< int, std::ratio_multiply< Days::period, std::ratio< 7 > > > Weeks
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
std::array< std::uint8_t, 32 > result_type
static constexpr auto kAmendments
static constexpr auto kVetoAmendments