rippled
Loading...
Searching...
No Matches
Ledger.h
1#ifndef XRPL_APP_LEDGER_LEDGER_H_INCLUDED
2#define XRPL_APP_LEDGER_LEDGER_H_INCLUDED
3
4#include <xrpld/core/Config.h>
5#include <xrpld/core/TimeKeeper.h>
6
7#include <xrpl/basics/CountedObject.h>
8#include <xrpl/beast/utility/Journal.h>
9#include <xrpl/ledger/CachedView.h>
10#include <xrpl/ledger/View.h>
11#include <xrpl/protocol/Indexes.h>
12#include <xrpl/protocol/STLedgerEntry.h>
13#include <xrpl/protocol/Serializer.h>
14#include <xrpl/protocol/TxMeta.h>
15#include <xrpl/shamap/SHAMap.h>
16
17namespace xrpl {
18
19class Application;
20class Job;
21class TransactionMaster;
22
23class SqliteStatement;
24
26{
27 explicit create_genesis_t() = default;
28};
30
57class Ledger final : public std::enable_shared_from_this<Ledger>,
59 public TxsRawView,
60 public CountedObject<Ledger>
61{
62public:
63 Ledger(Ledger const&) = delete;
64 Ledger&
65 operator=(Ledger const&) = delete;
66
67 Ledger(Ledger&&) = delete;
68 Ledger&
69 operator=(Ledger&&) = delete;
70
85 Ledger(
87 Config const& config,
88 std::vector<uint256> const& amendments,
89 Family& family);
90
91 Ledger(LedgerHeader const& info, Config const& config, Family& family);
92
97 Ledger(
98 LedgerHeader const& info,
99 bool& loaded,
100 bool acquire,
101 Config const& config,
102 Family& family,
104
111 Ledger(Ledger const& previous, NetClock::time_point closeTime);
112
113 // used for database ledgers
114 Ledger(
115 std::uint32_t ledgerSeq,
116 NetClock::time_point closeTime,
117 Config const& config,
118 Family& family);
119
120 ~Ledger() = default;
121
122 //
123 // ReadView
124 //
125
126 bool
127 open() const override
128 {
129 return false;
130 }
131
132 LedgerHeader const&
133 header() const override
134 {
135 return header_;
136 }
137
138 void
140 {
141 header_ = info;
142 }
143
144 Fees const&
145 fees() const override
146 {
147 return fees_;
148 }
149
150 Rules const&
151 rules() const override
152 {
153 return rules_;
154 }
155
156 bool
157 exists(Keylet const& k) const override;
158
159 bool
160 exists(uint256 const& key) const;
161
163 succ(uint256 const& key, std::optional<uint256> const& last = std::nullopt)
164 const override;
165
167 read(Keylet const& k) const override;
168
170 slesBegin() const override;
171
173 slesEnd() const override;
174
176 slesUpperBound(uint256 const& key) const override;
177
179 txsBegin() const override;
180
182 txsEnd() const override;
183
184 bool
185 txExists(uint256 const& key) const override;
186
187 tx_type
188 txRead(key_type const& key) const override;
189
190 //
191 // DigestAwareReadView
192 //
193
195 digest(key_type const& key) const override;
196
197 //
198 // RawView
199 //
200
201 void
202 rawErase(std::shared_ptr<SLE> const& sle) override;
203
204 void
205 rawInsert(std::shared_ptr<SLE> const& sle) override;
206
207 void
208 rawErase(uint256 const& key);
209
210 void
211 rawReplace(std::shared_ptr<SLE> const& sle) override;
212
213 void
214 rawDestroyXRP(XRPAmount const& fee) override
215 {
216 header_.drops -= fee;
217 }
218
219 //
220 // TxsRawView
221 //
222
223 void
225 uint256 const& key,
227 std::shared_ptr<Serializer const> const& metaData) override;
228
229 // Insert the transaction, and return the hash of the SHAMap leaf node
230 // holding the transaction. The hash can be used to fetch the transaction
231 // directly, instead of traversing the SHAMap
232 // @param key transaction ID
233 // @param txn transaction
234 // @param metaData transaction metadata
235 // @return hash of SHAMap leaf node that holds the transaction
236 uint256
238 uint256 const& key,
240 std::shared_ptr<Serializer const> const& metaData);
241
242 //--------------------------------------------------------------------------
243
244 void
246 {
247 header_.validated = true;
248 }
249
250 void
252 NetClock::time_point closeTime,
253 NetClock::duration closeResolution,
254 bool correctCloseTime);
255
256 void
257 setImmutable(bool rehash = true);
258
259 bool
261 {
262 return mImmutable;
263 }
264
265 /* Mark this ledger as "should be full".
266
267 "Full" is metadata property of the ledger, it indicates
268 that the local server wants all the corresponding nodes
269 in durable storage.
270
271 This is marked `const` because it reflects metadata
272 and not data that is in common with other nodes on the
273 network.
274 */
275 void
283
284 void
286 {
287 header_.drops = totDrops;
288 }
289
290 SHAMap const&
291 stateMap() const
292 {
293 return stateMap_;
294 }
295
296 SHAMap&
298 {
299 return stateMap_;
300 }
301
302 SHAMap const&
303 txMap() const
304 {
305 return txMap_;
306 }
307
308 SHAMap&
310 {
311 return txMap_;
312 }
313
314 // returns false on error
315 bool
316 addSLE(SLE const& sle);
317
318 //--------------------------------------------------------------------------
319
320 void
322
323 bool
324 walkLedger(beast::Journal j, bool parallel = false) const;
325
326 bool
327 assertSensible(beast::Journal ledgerJ) const;
328
329 void
330 invariants() const;
331 void
332 unshare() const;
333
340 negativeUNL() const;
341
348 validatorToDisable() const;
349
356 validatorToReEnable() const;
357
363 void
365
367 bool
368 isFlagLedger() const;
369
371 bool
372 isVotingLedger() const;
373
375 peek(Keylet const& k) const;
376
377private:
378 class sles_iter_impl;
379 class txs_iter_impl;
380
381 bool
382 setup();
383
384 void
385 defaultFees(Config const& config);
386
388
389 // A SHAMap containing the transactions associated with this ledger.
390 SHAMap mutable txMap_;
391
392 // A SHAMap containing the state objects for this ledger.
394
395 // Protects fee variables
397
402};
403
406
409bool
411
412//------------------------------------------------------------------------------
413//
414// API
415//
416//------------------------------------------------------------------------------
417
418extern bool
420 Application& app,
421 std::shared_ptr<Ledger const> const& ledger,
422 bool isSynchronous,
423 bool isCurrent);
424
426loadLedgerHelper(LedgerHeader const& sinfo, Application& app, bool acquire);
427
429loadByIndex(std::uint32_t ledgerIndex, Application& app, bool acquire = true);
430
432loadByHash(uint256 const& ledgerHash, Application& app, bool acquire = true);
433
434// Fetch the ledger with the highest sequence contained in the database
437
445deserializeTx(SHAMapItem const& item);
446
458
461
462} // namespace xrpl
463
464#endif
A generic endpoint for log messages.
Definition Journal.h:41
Wraps a DigestAwareReadView to provide caching.
Definition CachedView.h:137
Tracks the number of instances of an object.
ReadView that associates keys with digests.
Definition ReadView.h:236
Holds a ledger.
Definition Ledger.h:61
bool txExists(uint256 const &key) const override
Definition Ledger.cpp:468
std::unique_ptr< txs_type::iter_base > txsEnd() const override
Definition Ledger.cpp:462
Fees fees_
Definition Ledger.h:398
bool isFlagLedger() const
Returns true if the ledger is a flag ledger.
Definition Ledger.cpp:934
std::optional< digest_type > digest(key_type const &key) const override
Return the digest associated with the key.
Definition Ledger.cpp:488
void rawTxInsert(uint256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData) override
Definition Ledger.cpp:537
std::optional< PublicKey > validatorToDisable() const
get the to be disabled validator's master public key if any
Definition Ledger.cpp:708
void setLedgerInfo(LedgerHeader const &info)
Definition Ledger.h:139
uint256 rawTxInsertWithHash(uint256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData)
Definition Ledger.cpp:555
std::shared_ptr< SLE const > read(Keylet const &k) const override
Return the state item associated with a key.
Definition Ledger.cpp:416
void setValidated() const
Definition Ledger.h:245
void updateNegativeUNL()
update the Negative UNL ledger component.
Definition Ledger.cpp:738
bool assertSensible(beast::Journal ledgerJ) const
Definition Ledger.cpp:840
bool isVotingLedger() const
Returns true if the ledger directly precedes a flag ledger.
Definition Ledger.cpp:939
std::unique_ptr< sles_type::iter_base > slesBegin() const override
Definition Ledger.cpp:437
Rules rules_
Definition Ledger.h:399
void invariants() const
Definition Ledger.cpp:1038
hash_set< PublicKey > negativeUNL() const
get Negative UNL validators' master public keys
Definition Ledger.cpp:682
Ledger(Ledger &&)=delete
void rawInsert(std::shared_ptr< SLE > const &sle) override
Unconditionally insert a state item.
Definition Ledger.cpp:515
void rawReplace(std::shared_ptr< SLE > const &sle) override
Unconditionally replace a state item.
Definition Ledger.cpp:526
void setImmutable(bool rehash=true)
Definition Ledger.cpp:321
SHAMap const & stateMap() const
Definition Ledger.h:291
void updateSkipList()
Definition Ledger.cpp:866
SHAMap const & txMap() const
Definition Ledger.h:303
bool isImmutable() const
Definition Ledger.h:260
void defaultFees(Config const &config)
Definition Ledger.cpp:656
bool open() const override
Returns true if this reflects an open ledger.
Definition Ledger.h:127
void rawErase(std::shared_ptr< SLE > const &sle) override
Delete an existing state item.
Definition Ledger.cpp:501
LedgerHeader const & header() const override
Returns information about the ledger.
Definition Ledger.h:133
void setAccepted(NetClock::time_point closeTime, NetClock::duration closeResolution, bool correctCloseTime)
Definition Ledger.cpp:341
bool mImmutable
Definition Ledger.h:387
Ledger(Ledger const &)=delete
Ledger & operator=(Ledger const &)=delete
std::shared_ptr< SLE > peek(Keylet const &k) const
Definition Ledger.cpp:670
LedgerHeader header_
Definition Ledger.h:400
std::mutex mutex_
Definition Ledger.h:396
SHAMap & txMap()
Definition Ledger.h:309
void setTotalDrops(std::uint64_t totDrops)
Definition Ledger.h:285
~Ledger()=default
bool addSLE(SLE const &sle)
Definition Ledger.cpp:356
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
Definition Ledger.h:214
std::unique_ptr< txs_type::iter_base > txsBegin() const override
Definition Ledger.cpp:456
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition Ledger.cpp:443
SHAMap & stateMap()
Definition Ledger.h:297
SHAMap stateMap_
Definition Ledger.h:393
std::optional< uint256 > succ(uint256 const &key, std::optional< uint256 > const &last=std::nullopt) const override
Definition Ledger.cpp:405
Ledger & operator=(Ledger &&)=delete
SHAMap txMap_
Definition Ledger.h:390
Rules const & rules() const override
Returns the tx processing rules.
Definition Ledger.h:151
Fees const & fees() const override
Returns the fees for the base ledger.
Definition Ledger.h:145
beast::Journal j_
Definition Ledger.h:401
bool walkLedger(beast::Journal j, bool parallel=false) const
Definition Ledger.cpp:789
void unshare() const
Definition Ledger.cpp:1031
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition Ledger.cpp:449
std::optional< PublicKey > validatorToReEnable() const
get the to be re-enabled validator's master public key if any
Definition Ledger.cpp:723
tx_type txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition Ledger.cpp:474
bool setup()
Definition Ledger.cpp:577
void setFull() const
Definition Ledger.h:276
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition Ledger.cpp:392
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition ReadView.h:35
uint256 key_type
Definition ReadView.h:37
Rules controlling protocol behavior.
Definition Rules.h:19
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition SHAMap.h:78
void setLedgerSeq(std::uint32_t lseq)
Definition SHAMap.h:577
void setFull()
Definition SHAMap.h:571
Interface for changing ledger entries with transactions.
Definition RawView.h:76
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::shared_ptr< Ledger > loadByHash(uint256 const &ledgerHash, Application &app, bool acquire)
Definition Ledger.cpp:1115
std::shared_ptr< Ledger > loadLedgerHelper(LedgerHeader const &info, Application &app, bool acquire)
Definition Ledger.cpp:1054
bool isCurrent(ValidationParms const &p, NetClock::time_point now, NetClock::time_point signTime, NetClock::time_point seenTime)
Whether a validation is still current.
bool isFlagLedger(LedgerIndex seq)
Returns true if the given ledgerIndex is a flag ledgerIndex.
Definition Ledger.cpp:945
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > deserializeTxPlusMeta(SHAMapItem const &item)
Deserialize a SHAMapItem containing STTx + STObject metadata.
Definition Ledger.cpp:373
std::tuple< std::shared_ptr< Ledger >, std::uint32_t, uint256 > getLatestLedger(Application &app)
Definition Ledger.cpp:1092
std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
Definition Ledger.cpp:366
create_genesis_t const create_genesis
Definition Ledger.cpp:32
bool pendSaveValidated(Application &app, std::shared_ptr< Ledger const > const &ledger, bool isSynchronous, bool isCurrent)
Save, or arrange to save, a fully-validated ledger Returns false on error.
Definition Ledger.cpp:981
uint256 calculateLedgerHash(LedgerHeader const &info)
Definition Ledger.cpp:35
std::shared_ptr< Ledger > loadByIndex(std::uint32_t ledgerIndex, Application &app, bool acquire)
Definition Ledger.cpp:1102
std::uint32_t constexpr FLAG_LEDGER_INTERVAL
Definition Ledger.h:407
Reflects the fee settings for a particular ledger.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
Information about the notional ledger backing the view.