rippled
Loading...
Searching...
No Matches
Ledger.h
1#pragma once
2
3#include <xrpld/core/Config.h>
4#include <xrpld/core/TimeKeeper.h>
5
6#include <xrpl/basics/CountedObject.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/ledger/CachedView.h>
9#include <xrpl/ledger/View.h>
10#include <xrpl/protocol/Indexes.h>
11#include <xrpl/protocol/STLedgerEntry.h>
12#include <xrpl/protocol/Serializer.h>
13#include <xrpl/protocol/TxMeta.h>
14#include <xrpl/shamap/SHAMap.h>
15
16namespace xrpl {
17
18class Application;
19class Job;
20class TransactionMaster;
21
22class SqliteStatement;
23
25{
26 explicit create_genesis_t() = default;
27};
29
56class Ledger final : public std::enable_shared_from_this<Ledger>,
58 public TxsRawView,
59 public CountedObject<Ledger>
60{
61public:
62 Ledger(Ledger const&) = delete;
63 Ledger&
64 operator=(Ledger const&) = delete;
65
66 Ledger(Ledger&&) = delete;
67 Ledger&
68 operator=(Ledger&&) = delete;
69
84 Ledger(create_genesis_t, Config const& config, std::vector<uint256> const& amendments, Family& family);
85
86 Ledger(LedgerHeader const& info, Config const& config, Family& family);
87
92 Ledger(
93 LedgerHeader const& info,
94 bool& loaded,
95 bool acquire,
96 Config const& config,
97 Family& family,
99
106 Ledger(Ledger const& previous, NetClock::time_point closeTime);
107
108 // used for database ledgers
109 Ledger(std::uint32_t ledgerSeq, NetClock::time_point closeTime, Config const& config, Family& family);
110
111 ~Ledger() = default;
112
113 //
114 // ReadView
115 //
116
117 bool
118 open() const override
119 {
120 return false;
121 }
122
123 LedgerHeader const&
124 header() const override
125 {
126 return header_;
127 }
128
129 void
131 {
132 header_ = info;
133 }
134
135 Fees const&
136 fees() const override
137 {
138 return fees_;
139 }
140
141 Rules const&
142 rules() const override
143 {
144 return rules_;
145 }
146
147 bool
148 exists(Keylet const& k) const override;
149
150 bool
151 exists(uint256 const& key) const;
152
154 succ(uint256 const& key, std::optional<uint256> const& last = std::nullopt) const override;
155
157 read(Keylet const& k) const override;
158
160 slesBegin() const override;
161
163 slesEnd() const override;
164
166 slesUpperBound(uint256 const& key) const override;
167
169 txsBegin() const override;
170
172 txsEnd() const override;
173
174 bool
175 txExists(uint256 const& key) const override;
176
177 tx_type
178 txRead(key_type const& key) const override;
179
180 //
181 // DigestAwareReadView
182 //
183
185 digest(key_type const& key) const override;
186
187 //
188 // RawView
189 //
190
191 void
192 rawErase(std::shared_ptr<SLE> const& sle) override;
193
194 void
195 rawInsert(std::shared_ptr<SLE> const& sle) override;
196
197 void
198 rawErase(uint256 const& key);
199
200 void
201 rawReplace(std::shared_ptr<SLE> const& sle) override;
202
203 void
204 rawDestroyXRP(XRPAmount const& fee) override
205 {
206 header_.drops -= fee;
207 }
208
209 //
210 // TxsRawView
211 //
212
213 void
215 uint256 const& key,
217 std::shared_ptr<Serializer const> const& metaData) override;
218
219 // Insert the transaction, and return the hash of the SHAMap leaf node
220 // holding the transaction. The hash can be used to fetch the transaction
221 // directly, instead of traversing the SHAMap
222 // @param key transaction ID
223 // @param txn transaction
224 // @param metaData transaction metadata
225 // @return hash of SHAMap leaf node that holds the transaction
226 uint256
228 uint256 const& key,
230 std::shared_ptr<Serializer const> const& metaData);
231
232 //--------------------------------------------------------------------------
233
234 void
236 {
237 header_.validated = true;
238 }
239
240 void
241 setAccepted(NetClock::time_point closeTime, NetClock::duration closeResolution, bool correctCloseTime);
242
243 void
244 setImmutable(bool rehash = true);
245
246 bool
248 {
249 return mImmutable;
250 }
251
252 /* Mark this ledger as "should be full".
253
254 "Full" is metadata property of the ledger, it indicates
255 that the local server wants all the corresponding nodes
256 in durable storage.
257
258 This is marked `const` because it reflects metadata
259 and not data that is in common with other nodes on the
260 network.
261 */
262 void
270
271 void
273 {
274 header_.drops = totDrops;
275 }
276
277 SHAMap const&
278 stateMap() const
279 {
280 return stateMap_;
281 }
282
283 SHAMap&
285 {
286 return stateMap_;
287 }
288
289 SHAMap const&
290 txMap() const
291 {
292 return txMap_;
293 }
294
295 SHAMap&
297 {
298 return txMap_;
299 }
300
301 // returns false on error
302 bool
303 addSLE(SLE const& sle);
304
305 //--------------------------------------------------------------------------
306
307 void
309
310 bool
311 walkLedger(beast::Journal j, bool parallel = false) const;
312
313 bool
314 assertSensible(beast::Journal ledgerJ) const;
315
316 void
317 invariants() const;
318 void
319 unshare() const;
320
327 negativeUNL() const;
328
335 validatorToDisable() const;
336
343 validatorToReEnable() const;
344
350 void
352
354 bool
355 isFlagLedger() const;
356
358 bool
359 isVotingLedger() const;
360
362 peek(Keylet const& k) const;
363
364private:
365 class sles_iter_impl;
366 class txs_iter_impl;
367
368 bool
369 setup();
370
371 void
372 defaultFees(Config const& config);
373
375
376 // A SHAMap containing the transactions associated with this ledger.
377 SHAMap mutable txMap_;
378
379 // A SHAMap containing the state objects for this ledger.
381
382 // Protects fee variables
384
389};
390
393
396bool
398
399//------------------------------------------------------------------------------
400//
401// API
402//
403//------------------------------------------------------------------------------
404
405extern bool
406pendSaveValidated(Application& app, std::shared_ptr<Ledger const> const& ledger, bool isSynchronous, bool isCurrent);
407
409loadLedgerHelper(LedgerHeader const& sinfo, Application& app, bool acquire);
410
412loadByIndex(std::uint32_t ledgerIndex, Application& app, bool acquire = true);
413
415loadByHash(uint256 const& ledgerHash, Application& app, bool acquire = true);
416
417// Fetch the ledger with the highest sequence contained in the database
420
428deserializeTx(SHAMapItem const& item);
429
441
444
445} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Wraps a DigestAwareReadView to provide caching.
Definition CachedView.h:133
Tracks the number of instances of an object.
ReadView that associates keys with digests.
Definition ReadView.h:229
Holds a ledger.
Definition Ledger.h:60
bool txExists(uint256 const &key) const override
Definition Ledger.cpp:438
std::unique_ptr< txs_type::iter_base > txsEnd() const override
Definition Ledger.cpp:432
Fees fees_
Definition Ledger.h:385
bool isFlagLedger() const
Returns true if the ledger is a flag ledger.
Definition Ledger.cpp:879
std::optional< digest_type > digest(key_type const &key) const override
Return the digest associated with the key.
Definition Ledger.cpp:458
void rawTxInsert(uint256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData) override
Definition Ledger.cpp:503
std::optional< PublicKey > validatorToDisable() const
get the to be disabled validator's master public key if any
Definition Ledger.cpp:664
void setLedgerInfo(LedgerHeader const &info)
Definition Ledger.h:130
uint256 rawTxInsertWithHash(uint256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData)
Definition Ledger.cpp:519
std::shared_ptr< SLE const > read(Keylet const &k) const override
Return the state item associated with a key.
Definition Ledger.cpp:387
void setValidated() const
Definition Ledger.h:235
void updateNegativeUNL()
update the Negative UNL ledger component.
Definition Ledger.cpp:692
bool assertSensible(beast::Journal ledgerJ) const
Definition Ledger.cpp:790
bool isVotingLedger() const
Returns true if the ledger directly precedes a flag ledger.
Definition Ledger.cpp:884
std::unique_ptr< sles_type::iter_base > slesBegin() const override
Definition Ledger.cpp:408
Rules rules_
Definition Ledger.h:386
void invariants() const
Definition Ledger.cpp:969
hash_set< PublicKey > negativeUNL() const
get Negative UNL validators' master public keys
Definition Ledger.cpp:639
Ledger(Ledger &&)=delete
void rawInsert(std::shared_ptr< SLE > const &sle) override
Unconditionally insert a state item.
Definition Ledger.cpp:485
void rawReplace(std::shared_ptr< SLE > const &sle) override
Unconditionally replace a state item.
Definition Ledger.cpp:494
void setImmutable(bool rehash=true)
Definition Ledger.cpp:297
SHAMap const & stateMap() const
Definition Ledger.h:278
void updateSkipList()
Definition Ledger.cpp:815
SHAMap const & txMap() const
Definition Ledger.h:290
bool isImmutable() const
Definition Ledger.h:247
void defaultFees(Config const &config)
Definition Ledger.cpp:615
bool open() const override
Returns true if this reflects an open ledger.
Definition Ledger.h:118
void rawErase(std::shared_ptr< SLE > const &sle) override
Delete an existing state item.
Definition Ledger.cpp:471
LedgerHeader const & header() const override
Returns information about the ledger.
Definition Ledger.h:124
void setAccepted(NetClock::time_point closeTime, NetClock::duration closeResolution, bool correctCloseTime)
Definition Ledger.cpp:317
bool mImmutable
Definition Ledger.h:374
Ledger(Ledger const &)=delete
Ledger & operator=(Ledger const &)=delete
std::shared_ptr< SLE > peek(Keylet const &k) const
Definition Ledger.cpp:627
LedgerHeader header_
Definition Ledger.h:387
std::mutex mutex_
Definition Ledger.h:383
SHAMap & txMap()
Definition Ledger.h:296
void setTotalDrops(std::uint64_t totDrops)
Definition Ledger.h:272
~Ledger()=default
bool addSLE(SLE const &sle)
Definition Ledger.cpp:329
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
Definition Ledger.h:204
std::unique_ptr< txs_type::iter_base > txsBegin() const override
Definition Ledger.cpp:426
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition Ledger.cpp:414
SHAMap & stateMap()
Definition Ledger.h:284
SHAMap stateMap_
Definition Ledger.h:380
std::optional< uint256 > succ(uint256 const &key, std::optional< uint256 > const &last=std::nullopt) const override
Definition Ledger.cpp:376
Ledger & operator=(Ledger &&)=delete
SHAMap txMap_
Definition Ledger.h:377
Rules const & rules() const override
Returns the tx processing rules.
Definition Ledger.h:142
Fees const & fees() const override
Returns the fees for the base ledger.
Definition Ledger.h:136
beast::Journal j_
Definition Ledger.h:388
bool walkLedger(beast::Journal j, bool parallel=false) const
Definition Ledger.cpp:741
void unshare() const
Definition Ledger.cpp:962
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition Ledger.cpp:420
std::optional< PublicKey > validatorToReEnable() const
get the to be re-enabled validator's master public key if any
Definition Ledger.cpp:678
tx_type txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition Ledger.cpp:444
bool setup()
Definition Ledger.cpp:539
void setFull() const
Definition Ledger.h:263
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition Ledger.cpp:363
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition ReadView.h:33
uint256 key_type
Definition ReadView.h:35
Rules controlling protocol behavior.
Definition Rules.h:18
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition SHAMap.h:77
void setLedgerSeq(std::uint32_t lseq)
Definition SHAMap.h:517
void setFull()
Definition SHAMap.h:511
Interface for changing ledger entries with transactions.
Definition RawView.h:75
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::shared_ptr< Ledger > loadByHash(uint256 const &ledgerHash, Application &app, bool acquire)
Definition Ledger.cpp:1035
std::shared_ptr< Ledger > loadLedgerHelper(LedgerHeader const &info, Application &app, bool acquire)
Definition Ledger.cpp:985
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:890
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:345
std::tuple< std::shared_ptr< Ledger >, std::uint32_t, uint256 > getLatestLedger(Application &app)
Definition Ledger.cpp:1014
std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
Definition Ledger.cpp:338
create_genesis_t const create_genesis
Definition Ledger.cpp:31
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:921
uint256 calculateLedgerHash(LedgerHeader const &info)
Definition Ledger.cpp:34
std::shared_ptr< Ledger > loadByIndex(std::uint32_t ledgerIndex, Application &app, bool acquire)
Definition Ledger.cpp:1023
std::uint32_t constexpr FLAG_LEDGER_INTERVAL
Definition Ledger.h:394
Reflects the fee settings for a particular ledger.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
Information about the notional ledger backing the view.