rippled
Loading...
Searching...
No Matches
LedgerMaster.h
1#pragma once
2
3#include <xrpld/app/ledger/AbstractFetchPackContainer.h>
4#include <xrpld/app/ledger/InboundLedgers.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/RangeSet.h>
12#include <xrpl/basics/UptimeClock.h>
13#include <xrpl/basics/chrono.h>
14#include <xrpl/beast/insight/Collector.h>
15#include <xrpl/ledger/CanonicalTXSet.h>
16#include <xrpl/ledger/Ledger.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 xrpl {
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
92 setFullLedger(std::shared_ptr<Ledger const> const& ledger, bool isSynchronous, bool isCurrent);
93
98 bool
100
101 void
102 switchLCL(std::shared_ptr<Ledger const> const& lastClosed);
103
104 void
105 failedSave(std::uint32_t seq, uint256 const& hash);
106
109
115 void
117
125
128 uint256
130
134
144 std::uint32_t index,
145 std::shared_ptr<ReadView const> const& referenceLedger,
146 InboundLedger::Reason reason);
147
150
152 getLedgerByHash(uint256 const& hash);
153
154 void
156
158 getCloseTimeBySeq(LedgerIndex ledgerIndex);
159
161 getCloseTimeByHash(LedgerHash const& ledgerHash, LedgerIndex ledgerIndex);
162
163 void
165 void
166 fixMismatch(ReadView const& ledger);
167
168 bool
170 void
172 bool
173 isValidated(ReadView const& ledger);
174 bool
176 bool
178
179 void
180 sweep();
181 float
183
184 void
186 void
187 checkAccept(uint256 const& hash, std::uint32_t seq);
188 void
190 std::shared_ptr<Ledger const> const& ledger,
191 uint256 const& consensusHash,
192 Json::Value consensus);
193
194 void
196
197 void
198 tryAdvance();
199 bool
200 newPathRequest(); // Returns true if path request successfully placed.
201 bool
203 bool
204 newOrderBookDB(); // Returns true if able to fulfill request.
205
206 bool
207 fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash);
208
209 void
211
212 void
214
215 // ledger replay
216 void
220
221 // Fetch Packs
222 void
223 gotFetchPack(bool progress, std::uint32_t seq);
224
225 void
226 addFetchPack(uint256 const& hash, std::shared_ptr<Blob> data);
227
229 getFetchPack(uint256 const& hash) override;
230
231 void
233 std::weak_ptr<Peer> const& wPeer,
235 uint256 haveLedgerHash,
237
239 getFetchPackCacheSize() const;
240
242 bool
244 {
245 return !mValidLedger.empty();
246 }
247
248 // Returns the minimum ledger sequence in SQL database, if any.
250 minSqlSeq();
251
252 // Iff a txn exists at the specified ledger and offset then return its txnid
254 txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex);
255
256private:
257 void
259 void
261
262 void
264
265 void
267
270
273 void
275 std::uint32_t missing,
276 bool& progress,
279 // Try to publish ledgers, acquire missing ledgers. Always called with
280 // m_mutex locked. The passed lock is a reminder to callers.
281 void
283
286
287 void
288 updatePaths();
289
290 // Returns true if work started. Always called with m_mutex locked.
291 // The passed lock is a reminder to callers.
292 bool
294
297
299
300 // The ledger that most recently closed.
302
303 // The highest-sequence ledger we have fully accepted.
305
306 // The last ledger we have published.
308
309 // The last ledger we did pathfinding against.
311
312 // The last ledger we handled fetching history
314
315 // Fully validated ledger, whether or not we have the ledger resident.
317
319
321
322 // A set of transactions to replay during the next close
324
327
328 // Publish thread is running.
329 bool mAdvanceThread{false};
330
331 // Publish thread has work to do.
332 bool mAdvanceWork{false};
334
335 int mPathFindThread{0}; // Pathfinder jobs dispatched
337
338 std::atomic_flag mGotFetchPackThread = ATOMIC_FLAG_INIT; // GotFetchPack jobs dispatched
339
345
346 // The server is in standalone mode
347 bool const standalone_;
348
349 // How many ledgers before the current ledger do we allow peers to request?
351
352 // How much history do we want to keep
354
356
358
360
361 // Try to keep a validator from switching from test to live network
362 // without first wiping the database.
364
365 // Time that the previous upgrade warning was issued.
367
368private:
369 struct Stats
370 {
371 template <class Handler>
372 Stats(Handler const& handler, beast::insight::Collector::ptr const& collector)
373 : hook(collector->make_hook(handler))
374 , validatedLedgerAge(collector->make_gauge("LedgerMaster", "Validated_Ledger_Age"))
375 , publishedLedgerAge(collector->make_gauge("LedgerMaster", "Published_Ledger_Age"))
376 {
377 }
378
382 };
383
385
386private:
387 void
394};
395
396} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Provide a light-weight way to check active() before string formatting.
Definition Journal.h:180
A generic endpoint for log messages.
Definition Journal.h:40
typename Clock::time_point time_point
A metric for measuring an integral value.
Definition Gauge.h:20
void set(value_type value) const
Set the value on the gauge.
Definition Gauge.h:48
A reference to a handler for performing polled collection.
Definition Hook.h:12
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()
std::optional< LedgerIndex > minSqlSeq()
std::atomic< LedgerIndex > mValidLedgerSeq
void consensusBuilt(std::shared_ptr< Ledger const > const &ledger, uint256 const &consensusHash, Json::Value consensus)
Report that the consensus process built a particular ledger.
bool haveLedger(std::uint32_t seq)
TaggedCache< uint256, Blob > fetch_packs_
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::uint32_t fetch_seq_
std::chrono::seconds getValidatedLedgerAge()
TimeKeeper::time_point upgradeWarningPrevTime_
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)
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)
void clearPriorLedgers(LedgerIndex seq)
LedgerIndex const max_ledger_difference_
CanonicalTXSet mHeldTransactions
std::uint32_t const ledger_fetch_size_
void setBuildingLedger(LedgerIndex index)
std::pair< uint256, LedgerIndex > mLastValidLedger
bool isCaughtUp(std::string &reason)
std::size_t getFetchPackCacheSize() const
std::optional< Blob > getFetchPack(uint256 const &hash) override
Retrieves partial ledger data of the corresponding hash from peers.
std::vector< std::shared_ptr< Ledger const > > findNewLedgersToPublish(std::unique_lock< std::recursive_mutex > &)
std::atomic< std::uint32_t > mPubLedgerClose
std::optional< NetClock::time_point > getCloseTimeByHash(LedgerHash const &ledgerHash, LedgerIndex ledgerIndex)
std::uint32_t const fetch_depth_
std::atomic< LedgerIndex > mPubLedgerSeq
void clearLedger(std::uint32_t seq)
void clearLedgerCachePrior(LedgerIndex seq)
std::atomic_flag mGotFetchPackThread
uint256 getHashBySeq(std::uint32_t index)
Get a ledger's hash by sequence number using the cache.
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()
virtual ~LedgerMaster()=default
void fetchForHistory(std::uint32_t missing, bool &progress, InboundLedger::Reason reason, std::unique_lock< std::recursive_mutex > &)
std::uint32_t const ledger_history_
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()
std::shared_ptr< Ledger const > mPathLedger
bool const standalone_
std::shared_ptr< ReadView const > getPublishedLedger()
std::shared_ptr< Ledger const > mHistLedger
std::optional< LedgerHash > walkHashBySeq(std::uint32_t index, InboundLedger::Reason reason)
Walk to a ledger's hash using the skip list.
LedgerHistory mLedgerHistory
std::atomic< std::uint32_t > mValidLedgerSign
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...
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 > mCompleteLedgers
LedgerHolder mValidLedger
void checkAccept(std::shared_ptr< Ledger const > const &ledger)
LedgerHolder mClosedLedger
void doAdvance(std::unique_lock< std::recursive_mutex > &)
void addHeldTransaction(std::shared_ptr< Transaction > const &trans)
std::shared_ptr< ReadView const > getCurrentLedger()
beast::Journal m_journal
void takeReplay(std::unique_ptr< LedgerReplay > replay)
std::unique_ptr< LedgerReplay > replayData
std::unique_ptr< LedgerReplay > releaseReplay()
std::shared_ptr< Ledger const > getLedgerByHash(uint256 const &hash)
std::uint32_t getEarliestFetch()
std::recursive_mutex m_mutex
std::shared_ptr< Ledger const > mPubLedger
void failedSave(std::uint32_t seq, uint256 const &hash)
Application & app_
std::atomic< LedgerIndex > mBuildingLedgerSeq
std::recursive_mutex mCompleteLock
A view into a ledger.
Definition ReadView.h:31
Rules controlling protocol behavior.
Definition Rules.h:18
Map/cache combination.
Definition TaggedCache.h:42
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool isCurrent(ValidationParms const &p, NetClock::time_point now, NetClock::time_point signTime, NetClock::time_point seenTime)
Whether a validation is still current.
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition chrono.h:94
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition RangeSet.h:50
base_uint< 256 > uint256
Definition base_uint.h:531
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector)
beast::insight::Hook hook
beast::insight::Gauge publishedLedgerAge
beast::insight::Gauge validatedLedgerAge