rippled
Loading...
Searching...
No Matches
LedgerMaster.h
1#ifndef XRPL_APP_LEDGER_LEDGERMASTER_H_INCLUDED
2#define XRPL_APP_LEDGER_LEDGERMASTER_H_INCLUDED
3
4#include <xrpld/app/ledger/AbstractFetchPackContainer.h>
5#include <xrpld/app/ledger/InboundLedgers.h>
6#include <xrpld/app/ledger/Ledger.h>
7#include <xrpld/app/ledger/LedgerHistory.h>
8#include <xrpld/app/ledger/LedgerHolder.h>
9#include <xrpld/app/ledger/LedgerReplay.h>
10#include <xrpld/app/main/Application.h>
11#include <xrpld/app/misc/CanonicalTXSet.h>
12
13#include <xrpl/basics/RangeSet.h>
14#include <xrpl/basics/UptimeClock.h>
15#include <xrpl/basics/chrono.h>
16#include <xrpl/beast/insight/Collector.h>
17#include <xrpl/protocol/Protocol.h>
18#include <xrpl/protocol/RippleLedgerHash.h>
19#include <xrpl/protocol/messages.h>
20
21#include <mutex>
22#include <optional>
23
24namespace ripple {
25
26class Peer;
27class Transaction;
28
29// Tracks the current ledger and any ledgers in the process of closing
30// Tracks ledger history
31// Tracks held transactions
33{
34public:
35 explicit LedgerMaster(
36 Application& app,
38 beast::insight::Collector::ptr const& collector,
39 beast::Journal journal);
40
41 virtual ~LedgerMaster() = default;
42
47
48 bool
49 isCompatible(ReadView const&, beast::Journal::Stream, char const* reason);
50
52 peekMutex();
53
54 // The current ledger is the ledger we believe new transactions should go in
57
58 // The finalized ledger is the last closed/accepted ledger
61 {
62 return mClosedLedger.get();
63 }
64
65 // The validated ledger is the last fully validated ledger.
68
69 // The Rules are in the last fully validated ledger if there is one.
70 Rules
72
73 // This is the last ledger we published to clients and can lag the validated
74 // ledger
77
82 bool
83 isCaughtUp(std::string& reason);
84
87
88 bool
90
91 void
94 bool isSynchronous,
95 bool isCurrent);
96
101 bool
103
104 void
105 switchLCL(std::shared_ptr<Ledger const> const& lastClosed);
106
107 void
108 failedSave(std::uint32_t seq, uint256 const& hash);
109
112
118 void
120
128
131 uint256
133
137
147 std::uint32_t index,
148 std::shared_ptr<ReadView const> const& referenceLedger,
149 InboundLedger::Reason reason);
150
153
155 getLedgerByHash(uint256 const& hash);
156
157 void
159
161 getCloseTimeBySeq(LedgerIndex ledgerIndex);
162
164 getCloseTimeByHash(LedgerHash const& ledgerHash, LedgerIndex ledgerIndex);
165
166 void
168 void
169 fixMismatch(ReadView const& ledger);
170
171 bool
173 void
175 bool
176 isValidated(ReadView const& ledger);
177 bool
179 bool
181
182 void
183 sweep();
184 float
186
187 void
189 void
190 checkAccept(uint256 const& hash, std::uint32_t seq);
191 void
193 std::shared_ptr<Ledger const> const& ledger,
194 uint256 const& consensusHash,
195 Json::Value consensus);
196
197 void
199
200 void
201 tryAdvance();
202 bool
203 newPathRequest(); // Returns true if path request successfully placed.
204 bool
206 bool
207 newOrderBookDB(); // Returns true if able to fulfill request.
208
209 bool
210 fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash);
211
212 void
214
215 void
217
218 // ledger replay
219 void
223
224 // Fetch Packs
225 void
226 gotFetchPack(bool progress, std::uint32_t seq);
227
228 void
229 addFetchPack(uint256 const& hash, std::shared_ptr<Blob> data);
230
232 getFetchPack(uint256 const& hash) override;
233
234 void
236 std::weak_ptr<Peer> const& wPeer,
238 uint256 haveLedgerHash,
240
242 getFetchPackCacheSize() const;
243
245 bool
247 {
248 return !mValidLedger.empty();
249 }
250
251 // Returns the minimum ledger sequence in SQL database, if any.
253 minSqlSeq();
254
255 // Iff a txn exists at the specified ledger and offset then return its txnid
257 txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex);
258
259private:
260 void
262 void
264
265 void
267
268 void
270
273
276 void
278 std::uint32_t missing,
279 bool& progress,
282 // Try to publish ledgers, acquire missing ledgers. Always called with
283 // m_mutex locked. The passed lock is a reminder to callers.
284 void
286
289
290 void
291 updatePaths();
292
293 // Returns true if work started. Always called with m_mutex locked.
294 // The passed lock is a reminder to callers.
295 bool
297
300
302
303 // The ledger that most recently closed.
305
306 // The highest-sequence ledger we have fully accepted.
308
309 // The last ledger we have published.
311
312 // The last ledger we did pathfinding against.
314
315 // The last ledger we handled fetching history
317
318 // Fully validated ledger, whether or not we have the ledger resident.
320
322
324
325 // A set of transactions to replay during the next close
327
330
331 // Publish thread is running.
332 bool mAdvanceThread{false};
333
334 // Publish thread has work to do.
335 bool mAdvanceWork{false};
337
338 int mPathFindThread{0}; // Pathfinder jobs dispatched
340
342 ATOMIC_FLAG_INIT; // GotFetchPack jobs dispatched
343
349
350 // The server is in standalone mode
351 bool const standalone_;
352
353 // How many ledgers before the current ledger do we allow peers to request?
355
356 // How much history do we want to keep
358
360
362
364
365 // Try to keep a validator from switching from test to live network
366 // without first wiping the database.
368
369 // Time that the previous upgrade warning was issued.
371
372private:
373 struct Stats
374 {
375 template <class Handler>
377 Handler const& handler,
378 beast::insight::Collector::ptr const& collector)
379 : hook(collector->make_hook(handler))
381 collector->make_gauge("LedgerMaster", "Validated_Ledger_Age"))
383 collector->make_gauge("LedgerMaster", "Published_Ledger_Age"))
384 {
385 }
386
390 };
391
393
394private:
395 void
402};
403
404} // namespace ripple
405
406#endif
Represents a JSON value.
Definition json_value.h:130
Provide a light-weight way to check active() before string formatting.
Definition Journal.h:186
A generic endpoint for log messages.
Definition Journal.h:41
typename Clock::time_point time_point
A metric for measuring an integral value.
Definition Gauge.h:21
void set(value_type value) const
Set the value on the gauge.
Definition Gauge.h:49
A reference to a handler for performing polled collection.
Definition Hook.h:13
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::shared_ptr< Ledger const > get()
bool haveLedger(std::uint32_t seq)
std::shared_ptr< Ledger const > getValidatedLedger()
void clearLedgerCachePrior(LedgerIndex seq)
RangeSet< std::uint32_t > mCompleteLedgers
void setBuildingLedger(LedgerIndex index)
std::unique_ptr< LedgerReplay > releaseReplay()
void failedSave(std::uint32_t seq, uint256 const &hash)
void takeReplay(std::unique_ptr< LedgerReplay > replay)
std::uint32_t const ledger_history_
void addHeldTransaction(std::shared_ptr< Transaction > const &trans)
void checkAccept(std::shared_ptr< Ledger const > const &ledger)
std::optional< NetClock::time_point > getCloseTimeByHash(LedgerHash const &ledgerHash, LedgerIndex ledgerIndex)
std::size_t getNeededValidations()
Determines how many validations are needed to fully validate a ledger.
std::unique_ptr< LedgerReplay > replayData
bool haveValidated()
Whether we have ever fully validated a ledger.
void setLedgerRangePresent(std::uint32_t minV, std::uint32_t maxV)
TimeKeeper::time_point upgradeWarningPrevTime_
LedgerHistory mLedgerHistory
std::optional< NetClock::time_point > getCloseTimeBySeq(LedgerIndex ledgerIndex)
void fixMismatch(ReadView const &ledger)
std::atomic< LedgerIndex > mPubLedgerSeq
void clearPriorLedgers(LedgerIndex seq)
std::shared_ptr< Ledger const > mPubLedger
void makeFetchPack(std::weak_ptr< Peer > const &wPeer, std::shared_ptr< protocol::TMGetObjectByHash > const &request, uint256 haveLedgerHash, UptimeClock::time_point uptime)
std::atomic< LedgerIndex > mBuildingLedgerSeq
std::shared_ptr< ReadView const > getCurrentLedger()
void tryFill(std::shared_ptr< Ledger const > ledger)
std::uint32_t const fetch_depth_
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...
bool isValidated(ReadView const &ledger)
std::uint32_t getEarliestFetch()
std::recursive_mutex m_mutex
std::optional< LedgerHash > walkHashBySeq(std::uint32_t index, InboundLedger::Reason reason)
Walk to a ledger's hash using the skip list.
uint256 getHashBySeq(std::uint32_t index)
Get a ledger's hash by sequence number using the cache.
std::shared_ptr< STTx const > popAcctTransaction(std::shared_ptr< STTx const > const &tx)
Get the next transaction held for a particular account if any.
LedgerIndex const max_ledger_difference_
bool fixIndex(LedgerIndex ledgerIndex, LedgerHash const &ledgerHash)
TaggedCache< uint256, Blob > fetch_packs_
bool newPFWork(char const *name, std::unique_lock< std::recursive_mutex > &)
A thread needs to be dispatched to handle pathfinding work of some kind.
bool isCaughtUp(std::string &reason)
void setPubLedger(std::shared_ptr< Ledger const > const &l)
std::optional< uint256 > txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex)
beast::Journal m_journal
bool getValidatedRange(std::uint32_t &minVal, std::uint32_t &maxVal)
void clearLedger(std::uint32_t seq)
std::pair< uint256, LedgerIndex > mLastValidLedger
std::shared_ptr< Ledger const > getClosedLedger()
std::optional< LedgerIndex > minSqlSeq()
void setFullLedger(std::shared_ptr< Ledger const > const &ledger, bool isSynchronous, bool isCurrent)
std::atomic< std::uint32_t > mValidLedgerSign
virtual ~LedgerMaster()=default
CanonicalTXSet mHeldTransactions
std::uint32_t const ledger_fetch_size_
void applyHeldTransactions()
Apply held transactions to the open ledger This is normally called as we close the ledger.
std::chrono::seconds getPublishedLedgerAge()
std::shared_ptr< Ledger const > mHistLedger
std::recursive_mutex mCompleteLock
std::string getCompleteLedgers()
std::atomic< LedgerIndex > mValidLedgerSeq
std::size_t getFetchPackCacheSize() const
bool getFullValidatedRange(std::uint32_t &minVal, std::uint32_t &maxVal)
std::optional< Blob > getFetchPack(uint256 const &hash) override
Retrieves partial ledger data of the coresponding hash from peers.
void gotFetchPack(bool progress, std::uint32_t seq)
std::recursive_mutex & peekMutex()
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::shared_ptr< Ledger const > mPathLedger
void setValidLedger(std::shared_ptr< Ledger const > const &l)
std::optional< LedgerHash > getLedgerHashForHistory(LedgerIndex index, InboundLedger::Reason reason)
void addFetchPack(uint256 const &hash, std::shared_ptr< Blob > data)
std::atomic< std::uint32_t > mPubLedgerClose
void switchLCL(std::shared_ptr< Ledger const > const &lastClosed)
LedgerHolder mValidLedger
std::shared_ptr< ReadView const > getPublishedLedger()
std::atomic_flag mGotFetchPackThread
void doAdvance(std::unique_lock< std::recursive_mutex > &)
LedgerHolder mClosedLedger
bool storeLedger(std::shared_ptr< Ledger const > ledger)
std::vector< std::shared_ptr< Ledger const > > findNewLedgersToPublish(std::unique_lock< std::recursive_mutex > &)
LedgerIndex getCurrentLedgerIndex()
bool isCompatible(ReadView const &, beast::Journal::Stream, char const *reason)
std::shared_ptr< Ledger const > getLedgerBySeq(std::uint32_t index)
void fetchForHistory(std::uint32_t missing, bool &progress, InboundLedger::Reason reason, std::unique_lock< std::recursive_mutex > &)
std::shared_ptr< Ledger const > getLedgerByHash(uint256 const &hash)
std::uint32_t fetch_seq_
LedgerIndex getValidLedgerIndex()
std::chrono::seconds getValidatedLedgerAge()
A view into a ledger.
Definition ReadView.h:32
Rules controlling protocol behavior.
Definition Rules.h:19
Map/cache combination.
Definition TaggedCache.h:43
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
base_uint< 256 > uint256
Definition base_uint.h:539
bool isCurrent(ValidationParms const &p, NetClock::time_point now, NetClock::time_point signTime, NetClock::time_point seenTime)
Whether a validation is still current.
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition RangeSet.h:51
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition chrono.h:100
beast::insight::Gauge publishedLedgerAge
beast::insight::Gauge validatedLedgerAge
beast::insight::Hook hook
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector)