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
32#include <xrpl/basics/RangeSet.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/messages.h>
39
40#include <mutex>
41#include <optional>
42
43namespace ripple {
44
45class Peer;
46class Transaction;
47
48// Tracks the current ledger and any ledgers in the process of closing
49// Tracks ledger history
50// Tracks held transactions
52{
53public:
54 explicit LedgerMaster(
55 Application& app,
57 beast::insight::Collector::ptr const& collector,
58 beast::Journal journal);
59
60 virtual ~LedgerMaster() = default;
61
66
67 bool
68 isCompatible(ReadView const&, beast::Journal::Stream, char const* reason);
69
71 peekMutex();
72
73 // The current ledger is the ledger we believe new transactions should go in
76
77 // The finalized ledger is the last closed/accepted ledger
80 {
81 return mClosedLedger.get();
82 }
83
84 // The validated ledger is the last fully validated ledger.
87
88 // The Rules are in the last fully validated ledger if there is one.
89 Rules
91
92 // This is the last ledger we published to clients and can lag the validated
93 // ledger
96
101 bool
102 isCaughtUp(std::string& reason);
103
106
107 bool
109
110 void
112 std::shared_ptr<Ledger const> const& ledger,
113 bool isSynchronous,
114 bool isCurrent);
115
120 bool
122
123 void
124 switchLCL(std::shared_ptr<Ledger const> const& lastClosed);
125
126 void
127 failedSave(std::uint32_t seq, uint256 const& hash);
128
131
137 void
139
147
150 uint256
152
156
166 std::uint32_t index,
167 std::shared_ptr<ReadView const> const& referenceLedger,
168 InboundLedger::Reason reason);
169
172
174 getLedgerByHash(uint256 const& hash);
175
176 void
178
180 getCloseTimeBySeq(LedgerIndex ledgerIndex);
181
183 getCloseTimeByHash(LedgerHash const& ledgerHash, LedgerIndex ledgerIndex);
184
185 void
187 void
188 fixMismatch(ReadView const& ledger);
189
190 bool
192 void
194 bool
195 isValidated(ReadView const& ledger);
196 bool
198 bool
200
201 void
202 sweep();
203 float
205
206 void
208 void
209 checkAccept(uint256 const& hash, std::uint32_t seq);
210 void
212 std::shared_ptr<Ledger const> const& ledger,
213 uint256 const& consensusHash,
214 Json::Value consensus);
215
216 void
218
219 void
220 tryAdvance();
221 bool
222 newPathRequest(); // Returns true if path request successfully placed.
223 bool
225 bool
226 newOrderBookDB(); // Returns true if able to fulfill request.
227
228 bool
229 fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash);
230
231 void
233
234 void
236
237 // ledger replay
238 void
242
243 // Fetch Packs
244 void
245 gotFetchPack(bool progress, std::uint32_t seq);
246
247 void
248 addFetchPack(uint256 const& hash, std::shared_ptr<Blob> data);
249
251 getFetchPack(uint256 const& hash) override;
252
253 void
255 std::weak_ptr<Peer> const& wPeer,
257 uint256 haveLedgerHash,
259
261 getFetchPackCacheSize() const;
262
264 bool
266 {
267 return !mValidLedger.empty();
268 }
269
270 // Returns the minimum ledger sequence in SQL database, if any.
272 minSqlSeq();
273
274 // Iff a txn exists at the specified ledger and offset then return its txnid
276 txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex);
277
278private:
279 void
281 void
283
284 void
286
287 void
289
292
295 void
297 std::uint32_t missing,
298 bool& progress,
301 // Try to publish ledgers, acquire missing ledgers. Always called with
302 // m_mutex locked. The passed lock is a reminder to callers.
303 void
305
308
309 void
310 updatePaths();
311
312 // Returns true if work started. Always called with m_mutex locked.
313 // The passed lock is a reminder to callers.
314 bool
316
319
321
322 // The ledger that most recently closed.
324
325 // The highest-sequence ledger we have fully accepted.
327
328 // The last ledger we have published.
330
331 // The last ledger we did pathfinding against.
333
334 // The last ledger we handled fetching history
336
337 // Fully validated ledger, whether or not we have the ledger resident.
339
341
343
344 // A set of transactions to replay during the next close
346
349
350 // Publish thread is running.
351 bool mAdvanceThread{false};
352
353 // Publish thread has work to do.
354 bool mAdvanceWork{false};
356
357 int mPathFindThread{0}; // Pathfinder jobs dispatched
359
361 ATOMIC_FLAG_INIT; // GotFetchPack jobs dispatched
362
368
369 // The server is in standalone mode
370 bool const standalone_;
371
372 // How many ledgers before the current ledger do we allow peers to request?
374
375 // How much history do we want to keep
377
379
381
383
384 // Try to keep a validator from switching from test to live network
385 // without first wiping the database.
387
388 // Time that the previous upgrade warning was issued.
390
391private:
392 struct Stats
393 {
394 template <class Handler>
396 Handler const& handler,
397 beast::insight::Collector::ptr const& collector)
398 : hook(collector->make_hook(handler))
400 collector->make_gauge("LedgerMaster", "Validated_Ledger_Age"))
402 collector->make_gauge("LedgerMaster", "Published_Ledger_Age"))
403 {
404 }
405
409 };
410
412
413private:
414 void
421};
422
423} // namespace ripple
424
425#endif
Represents a JSON value.
Definition json_value.h:149
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.
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:52
Rules controlling protocol behavior.
Definition Rules.h:38
Map/cache combination.
Definition TaggedCache.h:62
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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.
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
beast::insight::Gauge validatedLedgerAge
beast::insight::Hook hook
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector)