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  Config const& config);
271 
272  void
273  setImmutable(Config const& config, bool rehash = true);
274 
275  bool
276  isImmutable() const
277  {
278  return mImmutable;
279  }
280 
281  /* Mark this ledger as "should be full".
282 
283  "Full" is metadata property of the ledger, it indicates
284  that the local server wants all the corresponding nodes
285  in durable storage.
286 
287  This is marked `const` because it reflects metadata
288  and not data that is in common with other nodes on the
289  network.
290  */
291  void
292  setFull() const
293  {
294  txMap_->setFull();
295  stateMap_->setFull();
296  txMap_->setLedgerSeq(info_.seq);
297  stateMap_->setLedgerSeq(info_.seq);
298  }
299 
300  void
302  {
303  info_.drops = totDrops;
304  }
305 
306  SHAMap const&
307  stateMap() const
308  {
309  return *stateMap_;
310  }
311 
312  SHAMap&
314  {
315  return *stateMap_;
316  }
317 
318  SHAMap const&
319  txMap() const
320  {
321  return *txMap_;
322  }
323 
324  SHAMap&
326  {
327  return *txMap_;
328  }
329 
330  // returns false on error
331  bool
332  addSLE(SLE const& sle);
333 
334  //--------------------------------------------------------------------------
335 
336  void
337  updateSkipList();
338 
339  bool
340  walkLedger(beast::Journal j, bool parallel = false) const;
341 
342  bool
343  assertSensible(beast::Journal ledgerJ) const;
344 
345  void
346  invariants() const;
347  void
348  unshare() const;
349 
356  negativeUNL() const;
357 
364  validatorToDisable() const;
365 
372  validatorToReEnable() const;
373 
379  void
381 
383  bool
384  isFlagLedger() const;
385 
387  bool
388  isVotingLedger() const;
389 
391  peek(Keylet const& k) const;
392 
393 private:
394  class sles_iter_impl;
395  class txs_iter_impl;
396 
397  bool
398  setup(Config const& config);
399 
401 
404 
405  // Protects fee variables
407 
411 };
412 
415 
418 bool
420 
421 //------------------------------------------------------------------------------
422 //
423 // API
424 //
425 //------------------------------------------------------------------------------
426 
427 extern bool
429  Application& app,
430  std::shared_ptr<Ledger const> const& ledger,
431  bool isSynchronous,
432  bool isCurrent);
433 
435 loadLedgerHelper(LedgerInfo const& sinfo, Application& app, bool acquire);
436 
438 loadByIndex(std::uint32_t ledgerIndex, Application& app, bool acquire = true);
439 
441 loadByHash(uint256 const& ledgerHash, Application& app, bool acquire = true);
442 
443 // Fetch the ledger with the highest sequence contained in the database
446 
447 // *** Reporting Mode Only ***
448 // Fetch all of the transactions contained in ledger from the nodestore.
449 // The transactions are fetched directly as a batch, instead of traversing the
450 // transaction SHAMap. Fetching directly is significantly faster than
451 // traversing, as there are less database reads, and all of the reads can
452 // executed concurrently. This function only works in reporting mode.
453 // @param ledger the ledger for which to fetch the contained transactions
454 // @param app reference to the Application
455 // @return vector of (transaction, metadata) pairs
456 extern std::vector<
458 flatFetchTransactions(ReadView const& ledger, Application& app);
459 
460 // *** Reporting Mode Only ***
461 // For each nodestore hash, fetch the transaction.
462 // The transactions are fetched directly as a batch, instead of traversing the
463 // transaction SHAMap. Fetching directly is significantly faster than
464 // traversing, as there are less database reads, and all of the reads can
465 // executed concurrently. This function only works in reporting mode.
466 // @param nodestoreHashes hashes of the transactions to fetch
467 // @param app reference to the Application
468 // @return vector of (transaction, metadata) pairs
469 extern std::vector<
472 
480 deserializeTx(SHAMapItem const& item);
481 
492 deserializeTxPlusMeta(SHAMapItem const& item);
493 
494 uint256
495 calculateLedgerHash(LedgerInfo const& info);
496 
497 } // namespace ripple
498 
499 #endif
ripple::Ledger::mutex_
std::mutex mutex_
Definition: Ledger.h:406
ripple::Ledger::slesUpperBound
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition: Ledger.cpp:463
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:451
ripple::Ledger::addSLE
bool addSLE(SLE const &sle)
Definition: Ledger.cpp:372
ripple::isFlagLedger
bool isFlagLedger(LedgerIndex seq)
Returns true if the given ledgerIndex is a flag ledgerIndex.
Definition: Ledger.cpp:940
ripple::Ledger::rawReplace
void rawReplace(std::shared_ptr< SLE > const &sle) override
Unconditionally replace a state item.
Definition: Ledger.cpp:541
ripple::Ledger::txMap_
std::shared_ptr< SHAMap > txMap_
Definition: Ledger.h:402
ripple::Ledger::setImmutable
void setImmutable(Config const &config, bool rehash=true)
Definition: Ledger.cpp:336
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::Ledger::mImmutable
bool mImmutable
Definition: Ledger.h:400
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:934
ripple::Ledger::succ
std::optional< uint256 > succ(uint256 const &key, std::optional< uint256 > const &last=std::nullopt) const override
Definition: Ledger.cpp:421
ripple::Ledger::txRead
tx_type txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition: Ledger.cpp:488
std::shared_ptr
STL class.
ripple::loadByIndex
std::shared_ptr< Ledger > loadByIndex(std::uint32_t ledgerIndex, Application &app, bool acquire)
Definition: Ledger.cpp:1092
ripple::Ledger::fees_
Fees fees_
Definition: Ledger.h:408
ripple::Ledger::unshare
void unshare() const
Definition: Ledger.cpp:1025
ripple::Ledger::~Ledger
~Ledger()=default
ripple::Ledger::slesEnd
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition: Ledger.cpp:457
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:790
std::vector
STL class.
std::chrono::duration
ripple::getLatestLedger
std::tuple< std::shared_ptr< Ledger >, std::uint32_t, uint256 > getLatestLedger(Application &app)
Definition: Ledger.cpp:1082
ripple::FLAG_LEDGER_INTERVAL
constexpr std::uint32_t FLAG_LEDGER_INTERVAL
Definition: Ledger.h:416
ripple::Ledger::exists
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition: Ledger.cpp:408
ripple::Ledger::peek
std::shared_ptr< SLE > peek(Keylet const &k) const
Definition: Ledger.cpp:671
ripple::Ledger::invariants
void invariants() const
Definition: Ledger.cpp:1032
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
Definition: Ledger.cpp:552
ripple::Ledger::rawErase
void rawErase(std::shared_ptr< SLE > const &sle) override
Delete an existing state item.
Definition: Ledger.cpp:516
ripple::Ledger::updateNegativeUNL
void updateNegativeUNL()
update the Negative UNL ledger component.
Definition: Ledger.cpp:739
ripple::Ledger::info_
LedgerInfo info_
Definition: Ledger.h:410
ripple::deserializeTx
std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
Definition: Ledger.cpp:382
ripple::Ledger::txMap
SHAMap & txMap()
Definition: Ledger.h:325
ripple::Ledger::validatorToDisable
std::optional< PublicKey > validatorToDisable() const
get the to be disabled validator's master public key if any
Definition: Ledger.cpp:709
ripple::Ledger::txsEnd
std::unique_ptr< txs_type::iter_base > txsEnd() const override
Definition: Ledger.cpp:476
ripple::create_genesis_t::create_genesis_t
create_genesis_t()=default
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:81
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
Definition: Ledger.cpp:482
ripple::loadByHash
std::shared_ptr< Ledger > loadByHash(uint256 const &ledgerHash, Application &app, bool acquire)
Definition: Ledger.cpp:1105
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::setAccepted
void setAccepted(NetClock::time_point closeTime, NetClock::duration closeResolution, bool correctCloseTime, Config const &config)
Definition: Ledger.cpp:356
ripple::Ledger
Holds a ledger.
Definition: Ledger.h:76
ripple::Ledger::setFull
void setFull() const
Definition: Ledger.h:292
ripple::SHAMapItem
Definition: SHAMapItem.h:31
ripple::Config
Definition: Config.h:69
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:389
ripple::calculateLedgerHash
uint256 calculateLedgerHash(LedgerInfo const &info)
Definition: Ledger.cpp:65
ripple::Ledger::stateMap
SHAMap & stateMap()
Definition: Ledger.h:313
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:307
ripple::DigestAwareReadView
ReadView that associates keys with digests.
Definition: ReadView.h:329
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:929
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:530
std::chrono::time_point
ripple::Ledger::assertSensible
bool assertSensible(beast::Journal ledgerJ) const
Definition: Ledger.cpp:841
ripple::Family
Definition: Family.h:32
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
ripple::Ledger::isImmutable
bool isImmutable() const
Definition: Ledger.h:276
ripple::Ledger::validatorToReEnable
std::optional< PublicKey > validatorToReEnable() const
get the to be re-enabled validator's master public key if any
Definition: Ledger.cpp:724
ripple::LedgerInfo::drops
XRPAmount drops
Definition: ReadView.h:96
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:432
ripple::Ledger::txMap
SHAMap const & txMap() const
Definition: Ledger.h:319
ripple::Ledger::txsBegin
std::unique_ptr< txs_type::iter_base > txsBegin() const override
Definition: Ledger.cpp:470
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::Ledger::setup
bool setup(Config const &config)
Definition: Ledger.cpp:590
ripple::create_genesis
const create_genesis_t create_genesis
Definition: Ledger.cpp:62
ripple::Ledger::stateMap_
std::shared_ptr< SHAMap > stateMap_
Definition: Ledger.h:403
ripple::Ledger::updateSkipList
void updateSkipList()
Definition: Ledger.cpp:865
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:683
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:570
ripple::Ledger::setTotalDrops
void setTotalDrops(std::uint64_t totDrops)
Definition: Ledger.h:301
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:976
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:503
ripple::loadLedgerHelper
std::shared_ptr< Ledger > loadLedgerHelper(LedgerInfo const &info, Application &app, bool acquire)
Definition: Ledger.cpp:1048
ripple::Ledger::rules_
Rules rules_
Definition: Ledger.h:409
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:1120