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 
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,
122  beast::Journal j);
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
243  rawTxInsert(
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
264  setValidated() const
265  {
266  info_.validated = true;
267  }
268 
269  void
270  setAccepted(
271  NetClock::time_point closeTime,
272  NetClock::duration closeResolution,
273  bool correctCloseTime);
274 
275  void
276  setImmutable(bool rehash = true);
277 
278  bool
279  isImmutable() const
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();
299  stateMap_.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
340  updateSkipList();
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 
396 private:
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.
412  SHAMap mutable stateMap_;
413 
414  // Protects fee variables
416 
421 };
422 
425 
428 bool
430 
431 //------------------------------------------------------------------------------
432 //
433 // API
434 //
435 //------------------------------------------------------------------------------
436 
437 extern bool
439  Application& app,
440  std::shared_ptr<Ledger const> const& ledger,
441  bool isSynchronous,
442  bool isCurrent);
443 
445 loadLedgerHelper(LedgerInfo const& sinfo, Application& app, bool acquire);
446 
448 loadByIndex(std::uint32_t ledgerIndex, Application& app, bool acquire = true);
449 
451 loadByHash(uint256 const& ledgerHash, Application& app, bool acquire = true);
452 
453 // Fetch the ledger with the highest sequence contained in the database
456 
457 // *** Reporting Mode Only ***
458 // Fetch all of the transactions contained in ledger from the nodestore.
459 // The transactions are fetched directly as a batch, instead of traversing the
460 // transaction SHAMap. Fetching directly is significantly faster than
461 // traversing, as there are less database reads, and all of the reads can
462 // executed concurrently. This function only works in reporting mode.
463 // @param ledger the ledger for which to fetch the contained transactions
464 // @param app reference to the Application
465 // @return vector of (transaction, metadata) pairs
466 extern std::vector<
468 flatFetchTransactions(ReadView const& ledger, Application& app);
469 
470 // *** Reporting Mode Only ***
471 // For each nodestore hash, fetch the transaction.
472 // The transactions are fetched directly as a batch, instead of traversing the
473 // transaction SHAMap. Fetching directly is significantly faster than
474 // traversing, as there are less database reads, and all of the reads can
475 // executed concurrently. This function only works in reporting mode.
476 // @param nodestoreHashes hashes of the transactions to fetch
477 // @param app reference to the Application
478 // @return vector of (transaction, metadata) pairs
479 extern std::vector<
482 
490 deserializeTx(SHAMapItem const& item);
491 
502 deserializeTxPlusMeta(SHAMapItem const& item);
503 
504 uint256
505 calculateLedgerHash(LedgerInfo const& info);
506 
507 } // namespace ripple
508 
509 #endif
ripple::Ledger::mutex_
std::mutex mutex_
Definition: Ledger.h:415
ripple::Ledger::slesUpperBound
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition: Ledger.cpp:484
ripple::Ledger::fees
Fees const & fees() const override
Returns the fees for the base ledger.
Definition: Ledger.h:164
ripple::Application
Definition: Application.h:115
ripple::Ledger::slesBegin
std::unique_ptr< sles_type::iter_base > slesBegin() const override
Definition: Ledger.cpp:472
ripple::Ledger::addSLE
bool addSLE(SLE const &sle)
Definition: Ledger.cpp:393
ripple::isFlagLedger
bool isFlagLedger(LedgerIndex seq)
Returns true if the given ledgerIndex is a flag ledgerIndex.
Definition: Ledger.cpp:969
ripple::Ledger::rawReplace
void rawReplace(std::shared_ptr< SLE > const &sle) override
Unconditionally replace a state item.
Definition: Ledger.cpp:561
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::Ledger::mImmutable
bool mImmutable
Definition: Ledger.h:406
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:963
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:442
ripple::Ledger::txRead
tx_type txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition: Ledger.cpp:509
std::shared_ptr
STL class.
ripple::loadByIndex
std::shared_ptr< Ledger > loadByIndex(std::uint32_t ledgerIndex, Application &app, bool acquire)
Definition: Ledger.cpp:1123
ripple::Ledger::fees_
Fees fees_
Definition: Ledger.h:417
ripple::Ledger::unshare
void unshare() const
Definition: Ledger.cpp:1053
ripple::Ledger::~Ledger
~Ledger()=default
ripple::Ledger::slesEnd
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition: Ledger.cpp:478
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:819
std::vector
STL class.
ripple::Ledger::defaultFees
void defaultFees(Config const &config)
Definition: Ledger.cpp:688
std::chrono::duration
ripple::getLatestLedger
std::tuple< std::shared_ptr< Ledger >, std::uint32_t, uint256 > getLatestLedger(Application &app)
Definition: Ledger.cpp:1113
ripple::FLAG_LEDGER_INTERVAL
constexpr std::uint32_t FLAG_LEDGER_INTERVAL
Definition: Ledger.h:426
ripple::Ledger::exists
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition: Ledger.cpp:429
ripple::Ledger::peek
std::shared_ptr< SLE > peek(Keylet const &k) const
Definition: Ledger.cpp:700
ripple::Ledger::invariants
void invariants() const
Definition: Ledger.cpp:1060
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:572
ripple::Ledger::rawErase
void rawErase(std::shared_ptr< SLE > const &sle) override
Delete an existing state item.
Definition: Ledger.cpp:536
ripple::Ledger::updateNegativeUNL
void updateNegativeUNL()
update the Negative UNL ledger component.
Definition: Ledger.cpp:768
ripple::Ledger::info_
LedgerInfo info_
Definition: Ledger.h:419
ripple::deserializeTx
std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
Definition: Ledger.cpp:403
ripple::SHAMap::setFull
void setFull()
Definition: SHAMap.h:588
ripple::Ledger::txMap
SHAMap & txMap()
Definition: Ledger.h:328
ripple::Ledger::validatorToDisable
std::optional< PublicKey > validatorToDisable() const
get the to be disabled validator's master public key if any
Definition: Ledger.cpp:738
ripple::Ledger::txsEnd
std::unique_ptr< txs_type::iter_base > txsEnd() const override
Definition: Ledger.cpp:497
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:158
ripple::Ledger::info
LedgerInfo const & info() const override
Returns information about the ledger.
Definition: Ledger.h:152
ripple::Ledger::txExists
bool txExists(uint256 const &key) const override
Returns true if a tx exists in the tx map.
Definition: Ledger.cpp:503
ripple::Ledger::txMap_
SHAMap txMap_
Definition: Ledger.h:409
ripple::loadByHash
std::shared_ptr< Ledger > loadByHash(uint256 const &ledgerHash, Application &app, bool acquire)
Definition: Ledger.cpp:1136
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:170
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:295
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:410
ripple::calculateLedgerHash
uint256 calculateLedgerHash(LedgerInfo const &info)
Definition: Ledger.cpp:65
ripple::Ledger::stateMap
SHAMap & stateMap()
Definition: Ledger.h:316
ripple::Ledger::rawDestroyXRP
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
Definition: Ledger.h:233
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:310
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:378
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:958
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:550
std::chrono::time_point
ripple::Ledger::assertSensible
bool assertSensible(beast::Journal ledgerJ) const
Definition: Ledger.cpp:870
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:609
std::uint32_t
ripple::Ledger::isImmutable
bool isImmutable() const
Definition: Ledger.h:279
ripple::Ledger::validatorToReEnable
std::optional< PublicKey > validatorToReEnable() const
get the to be re-enabled validator's master public key if any
Definition: Ledger.cpp:753
ripple::SHAMap::setLedgerSeq
void setLedgerSeq(std::uint32_t lseq)
Definition: SHAMap.h:594
ripple::LedgerInfo::drops
XRPAmount drops
Definition: ReadView.h:96
ripple::Ledger::setImmutable
void setImmutable(bool rehash=true)
Definition: Ledger.cpp:358
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:453
ripple::Ledger::txMap
SHAMap const & txMap() const
Definition: Ledger.h:322
ripple::Ledger::txsBegin
std::unique_ptr< txs_type::iter_base > txsBegin() const override
Definition: Ledger.cpp:491
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::Ledger::stateMap_
SHAMap stateMap_
Definition: Ledger.h:412
ripple::ShardState::acquire
@ acquire
ripple::create_genesis
const create_genesis_t create_genesis
Definition: Ledger.cpp:62
ripple::Ledger::updateSkipList
void updateSkipList()
Definition: Ledger.cpp:894
ripple::Ledger::open
bool open() const override
Returns true if this reflects an open ledger.
Definition: Ledger.h:146
ripple::Ledger::negativeUNL
hash_set< PublicKey > negativeUNL() const
get Negative UNL validators' master public keys
Definition: Ledger.cpp:712
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:589
ripple::Ledger::setTotalDrops
void setTotalDrops(std::uint64_t totDrops)
Definition: Ledger.h:304
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:1005
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:523
ripple::Ledger::j_
beast::Journal j_
Definition: Ledger.h:420
ripple::loadLedgerHelper
std::shared_ptr< Ledger > loadLedgerHelper(LedgerInfo const &info, Application &app, bool acquire)
Definition: Ledger.cpp:1076
ripple::Ledger::rules_
Rules rules_
Definition: Ledger.h:418
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::Ledger::setValidated
void setValidated() const
Definition: Ledger.h:264
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:1151