xrpld
Loading...
Searching...
No Matches
LedgerHistory.h
1#pragma once
2
3#include <xrpld/app/main/Application.h>
4
5#include <xrpl/basics/Mutex.hpp>
6#include <xrpl/basics/TaggedCache.h>
7#include <xrpl/basics/base_uint.h>
8#include <xrpl/beast/insight/Collector.h>
9#include <xrpl/beast/insight/Counter.h>
10#include <xrpl/beast/utility/Journal.h>
11#include <xrpl/json/json_value.h>
12#include <xrpl/ledger/Ledger.h>
13#include <xrpl/protocol/Protocol.h>
14#include <xrpl/protocol/RippleLedgerHash.h>
15
16#include <map>
17#include <memory>
18#include <optional>
19
20namespace xrpl {
21
22// VFALCO TODO Rename to OldLedgers ?
23
28{
29public:
31
36 bool
37 insert(std::shared_ptr<Ledger const> const& ledger, bool validated);
38
43 float
45 {
46 auto lockedMaps = ledgerMaps_.lock();
47 return lockedMaps->byHash->getHitRate();
48 }
49
54 getLedgerBySeq(LedgerIndex ledgerIndex);
55
60 getLedgerByHash(LedgerHash const& ledgerHash);
61
68 getLedgerHash(LedgerIndex ledgerIndex);
69
73 void
75 {
76 auto* const byHash = [this] {
77 auto lockedMaps = ledgerMaps_.lock();
78 return lockedMaps->byHash.get();
79 }();
80 byHash->sweep();
81 consensusValidated_.sweep();
82 }
83
87 void
88 builtLedger(std::shared_ptr<Ledger const> const&, UInt256 const& consensusHash, json::Value);
89
93 void
96 std::optional<UInt256> const& consensusHash);
97
104 bool
105 fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash);
106
107 void
109
110private:
122 void
124 LedgerHash const& built,
125 LedgerHash const& valid,
126 std::optional<UInt256> const& builtConsensusHash,
127 std::optional<UInt256> const& validatedConsensusHash,
128 json::Value const& consensus);
129
133
135 {
137
139
140 // Validated ledgers, seq -> hash. A cache over the ledger database:
141 // every consumer falls back to the database on a miss. Pruned only by
142 // clearLedgerCachePrior() on online_delete rotation. On nodes without
143 // online_delete it grows for the life of the process
144 // (~65 bytes per validated ledger).
146 };
147
148 // No lock site re-enters ledgerMaps_: getLedgerBySeq, clearLedgerCachePrior
149 // and sweep release it before calling back into LedgerHistory, and the
150 // only work done under it is TaggedCache/std::map bookkeeping. A plain
151 // std::mutex is therefore sufficient, and any future re-entry deadlocks
152 // deterministically instead of silently relying on recursion. Do not
153 // fetch SHAMap nodes or load ledgers while holding it: both can call
154 // back into LedgerHistory through NodeFamily::missingNodeAcquireBySeq.
155 xrpl::Mutex<LedgerMaps> ledgerMaps_;
156
157 // Maps ledger indexes to the corresponding hashes
158 // For debug and logging purposes
159 struct CvEntry
160 {
161 // Hash of locally built ledger
163 // Hash of the validated ledger
165 // Hash of locally accepted consensus transaction set
167 // Hash of validated consensus transaction set
169 // Consensus metadata of built ledger
171 };
174
176};
177
178} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
std::shared_ptr< Collector > Ptr
Definition Collector.h:29
A metric for measuring an integral value.
Definition Counter.h:20
Represents a JSON value.
Definition json_value.h:121
xrpl::Mutex< LedgerMaps > ledgerMaps_
ConsensusValidated consensusValidated_
beast::insight::Collector::Ptr collector_
bool insert(std::shared_ptr< Ledger const > const &ledger, bool validated)
Track a ledger.
float getCacheHitRate()
Get the byHash cache hit rate.
LedgerHash getLedgerHash(LedgerIndex ledgerIndex)
Get a ledger's hash given its sequence number.
void handleMismatch(LedgerHash const &built, LedgerHash const &valid, std::optional< UInt256 > const &builtConsensusHash, std::optional< UInt256 > const &validatedConsensusHash, json::Value const &consensus)
Log details in the case where we build one ledger but validate a different one.
bool fixIndex(LedgerIndex ledgerIndex, LedgerHash const &ledgerHash)
Repair a hash to index mapping.
std::shared_ptr< Ledger const > getLedgerByHash(LedgerHash const &ledgerHash)
Retrieve a ledger given its hash.
void builtLedger(std::shared_ptr< Ledger const > const &, UInt256 const &consensusHash, json::Value)
Report that we have locally built a particular ledger.
void sweep()
Remove stale cache entries.
LedgerHistory(beast::insight::Collector::Ptr const &collector, Application &app)
beast::insight::Counter mismatchCounter_
std::shared_ptr< Ledger const > getLedgerBySeq(LedgerIndex ledgerIndex)
Get a ledger given its sequence number.
void validatedLedger(std::shared_ptr< Ledger const > const &, std::optional< UInt256 > const &consensusHash)
Report that we have validated a particular ledger.
beast::Journal j_
void clearLedgerCachePrior(LedgerIndex seq)
TaggedCache< LedgerIndex, CvEntry > ConsensusValidated
Map/cache combination.
Definition TaggedCache.h:67
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:409
UInt256 LedgerHash
BaseUInt< 256 > UInt256
Definition base_uint.h:581
std::optional< LedgerHash > validated
std::optional< LedgerHash > built
std::optional< UInt256 > validatedConsensusHash
std::optional< json::Value > consensus
std::optional< UInt256 > builtConsensusHash
std::map< LedgerIndex, LedgerHash > byIndex
TaggedCache< LedgerHash, Ledger const > LedgersByHash
std::unique_ptr< LedgersByHash > byHash