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/TimeKeeper.h>
24#include <xrpld/ledger/CachedView.h>
25#include <xrpld/ledger/View.h>
26#include <xrpld/shamap/SHAMap.h>
27#include <xrpl/basics/CountedObject.h>
28#include <xrpl/beast/utility/Journal.h>
29#include <xrpl/protocol/Book.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#include <mutex>
35
36namespace ripple {
37
38class Application;
39class Job;
40class TransactionMaster;
41
42class SqliteStatement;
43
45{
46 explicit create_genesis_t() = default;
47};
49
76class Ledger final : public std::enable_shared_from_this<Ledger>,
78 public TxsRawView,
79 public CountedObject<Ledger>
80{
81public:
82 Ledger(Ledger const&) = delete;
83 Ledger&
84 operator=(Ledger const&) = delete;
85
86 Ledger(Ledger&&) = delete;
87 Ledger&
88 operator=(Ledger&&) = delete;
89
104 Ledger(
106 Config const& config,
107 std::vector<uint256> const& amendments,
108 Family& family);
109
110 Ledger(LedgerInfo const& info, Config const& config, Family& family);
111
116 Ledger(
117 LedgerInfo const& info,
118 bool& loaded,
119 bool acquire,
120 Config const& config,
121 Family& family,
123
130 Ledger(Ledger const& previous, NetClock::time_point closeTime);
131
132 // used for database ledgers
133 Ledger(
134 std::uint32_t ledgerSeq,
135 NetClock::time_point closeTime,
136 Config const& config,
137 Family& family);
138
139 ~Ledger() = default;
140
141 //
142 // ReadView
143 //
144
145 bool
146 open() const override
147 {
148 return false;
149 }
150
151 LedgerInfo const&
152 info() const override
153 {
154 return info_;
155 }
156
157 void
159 {
160 info_ = info;
161 }
162
163 Fees const&
164 fees() const override
165 {
166 return fees_;
167 }
168
169 Rules const&
170 rules() const override
171 {
172 return rules_;
173 }
174
175 bool
176 exists(Keylet const& k) const override;
177
178 bool
179 exists(uint256 const& key) const;
180
182 succ(uint256 const& key, std::optional<uint256> const& last = std::nullopt)
183 const override;
184
186 read(Keylet const& k) const override;
187
189 slesBegin() const override;
190
192 slesEnd() const override;
193
195 slesUpperBound(uint256 const& key) const override;
196
198 txsBegin() const override;
199
201 txsEnd() const override;
202
203 bool
204 txExists(uint256 const& key) const override;
205
206 tx_type
207 txRead(key_type const& key) const override;
208
209 //
210 // DigestAwareReadView
211 //
212
214 digest(key_type const& key) const override;
215
216 //
217 // RawView
218 //
219
220 void
221 rawErase(std::shared_ptr<SLE> const& sle) override;
222
223 void
224 rawInsert(std::shared_ptr<SLE> const& sle) override;
225
226 void
227 rawErase(uint256 const& key);
228
229 void
230 rawReplace(std::shared_ptr<SLE> const& sle) override;
231
232 void
233 rawDestroyXRP(XRPAmount const& fee) override
234 {
235 info_.drops -= fee;
236 }
237
238 //
239 // TxsRawView
240 //
241
242 void
244 uint256 const& key,
246 std::shared_ptr<Serializer const> const& metaData) override;
247
248 // Insert the transaction, and return the hash of the SHAMap leaf node
249 // holding the transaction. The hash can be used to fetch the transaction
250 // directly, instead of traversing the SHAMap
251 // @param key transaction ID
252 // @param txn transaction
253 // @param metaData transaction metadata
254 // @return hash of SHAMap leaf node that holds the transaction
255 uint256
257 uint256 const& key,
259 std::shared_ptr<Serializer const> const& metaData);
260
261 //--------------------------------------------------------------------------
262
263 void
265 {
266 info_.validated = true;
267 }
268
269 void
271 NetClock::time_point closeTime,
272 NetClock::duration closeResolution,
273 bool correctCloseTime);
274
275 void
276 setImmutable(bool rehash = true);
277
278 bool
280 {
281 return mImmutable;
282 }
283
284 /* Mark this ledger as "should be full".
285
286 "Full" is metadata property of the ledger, it indicates
287 that the local server wants all the corresponding nodes
288 in durable storage.
289
290 This is marked `const` because it reflects metadata
291 and not data that is in common with other nodes on the
292 network.
293 */
294 void
295 setFull() const
296 {
297 txMap_.setFull();
301 }
302
303 void
305 {
306 info_.drops = totDrops;
307 }
308
309 SHAMap const&
310 stateMap() const
311 {
312 return stateMap_;
313 }
314
315 SHAMap&
317 {
318 return stateMap_;
319 }
320
321 SHAMap const&
322 txMap() const
323 {
324 return txMap_;
325 }
326
327 SHAMap&
329 {
330 return txMap_;
331 }
332
333 // returns false on error
334 bool
335 addSLE(SLE const& sle);
336
337 //--------------------------------------------------------------------------
338
339 void
341
342 bool
343 walkLedger(beast::Journal j, bool parallel = false) const;
344
345 bool
346 assertSensible(beast::Journal ledgerJ) const;
347
348 void
349 invariants() const;
350 void
351 unshare() const;
352
359 negativeUNL() const;
360
367 validatorToDisable() const;
368
375 validatorToReEnable() const;
376
382 void
384
386 bool
387 isFlagLedger() const;
388
390 bool
391 isVotingLedger() const;
392
394 peek(Keylet const& k) const;
395
396private:
397 class sles_iter_impl;
398 class txs_iter_impl;
399
400 bool
401 setup();
402
403 void
404 defaultFees(Config const& config);
405
407
408 // A SHAMap containing the transactions associated with this ledger.
409 SHAMap mutable txMap_;
410
411 // A SHAMap containing the state objects for this ledger.
413
414 // Protects fee variables
416
421};
422
425
428bool
430
431//------------------------------------------------------------------------------
432//
433// API
434//
435//------------------------------------------------------------------------------
436
437extern bool
439 Application& app,
440 std::shared_ptr<Ledger const> const& ledger,
441 bool isSynchronous,
442 bool isCurrent);
443
445loadLedgerHelper(LedgerInfo const& sinfo, Application& app, bool acquire);
446
448loadByIndex(std::uint32_t ledgerIndex, Application& app, bool acquire = true);
449
451loadByHash(uint256 const& ledgerHash, Application& app, bool acquire = true);
452
453// Fetch the ledger with the highest sequence contained in the database
456
464deserializeTx(SHAMapItem const& item);
465
477
480
481} // namespace ripple
482
483#endif
A generic endpoint for log messages.
Definition: Journal.h:59
Wraps a DigestAwareReadView to provide caching.
Definition: CachedView.h:157
Tracks the number of instances of an object.
ReadView that associates keys with digests.
Definition: ReadView.h:259
Holds a ledger.
Definition: Ledger.h:80
LedgerInfo const & info() const override
Returns information about the ledger.
Definition: Ledger.h:152
void setAccepted(NetClock::time_point closeTime, NetClock::duration closeResolution, bool correctCloseTime)
Definition: Ledger.cpp:366
void defaultFees(Config const &config)
Definition: Ledger.cpp:679
void rawTxInsert(uint256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData) override
Definition: Ledger.cpp:560
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition: Ledger.cpp:472
void unshare() const
Definition: Ledger.cpp:1051
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
Definition: Ledger.h:233
bool open() const override
Returns true if this reflects an open ledger.
Definition: Ledger.h:146
bool assertSensible(beast::Journal ledgerJ) const
Definition: Ledger.cpp:863
void invariants() const
Definition: Ledger.cpp:1058
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition: Ledger.cpp:417
void rawReplace(std::shared_ptr< SLE > const &sle) override
Unconditionally replace a state item.
Definition: Ledger.cpp:549
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition: Ledger.cpp:466
Ledger & operator=(Ledger const &)=delete
bool isImmutable() const
Definition: Ledger.h:279
SHAMap & stateMap()
Definition: Ledger.h:316
void rawErase(std::shared_ptr< SLE > const &sle) override
Delete an existing state item.
Definition: Ledger.cpp:524
std::optional< PublicKey > validatorToReEnable() const
get the to be re-enabled validator's master public key if any
Definition: Ledger.cpp:746
SHAMap stateMap_
Definition: Ledger.h:412
bool isFlagLedger() const
Returns true if the ledger is a flag ledger.
Definition: Ledger.cpp:955
hash_set< PublicKey > negativeUNL() const
get Negative UNL validators' master public keys
Definition: Ledger.cpp:705
SHAMap const & stateMap() const
Definition: Ledger.h:310
bool txExists(uint256 const &key) const override
Definition: Ledger.cpp:491
Ledger & operator=(Ledger &&)=delete
void setValidated() const
Definition: Ledger.h:264
~Ledger()=default
std::optional< PublicKey > validatorToDisable() const
get the to be disabled validator's master public key if any
Definition: Ledger.cpp:731
bool isVotingLedger() const
Returns true if the ledger directly precedes a flag ledger.
Definition: Ledger.cpp:960
bool mImmutable
Definition: Ledger.h:406
void updateNegativeUNL()
update the Negative UNL ledger component.
Definition: Ledger.cpp:761
std::shared_ptr< SLE > peek(Keylet const &k) const
Definition: Ledger.cpp:693
std::optional< digest_type > digest(key_type const &key) const override
Return the digest associated with the key.
Definition: Ledger.cpp:511
Fees const & fees() const override
Returns the fees for the base ledger.
Definition: Ledger.h:164
tx_type txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition: Ledger.cpp:497
void setLedgerInfo(LedgerInfo const &info)
Definition: Ledger.h:158
void rawInsert(std::shared_ptr< SLE > const &sle) override
Unconditionally insert a state item.
Definition: Ledger.cpp:538
bool walkLedger(beast::Journal j, bool parallel=false) const
Definition: Ledger.cpp:812
void setFull() const
Definition: Ledger.h:295
beast::Journal j_
Definition: Ledger.h:420
SHAMap txMap_
Definition: Ledger.h:409
SHAMap & txMap()
Definition: Ledger.h:328
std::shared_ptr< SLE const > read(Keylet const &k) const override
Return the state item associated with a key.
Definition: Ledger.cpp:441
Rules const & rules() const override
Returns the tx processing rules.
Definition: Ledger.h:170
std::mutex mutex_
Definition: Ledger.h:415
uint256 rawTxInsertWithHash(uint256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData)
Definition: Ledger.cpp:578
std::unique_ptr< sles_type::iter_base > slesBegin() const override
Definition: Ledger.cpp:460
SHAMap const & txMap() const
Definition: Ledger.h:322
void setTotalDrops(std::uint64_t totDrops)
Definition: Ledger.h:304
bool setup()
Definition: Ledger.cpp:600
Ledger(Ledger &&)=delete
Fees fees_
Definition: Ledger.h:417
std::unique_ptr< txs_type::iter_base > txsEnd() const override
Definition: Ledger.cpp:485
std::unique_ptr< txs_type::iter_base > txsBegin() const override
Definition: Ledger.cpp:479
std::optional< uint256 > succ(uint256 const &key, std::optional< uint256 > const &last=std::nullopt) const override
Definition: Ledger.cpp:430
void updateSkipList()
Definition: Ledger.cpp:887
LedgerInfo info_
Definition: Ledger.h:419
bool addSLE(SLE const &sle)
Definition: Ledger.cpp:381
Rules rules_
Definition: Ledger.h:418
Ledger(Ledger const &)=delete
void setImmutable(bool rehash=true)
Definition: Ledger.cpp:346
uint256 key_type
Definition: ReadView.h:60
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:58
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:96
void setLedgerSeq(std::uint32_t lseq)
Definition: SHAMap.h:594
void setFull()
Definition: SHAMap.h:588
Interface for changing ledger entries with transactions.
Definition: RawView.h:98
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:1112
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:398
std::shared_ptr< Ledger > loadByIndex(std::uint32_t ledgerIndex, Application &app, bool acquire)
Definition: Ledger.cpp:1122
std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
Definition: Ledger.cpp:391
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:1074
uint256 calculateLedgerHash(LedgerInfo const &info)
Definition: Ledger.cpp:63
std::shared_ptr< Ledger > loadByHash(uint256 const &ledgerHash, Application &app, bool acquire)
Definition: Ledger.cpp:1135
bool isFlagLedger(LedgerIndex seq)
Returns true if the given ledgerIndex is a flag ledgerIndex.
Definition: Ledger.cpp:966
create_genesis_t const create_genesis
Definition: Ledger.cpp:60
std::uint32_t constexpr FLAG_LEDGER_INTERVAL
Definition: Ledger.h:426
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:1002
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