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 c.now().time_since_epoch() + 86400s));
124 ledger.
id(), ledger.
seq(), signOffset, seenOffset,
true);
244 using namespace std::chrono_literals;
249 Ledger ledgerAB = h[
"ab"];
250 Ledger ledgerAZ = h[
"az"];
251 Ledger ledgerABC = h[
"abc"];
252 Ledger ledgerABCD = h[
"abcd"];
253 Ledger ledgerABCDE = h[
"abcde"];
268 harness.
clock().advance(1s);
284 harness.
clock().advance(1s);
294 harness.
clock().advance(1s);
304 harness.
clock().advance(2s);
305 auto const valABCDE = n.
validate(ledgerABCDE);
307 harness.
clock().advance(4s);
308 auto const valABCD = n.
validate(ledgerABCD);
361 for (
bool doFull : {
true,
false})
366 auto process = [&](
Ledger& lgr) {
373 harness.
clock().advance(1s);
374 BEAST_EXPECT(ledgerAB.
seq() < ledgerABC.
seq());
380 harness.
clock().advance(
396 Ledger ledgerAB = h[
"ab"];
405 vals.getNodesAfter(ledgerA, ledgerA.
id());
407 for (Trigger trigger : triggers)
414 trigger(harness.
vals());
423 trigger(harness.
vals());
439 using namespace std::chrono_literals;
444 Ledger ledgerAB = h[
"ab"];
445 Ledger ledgerABC = h[
"abc"];
446 Ledger ledgerAD = h[
"ad"];
459 for (
Ledger const& ledger : {ledgerA, ledgerAB, ledgerABC, ledgerAD})
463 harness.
clock().advance(5s);
488 using namespace std::chrono_literals;
489 testcase(
"Current trusted validations");
494 Ledger ledgerAC = h[
"ac"];
509 harness.
clock().advance(3s);
511 for (
auto const& node : {a, b})
530 using namespace std::chrono_literals;
535 Ledger ledgerAC = h[
"ac"];
541 for (
auto const& node : {a, b})
550 harness.
clock().advance(3s);
556 for (
auto const& node : {a, b})
574 using namespace std::chrono_literals;
602 auto sorted = [](
auto vec) {
606 auto compare = [&]() {
607 for (
auto& it : trustedValidations)
609 auto const&
id = it.first.first;
610 auto const&
seq = it.first.second;
611 auto const& expectedValidations = it.second;
615 expectedValidations.size());
618 sorted(expectedValidations));
622 for (
auto const& val : expectedValidations)
624 expectedFees.
push_back(val.loadFee().value_or(baseFee));
628 sorted(harness.
vals().
fees(
id, baseFee)) ==
629 sorted(expectedFees));
636 Ledger ledgerAC = h[
"ac"];
642 for (
auto const& node : {a, b, c})
644 auto const val = node.validate(ledgerA);
647 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(
652 auto const val = d.validate(ledgerB);
654 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(val);
661 harness.
clock().advance(5s);
663 for (
auto const& node : {a, b, c})
665 auto const val = node.validate(ledgerAC);
668 trustedValidations[{val.ledgerID(), val.seq()}].emplace_back(
699 Ledger const ledgerA = h[
"a"];
709 Ledger const ledgerB = h[
"ab"];
722 for (
int i = 0; i < loops; ++i)
730 Ledger const ledgerC = h[
"abc"];
743 using namespace std::chrono_literals;
753 Ledger ledgerAB = h[
"ab"];
756 for (
auto const& node : {a, b, c})
758 auto const val = node.validate(ledgerA);
760 expected.
emplace(node.nodeID(), val);
765 harness.
clock().advance(1s);
774 using namespace std::chrono_literals;
785 Ledger ledgerAC = h[
"ac"];
786 Ledger ledgerACD = h[
"acd"];
790 auto pref = [](
Ledger ledger) {
810 BEAST_EXPECT(ledgerB.
id() > ledgerA.
id());
819 harness.
clock().advance(5s);
822 for (
auto const& node : {a, b, c, d})
833 harness.
clock().advance(5s);
834 for (
auto const& node : {a, b, c, d})
837 for (
auto const& ledger : {ledgerA, ledgerB, ledgerACD})
845 using namespace std::chrono_literals;
866 ++peerCounts[ledgerB.
id()];
873 ++peerCounts[ledgerC.
id()];
875 BEAST_EXPECT(ledgerC.
id() > ledgerB.
id());
881 peerCounts[ledgerC.
id()] += 1000;
905 using namespace std::chrono_literals;
906 testcase(
"Acquire validated ledger");
934 harness.
add(b.
validate(ID{3}, Seq{2}, 0s, 0s,
true)));
940 Ledger ledgerAB = h[
"ab"];
945 harness.
clock().advance(5s);
962 harness.
clock().advance(5s);
963 Ledger ledgerABCDE = h[
"abcde"];
1000 BEAST_EXPECT(enforcer(clock.now(), Seq{1}, p));
1001 BEAST_EXPECT(enforcer(clock.now(), Seq{10}, p));
1002 BEAST_EXPECT(!enforcer(clock.now(), Seq{5}, p));
1003 BEAST_EXPECT(!enforcer(clock.now(), Seq{9}, p));
1005 BEAST_EXPECT(!enforcer(clock.now(), Seq{1}, p));
1007 BEAST_EXPECT(enforcer(clock.now(), Seq{1}, p));
1016 auto checker = [
this](
1021 : trustedVals[0].ledgerID();
1024 : trustedVals[0].seq();
1029 trustedVals.size());
1030 if (trustedVals.empty())
1035 vals.
getPreferred(this->genesisLedger)->second == testID);
1047 Ledger ledgerAB = h[
"ab"];
1053 checker(harness.
vals(), listed, trustedVals);
1055 trustedVals.clear();
1057 checker(harness.
vals(), listed, trustedVals);
1066 Ledger ledgerAB = h[
"ab"];
1072 checker(harness.
vals(), listed, trustedVals);
1074 trustedVals.push_back(v);
1076 checker(harness.
vals(), listed, trustedVals);
1090 auto& vals = harness.
vals();
1091 BEAST_EXPECT(vals.currentTrusted() == trustedVals);
1097 trustedVals.clear();
1101 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.