3#include <xrpld/consensus/LedgerTrie.h>
5#include <xrpl/basics/Log.h>
6#include <xrpl/basics/UnorderedContainers.h>
7#include <xrpl/basics/chrono.h>
8#include <xrpl/beast/container/aged_container_utility.h>
9#include <xrpl/beast/container/aged_unordered_map.h>
10#include <xrpl/protocol/PublicKey.h>
81 using time_point = std::chrono::steady_clock::time_point;
174 return "conflicting";
266template <
class Adaptor>
269 using Mutex =
typename Adaptor::Mutex;
272 using ID =
typename Ledger::ID;
273 using Seq =
typename Ledger::Seq;
274 using NodeID =
typename Validation::NodeID;
331 it->second.erase(nodeID);
332 if (it->second.empty())
338 if (it !=
lastLedger_.end() && it->second.id() == val.ledgerID())
340 trie_.remove(it->second);
354 for (
NodeID const& nodeID : it->second)
368 auto const [it, inserted] =
lastLedger_.emplace(nodeID, ledger);
371 trie_.remove(it->second);
374 trie_.insert(ledger);
397 XRPL_ASSERT(val.trusted(),
"xrpl::Validations::updateTrie : trusted input validation");
405 it->second.erase(nodeID);
406 if (it->second.empty())
417 it->
second.insert(nodeID);
445 current(lock, [](
auto) {}, [](
auto,
auto) {});
466 template <
class Pre,
class F>
476 if (!
isCurrent(
parms_, t, it->second.signTime(), it->second.seenTime()))
485 f(cit->first, cit->second);
503 template <
class Pre,
class F>
512 pre(it->second.size());
513 for (
auto const& [key, val] : it->second)
525 template <
class... Ts>
581 auto const now =
byLedger_.clock().now();
583 auto const [seqit, seqinserted] =
bySequence_[val.seq()].emplace(nodeID, val);
589 auto const diff =
std::max(seqit->second.signTime(), val.signTime()) -
590 std::min(seqit->second.signTime(), val.signTime());
602 if (seqit->second.seq() == val.seq())
607 if (seqit->second.ledgerID() != val.ledgerID())
614 if (seqit->second.signTime() != val.signTime())
619 if (seqit->second.cookie() != val.cookie())
626 byLedger_[val.ledgerID()].insert_or_assign(nodeID, val);
628 auto const [it, inserted] =
current_.emplace(nodeID, val);
633 if (val.signTime() > oldVal.signTime())
643 else if (val.trusted())
662 XRPL_ASSERT(low < high,
"xrpl::Validations::setSeqToKeep : valid inputs");
681 static std::chrono::steady_clock::time_point refreshTime;
682 if (
auto const now =
byLedger_.clock().now(); refreshTime <= now)
690 auto const& validationMap = i->second;
691 if (!validationMap.empty())
693 auto const seq = validationMap.begin()->second.seq();
694 if (
toKeep_->low_ <= seq && seq < toKeep_->high_)
715 <<
"Validations sets sweep lock duration "
736 if (added.
find(nodeId) != added.
end())
741 else if (removed.
find(nodeId) != removed.
end())
748 for (
auto& [_, validationMap] :
byLedger_)
751 for (
auto& [nodeId,
validation] : validationMap)
753 if (added.
find(nodeId) != added.
end())
757 else if (removed.
find(nodeId) != removed.
end())
769 return trie_.getJson();
795 std::pair<Seq, ID> const& aKey = a.first;
796 typename hash_set<NodeID>::size_type const& aSize = a.second.size();
797 std::pair<Seq, ID> const& bKey = b.first;
798 typename hash_set<NodeID>::size_type const& bSize = b.second.size();
801 return std::tie(aSize, aKey.second) < std::tie(bSize, bKey.second);
810 if (preferred->seq == curr.seq() +
Seq{1} && preferred->ancestor(curr.seq()) == curr.id())
815 if (preferred->seq > curr.seq())
820 if (curr[preferred->seq] != preferred->id)
840 if (preferred && preferred->first >= minValidSeq)
841 return preferred->second;
869 return (preferred->first >= minSeq) ? preferred->second : lcl.id();
875 return std::tie(a.second, a.first) < std::tie(b.second, b.first);
878 if (it != peerCounts.
end())
900 if (ledger.id() == ledgerID)
907 auto const& curr = it.second;
908 return curr.seq() > Seq{0} && curr[curr.seq() -
Seq{1}] == ledgerID;
925 if (v.trusted() && v.full())
963 if (v.trusted() && v.full())
985 if (v.trusted() && v.full() && v.seq() == seq)
1008 if (v.trusted() && v.full())
1053 if (adaptor_.now() < v.seenTime() + parms_.validationFRESHNESS &&
1054 trustedKeys.
find(v.key()) != trustedKeys.
end())
1056 trustedKeys.
erase(v.key());
1069 return current_.size();
1076 return seqEnforcers_.size();
1083 return byLedger_.size();
1090 return bySequence_.size();
A generic endpoint for log messages.
Abstract interface to a clock.
Associative container where each element is also indexed by time.
Ancestry trie of ledgers.
std::uint32_t tipSupport(Ledger const &ledger) const
Return count of tip support for the specific ledger.
std::uint32_t branchSupport(Ledger const &ledger) const
Return the count of branch support for the specific ledger.
std::optional< SpanTip< Ledger > > getPreferred(Seq const largestIssued) const
Return the preferred ledger ID.
Enforce validation increasing sequence requirement.
std::chrono::steady_clock::time_point time_point
bool operator()(time_point now, Seq s, ValidationParms const &p)
Try advancing the largest observed validation ledger sequence.
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.
bool canValidateSeq(Seq const s)
Return whether the local node can issue a validation for the given sequence number.
beast::aged_unordered_map< ID, hash_map< NodeID, Validation >, std::chrono::steady_clock, beast::uhash<> > byLedger_
Validations from listed nodes, indexed by ledger id (partial and full)
LedgerTrie< Ledger > trie_
auto getCurrentNodeIDs() -> hash_set< NodeID >
Get the set of node ids associated with current validations.
typename Adaptor::Mutex Mutex
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.
void byLedger(std::lock_guard< Mutex > const &, ID const &ledgerID, Pre &&pre, F &&f)
Iterate the set of validations associated with a given ledger id.
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.
void checkAcquired(std::lock_guard< Mutex > const &lock)
ValidationParms const & parms() const
Return the validation timing parameters.
void current(std::lock_guard< Mutex > const &lock, Pre &&pre, F &&f)
Iterate current validations.
void removeTrie(std::lock_guard< Mutex > const &, NodeID const &nodeID, Validation const &val)
hash_map< NodeID, Validation > current_
hash_map< NodeID, SeqEnforcer< Seq > > seqEnforcers_
Adaptor const & adaptor() const
Return the adaptor instance.
hash_map< NodeID, Ledger > lastLedger_
auto withTrie(std::lock_guard< Mutex > const &lock, F &&f)
Use the trie for a calculation.
SeqEnforcer< Seq > localSeqEnforcer_
Json::Value getJsonTrie() const
std::size_t sizeOfCurrentCache() const
std::optional< std::pair< Seq, ID > > getPreferred(Ledger const &curr)
Return the sequence number and ID of the preferred working ledger.
typename Validation::NodeKey NodeKey
void flush()
Flush all current 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.
beast::aged_unordered_map< Seq, hash_map< NodeID, Validation >, std::chrono::steady_clock, beast::uhash<> > bySequence_
Validations(ValidationParms const &p, beast::abstract_clock< std::chrono::steady_clock > &c, Ts &&... ts)
Constructor.
ValidationParms const parms_
void updateTrie(std::lock_guard< Mutex > const &, NodeID const &nodeID, Ledger ledger)
std::size_t sizeOfByLedgerCache() const
typename Adaptor::Validation Validation
std::size_t sizeOfBySequenceCache() const
typename Validation::NodeID NodeID
typename Adaptor::Ledger Ledger
std::size_t sizeOfSeqEnforcersCache() const
std::optional< KeepRange > toKeep_
void expire(beast::Journal &j)
Expire old validation sets.
std::size_t laggards(Seq const seq, hash_set< NodeKey > &trustedKeys)
Return quantity of lagging proposers, and remove online proposers for purposes of evaluating whether ...
ID getPreferred(Ledger const &curr, Seq minValidSeq)
Get the ID of the preferred working ledger that exceeds a minimum valid ledger sequence number.
std::vector< std::uint32_t > fees(ID const &ledgerID, std::uint32_t baseFee)
Returns fees reported by trusted full validators in the given ledger.
void updateTrie(std::lock_guard< Mutex > const &lock, NodeID const &nodeID, Validation const &val, std::optional< std::pair< Seq, ID > > prior)
Process a new validation.
hash_map< std::pair< Seq, ID >, hash_set< NodeID > > acquiring_
T emplace_back(T... args)
std::enable_if< is_aged_container< AgedContainer >::value, std::size_t >::type expire(AgedContainer &c, std::chrono::duration< Rep, Period > const &age)
Expire aged container items past the specified age.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool isCurrent(ValidationParms const &p, NetClock::time_point now, NetClock::time_point signTime, NetClock::time_point seenTime)
Whether a validation is still current.
std::string to_string(base_uint< Bits, Tag > const &a)
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.
@ multiple
Multiple validations by a validator for the same ledger.
Dir::const_iterator const_iterator
@ validation
validation for signing
@ stale
Sequence is too old.
Timing parameters to control validation staleness and expiration.
ValidationParms()=default
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.