20#include <test/csf/Validation.h>
21#include <test/unit_test/SuiteJournal.h>
22#include <xrpld/consensus/Validations.h>
23#include <xrpl/basics/tagged_integer.h>
24#include <xrpl/beast/clock/manual_clock.h>
25#include <xrpl/beast/unit_test.h>
46 c.now().time_since_epoch() + 86400s));
141 ledger.
id(), ledger.
seq(), signOffset, seenOffset,
true);
261 using namespace std::chrono_literals;
266 Ledger ledgerAB = h[
"ab"];
267 Ledger ledgerAZ = h[
"az"];
268 Ledger ledgerABC = h[
"abc"];
269 Ledger ledgerABCD = h[
"abcd"];
270 Ledger ledgerABCDE = h[
"abcde"];
285 harness.
clock().advance(1s);
301 harness.
clock().advance(1s);
311 harness.
clock().advance(1s);
321 harness.
clock().advance(2s);
322 auto const valABCDE = n.
validate(ledgerABCDE);
324 harness.
clock().advance(4s);
325 auto const valABCD = n.
validate(ledgerABCD);
378 for (
bool doFull : {
true,
false})
383 auto process = [&](
Ledger& lgr) {
390 harness.
clock().advance(1s);
391 BEAST_EXPECT(ledgerAB.
seq() < ledgerABC.
seq());
397 harness.
clock().advance(
413 Ledger ledgerAB = h[
"ab"];
422 vals.getNodesAfter(ledgerA, ledgerA.
id());
424 for (Trigger trigger : triggers)
431 trigger(harness.
vals());
440 trigger(harness.
vals());
456 using namespace std::chrono_literals;
461 Ledger ledgerAB = h[
"ab"];
462 Ledger ledgerABC = h[
"abc"];
463 Ledger ledgerAD = h[
"ad"];
476 for (
Ledger const& ledger : {ledgerA, ledgerAB, ledgerABC, ledgerAD})
480 harness.
clock().advance(5s);
505 using namespace std::chrono_literals;
506 testcase(
"Current trusted validations");
511 Ledger ledgerAC = h[
"ac"];
526 harness.
clock().advance(3s);
528 for (
auto const& node : {a, b})
547 using namespace std::chrono_literals;
552 Ledger ledgerAC = h[
"ac"];
558 for (
auto const& node : {a, b})
567 harness.
clock().advance(3s);
573 for (
auto const& node : {a, b})
591 using namespace std::chrono_literals;
619 auto sorted = [](
auto vec) {
623 auto compare = [&]() {
624 for (
auto& it : trustedValidations)
626 auto const&
id = it.first.first;
627 auto const&
seq = it.first.second;
628 auto const& expectedValidations = it.second;
632 expectedValidations.size());
635 sorted(expectedValidations));
639 for (
auto const& val : expectedValidations)
641 expectedFees.
push_back(val.loadFee().value_or(baseFee));
645 sorted(harness.
vals().
fees(
id, baseFee)) ==
646 sorted(expectedFees));
653 Ledger ledgerAC = h[
"ac"];
659 for (
auto const& node : {a, b, c})
661 auto const val = node.validate(ledgerA);
664 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(
669 auto const val = d.validate(ledgerB);
671 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(val);
678 harness.
clock().advance(5s);
680 for (
auto const& node : {a, b, c})
682 auto const val = node.validate(ledgerAC);
685 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(
716 Ledger const ledgerA = h[
"a"];
726 Ledger const ledgerB = h[
"ab"];
739 for (
int i = 0; i < loops; ++i)
747 Ledger const ledgerC = h[
"abc"];
760 using namespace std::chrono_literals;
770 Ledger ledgerAB = h[
"ab"];
773 for (
auto const& node : {a, b, c})
775 auto const val = node.validate(ledgerA);
777 expected.
emplace(node.nodeID(), val);
782 harness.
clock().advance(1s);
791 using namespace std::chrono_literals;
802 Ledger ledgerAC = h[
"ac"];
803 Ledger ledgerACD = h[
"acd"];
808 auto pref = [](
Ledger ledger) {
828 BEAST_EXPECT(ledgerB.
id() > ledgerA.
id());
837 harness.
clock().advance(5s);
840 for (
auto const& node : {a, b, c, d})
851 harness.
clock().advance(5s);
852 for (
auto const& node : {a, b, c, d})
855 for (
auto const& ledger : {ledgerA, ledgerB, ledgerACD})
863 using namespace std::chrono_literals;
884 ++peerCounts[ledgerB.
id()];
891 ++peerCounts[ledgerC.
id()];
893 BEAST_EXPECT(ledgerC.
id() > ledgerB.
id());
899 peerCounts[ledgerC.
id()] += 1000;
923 using namespace std::chrono_literals;
924 testcase(
"Acquire validated ledger");
952 harness.
add(b.
validate(ID{3}, Seq{2}, 0s, 0s,
true)));
958 Ledger ledgerAB = h[
"ab"];
963 harness.
clock().advance(5s);
980 harness.
clock().advance(5s);
981 Ledger ledgerABCDE = h[
"abcde"];
1018 BEAST_EXPECT(enforcer(clock.now(), Seq{1}, p));
1019 BEAST_EXPECT(enforcer(clock.now(), Seq{10}, p));
1020 BEAST_EXPECT(!enforcer(clock.now(), Seq{5}, p));
1021 BEAST_EXPECT(!enforcer(clock.now(), Seq{9}, p));
1023 BEAST_EXPECT(!enforcer(clock.now(), Seq{1}, p));
1025 BEAST_EXPECT(enforcer(clock.now(), Seq{1}, p));
1034 auto checker = [
this](
1039 : trustedVals[0].ledgerID();
1042 : trustedVals[0].seq();
1047 trustedVals.size());
1048 if (trustedVals.empty())
1050 vals.
getPreferred(this->genesisLedger) == std::nullopt);
1053 vals.
getPreferred(this->genesisLedger)->second == testID);
1065 Ledger ledgerAB = h[
"ab"];
1071 checker(harness.
vals(), listed, trustedVals);
1073 trustedVals.clear();
1075 checker(harness.
vals(), listed, trustedVals);
1084 Ledger ledgerAB = h[
"ab"];
1090 checker(harness.
vals(), listed, trustedVals);
1092 trustedVals.push_back(v);
1094 checker(harness.
vals(), listed, trustedVals);
1108 auto& vals = harness.
vals();
1109 BEAST_EXPECT(vals.currentTrusted() == trustedVals);
1115 trustedVals.clear();
1119 BEAST_EXPECT(vals.currentTrusted() == trustedVals);
1120 BEAST_EXPECT(vals.getPreferred(
genesisLedger) == std::nullopt);
Abstract interface to a clock.
Manual clock implementation.
testcase_t testcase
Memberspace for declaring test cases.
std::chrono::time_point< NetClock > time_point
Enforce validation increasing sequence requirement.
Maintains current and recent ledger validations.
std::optional< std::pair< Seq, ID > > getPreferred(Ledger const &curr)
Return the sequence number and ID of the preferred working ledger.
std::size_t numTrustedForLedger(ID const &ledgerID)
Count the number of trusted full validations for the given ledger.
auto getCurrentNodeIDs() -> hash_set< NodeID >
Get the set of node ids associated with current validations.
std::size_t getNodesAfter(Ledger const &ledger, ID const &ledgerID)
Count the number of current trusted validators working on a ledger after the specified one.
std::vector< WrappedValidationType > getTrustedForLedger(ID const &ledgerID, Seq const &seq)
Get trusted full validations for a specific ledger.
ID getPreferredLCL(Ledger const &lcl, Seq minSeq, hash_map< ID, std::uint32_t > const &peerCounts)
Determine the preferred last closed ledger for the next consensus round.
void trustChanged(hash_set< NodeID > const &added, hash_set< NodeID > const &removed)
Update trust status of validations.
std::vector< WrappedValidationType > currentTrusted()
Get the currently trusted full validations.
void setSeqToKeep(Seq const &low, Seq const &high)
Set the range [low, high) of validations to keep from expire.
ValStatus add(NodeID const &nodeID, Validation const &val)
Add a new validation.
void expire(beast::Journal &j)
Expire old validation sets.
std::vector< std::uint32_t > fees(ID const &ledgerID, std::uint32_t baseFee)
Returns fees reported by trusted full validators in the given ledger.
Oracle maintaining unique ledgers for a simulation.
std::optional< Ledger > lookup(Ledger::ID const &id) const
Find the ledger with the given ID.
A ledger is a set of observed transactions and a sequence number identifying the ledger.
tagged_integer< std::uint32_t, SeqTag > Seq
tagged_integer< std::uint32_t, IdTag > ID
Validation of a specific ledger by a specific Peer.
Ledger::ID ledgerID() const
PeerID const & nodeID() const
NetClock::time_point now() const
std::optional< Ledger > acquire(Ledger::ID const &id)
Adaptor(clock_type &c, LedgerOracle &o)
Validation partial(Ledger ledger) const
std::optional< std::uint32_t > loadFee_
void setLoadFee(std::uint32_t fee)
Validation validate(Ledger ledger) const
Node(PeerID nodeID, clock_type const &c)
Validation validate(Ledger ledger, NetClock::duration signOffset, NetClock::duration seenOffset) const
Validation validate(Ledger::ID id, Ledger::Seq seq, NetClock::duration signOffset, NetClock::duration seenOffset, bool full) const
PeerKey masterKey() const
NetClock::time_point now() const
ValStatus add(Validation const &v)
TestHarness(LedgerOracle &o)
beast::manual_clock< std::chrono::steady_clock > clock_
ValidationParms parms() const
void testAcquireValidatedLedger()
void testGetPreferredLedger()
void testGetCurrentPublicKeys()
void testCurrentTrusted()
Ledger const genesisLedger
beast::abstract_clock< std::chrono::steady_clock > const clock_type
void testNumTrustedForLedger()
void run() override
Runs the suite.
void testTrustedByLedgerFunctions()
void testGetPreferredLCL()
static NetClock::time_point toNetClock(clock_type const &c)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
ValStatus
Status of validation we received.
@ badSeq
A validation violates the increasing seq requirement.
@ stale
Not current or was older than current from this node.
@ current
This was a new validation and was added.
@ conflicting
Multiple validations by a validator for different ledgers.
Timing parameters to control validation staleness and expiration.
std::chrono::seconds validationCURRENT_LOCAL
Duration a validation remains current after first observed.
std::chrono::seconds validationFRESHNESS
How long we consider a validation fresh.
std::chrono::seconds validationSET_EXPIRES
Duration a set of validations for a given ledger hash remain valid.
std::chrono::seconds validationCURRENT_WALL
The number of seconds a validation remains current after its ledger's close time.
std::chrono::seconds validationCURRENT_EARLY
Duration pre-close in which validations are acceptable.
Helper for writing unit tests with controlled ledger histories.
Set the sequence number on a JTx.