rippled
Loading...
Searching...
No Matches
Ledger.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_LEDGER_H_INCLUDED
21#define RIPPLE_APP_LEDGER_LEDGER_H_INCLUDED
22
23#include <xrpld/core/Config.h>
24#include <xrpld/core/TimeKeeper.h>
25#include <xrpld/ledger/CachedView.h>
26#include <xrpld/ledger/View.h>
27#include <xrpld/shamap/SHAMap.h>
28#include <xrpl/basics/CountedObject.h>
29#include <xrpl/beast/utility/Journal.h>
30#include <xrpl/protocol/Indexes.h>
31#include <xrpl/protocol/STLedgerEntry.h>
32#include <xrpl/protocol/Serializer.h>
33#include <xrpl/protocol/TxMeta.h>
34
35namespace ripple {
36
37class Application;
38class Job;
39class TransactionMaster;
40
41class SqliteStatement;
42
44{
45 explicit create_genesis_t() = default;
46};
48
75class Ledger final : public std::enable_shared_from_this<Ledger>,
77 public TxsRawView,
78 public CountedObject<Ledger>
79{
80public:
81 Ledger(Ledger const&) = delete;
82 Ledger&
83 operator=(Ledger const&) = delete;
84
85 Ledger(Ledger&&) = delete;
86 Ledger&
87 operator=(Ledger&&) = delete;
88
103 Ledger(
105 Config const& config,
106 std::vector<uint256> const& amendments,
107 Family& family);
108
109 Ledger(LedgerInfo const& info, Config const& config, Family& family);
110
115 Ledger(
116 LedgerInfo const& info,
117 bool& loaded,
118 bool acquire,
119 Config const& config,
120 Family& family,
122
129 Ledger(Ledger const& previous, NetClock::time_point closeTime);
130
131 // used for database ledgers
132 Ledger(
133 std::uint32_t ledgerSeq,
134 NetClock::time_point closeTime,
135 Config const& config,
136 Family& family);
137
138 ~Ledger() = default;
139
140 //
141 // ReadView
142 //
143
144 bool
145 open() const override
146 {
147 return false;
148 }
149
150 LedgerInfo const&
151 info() const override
152 {
153 return info_;
154 }
155
156 void
158 {
159 info_ = info;
160 }
161
162 Fees const&
163 fees() const override
164 {
165 return fees_;
166 }
167
168 Rules const&
169 rules() const override
170 {
171 return rules_;
172 }
173
174 bool
175 exists(Keylet const& k) const override;
176
177 bool
178 exists(uint256 const& key) const;
179
181 succ(uint256 const& key, std::optional<uint256> const& last = std::nullopt)
182 const override;
183
185 read(Keylet const& k) const override;
186
188 slesBegin() const override;
189
191 slesEnd() const override;
192
194 slesUpperBound(uint256 const& key) const override;
195
197 txsBegin() const override;
198
200 txsEnd() const override;
201
202 bool
203 txExists(uint256 const& key) const override;
204
205 tx_type
206 txRead(key_type const& key) const override;
207
208 //
209 // DigestAwareReadView
210 //
211
213 digest(key_type const& key) const override;
214
215 //
216 // RawView
217 //
218
219 void
220 rawErase(std::shared_ptr<SLE> const& sle) override;
221
222 void
223 rawInsert(std::shared_ptr<SLE> const& sle) override;
224
225 void
226 rawErase(uint256 const& key);
227
228 void
229 rawReplace(std::shared_ptr<SLE> const& sle) override;
230
231 void
232 rawDestroyXRP(XRPAmount const& fee) override
233 {
234 info_.drops -= fee;
235 }
236
237 //
238 // TxsRawView
239 //
240
241 void
243 uint256 const& key,
245 std::shared_ptr<Serializer const> const& metaData) override;
246
247 // Insert the transaction, and return the hash of the SHAMap leaf node
248 // holding the transaction. The hash can be used to fetch the transaction
249 // directly, instead of traversing the SHAMap
250 // @param key transaction ID
251 // @param txn transaction
252 // @param metaData transaction metadata
253 // @return hash of SHAMap leaf node that holds the transaction
254 uint256
256 uint256 const& key,
258 std::shared_ptr<Serializer const> const& metaData);
259
260 //--------------------------------------------------------------------------
261
262 void
264 {
265 info_.validated = true;
266 }
267
268 void
270 NetClock::time_point closeTime,
271 NetClock::duration closeResolution,
272 bool correctCloseTime);
273
274 void
275 setImmutable(bool rehash = true);
276
277 bool
279 {
280 return mImmutable;
281 }
282
283 /* Mark this ledger as "should be full".
284
285 "Full" is metadata property of the ledger, it indicates
286 that the local server wants all the corresponding nodes
287 in durable storage.
288
289 This is marked `const` because it reflects metadata
290 and not data that is in common with other nodes on the
291 network.
292 */
293 void
294 setFull() const
295 {
296 txMap_.setFull();
300 }
301
302 void
304 {
305 info_.drops = totDrops;
306 }
307
308 SHAMap const&
309 stateMap() const
310 {
311 return stateMap_;
312 }
313
314 SHAMap&
316 {
317 return stateMap_;
318 }
319
320 SHAMap const&
321 txMap() const
322 {
323 return txMap_;
324 }
325
326 SHAMap&
328 {
329 return txMap_;
330 }
331
332 // returns false on error
333 bool
334 addSLE(SLE const& sle);
335
336 //--------------------------------------------------------------------------
337
338 void
340
341 bool
342 walkLedger(beast::Journal j, bool parallel = false) const;
343
344 bool
345 assertSensible(beast::Journal ledgerJ) const;
346
347 void
348 invariants() const;
349 void
350 unshare() const;
351
358 negativeUNL() const;
359
366 validatorToDisable() const;
367
374 validatorToReEnable() const;
375
381 void
383
385 bool
386 isFlagLedger() const;
387
389 bool
390 isVotingLedger() const;
391
393 peek(Keylet const& k) const;
394
395private:
396 class sles_iter_impl;
397 class txs_iter_impl;
398
399 bool
400 setup();
401
402 void
403 defaultFees(Config const& config);
404
406
407 // A SHAMap containing the transactions associated with this ledger.
408 SHAMap mutable txMap_;
409
410 // A SHAMap containing the state objects for this ledger.
412
413 // Protects fee variables
415
420};
421
424
427bool
429
430//------------------------------------------------------------------------------
431//
432// API
433//
434//------------------------------------------------------------------------------
435
436extern bool
438 Application& app,
439 std::shared_ptr<Ledger const> const& ledger,
440 bool isSynchronous,
441 bool isCurrent);
442
444loadLedgerHelper(LedgerInfo const& sinfo, Application& app, bool acquire);
445
447loadByIndex(std::uint32_t ledgerIndex, Application& app, bool acquire = true);
448
450loadByHash(uint256 const& ledgerHash, Application& app, bool acquire = true);
451
452// Fetch the ledger with the highest sequence contained in the database
455
463deserializeTx(SHAMapItem const& item);
464
476
479
480} // namespace ripple
481
482#endif
A generic endpoint for log messages.
Definition: Journal.h:60
Wraps a DigestAwareReadView to provide caching.
Definition: CachedView.h:156
Tracks the number of instances of an object.
ReadView that associates keys with digests.
Definition: ReadView.h:255
Holds a ledger.
Definition: Ledger.h:79
LedgerInfo const & info() const override
Returns information about the ledger.
Definition: Ledger.h:151
void setAccepted(NetClock::time_point closeTime, NetClock::duration closeResolution, bool correctCloseTime)
Definition: Ledger.cpp:356
void defaultFees(Config const &config)
Definition: Ledger.cpp:669
void rawTxInsert(uint256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData) override
Definition: Ledger.cpp:550
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition: Ledger.cpp:462
void unshare() const
Definition: Ledger.cpp:1041
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
Definition: Ledger.h:232
bool open() const override
Returns true if this reflects an open ledger.
Definition: Ledger.h:145
bool assertSensible(beast::Journal ledgerJ) const
Definition: Ledger.cpp:853
void invariants() const
Definition: Ledger.cpp:1048
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition: Ledger.cpp:407
void rawReplace(std::shared_ptr< SLE > const &sle) override
Unconditionally replace a state item.
Definition: Ledger.cpp:539
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition: Ledger.cpp:456
Ledger & operator=(Ledger const &)=delete
bool isImmutable() const
Definition: Ledger.h:278
SHAMap & stateMap()
Definition: Ledger.h:315
void rawErase(std::shared_ptr< SLE > const &sle) override
Delete an existing state item.
Definition: Ledger.cpp:514
std::optional< PublicKey > validatorToReEnable() const
get the to be re-enabled validator's master public key if any
Definition: Ledger.cpp:736
SHAMap stateMap_
Definition: Ledger.h:411
bool isFlagLedger() const
Returns true if the ledger is a flag ledger.
Definition: Ledger.cpp:945
hash_set< PublicKey > negativeUNL() const
get Negative UNL validators' master public keys
Definition: Ledger.cpp:695
SHAMap const & stateMap() const
Definition: Ledger.h:309
bool txExists(uint256 const &key) const override
Definition: Ledger.cpp:481
Ledger & operator=(Ledger &&)=delete
void setValidated() const
Definition: Ledger.h:263
~Ledger()=default
std::optional< PublicKey > validatorToDisable() const
get the to be disabled validator's master public key if any
Definition: Ledger.cpp:721
bool isVotingLedger() const
Returns true if the ledger directly precedes a flag ledger.
Definition: Ledger.cpp:950
bool mImmutable
Definition: Ledger.h:405
void updateNegativeUNL()
update the Negative UNL ledger component.
Definition: Ledger.cpp:751
std::shared_ptr< SLE > peek(Keylet const &k) const
Definition: Ledger.cpp:683
std::optional< digest_type > digest(key_type const &key) const override
Return the digest associated with the key.
Definition: Ledger.cpp:501
Fees const & fees() const override
Returns the fees for the base ledger.
Definition: Ledger.h:163
tx_type txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition: Ledger.cpp:487
void setLedgerInfo(LedgerInfo const &info)
Definition: Ledger.h:157
void rawInsert(std::shared_ptr< SLE > const &sle) override
Unconditionally insert a state item.
Definition: Ledger.cpp:528
bool walkLedger(beast::Journal j, bool parallel=false) const
Definition: Ledger.cpp:802
void setFull() const
Definition: Ledger.h:294
beast::Journal j_
Definition: Ledger.h:419
SHAMap txMap_
Definition: Ledger.h:408
SHAMap & txMap()
Definition: Ledger.h:327
std::shared_ptr< SLE const > read(Keylet const &k) const override
Return the state item associated with a key.
Definition: Ledger.cpp:431
Rules const & rules() const override
Returns the tx processing rules.
Definition: Ledger.h:169
std::mutex mutex_
Definition: Ledger.h:414
uint256 rawTxInsertWithHash(uint256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData)
Definition: Ledger.cpp:568
std::unique_ptr< sles_type::iter_base > slesBegin() const override
Definition: Ledger.cpp:450
SHAMap const & txMap() const
Definition: Ledger.h:321
void setTotalDrops(std::uint64_t totDrops)
Definition: Ledger.h:303
bool setup()
Definition: Ledger.cpp:590
Ledger(Ledger &&)=delete
Fees fees_
Definition: Ledger.h:416
std::unique_ptr< txs_type::iter_base > txsEnd() const override
Definition: Ledger.cpp:475
std::unique_ptr< txs_type::iter_base > txsBegin() const override
Definition: Ledger.cpp:469
std::optional< uint256 > succ(uint256 const &key, std::optional< uint256 > const &last=std::nullopt) const override
Definition: Ledger.cpp:420
void updateSkipList()
Definition: Ledger.cpp:877
LedgerInfo info_
Definition: Ledger.h:418
bool addSLE(SLE const &sle)
Definition: Ledger.cpp:371
Rules rules_
Definition: Ledger.h:417
Ledger(Ledger const &)=delete
void setImmutable(bool rehash=true)
Definition: Ledger.cpp:336
uint256 key_type
Definition: ReadView.h:56
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:54
Rules controlling protocol behavior.
Definition: Rules.h:35
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition: SHAMap.h:94
void setLedgerSeq(std::uint32_t lseq)
Definition: SHAMap.h:592
void setFull()
Definition: SHAMap.h:586
Interface for changing ledger entries with transactions.
Definition: RawView.h:95
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::tuple< std::shared_ptr< Ledger >, std::uint32_t, uint256 > getLatestLedger(Application &app)
Definition: Ledger.cpp:1102
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:388
std::shared_ptr< Ledger > loadByIndex(std::uint32_t ledgerIndex, Application &app, bool acquire)
Definition: Ledger.cpp:1112
std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
Definition: Ledger.cpp:381
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
std::shared_ptr< Ledger > loadLedgerHelper(LedgerInfo const &info, Application &app, bool acquire)
Definition: Ledger.cpp:1064
uint256 calculateLedgerHash(LedgerInfo const &info)
Definition: Ledger.cpp:53
std::shared_ptr< Ledger > loadByHash(uint256 const &ledgerHash, Application &app, bool acquire)
Definition: Ledger.cpp:1125
bool isFlagLedger(LedgerIndex seq)
Returns true if the given ledgerIndex is a flag ledgerIndex.
Definition: Ledger.cpp:956
create_genesis_t const create_genesis
Definition: Ledger.cpp:50
std::uint32_t constexpr FLAG_LEDGER_INTERVAL
Definition: Ledger.h:425
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:992
Reflects the fee settings for a particular ledger.
Definition: protocol/Fees.h:33
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:39
Information about the notional ledger backing the view.
Definition: LedgerHeader.h:34