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/StringUtilities.h>
33#include <xrpl/basics/UptimeClock.h>
34#include <xrpl/basics/chrono.h>
35#include <xrpl/beast/insight/Collector.h>
36#include <xrpl/protocol/Protocol.h>
37#include <xrpl/protocol/RippleLedgerHash.h>
38#include <xrpl/protocol/STValidation.h>
39#include <xrpl/protocol/messages.h>
40#include <optional>
41
42#include <mutex>
43
44namespace ripple {
45
46class Peer;
47class Transaction;
48
49// Tracks the current ledger and any ledgers in the process of closing
50// Tracks ledger history
51// Tracks held transactions
53{
54public:
55 explicit LedgerMaster(
56 Application& app,
58 beast::insight::Collector::ptr const& collector,
59 beast::Journal journal);
60
61 virtual ~LedgerMaster() = default;
62
67
68 bool
69 isCompatible(ReadView const&, beast::Journal::Stream, char const* reason);
70
72 peekMutex();
73
74 // The current ledger is the ledger we believe new transactions should go in
77
78 // The finalized ledger is the last closed/accepted ledger
81 {
82 return mClosedLedger.get();
83 }
84
85 // The validated ledger is the last fully validated ledger.
88
89 // The Rules are in the last fully validated ledger if there is one.
90 Rules
92
93 // This is the last ledger we published to clients and can lag the validated
94 // ledger
97
102 bool
103 isCaughtUp(std::string& reason);
104
107
108 bool
110
111 void
113 std::shared_ptr<Ledger const> const& ledger,
114 bool isSynchronous,
115 bool isCurrent);
116
121 bool
123
124 void
125 switchLCL(std::shared_ptr<Ledger const> const& lastClosed);
126
127 void
128 failedSave(std::uint32_t seq, uint256 const& hash);
129
132
138 void
140
148
151 uint256
153
157
167 std::uint32_t index,
168 std::shared_ptr<ReadView const> const& referenceLedger,
169 InboundLedger::Reason reason);
170
173
175 getLedgerByHash(uint256 const& hash);
176
177 void
179
181 getCloseTimeBySeq(LedgerIndex ledgerIndex);
182
184 getCloseTimeByHash(LedgerHash const& ledgerHash, LedgerIndex ledgerIndex);
185
186 void
188 void
189 fixMismatch(ReadView const& ledger);
190
191 bool
193 void
195 bool
196 isValidated(ReadView const& ledger);
197 bool
199 bool
201
202 void
203 sweep();
204 float
206
207 void
209 void
210 checkAccept(uint256 const& hash, std::uint32_t seq);
211 void
213 std::shared_ptr<Ledger const> const& ledger,
214 uint256 const& consensusHash,
215 Json::Value consensus);
216
217 void
219
220 void
221 tryAdvance();
222 bool
223 newPathRequest(); // Returns true if path request successfully placed.
224 bool
226 bool
227 newOrderBookDB(); // Returns true if able to fulfill request.
228
229 bool
230 fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash);
231
232 void
234
235 void
237
238 // ledger replay
239 void
243
244 // Fetch Packs
245 void
246 gotFetchPack(bool progress, std::uint32_t seq);
247
248 void
249 addFetchPack(uint256 const& hash, std::shared_ptr<Blob> data);
250
252 getFetchPack(uint256 const& hash) override;
253
254 void
256 std::weak_ptr<Peer> const& wPeer,
258 uint256 haveLedgerHash,
260
262 getFetchPackCacheSize() const;
263
265 bool
267 {
268 return !mValidLedger.empty();
269 }
270
271 // Returns the minimum ledger sequence in SQL database, if any.
273 minSqlSeq();
274
275 // Iff a txn exists at the specified ledger and offset then return its txnid
277 txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex);
278
279private:
280 void
282 void
284
285 void
287
288 void
290
293
296 void
298 std::uint32_t missing,
299 bool& progress,
302 // Try to publish ledgers, acquire missing ledgers. Always called with
303 // m_mutex locked. The passed lock is a reminder to callers.
304 void
306
309
310 void
311 updatePaths();
312
313 // Returns true if work started. Always called with m_mutex locked.
314 // The passed lock is a reminder to callers.
315 bool
317
320
322
323 // The ledger that most recently closed.
325
326 // The highest-sequence ledger we have fully accepted.
328
329 // The last ledger we have published.
331
332 // The last ledger we did pathfinding against.
334
335 // The last ledger we handled fetching history
337
338 // Fully validated ledger, whether or not we have the ledger resident.
340
342
344
345 // A set of transactions to replay during the next close
347
350
351 // Publish thread is running.
352 bool mAdvanceThread{false};
353
354 // Publish thread has work to do.
355 bool mAdvanceWork{false};
357
358 int mPathFindThread{0}; // Pathfinder jobs dispatched
360
362 ATOMIC_FLAG_INIT; // GotFetchPack jobs dispatched
363
369
370 // The server is in standalone mode
371 bool const standalone_;
372
373 // How many ledgers before the current ledger do we allow peers to request?
375
376 // How much history do we want to keep
378
380
382
384
385 // Try to keep a validator from switching from test to live network
386 // without first wiping the database.
388
389 // Time that the previous upgrade warning was issued.
391
392private:
393 struct Stats
394 {
395 template <class Handler>
397 Handler const& handler,
398 beast::insight::Collector::ptr const& collector)
399 : hook(collector->make_hook(handler))
401 collector->make_gauge("LedgerMaster", "Validated_Ledger_Age"))
403 collector->make_gauge("LedgerMaster", "Published_Ledger_Age"))
404 {
405 }
406
410 };
411
413
414private:
415 void
417 {
421 }
422};
423
424} // namespace ripple
425
426#endif
Represents a JSON value.
Definition: json_value.h:147
Provide a light-weight way to check active() before string formatting.
Definition: Journal.h:194
A generic endpoint for log messages.
Definition: Journal.h:59
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:37
Hold a ledger in a thread-safe way.
Definition: LedgerHolder.h:40
std::shared_ptr< Ledger const > get()
Definition: LedgerHolder.h:56
bool haveLedger(std::uint32_t seq)
std::shared_ptr< Ledger const > getValidatedLedger()
void clearLedgerCachePrior(LedgerIndex seq)
RangeSet< std::uint32_t > mCompleteLedgers
Definition: LedgerMaster.h:349
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:377
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:346
bool haveValidated()
Whether we have ever fully validated a ledger.
Definition: LedgerMaster.h:266
void setLedgerRangePresent(std::uint32_t minV, std::uint32_t maxV)
Application & app_
Definition: LedgerMaster.h:318
TimeKeeper::time_point upgradeWarningPrevTime_
Definition: LedgerMaster.h:390
LedgerHistory mLedgerHistory
Definition: LedgerMaster.h:341
std::optional< NetClock::time_point > getCloseTimeBySeq(LedgerIndex ledgerIndex)
void fixMismatch(ReadView const &ledger)
std::atomic< LedgerIndex > mPubLedgerSeq
Definition: LedgerMaster.h:365
void clearPriorLedgers(LedgerIndex seq)
std::shared_ptr< Ledger const > mPubLedger
Definition: LedgerMaster.h:330
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:368
std::shared_ptr< ReadView const > getCurrentLedger()
void tryFill(std::shared_ptr< Ledger const > ledger)
std::uint32_t const fetch_depth_
Definition: LedgerMaster.h:374
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:321
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:387
bool fixIndex(LedgerIndex ledgerIndex, LedgerHash const &ledgerHash)
TaggedCache< uint256, Blob > fetch_packs_
Definition: LedgerMaster.h:381
bool const standalone_
Definition: LedgerMaster.h:371
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:319
bool getValidatedRange(std::uint32_t &minVal, std::uint32_t &maxVal)
void clearLedger(std::uint32_t seq)
std::pair< uint256, LedgerIndex > mLastValidLedger
Definition: LedgerMaster.h:339
std::shared_ptr< Ledger const > getClosedLedger()
Definition: LedgerMaster.h:80
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:366
virtual ~LedgerMaster()=default
CanonicalTXSet mHeldTransactions
Definition: LedgerMaster.h:343
std::uint32_t const ledger_fetch_size_
Definition: LedgerMaster.h:379
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:336
std::recursive_mutex mCompleteLock
Definition: LedgerMaster.h:348
std::string getCompleteLedgers()
std::atomic< LedgerIndex > mValidLedgerSeq
Definition: LedgerMaster.h:367
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:333
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:364
void switchLCL(std::shared_ptr< Ledger const > const &lastClosed)
LedgerHolder mValidLedger
Definition: LedgerMaster.h:327
std::shared_ptr< ReadView const > getPublishedLedger()
std::atomic_flag mGotFetchPackThread
Definition: LedgerMaster.h:361
void doAdvance(std::unique_lock< std::recursive_mutex > &)
LedgerHolder mClosedLedger
Definition: LedgerMaster.h:324
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:383
LedgerIndex getValidLedgerIndex()
std::chrono::seconds getValidatedLedgerAge()
A view into a ledger.
Definition: ReadView.h:55
Rules controlling protocol behavior.
Definition: Rules.h:35
Map/cache combination.
Definition: TaggedCache.h:57
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:557
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:120
beast::insight::Gauge publishedLedgerAge
Definition: LedgerMaster.h:409
beast::insight::Gauge validatedLedgerAge
Definition: LedgerMaster.h:408
beast::insight::Hook hook
Definition: LedgerMaster.h:407
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector)
Definition: LedgerMaster.h:396