rippled
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 <ripple/basics/CountedObject.h>
24 #include <ripple/beast/utility/Journal.h>
25 #include <ripple/core/TimeKeeper.h>
26 #include <ripple/ledger/CachedView.h>
27 #include <ripple/ledger/View.h>
28 #include <ripple/protocol/Book.h>
29 #include <ripple/protocol/Indexes.h>
30 #include <ripple/protocol/STLedgerEntry.h>
31 #include <ripple/protocol/Serializer.h>
32 #include <ripple/protocol/TxMeta.h>
33 #include <ripple/shamap/SHAMap.h>
34 #include <mutex>
35 
36 namespace ripple {
37 
38 class Application;
39 class Job;
40 class TransactionMaster;
41 
42 class SqliteStatement;
43 
45 {
46  explicit create_genesis_t() = default;
47 };
48 extern create_genesis_t const create_genesis;
49 
76 class Ledger final : public std::enable_shared_from_this<Ledger>,
77  public DigestAwareReadView,
78  public TxsRawView,
79  public CountedObject<Ledger>
80 {
81 public:
82  Ledger(Ledger const&) = delete;
83  Ledger&
84  operator=(Ledger const&) = delete;
85 
100  Ledger(
102  Config const& config,
103  std::vector<uint256> const& amendments,
104  Family& family);
105 
106  Ledger(LedgerInfo const& info, Config const& config, Family& family);
107 
112  Ledger(
113  LedgerInfo const& info,
114  bool& loaded,
115  bool acquire,
116  Config const& config,
117  Family& family,
118  beast::Journal j);
119 
126  Ledger(Ledger const& previous, NetClock::time_point closeTime);
127 
128  // used for database ledgers
129  Ledger(
130  std::uint32_t ledgerSeq,
131  NetClock::time_point closeTime,
132  Config const& config,
133  Family& family);
134 
135  ~Ledger() = default;
136 
137  //
138  // ReadView
139  //
140 
141  bool
142  open() const override
143  {
144  return false;
145  }
146 
147  LedgerInfo const&
148  info() const override
149  {
150  return info_;
151  }
152 
153  void
155  {
156  info_ = info;
157  }
158 
159  Fees const&
160  fees() const override
161  {
162  return fees_;
163  }
164 
165  Rules const&
166  rules() const override
167  {
168  return rules_;
169  }
170 
171  bool
172  exists(Keylet const& k) const override;
173 
174  bool
175  exists(uint256 const& key) const;
176 
178  succ(uint256 const& key, std::optional<uint256> const& last = std::nullopt)
179  const override;
180 
182  read(Keylet const& k) const override;
183 
185  slesBegin() const override;
186 
188  slesEnd() const override;
189 
191  slesUpperBound(uint256 const& key) const override;
192 
194  txsBegin() const override;
195 
197  txsEnd() const override;
198 
199  bool
200  txExists(uint256 const& key) const override;
201 
202  tx_type
203  txRead(key_type const& key) const override;
204 
205  //
206  // DigestAwareReadView
207  //
208 
210  digest(key_type const& key) const override;
211 
212  //
213  // RawView
214  //
215 
216  void
217  rawErase(std::shared_ptr<SLE> const& sle) override;
218 
219  void
220  rawInsert(std::shared_ptr<SLE> const& sle) override;
221 
222  void
223  rawErase(uint256 const& key);
224 
225  void
226  rawReplace(std::shared_ptr<SLE> const& sle) override;
227 
228  void
229  rawDestroyXRP(XRPAmount const& fee) override
230  {
231  info_.drops -= fee;
232  }
233 
234  //
235  // TxsRawView
236  //
237 
238  void
239  rawTxInsert(
240  uint256 const& key,
242  std::shared_ptr<Serializer const> const& metaData) override;
243 
244  // Insert the transaction, and return the hash of the SHAMap leaf node
245  // holding the transaction. The hash can be used to fetch the transaction
246  // directly, instead of traversing the SHAMap
247  // @param key transaction ID
248  // @param txn transaction
249  // @param metaData transaction metadata
250  // @return hash of SHAMap leaf node that holds the transaction
251  uint256
253  uint256 const& key,
255  std::shared_ptr<Serializer const> const& metaData);
256 
257  //--------------------------------------------------------------------------
258 
259  void
260  setValidated() const
261  {
262  info_.validated = true;
263  }
264 
265  void
266  setAccepted(
267  NetClock::time_point closeTime,
268  NetClock::duration closeResolution,
269  bool correctCloseTime);
270 
271  void
272  setImmutable(bool rehash = true);
273 
274  bool
275  isImmutable() const
276  {
277  return mImmutable;
278  }
279 
280  /* Mark this ledger as "should be full".
281 
282  "Full" is metadata property of the ledger, it indicates
283  that the local server wants all the corresponding nodes
284  in durable storage.
285 
286  This is marked `const` because it reflects metadata
287  and not data that is in common with other nodes on the
288  network.
289  */
290  void
291  setFull() const
292  {
293  txMap_->setFull();
294  stateMap_->setFull();
295  txMap_->setLedgerSeq(info_.seq);
296  stateMap_->setLedgerSeq(info_.seq);
297  }
298 
299  void
301  {
302  info_.drops = totDrops;
303  }
304 
305  SHAMap const&
306  stateMap() const
307  {
308  return *stateMap_;
309  }
310 
311  SHAMap&
313  {
314  return *stateMap_;
315  }
316 
317  SHAMap const&
318  txMap() const
319  {
320  return *txMap_;
321  }
322 
323  SHAMap&
325  {
326  return *txMap_;
327  }
328 
329  // returns false on error
330  bool
331  addSLE(SLE const& sle);
332 
333  //--------------------------------------------------------------------------
334 
335  void
336  updateSkipList();
337 
338  bool
339  walkLedger(beast::Journal j, bool parallel = false) const;
340 
341  bool
342  assertSensible(beast::Journal ledgerJ) const;
343 
344  void
345  invariants() const;
346  void
347  unshare() const;
348 
355  negativeUNL() const;
356 
363  validatorToDisable() const;
364 
371  validatorToReEnable() const;
372 
378  void
380 
382  bool
383  isFlagLedger() const;
384 
386  bool
387  isVotingLedger() const;
388 
390  peek(Keylet const& k) const;
391 
392 private:
393  class sles_iter_impl;
394  class txs_iter_impl;
395 
396  bool
397  setup();
398 
399  void
400  defaultFees(Config const& config);
401 
403 
406 
407  // Protects fee variables
409 
414 };
415 
418 
421 bool
423 
424 //------------------------------------------------------------------------------
425 //
426 // API
427 //
428 //------------------------------------------------------------------------------
429 
430 extern bool
432  Application& app,
433  std::shared_ptr<Ledger const> const& ledger,
434  bool isSynchronous,
435  bool isCurrent);
436 
438 loadLedgerHelper(LedgerInfo const& sinfo, Application& app, bool acquire);
439 
441 loadByIndex(std::uint32_t ledgerIndex, Application& app, bool acquire = true);
442 
444 loadByHash(uint256 const& ledgerHash, Application& app, bool acquire = true);
445 
446 // Fetch the ledger with the highest sequence contained in the database
449 
450 // *** Reporting Mode Only ***
451 // Fetch all of the transactions contained in ledger from the nodestore.
452 // The transactions are fetched directly as a batch, instead of traversing the
453 // transaction SHAMap. Fetching directly is significantly faster than
454 // traversing, as there are less database reads, and all of the reads can
455 // executed concurrently. This function only works in reporting mode.
456 // @param ledger the ledger for which to fetch the contained transactions
457 // @param app reference to the Application
458 // @return vector of (transaction, metadata) pairs
459 extern std::vector<
461 flatFetchTransactions(ReadView const& ledger, Application& app);
462 
463 // *** Reporting Mode Only ***
464 // For each nodestore hash, fetch the transaction.
465 // The transactions are fetched directly as a batch, instead of traversing the
466 // transaction SHAMap. Fetching directly is significantly faster than
467 // traversing, as there are less database reads, and all of the reads can
468 // executed concurrently. This function only works in reporting mode.
469 // @param nodestoreHashes hashes of the transactions to fetch
470 // @param app reference to the Application
471 // @return vector of (transaction, metadata) pairs
472 extern std::vector<
475 
483 deserializeTx(SHAMapItem const& item);
484 
495 deserializeTxPlusMeta(SHAMapItem const& item);
496 
497 uint256
498 calculateLedgerHash(LedgerInfo const& info);
499 
500 } // namespace ripple
501 
502 #endif
ripple::Ledger::mutex_
std::mutex mutex_
Definition: Ledger.h:408
ripple::Ledger::slesUpperBound
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition: Ledger.cpp:494
ripple::Ledger::fees
Fees const & fees() const override
Returns the fees for the base ledger.
Definition: Ledger.h:160
ripple::Application
Definition: Application.h:115
ripple::Ledger::slesBegin
std::unique_ptr< sles_type::iter_base > slesBegin() const override
Definition: Ledger.cpp:482
ripple::Ledger::addSLE
bool addSLE(SLE const &sle)
Definition: Ledger.cpp:403
ripple::isFlagLedger
bool isFlagLedger(LedgerIndex seq)
Returns true if the given ledgerIndex is a flag ledgerIndex.
Definition: Ledger.cpp:980
ripple::Ledger::rawReplace
void rawReplace(std::shared_ptr< SLE > const &sle) override
Unconditionally replace a state item.
Definition: Ledger.cpp:572
ripple::Ledger::txMap_
std::shared_ptr< SHAMap > txMap_
Definition: Ledger.h:404
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::Ledger::mImmutable
bool mImmutable
Definition: Ledger.h:402
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
ripple::STLedgerEntry
Definition: STLedgerEntry.h:30
ripple::Ledger::isVotingLedger
bool isVotingLedger() const
Returns true if the ledger directly precedes a flag ledger.
Definition: Ledger.cpp:974
ripple::Ledger::succ
std::optional< uint256 > succ(uint256 const &key, std::optional< uint256 > const &last=std::nullopt) const override
Return the key of the next state item.
Definition: Ledger.cpp:452
ripple::Ledger::txRead
tx_type txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition: Ledger.cpp:519
std::shared_ptr
STL class.
ripple::loadByIndex
std::shared_ptr< Ledger > loadByIndex(std::uint32_t ledgerIndex, Application &app, bool acquire)
Definition: Ledger.cpp:1135
ripple::Ledger::fees_
Fees fees_
Definition: Ledger.h:410
ripple::Ledger::unshare
void unshare() const
Definition: Ledger.cpp:1065
ripple::Ledger::~Ledger
~Ledger()=default
ripple::Ledger::slesEnd
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition: Ledger.cpp:488
std::unordered_set
STL class.
std::pair
ripple::ReadView::key_type
uint256 key_type
Definition: ReadView.h:131
ripple::Ledger::walkLedger
bool walkLedger(beast::Journal j, bool parallel=false) const
Definition: Ledger.cpp:830
std::vector
STL class.
ripple::Ledger::defaultFees
void defaultFees(Config const &config)
Definition: Ledger.cpp:699
std::chrono::duration
ripple::getLatestLedger
std::tuple< std::shared_ptr< Ledger >, std::uint32_t, uint256 > getLatestLedger(Application &app)
Definition: Ledger.cpp:1125
ripple::FLAG_LEDGER_INTERVAL
constexpr std::uint32_t FLAG_LEDGER_INTERVAL
Definition: Ledger.h:419
ripple::Ledger::exists
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition: Ledger.cpp:439
ripple::Ledger::peek
std::shared_ptr< SLE > peek(Keylet const &k) const
Definition: Ledger.cpp:711
ripple::Ledger::invariants
void invariants() const
Definition: Ledger.cpp:1072
ripple::Ledger::operator=
Ledger & operator=(Ledger const &)=delete
std::tuple
ripple::LedgerInfo::seq
LedgerIndex seq
Definition: ReadView.h:83
ripple::Ledger::rawTxInsert
void rawTxInsert(uint256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData) override
Add a transaction to the tx map.
Definition: Ledger.cpp:583
ripple::Ledger::rawErase
void rawErase(std::shared_ptr< SLE > const &sle) override
Delete an existing state item.
Definition: Ledger.cpp:547
ripple::Ledger::updateNegativeUNL
void updateNegativeUNL()
update the Negative UNL ledger component.
Definition: Ledger.cpp:779
ripple::Ledger::info_
LedgerInfo info_
Definition: Ledger.h:412
ripple::deserializeTx
std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
Definition: Ledger.cpp:413
ripple::Ledger::txMap
SHAMap & txMap()
Definition: Ledger.h:324
ripple::Ledger::validatorToDisable
std::optional< PublicKey > validatorToDisable() const
get the to be disabled validator's master public key if any
Definition: Ledger.cpp:749
ripple::Ledger::txsEnd
std::unique_ptr< txs_type::iter_base > txsEnd() const override
Definition: Ledger.cpp:507
ripple::create_genesis_t::create_genesis_t
create_genesis_t()=default
ripple::base_uint< 256 >
ripple::Ledger::setLedgerInfo
void setLedgerInfo(LedgerInfo const &info)
Definition: Ledger.h:154
ripple::Ledger::info
LedgerInfo const & info() const override
Returns information about the ledger.
Definition: Ledger.h:148
ripple::Ledger::txExists
bool txExists(uint256 const &key) const override
Returns true if a tx exists in the tx map.
Definition: Ledger.cpp:513
ripple::loadByHash
std::shared_ptr< Ledger > loadByHash(uint256 const &ledgerHash, Application &app, bool acquire)
Definition: Ledger.cpp:1148
ripple::Fees
Reflects the fee settings for a particular ledger.
Definition: ReadView.h:49
ripple::Ledger::rules
Rules const & rules() const override
Returns the tx processing rules.
Definition: Ledger.h:166
ripple::CachedView
Wraps a DigestAwareReadView to provide caching.
Definition: CachedView.h:159
ripple::Ledger
Holds a ledger.
Definition: Ledger.h:76
ripple::Ledger::setFull
void setFull() const
Definition: Ledger.h:291
ripple::SHAMapItem
Definition: SHAMapItem.h:34
ripple::Config
Definition: Config.h:89
ripple::deserializeTxPlusMeta
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:420
ripple::calculateLedgerHash
uint256 calculateLedgerHash(LedgerInfo const &info)
Definition: Ledger.cpp:65
ripple::Ledger::stateMap
SHAMap & stateMap()
Definition: Ledger.h:312
ripple::Ledger::rawDestroyXRP
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
Definition: Ledger.h:229
ripple::isCurrent
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
ripple::Ledger::stateMap
SHAMap const & stateMap() const
Definition: Ledger.h:306
ripple::DigestAwareReadView
ReadView that associates keys with digests.
Definition: ReadView.h:329
ripple::Ledger::setAccepted
void setAccepted(NetClock::time_point closeTime, NetClock::duration closeResolution, bool correctCloseTime)
Definition: Ledger.cpp:388
ripple::SHAMap
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition: SHAMap.h:95
ripple::create_genesis_t
Definition: Ledger.h:44
ripple::Ledger::isFlagLedger
bool isFlagLedger() const
Returns true if the ledger is a flag ledger.
Definition: Ledger.cpp:969
std::enable_shared_from_this
ripple::Ledger::rawInsert
void rawInsert(std::shared_ptr< SLE > const &sle) override
Unconditionally insert a state item.
Definition: Ledger.cpp:561
std::chrono::time_point
ripple::Ledger::assertSensible
bool assertSensible(beast::Journal ledgerJ) const
Definition: Ledger.cpp:881
ripple::Family
Definition: Family.h:32
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::Ledger::setup
bool setup()
Definition: Ledger.cpp:620
std::uint32_t
ripple::Ledger::isImmutable
bool isImmutable() const
Definition: Ledger.h:275
ripple::Ledger::validatorToReEnable
std::optional< PublicKey > validatorToReEnable() const
get the to be re-enabled validator's master public key if any
Definition: Ledger.cpp:764
ripple::LedgerInfo::drops
XRPAmount drops
Definition: ReadView.h:96
ripple::Ledger::setImmutable
void setImmutable(bool rehash=true)
Definition: Ledger.cpp:368
ripple::ReadView::tx_type
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:129
ripple::Ledger::read
std::shared_ptr< SLE const > read(Keylet const &k) const override
Return the state item associated with a key.
Definition: Ledger.cpp:463
ripple::Ledger::txMap
SHAMap const & txMap() const
Definition: Ledger.h:318
ripple::Ledger::txsBegin
std::unique_ptr< txs_type::iter_base > txsBegin() const override
Definition: Ledger.cpp:501
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:125
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ShardState::acquire
@ acquire
ripple::create_genesis
const create_genesis_t create_genesis
Definition: Ledger.cpp:62
ripple::Ledger::stateMap_
std::shared_ptr< SHAMap > stateMap_
Definition: Ledger.h:405
ripple::Ledger::updateSkipList
void updateSkipList()
Definition: Ledger.cpp:905
ripple::Ledger::open
bool open() const override
Returns true if this reflects an open ledger.
Definition: Ledger.h:142
ripple::Ledger::negativeUNL
hash_set< PublicKey > negativeUNL() const
get Negative UNL validators' master public keys
Definition: Ledger.cpp:723
ripple::Rules
Rules controlling protocol behavior.
Definition: Rules.h:33
std::optional
mutex
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:75
ripple::Ledger::rawTxInsertWithHash
uint256 rawTxInsertWithHash(uint256 const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData)
Definition: Ledger.cpp:600
ripple::Ledger::setTotalDrops
void setTotalDrops(std::uint64_t totDrops)
Definition: Ledger.h:300
ripple::TxsRawView
Interface for changing ledger entries with transactions.
Definition: RawView.h:97
ripple::pendSaveValidated
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:1016
ripple::LedgerInfo::validated
bool validated
Definition: ReadView.h:101
ripple::Ledger::Ledger
Ledger(Ledger const &)=delete
std::unique_ptr
STL class.
ripple::Ledger::digest
std::optional< digest_type > digest(key_type const &key) const override
Return the digest associated with the key.
Definition: Ledger.cpp:534
ripple::Ledger::j_
beast::Journal j_
Definition: Ledger.h:413
ripple::loadLedgerHelper
std::shared_ptr< Ledger > loadLedgerHelper(LedgerInfo const &info, Application &app, bool acquire)
Definition: Ledger.cpp:1088
ripple::Ledger::rules_
Rules rules_
Definition: Ledger.h:411
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::Ledger::setValidated
void setValidated() const
Definition: Ledger.h:260
ripple::flatFetchTransactions
std::vector< std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > > flatFetchTransactions(Application &app, std::vector< uint256 > &nodestoreHashes)
Definition: Ledger.cpp:1163