xrpld
Loading...
Searching...
No Matches
LedgerMaster.h
1#pragma once
2
3#include <xrpld/app/ledger/AbstractFetchPackContainer.h>
4#include <xrpld/app/ledger/InboundLedger.h>
5#include <xrpld/app/ledger/LedgerHistory.h>
6#include <xrpld/app/ledger/LedgerHolder.h>
7#include <xrpld/app/ledger/LedgerReplay.h>
8#include <xrpld/app/main/Application.h>
9#include <xrpld/core/TimeKeeper.h>
10
11#include <xrpl/basics/Blob.h>
12#include <xrpl/basics/RangeSet.h>
13#include <xrpl/basics/UptimeClock.h>
14#include <xrpl/basics/base_uint.h>
15#include <xrpl/basics/chrono.h>
16#include <xrpl/beast/insight/Collector.h>
17#include <xrpl/beast/insight/Gauge.h>
18#include <xrpl/beast/insight/Hook.h>
19#include <xrpl/beast/utility/Journal.h>
20#include <xrpl/json/json_value.h>
21#include <xrpl/ledger/CanonicalTXSet.h>
22#include <xrpl/ledger/Ledger.h>
23#include <xrpl/ledger/ReadView.h>
24#include <xrpl/protocol/Protocol.h>
25#include <xrpl/protocol/RippleLedgerHash.h>
26#include <xrpl/protocol/Rules.h>
27
28#include <xrpl.pb.h>
29
30#include <atomic>
31#include <chrono>
32#include <cstddef>
33#include <cstdint>
34#include <memory>
35#include <mutex>
36#include <optional>
37#include <string>
38#include <utility>
39#include <vector>
40
41namespace xrpl {
42
43class Peer;
44class Transaction;
45
46// Tracks the current ledger and any ledgers in the process of closing
47// Tracks ledger history
48// Tracks held transactions
50{
51public:
52 explicit LedgerMaster(
53 Application& app,
55 beast::insight::Collector::ptr const& collector,
56 beast::Journal journal);
57
58 ~LedgerMaster() override = default;
59
64
65 bool
66 isCompatible(ReadView const&, beast::Journal::Stream, char const* reason);
67
69 peekMutex();
70
71 // The current ledger is the ledger we believe new transactions should go in
74
75 // The finalized ledger is the last closed/accepted ledger
78 {
79 return closedLedger_.get();
80 }
81
82 // The validated ledger is the last fully validated ledger.
85
86 // The Rules are in the last fully validated ledger if there is one.
87 Rules
89
90 // This is the last ledger we published to clients and can lag the validated
91 // ledger
94
99 bool
100 isCaughtUp(std::string& reason);
101
104
105 bool
107
108 void
109 setFullLedger(std::shared_ptr<Ledger const> const& ledger, bool isSynchronous, bool isCurrent);
110
116 bool
118
119 void
120 switchLCL(std::shared_ptr<Ledger const> const& lastClosed);
121
122 void
123 failedSave(std::uint32_t seq, uint256 const& hash);
124
127
134 void
136
145
149 uint256
151
157
168 std::uint32_t index,
169 std::shared_ptr<ReadView const> const& referenceLedger,
170 InboundLedger::Reason reason);
171
174
176 getLedgerByHash(uint256 const& hash);
177
178 void
180
182 getCloseTimeBySeq(LedgerIndex ledgerIndex);
183
185 getCloseTimeByHash(LedgerHash const& ledgerHash, LedgerIndex ledgerIndex);
186
187 void
189 void
190 fixMismatch(ReadView const& ledger);
191
192 bool
194 void
196 bool
197 isValidated(ReadView const& ledger);
198 bool
200 bool
202
203 void
204 sweep();
205 float
207
208 void
210 void
211 checkAccept(uint256 const& hash, std::uint32_t seq);
212 void
214 std::shared_ptr<Ledger const> const& ledger,
215 uint256 const& consensusHash,
216 json::Value consensus);
217
218 void
220
221 void
222 tryAdvance();
223 bool
224 newPathRequest(); // Returns true if path request successfully placed.
225 bool
227 bool
228 newOrderBookDB(); // Returns true if able to fulfill request.
229
230 bool
231 fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash);
232
233 void
235
236 void
238
239 // ledger replay
240 void
244
245 // Fetch Packs
246 void
247 gotFetchPack(bool progress, std::uint32_t seq);
248
249 void
250 addFetchPack(uint256 const& hash, std::shared_ptr<Blob> data);
251
253 getFetchPack(uint256 const& hash) override;
254
255 void
257 std::weak_ptr<Peer> const& wPeer,
259 uint256 haveLedgerHash,
261
263 getFetchPackCacheSize() const;
264
268 bool
270 {
271 return !validLedger_.empty();
272 }
273
274 // Returns the minimum ledger sequence in SQL database, if any.
276 minSqlSeq();
277
278 // Iff a txn exists at the specified ledger and offset then return its txnid
280 txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex);
281
282private:
283 void
285 void
287
288 void
290
291 void
293
296
299 void
301 std::uint32_t missing,
302 bool& progress,
305 // Try to publish ledgers, acquire missing ledgers. Always called with
306 // mutex_ locked. The passed lock is a reminder to callers.
307 void
309
312
313 void
314 updatePaths();
315
316 // Returns true if work started. Always called with mutex_ locked.
317 // The passed lock is a reminder to callers.
318 bool
320
323
325
326 // The ledger that most recently closed.
328
329 // The highest-sequence ledger we have fully accepted.
331
332 // The last ledger we have published.
334
335 // The last ledger we did pathfinding against.
337
338 // The last ledger we handled fetching history
340
341 // Fully validated ledger, whether or not we have the ledger resident.
343
345
347
348 // A set of transactions to replay during the next close
350
353
354 // Publish thread is running.
355 bool advanceThread_{false};
356
357 // Publish thread has work to do.
358 bool advanceWork_{false};
360
361 int pathFindThread_{0}; // Pathfinder jobs dispatched
363
364 std::atomic_flag gotFetchPackThread_ = ATOMIC_FLAG_INIT; // GotFetchPack jobs dispatched
365
371
372 // The server is in standalone mode
373 bool const standalone_;
374
375 // How many ledgers before the current ledger do we allow peers to request?
377
378 // How much history do we want to keep
380
382
384
386
387 // Try to keep a validator from switching from test to live network
388 // without first wiping the database.
390
391 // Time that the previous upgrade warning was issued.
393
394private:
395 struct Stats
396 {
397 template <class Handler>
398 Stats(Handler const& handler, beast::insight::Collector::ptr const& collector)
399 : hook(collector->makeHook(handler))
400 , validatedLedgerAge(collector->makeGauge("LedgerMaster", "Validated_Ledger_Age"))
401 , publishedLedgerAge(collector->makeGauge("LedgerMaster", "Published_Ledger_Age"))
402 {
403 }
404
408 };
409
411
412private:
413 void
415 {
416 std::scoped_lock const lock(mutex_);
417 stats_.validatedLedgerAge.set(getValidatedLedgerAge().count());
418 stats_.publishedLedgerAge.set(getPublishedLedgerAge().count());
419 }
420};
421
422} // namespace xrpl
NetClock::time_point time_point
Provide a light-weight way to check active() before string formatting.
Definition Journal.h:199
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 Gauge.h:21
A reference to a handler for performing polled collection.
Definition Hook.h:14
Represents a JSON value.
Definition json_value.h:117
An interface facilitating retrieval of fetch packs without an application or ledgermaster object.
Holds transactions which were deferred to the next pass of consensus.
Retains historical ledgers.
Hold a ledger in a thread-safe way.
std::optional< LedgerIndex > minSqlSeq()
LedgerIndex const maxLedgerDifference_
std::atomic_flag gotFetchPackThread_
bool haveLedger(std::uint32_t seq)
std::size_t getNeededValidations()
Determines how many validations are needed to fully validate a ledger.
bool isCompatible(ReadView const &, beast::Journal::Stream, char const *reason)
std::shared_ptr< STTx const > popAcctTransaction(std::shared_ptr< STTx const > const &tx)
Get the next transaction held for a particular account if any.
void setValidLedger(std::shared_ptr< Ledger const > const &l)
void switchLCL(std::shared_ptr< Ledger const > const &lastClosed)
std::recursive_mutex & peekMutex()
std::chrono::seconds getValidatedLedgerAge()
TimeKeeper::time_point upgradeWarningPrevTime_
std::uint32_t const ledgerFetchSize_
std::atomic< std::uint32_t > pubLedgerClose_
LedgerIndex getCurrentLedgerIndex()
bool fixIndex(LedgerIndex ledgerIndex, LedgerHash const &ledgerHash)
bool getValidatedRange(std::uint32_t &minVal, std::uint32_t &maxVal)
void applyHeldTransactions()
Apply held transactions to the open ledger This is normally called as we close the ledger.
bool storeLedger(std::shared_ptr< Ledger const > ledger)
void gotFetchPack(bool progress, std::uint32_t seq)
beast::Journal journal_
void tryFill(std::shared_ptr< Ledger const > ledger)
std::shared_ptr< Ledger const > getLedgerBySeq(std::uint32_t index)
bool haveValidated()
Whether we have ever fully validated a ledger.
void setPubLedger(std::shared_ptr< Ledger const > const &l)
bool newPFWork(char const *name, std::unique_lock< std::recursive_mutex > &)
A thread needs to be dispatched to handle pathfinding work of some kind.
void setFullLedger(std::shared_ptr< Ledger const > const &ledger, bool isSynchronous, bool isCurrent)
std::atomic< LedgerIndex > pubLedgerSeq_
void clearPriorLedgers(LedgerIndex seq)
void setBuildingLedger(LedgerIndex index)
std::uint32_t fetchSeq_
bool isCaughtUp(std::string &reason)
std::size_t getFetchPackCacheSize() const
std::optional< Blob > getFetchPack(uint256 const &hash) override
std::vector< std::shared_ptr< Ledger const > > findNewLedgersToPublish(std::unique_lock< std::recursive_mutex > &)
std::atomic< LedgerIndex > buildingLedgerSeq_
std::optional< NetClock::time_point > getCloseTimeByHash(LedgerHash const &ledgerHash, LedgerIndex ledgerIndex)
~LedgerMaster() override=default
void clearLedger(std::uint32_t seq)
void clearLedgerCachePrior(LedgerIndex seq)
uint256 getHashBySeq(std::uint32_t index)
Get a ledger's hash by sequence number using the cache.
std::unique_ptr< LedgerReplay > replayData_
void consensusBuilt(std::shared_ptr< Ledger const > const &ledger, uint256 const &consensusHash, json::Value consensus)
Report that the consensus process built a particular ledger.
std::atomic< LedgerIndex > validLedgerSeq_
std::shared_ptr< Ledger const > getClosedLedger()
void setLedgerRangePresent(std::uint32_t minV, std::uint32_t maxV)
std::optional< NetClock::time_point > getCloseTimeBySeq(LedgerIndex ledgerIndex)
std::string getCompleteLedgers()
std::shared_ptr< Ledger const > getValidatedLedger()
void fetchForHistory(std::uint32_t missing, bool &progress, InboundLedger::Reason reason, std::unique_lock< std::recursive_mutex > &)
bool isValidated(ReadView const &ledger)
void fixMismatch(ReadView const &ledger)
void makeFetchPack(std::weak_ptr< Peer > const &wPeer, std::shared_ptr< protocol::TMGetObjectByHash > const &request, uint256 haveLedgerHash, UptimeClock::time_point uptime)
LedgerIndex getValidLedgerIndex()
LedgerHolder validLedger_
CanonicalTXSet heldTransactions_
bool const standalone_
std::shared_ptr< Ledger const > pathLedger_
std::shared_ptr< Ledger const > pubLedger_
std::shared_ptr< ReadView const > getPublishedLedger()
std::uint32_t const fetchDepth_
std::recursive_mutex mutex_
std::pair< uint256, LedgerIndex > lastValidLedger_
LedgerHistory ledgerHistory_
std::optional< LedgerHash > walkHashBySeq(std::uint32_t index, InboundLedger::Reason reason)
Walk to a ledger's hash using the skip list.
LedgerMaster(Application &app, Stopwatch &stopwatch, beast::insight::Collector::ptr const &collector, beast::Journal journal)
std::chrono::seconds getPublishedLedgerAge()
std::optional< uint256 > txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex)
bool canBeCurrent(std::shared_ptr< Ledger const > const &ledger)
Check the sequence number and parent close time of a ledger against our clock and last validated ledg...
LedgerHolder closedLedger_
void addFetchPack(uint256 const &hash, std::shared_ptr< Blob > data)
bool getFullValidatedRange(std::uint32_t &minVal, std::uint32_t &maxVal)
std::optional< LedgerHash > getLedgerHashForHistory(LedgerIndex index, InboundLedger::Reason reason)
RangeSet< std::uint32_t > completeLedgers_
void checkAccept(std::shared_ptr< Ledger const > const &ledger)
std::uint32_t const ledgerHistorySize_
std::atomic< std::uint32_t > validLedgerSign_
void doAdvance(std::unique_lock< std::recursive_mutex > &)
void addHeldTransaction(std::shared_ptr< Transaction > const &trans)
std::shared_ptr< ReadView const > getCurrentLedger()
std::recursive_mutex completeLock_
void takeReplay(std::unique_ptr< LedgerReplay > replay)
std::unique_ptr< LedgerReplay > releaseReplay()
std::shared_ptr< Ledger const > getLedgerByHash(uint256 const &hash)
std::uint32_t getEarliestFetch()
std::shared_ptr< Ledger const > histLedger_
TaggedCache< uint256, Blob > fetchPacks_
void failedSave(std::uint32_t seq, uint256 const &hash)
Application & app_
Represents a peer connection in the overlay.
A view into a ledger.
Definition ReadView.h:41
Rules controlling protocol behavior.
Definition Rules.h:40
Map/cache combination.
Definition TaggedCache.h:67
std::chrono::time_point< UptimeClock > time_point
Definition UptimeClock.h:24
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
beast::AbstractClock< std::chrono::steady_clock > Stopwatch
A clock for measuring elapsed time.
Definition chrono.h:90
bool isCurrent(ValidationParms const &p, NetClock::time_point now, NetClock::time_point signTime, NetClock::time_point seenTime)
Whether a validation is still current.
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:370
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition chrono.h:101
uint256 LedgerHash
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition RangeSet.h:54
BaseUInt< 256 > uint256
Definition base_uint.h:580
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector)
beast::insight::Hook hook
beast::insight::Gauge publishedLedgerAge
beast::insight::Gauge validatedLedgerAge