rippled
Loading...
Searching...
No Matches
LedgerMaster.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_APP_LEDGER_LEDGERMASTER_H_INCLUDED
21#define RIPPLE_APP_LEDGER_LEDGERMASTER_H_INCLUDED
22
23#include <xrpld/app/ledger/AbstractFetchPackContainer.h>
24#include <xrpld/app/ledger/InboundLedgers.h>
25#include <xrpld/app/ledger/Ledger.h>
26#include <xrpld/app/ledger/LedgerHistory.h>
27#include <xrpld/app/ledger/LedgerHolder.h>
28#include <xrpld/app/ledger/LedgerReplay.h>
29#include <xrpld/app/main/Application.h>
30#include <xrpld/app/misc/CanonicalTXSet.h>
31#include <xrpl/basics/RangeSet.h>
32#include <xrpl/basics/UptimeClock.h>
33#include <xrpl/basics/chrono.h>
34#include <xrpl/beast/insight/Collector.h>
35#include <xrpl/protocol/Protocol.h>
36#include <xrpl/protocol/RippleLedgerHash.h>
37#include <xrpl/protocol/messages.h>
38
39#include <mutex>
40#include <optional>
41
42namespace ripple {
43
44class Peer;
45class Transaction;
46
47// Tracks the current ledger and any ledgers in the process of closing
48// Tracks ledger history
49// Tracks held transactions
51{
52public:
53 explicit LedgerMaster(
54 Application& app,
56 beast::insight::Collector::ptr const& collector,
57 beast::Journal journal);
58
59 virtual ~LedgerMaster() = default;
60
65
66 bool
67 isCompatible(ReadView const&, beast::Journal::Stream, char const* reason);
68
70 peekMutex();
71
72 // The current ledger is the ledger we believe new transactions should go in
75
76 // The finalized ledger is the last closed/accepted ledger
79 {
80 return mClosedLedger.get();
81 }
82
83 // The validated ledger is the last fully validated ledger.
86
87 // The Rules are in the last fully validated ledger if there is one.
88 Rules
90
91 // This is the last ledger we published to clients and can lag the validated
92 // ledger
95
100 bool
101 isCaughtUp(std::string& reason);
102
105
106 bool
108
109 void
111 std::shared_ptr<Ledger const> const& ledger,
112 bool isSynchronous,
113 bool isCurrent);
114
119 bool
121
122 void
123 switchLCL(std::shared_ptr<Ledger const> const& lastClosed);
124
125 void
126 failedSave(std::uint32_t seq, uint256 const& hash);
127
130
136 void
138
146
149 uint256
151
155
165 std::uint32_t index,
166 std::shared_ptr<ReadView const> const& referenceLedger,
167 InboundLedger::Reason reason);
168
171
173 getLedgerByHash(uint256 const& hash);
174
175 void
177
179 getCloseTimeBySeq(LedgerIndex ledgerIndex);
180
182 getCloseTimeByHash(LedgerHash const& ledgerHash, LedgerIndex ledgerIndex);
183
184 void
186 void
187 fixMismatch(ReadView const& ledger);
188
189 bool
191 void
193 bool
194 isValidated(ReadView const& ledger);
195 bool
197 bool
199
200 void
201 sweep();
202 float
204
205 void
207 void
208 checkAccept(uint256 const& hash, std::uint32_t seq);
209 void
211 std::shared_ptr<Ledger const> const& ledger,
212 uint256 const& consensusHash,
213 Json::Value consensus);
214
215 void
217
218 void
219 tryAdvance();
220 bool
221 newPathRequest(); // Returns true if path request successfully placed.
222 bool
224 bool
225 newOrderBookDB(); // Returns true if able to fulfill request.
226
227 bool
228 fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash);
229
230 void
232
233 void
235
236 // ledger replay
237 void
241
242 // Fetch Packs
243 void
244 gotFetchPack(bool progress, std::uint32_t seq);
245
246 void
247 addFetchPack(uint256 const& hash, std::shared_ptr<Blob> data);
248
250 getFetchPack(uint256 const& hash) override;
251
252 void
254 std::weak_ptr<Peer> const& wPeer,
256 uint256 haveLedgerHash,
258
260 getFetchPackCacheSize() const;
261
263 bool
265 {
266 return !mValidLedger.empty();
267 }
268
269 // Returns the minimum ledger sequence in SQL database, if any.
271 minSqlSeq();
272
273 // Iff a txn exists at the specified ledger and offset then return its txnid
275 txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex);
276
277private:
278 void
280 void
282
283 void
285
286 void
288
291
294 void
296 std::uint32_t missing,
297 bool& progress,
300 // Try to publish ledgers, acquire missing ledgers. Always called with
301 // m_mutex locked. The passed lock is a reminder to callers.
302 void
304
307
308 void
309 updatePaths();
310
311 // Returns true if work started. Always called with m_mutex locked.
312 // The passed lock is a reminder to callers.
313 bool
315
318
320
321 // The ledger that most recently closed.
323
324 // The highest-sequence ledger we have fully accepted.
326
327 // The last ledger we have published.
329
330 // The last ledger we did pathfinding against.
332
333 // The last ledger we handled fetching history
335
336 // Fully validated ledger, whether or not we have the ledger resident.
338
340
342
343 // A set of transactions to replay during the next close
345
348
349 // Publish thread is running.
350 bool mAdvanceThread{false};
351
352 // Publish thread has work to do.
353 bool mAdvanceWork{false};
355
356 int mPathFindThread{0}; // Pathfinder jobs dispatched
358
360 ATOMIC_FLAG_INIT; // GotFetchPack jobs dispatched
361
367
368 // The server is in standalone mode
369 bool const standalone_;
370
371 // How many ledgers before the current ledger do we allow peers to request?
373
374 // How much history do we want to keep
376
378
380
382
383 // Try to keep a validator from switching from test to live network
384 // without first wiping the database.
386
387 // Time that the previous upgrade warning was issued.
389
390private:
391 struct Stats
392 {
393 template <class Handler>
395 Handler const& handler,
396 beast::insight::Collector::ptr const& collector)
397 : hook(collector->make_hook(handler))
399 collector->make_gauge("LedgerMaster", "Validated_Ledger_Age"))
401 collector->make_gauge("LedgerMaster", "Published_Ledger_Age"))
402 {
403 }
404
408 };
409
411
412private:
413 void
415 {
419 }
420};
421
422} // namespace ripple
423
424#endif
Represents a JSON value.
Definition: json_value.h:148
Provide a light-weight way to check active() before string formatting.
Definition: Journal.h:205
A generic endpoint for log messages.
Definition: Journal.h:60
typename Clock::time_point time_point
A metric for measuring an integral value.
Definition: Gauge.h:40
void set(value_type value) const
Set the value on the gauge.
Definition: Gauge.h:68
A reference to a handler for performing polled collection.
Definition: Hook.h:32
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.
Definition: LedgerHistory.h:36
Hold a ledger in a thread-safe way.
Definition: LedgerHolder.h:41
std::shared_ptr< Ledger const > get()
Definition: LedgerHolder.h:57
bool haveLedger(std::uint32_t seq)
std::shared_ptr< Ledger const > getValidatedLedger()
void clearLedgerCachePrior(LedgerIndex seq)
RangeSet< std::uint32_t > mCompleteLedgers
Definition: LedgerMaster.h:347
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_
Definition: LedgerMaster.h:375
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
Definition: LedgerMaster.h:344
bool haveValidated()
Whether we have ever fully validated a ledger.
Definition: LedgerMaster.h:264
void setLedgerRangePresent(std::uint32_t minV, std::uint32_t maxV)
Application & app_
Definition: LedgerMaster.h:316
TimeKeeper::time_point upgradeWarningPrevTime_
Definition: LedgerMaster.h:388
LedgerHistory mLedgerHistory
Definition: LedgerMaster.h:339
std::optional< NetClock::time_point > getCloseTimeBySeq(LedgerIndex ledgerIndex)
void fixMismatch(ReadView const &ledger)
std::atomic< LedgerIndex > mPubLedgerSeq
Definition: LedgerMaster.h:363
void clearPriorLedgers(LedgerIndex seq)
std::shared_ptr< Ledger const > mPubLedger
Definition: LedgerMaster.h:328
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
Definition: LedgerMaster.h:366
std::shared_ptr< ReadView const > getCurrentLedger()
void tryFill(std::shared_ptr< Ledger const > ledger)
std::uint32_t const fetch_depth_
Definition: LedgerMaster.h:372
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
Definition: LedgerMaster.h:319
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_
Definition: LedgerMaster.h:385
bool fixIndex(LedgerIndex ledgerIndex, LedgerHash const &ledgerHash)
TaggedCache< uint256, Blob > fetch_packs_
Definition: LedgerMaster.h:379
bool const standalone_
Definition: LedgerMaster.h:369
bool isCaughtUp(std::string &reason)
void setPubLedger(std::shared_ptr< Ledger const > const &l)
bool newPFWork(const char *name, std::unique_lock< std::recursive_mutex > &)
A thread needs to be dispatched to handle pathfinding work of some kind.
std::optional< uint256 > txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex)
beast::Journal m_journal
Definition: LedgerMaster.h:317
bool getValidatedRange(std::uint32_t &minVal, std::uint32_t &maxVal)
void clearLedger(std::uint32_t seq)
std::pair< uint256, LedgerIndex > mLastValidLedger
Definition: LedgerMaster.h:337
std::shared_ptr< Ledger const > getClosedLedger()
Definition: LedgerMaster.h:78
std::optional< LedgerIndex > minSqlSeq()
void setFullLedger(std::shared_ptr< Ledger const > const &ledger, bool isSynchronous, bool isCurrent)
std::atomic< std::uint32_t > mValidLedgerSign
Definition: LedgerMaster.h:364
virtual ~LedgerMaster()=default
CanonicalTXSet mHeldTransactions
Definition: LedgerMaster.h:341
std::uint32_t const ledger_fetch_size_
Definition: LedgerMaster.h:377
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
Definition: LedgerMaster.h:334
std::recursive_mutex mCompleteLock
Definition: LedgerMaster.h:346
std::string getCompleteLedgers()
std::atomic< LedgerIndex > mValidLedgerSeq
Definition: LedgerMaster.h:365
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
Definition: LedgerMaster.h:331
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
Definition: LedgerMaster.h:362
void switchLCL(std::shared_ptr< Ledger const > const &lastClosed)
LedgerHolder mValidLedger
Definition: LedgerMaster.h:325
std::shared_ptr< ReadView const > getPublishedLedger()
std::atomic_flag mGotFetchPackThread
Definition: LedgerMaster.h:359
void doAdvance(std::unique_lock< std::recursive_mutex > &)
LedgerHolder mClosedLedger
Definition: LedgerMaster.h:322
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_
Definition: LedgerMaster.h:381
LedgerIndex getValidLedgerIndex()
std::chrono::seconds getValidatedLedgerAge()
A view into a ledger.
Definition: ReadView.h:51
Rules controlling protocol behavior.
Definition: Rules.h:35
Map/cache combination.
Definition: TaggedCache.h:58
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
base_uint< 256 > uint256
Definition: base_uint.h:558
bool isCurrent(ValidationParms const &p, NetClock::time_point now, NetClock::time_point signTime, NetClock::time_point seenTime)
Whether a validation is still current.
Definition: Validations.h:148
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition: RangeSet.h:70
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:119
beast::insight::Gauge publishedLedgerAge
Definition: LedgerMaster.h:407
beast::insight::Gauge validatedLedgerAge
Definition: LedgerMaster.h:406
beast::insight::Hook hook
Definition: LedgerMaster.h:405
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector)
Definition: LedgerMaster.h:394