1#include <test/csf/Validation.h>
2#include <test/unit_test/SuiteJournal.h>
4#include <xrpld/consensus/Validations.h>
6#include <xrpl/basics/tagged_integer.h>
7#include <xrpl/beast/clock/manual_clock.h>
8#include <xrpl/beast/unit_test.h>
29 duration_cast<NetClock::duration>(c.now().time_since_epoch() + 86400s));
120 return validate(ledger.
id(), ledger.
seq(), signOffset, seenOffset,
true);
232 using namespace std::chrono_literals;
237 Ledger ledgerAB = h[
"ab"];
238 Ledger ledgerAZ = h[
"az"];
239 Ledger ledgerABC = h[
"abc"];
240 Ledger ledgerABCD = h[
"abcd"];
241 Ledger ledgerABCDE = h[
"abcde"];
256 harness.
clock().advance(1s);
269 harness.
clock().advance(1s);
277 harness.
clock().advance(1s);
284 harness.
clock().advance(2s);
285 auto const valABCDE = n.
validate(ledgerABCDE);
287 harness.
clock().advance(4s);
288 auto const valABCD = n.
validate(ledgerABCD);
333 for (
bool doFull : {
true,
false})
338 auto process = [&](
Ledger& lgr) {
345 harness.
clock().advance(1s);
346 BEAST_EXPECT(ledgerAB.
seq() < ledgerABC.
seq());
367 Ledger ledgerAB = h[
"ab"];
376 for (Trigger trigger : triggers)
382 trigger(harness.
vals());
390 trigger(harness.
vals());
404 using namespace std::chrono_literals;
409 Ledger ledgerAB = h[
"ab"];
410 Ledger ledgerABC = h[
"abc"];
411 Ledger ledgerAD = h[
"ad"];
424 for (
Ledger const& ledger : {ledgerA, ledgerAB, ledgerABC, ledgerAD})
427 harness.
clock().advance(5s);
448 using namespace std::chrono_literals;
449 testcase(
"Current trusted validations");
454 Ledger ledgerAC = h[
"ac"];
468 harness.
clock().advance(3s);
470 for (
auto const& node : {a, b})
486 using namespace std::chrono_literals;
491 Ledger ledgerAC = h[
"ac"];
497 for (
auto const& node : {a, b})
505 harness.
clock().advance(3s);
511 for (
auto const& node : {a, b})
528 using namespace std::chrono_literals;
554 auto sorted = [](
auto vec) {
558 auto compare = [&]() {
559 for (
auto& it : trustedValidations)
561 auto const&
id = it.first.first;
562 auto const&
seq = it.first.second;
563 auto const& expectedValidations = it.second;
568 sorted(expectedValidations));
572 for (
auto const& val : expectedValidations)
574 expectedFees.
push_back(val.loadFee().value_or(baseFee));
577 BEAST_EXPECT(sorted(harness.
vals().
fees(
id, baseFee)) == sorted(expectedFees));
584 Ledger ledgerAC = h[
"ac"];
590 for (
auto const& node : {a, b, c})
592 auto const val = node.validate(ledgerA);
595 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(val);
599 auto const val = d.validate(ledgerB);
601 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(val);
608 harness.
clock().advance(5s);
610 for (
auto const& node : {a, b, c})
612 auto const val = node.validate(ledgerAC);
615 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(val);
643 Ledger const ledgerA = h[
"a"];
653 Ledger const ledgerB = h[
"ab"];
665 for (
int i = 0; i < loops; ++i)
673 Ledger const ledgerC = h[
"abc"];
686 using namespace std::chrono_literals;
695 Ledger ledgerAB = h[
"ab"];
698 for (
auto const& node : {a, b, c})
700 auto const val = node.validate(ledgerA);
702 expected.
emplace(node.nodeID(), val);
707 harness.
clock().advance(1s);
716 using namespace std::chrono_literals;
727 Ledger ledgerAC = h[
"ac"];
728 Ledger ledgerACD = h[
"acd"];
749 BEAST_EXPECT(ledgerB.
id() > ledgerA.
id());
758 harness.
clock().advance(5s);
761 for (
auto const& node : {a, b, c, d})
771 harness.
clock().advance(5s);
772 for (
auto const& node : {a, b, c, d})
774 for (
auto const& ledger : {ledgerA, ledgerB, ledgerACD})
781 using namespace std::chrono_literals;
800 ++peerCounts[ledgerB.
id()];
805 ++peerCounts[ledgerC.
id()];
807 BEAST_EXPECT(ledgerC.
id() > ledgerB.
id());
811 peerCounts[ledgerC.
id()] += 1000;
827 using namespace std::chrono_literals;
828 testcase(
"Acquire validated ledger");
856 Ledger ledgerAB = h[
"ab"];
861 harness.
clock().advance(5s);
878 harness.
clock().advance(5s);
879 Ledger ledgerABCDE = h[
"abcde"];
916 BEAST_EXPECT(enforcer(clock.now(), Seq{1}, p));
917 BEAST_EXPECT(enforcer(clock.now(), Seq{10}, p));
918 BEAST_EXPECT(!enforcer(clock.now(), Seq{5}, p));
919 BEAST_EXPECT(!enforcer(clock.now(), Seq{9}, p));
921 BEAST_EXPECT(!enforcer(clock.now(), Seq{1}, p));
923 BEAST_EXPECT(enforcer(clock.now(), Seq{1}, p));
932 auto checker = [
this](
937 trustedVals.empty() ? this->
genesisLedger.
id() : trustedVals[0].ledgerID();
944 if (trustedVals.empty())
947 BEAST_EXPECT(vals.
getPreferred(this->genesisLedger)->second == testID);
957 Ledger ledgerAB = h[
"ab"];
963 checker(harness.
vals(), listed, trustedVals);
967 checker(harness.
vals(), listed, trustedVals);
976 Ledger ledgerAB = h[
"ab"];
982 checker(harness.
vals(), listed, trustedVals);
984 trustedVals.push_back(v);
986 checker(harness.
vals(), listed, trustedVals);
999 auto& vals = harness.
vals();
1000 BEAST_EXPECT(vals.currentTrusted() == trustedVals);
1004 trustedVals.clear();
1008 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.
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.
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.
auto getCurrentNodeIDs() -> hash_set< NodeID >
Get the set of node ids associated with current validations.
std::size_t numTrustedForLedger(ID const &ledgerID)
Count the number of trusted full validations for the given ledger.
std::vector< WrappedValidationType > getTrustedForLedger(ID const &ledgerID, Seq const &seq)
Get trusted full validations for a specific ledger.
ValStatus add(NodeID const &nodeID, Validation const &val)
Add a new validation.
void trustChanged(hash_set< NodeID > const &added, hash_set< NodeID > const &removed)
Update trust status of validations.
std::optional< std::pair< Seq, ID > > getPreferred(Ledger const &curr)
Return the sequence number and ID of the preferred working ledger.
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.
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, IdTag > ID
tagged_integer< std::uint32_t, SeqTag > Seq
Validation of a specific ledger by a specific Peer.
PeerID const & nodeID() const
Ledger::ID ledgerID() const
NetClock::time_point now() const
Adaptor(clock_type &c, LedgerOracle &o)
std::optional< Ledger > acquire(Ledger::ID const &id)
std::optional< std::uint32_t > loadFee_
NetClock::time_point now() const
Validation validate(Ledger::ID id, Ledger::Seq seq, NetClock::duration signOffset, NetClock::duration seenOffset, bool full) const
PeerKey masterKey() const
Node(PeerID nodeID, clock_type const &c)
Validation validate(Ledger ledger) const
void setLoadFee(std::uint32_t fee)
Validation partial(Ledger ledger) const
Validation validate(Ledger ledger, NetClock::duration signOffset, NetClock::duration seenOffset) const
ValStatus add(Validation const &v)
ValidationParms parms() const
beast::manual_clock< std::chrono::steady_clock > clock_
TestHarness(LedgerOracle &o)
void testGetCurrentPublicKeys()
static NetClock::time_point toNetClock(clock_type const &c)
void testAcquireValidatedLedger()
beast::abstract_clock< std::chrono::steady_clock > const clock_type
void testGetPreferredLedger()
void testGetPreferredLCL()
Ledger const genesisLedger
void testCurrentTrusted()
void run() override
Runs the suite.
void testTrustedByLedgerFunctions()
void testNumTrustedForLedger()
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 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_EARLY
Duration pre-close in which validations are acceptable.
std::chrono::seconds validationCURRENT_LOCAL
Duration a validation remains current after first observed.
std::chrono::seconds validationCURRENT_WALL
The number of seconds a validation remains current after its ledger's close time.
Helper for writing unit tests with controlled ledger histories.
Set the sequence number on a JTx.