20#include <test/csf/Validation.h>
21#include <test/unit_test/SuiteJournal.h>
23#include <xrpld/consensus/Validations.h>
25#include <xrpl/basics/tagged_integer.h>
26#include <xrpl/beast/clock/manual_clock.h>
27#include <xrpl/beast/unit_test.h>
48 c.now().time_since_epoch() + 86400s));
143 ledger.
id(), ledger.
seq(), signOffset, seenOffset,
true);
263 using namespace std::chrono_literals;
268 Ledger ledgerAB = h[
"ab"];
269 Ledger ledgerAZ = h[
"az"];
270 Ledger ledgerABC = h[
"abc"];
271 Ledger ledgerABCD = h[
"abcd"];
272 Ledger ledgerABCDE = h[
"abcde"];
287 harness.
clock().advance(1s);
303 harness.
clock().advance(1s);
313 harness.
clock().advance(1s);
323 harness.
clock().advance(2s);
324 auto const valABCDE = n.
validate(ledgerABCDE);
326 harness.
clock().advance(4s);
327 auto const valABCD = n.
validate(ledgerABCD);
380 for (
bool doFull : {
true,
false})
385 auto process = [&](
Ledger& lgr) {
392 harness.
clock().advance(1s);
393 BEAST_EXPECT(ledgerAB.
seq() < ledgerABC.
seq());
399 harness.
clock().advance(
415 Ledger ledgerAB = h[
"ab"];
424 vals.getNodesAfter(ledgerA, ledgerA.
id());
426 for (Trigger trigger : triggers)
433 trigger(harness.
vals());
442 trigger(harness.
vals());
458 using namespace std::chrono_literals;
463 Ledger ledgerAB = h[
"ab"];
464 Ledger ledgerABC = h[
"abc"];
465 Ledger ledgerAD = h[
"ad"];
478 for (
Ledger const& ledger : {ledgerA, ledgerAB, ledgerABC, ledgerAD})
482 harness.
clock().advance(5s);
507 using namespace std::chrono_literals;
508 testcase(
"Current trusted validations");
513 Ledger ledgerAC = h[
"ac"];
528 harness.
clock().advance(3s);
530 for (
auto const& node : {a, b})
549 using namespace std::chrono_literals;
554 Ledger ledgerAC = h[
"ac"];
560 for (
auto const& node : {a, b})
569 harness.
clock().advance(3s);
575 for (
auto const& node : {a, b})
593 using namespace std::chrono_literals;
621 auto sorted = [](
auto vec) {
625 auto compare = [&]() {
626 for (
auto& it : trustedValidations)
628 auto const&
id = it.first.first;
629 auto const&
seq = it.first.second;
630 auto const& expectedValidations = it.second;
634 expectedValidations.size());
637 sorted(expectedValidations));
641 for (
auto const& val : expectedValidations)
643 expectedFees.
push_back(val.loadFee().value_or(baseFee));
647 sorted(harness.
vals().
fees(
id, baseFee)) ==
648 sorted(expectedFees));
655 Ledger ledgerAC = h[
"ac"];
661 for (
auto const& node : {a, b, c})
663 auto const val = node.validate(ledgerA);
666 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(
671 auto const val = d.validate(ledgerB);
673 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(val);
680 harness.
clock().advance(5s);
682 for (
auto const& node : {a, b, c})
684 auto const val = node.validate(ledgerAC);
687 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(
718 Ledger const ledgerA = h[
"a"];
728 Ledger const ledgerB = h[
"ab"];
741 for (
int i = 0; i < loops; ++i)
749 Ledger const ledgerC = h[
"abc"];
762 using namespace std::chrono_literals;
772 Ledger ledgerAB = h[
"ab"];
775 for (
auto const& node : {a, b, c})
777 auto const val = node.validate(ledgerA);
779 expected.
emplace(node.nodeID(), val);
784 harness.
clock().advance(1s);
793 using namespace std::chrono_literals;
804 Ledger ledgerAC = h[
"ac"];
805 Ledger ledgerACD = h[
"acd"];
809 auto pref = [](
Ledger ledger) {
829 BEAST_EXPECT(ledgerB.
id() > ledgerA.
id());
838 harness.
clock().advance(5s);
841 for (
auto const& node : {a, b, c, d})
852 harness.
clock().advance(5s);
853 for (
auto const& node : {a, b, c, d})
856 for (
auto const& ledger : {ledgerA, ledgerB, ledgerACD})
864 using namespace std::chrono_literals;
885 ++peerCounts[ledgerB.
id()];
892 ++peerCounts[ledgerC.
id()];
894 BEAST_EXPECT(ledgerC.
id() > ledgerB.
id());
900 peerCounts[ledgerC.
id()] += 1000;
924 using namespace std::chrono_literals;
925 testcase(
"Acquire validated ledger");
953 harness.
add(b.
validate(ID{3}, Seq{2}, 0s, 0s,
true)));
959 Ledger ledgerAB = h[
"ab"];
964 harness.
clock().advance(5s);
981 harness.
clock().advance(5s);
982 Ledger ledgerABCDE = h[
"abcde"];
1019 BEAST_EXPECT(enforcer(clock.now(), Seq{1}, p));
1020 BEAST_EXPECT(enforcer(clock.now(), Seq{10}, p));
1021 BEAST_EXPECT(!enforcer(clock.now(), Seq{5}, p));
1022 BEAST_EXPECT(!enforcer(clock.now(), Seq{9}, p));
1024 BEAST_EXPECT(!enforcer(clock.now(), Seq{1}, p));
1026 BEAST_EXPECT(enforcer(clock.now(), Seq{1}, p));
1035 auto checker = [
this](
1040 : trustedVals[0].ledgerID();
1043 : trustedVals[0].seq();
1048 trustedVals.size());
1049 if (trustedVals.empty())
1054 vals.
getPreferred(this->genesisLedger)->second == testID);
1066 Ledger ledgerAB = h[
"ab"];
1072 checker(harness.
vals(), listed, trustedVals);
1074 trustedVals.clear();
1076 checker(harness.
vals(), listed, trustedVals);
1085 Ledger ledgerAB = h[
"ab"];
1091 checker(harness.
vals(), listed, trustedVals);
1093 trustedVals.push_back(v);
1095 checker(harness.
vals(), listed, trustedVals);
1109 auto& vals = harness.
vals();
1110 BEAST_EXPECT(vals.currentTrusted() == trustedVals);
1116 trustedVals.clear();
1120 BEAST_EXPECT(vals.currentTrusted() == trustedVals);
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.