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>
28 return NetClock::time_point(duration_cast<NetClock::duration>(c.now().time_since_epoch() + 86400s));
111 return validate(ledger.
id(), ledger.
seq(), signOffset, seenOffset,
true);
221 using namespace std::chrono_literals;
226 Ledger ledgerAB = h[
"ab"];
227 Ledger ledgerAZ = h[
"az"];
228 Ledger ledgerABC = h[
"abc"];
229 Ledger ledgerABCD = h[
"abcd"];
230 Ledger ledgerABCDE = h[
"abcde"];
245 harness.
clock().advance(1s);
258 harness.
clock().advance(1s);
266 harness.
clock().advance(1s);
273 harness.
clock().advance(2s);
274 auto const valABCDE = n.
validate(ledgerABCDE);
276 harness.
clock().advance(4s);
277 auto const valABCD = n.
validate(ledgerABCD);
319 for (
bool doFull : {
true,
false})
324 auto process = [&](
Ledger& lgr) {
331 harness.
clock().advance(1s);
332 BEAST_EXPECT(ledgerAB.
seq() < ledgerABC.
seq());
353 Ledger ledgerAB = h[
"ab"];
362 for (Trigger trigger : triggers)
368 trigger(harness.
vals());
374 trigger(harness.
vals());
388 using namespace std::chrono_literals;
393 Ledger ledgerAB = h[
"ab"];
394 Ledger ledgerABC = h[
"abc"];
395 Ledger ledgerAD = h[
"ad"];
407 for (
Ledger const& ledger : {ledgerA, ledgerAB, ledgerABC, ledgerAD})
410 harness.
clock().advance(5s);
431 using namespace std::chrono_literals;
432 testcase(
"Current trusted validations");
437 Ledger ledgerAC = h[
"ac"];
451 harness.
clock().advance(3s);
453 for (
auto const& node : {a, b})
469 using namespace std::chrono_literals;
474 Ledger ledgerAC = h[
"ac"];
480 for (
auto const& node : {a, b})
488 harness.
clock().advance(3s);
494 for (
auto const& node : {a, b})
511 using namespace std::chrono_literals;
537 auto sorted = [](
auto vec) {
541 auto compare = [&]() {
542 for (
auto& it : trustedValidations)
544 auto const&
id = it.first.first;
545 auto const&
seq = it.first.second;
546 auto const& expectedValidations = it.second;
553 for (
auto const& val : expectedValidations)
555 expectedFees.
push_back(val.loadFee().value_or(baseFee));
558 BEAST_EXPECT(sorted(harness.
vals().
fees(
id, baseFee)) == sorted(expectedFees));
565 Ledger ledgerAC = h[
"ac"];
571 for (
auto const& node : {a, b, c})
573 auto const val = node.validate(ledgerA);
576 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(val);
580 auto const val = d.validate(ledgerB);
582 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(val);
589 harness.
clock().advance(5s);
591 for (
auto const& node : {a, b, c})
593 auto const val = node.validate(ledgerAC);
596 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(val);
624 Ledger const ledgerA = h[
"a"];
634 Ledger const ledgerB = h[
"ab"];
645 for (
int i = 0; i < loops; ++i)
653 Ledger const ledgerC = h[
"abc"];
666 using namespace std::chrono_literals;
675 Ledger ledgerAB = h[
"ab"];
678 for (
auto const& node : {a, b, c})
680 auto const val = node.validate(ledgerA);
682 expected.
emplace(node.nodeID(), val);
687 harness.
clock().advance(1s);
696 using namespace std::chrono_literals;
706 Ledger ledgerAC = h[
"ac"];
707 Ledger ledgerACD = h[
"acd"];
728 BEAST_EXPECT(ledgerB.
id() > ledgerA.
id());
737 harness.
clock().advance(5s);
740 for (
auto const& node : {a, b, c, d})
750 harness.
clock().advance(5s);
751 for (
auto const& node : {a, b, c, d})
753 for (
auto const& ledger : {ledgerA, ledgerB, ledgerACD})
760 using namespace std::chrono_literals;
779 ++peerCounts[ledgerB.
id()];
784 ++peerCounts[ledgerC.
id()];
786 BEAST_EXPECT(ledgerC.
id() > ledgerB.
id());
790 peerCounts[ledgerC.
id()] += 1000;
806 using namespace std::chrono_literals;
807 testcase(
"Acquire validated ledger");
835 Ledger ledgerAB = h[
"ab"];
840 harness.
clock().advance(5s);
853 harness.
clock().advance(5s);
854 Ledger ledgerABCDE = h[
"abcde"];
889 BEAST_EXPECT(enforcer(clock.now(), Seq{1}, p));
890 BEAST_EXPECT(enforcer(clock.now(), Seq{10}, p));
891 BEAST_EXPECT(!enforcer(clock.now(), Seq{5}, p));
892 BEAST_EXPECT(!enforcer(clock.now(), Seq{9}, p));
894 BEAST_EXPECT(!enforcer(clock.now(), Seq{1}, p));
896 BEAST_EXPECT(enforcer(clock.now(), Seq{1}, p));
912 if (trustedVals.empty())
915 BEAST_EXPECT(vals.
getPreferred(this->genesisLedger)->second == testID);
925 Ledger ledgerAB = h[
"ab"];
931 checker(harness.
vals(), listed, trustedVals);
935 checker(harness.
vals(), listed, trustedVals);
944 Ledger ledgerAB = h[
"ab"];
950 checker(harness.
vals(), listed, trustedVals);
952 trustedVals.push_back(v);
954 checker(harness.
vals(), listed, trustedVals);
967 auto& vals = harness.
vals();
968 BEAST_EXPECT(vals.currentTrusted() == trustedVals);
976 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.