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/TxMeta.h>
28 #include <ripple/ledger/View.h>
29 #include <ripple/protocol/Book.h>
30 #include <ripple/protocol/Indexes.h>
31 #include <ripple/protocol/STLedgerEntry.h>
32 #include <ripple/protocol/Serializer.h>
33 #include <ripple/shamap/SHAMap.h>
34 #include <boost/optional.hpp>
35 #include <mutex>
36 
37 namespace ripple {
38 
39 class Application;
40 class Job;
41 class TransactionMaster;
42 
43 class SqliteStatement;
44 
46 {
47  explicit create_genesis_t() = default;
48 };
49 extern create_genesis_t const create_genesis;
50 
77 class Ledger final : public std::enable_shared_from_this<Ledger>,
78  public DigestAwareReadView,
79  public TxsRawView,
80  public CountedObject<Ledger>
81 {
82 public:
83  Ledger(Ledger const&) = delete;
84  Ledger&
85  operator=(Ledger const&) = delete;
86 
101  Ledger(
103  Config const& config,
104  std::vector<uint256> const& amendments,
105  Family& family);
106 
107  Ledger(LedgerInfo const& info, Config const& config, Family& family);
108 
113  Ledger(
114  LedgerInfo const& info,
115  bool& loaded,
116  bool acquire,
117  Config const& config,
118  Family& family,
119  beast::Journal j);
120 
127  Ledger(Ledger const& previous, NetClock::time_point closeTime);
128 
129  // used for database ledgers
130  Ledger(
131  std::uint32_t ledgerSeq,
132  NetClock::time_point closeTime,
133  Config const& config,
134  Family& family);
135 
136  ~Ledger() = default;
137 
138  //
139  // ReadView
140  //
141 
142  bool
143  open() const override
144  {
145  return false;
146  }
147 
148  LedgerInfo const&
149  info() const override
150  {
151  return info_;
152  }
153 
154  void
156  {
157  info_ = info;
158  }
159 
160  Fees const&
161  fees() const override
162  {
163  return fees_;
164  }
165 
166  Rules const&
167  rules() const override
168  {
169  return rules_;
170  }
171 
172  bool
173  exists(Keylet const& k) const override;
174 
175  bool
176  exists(uint256 const& key) const;
177 
178  boost::optional<uint256>
179  succ(uint256 const& key, boost::optional<uint256> const& last = boost::none)
180  const override;
181 
183  read(Keylet const& k) const override;
184 
186  slesBegin() const override;
187 
189  slesEnd() const override;
190 
192  slesUpperBound(uint256 const& key) const override;
193 
195  txsBegin() const override;
196 
198  txsEnd() const override;
199 
200  bool
201  txExists(uint256 const& key) const override;
202 
203  tx_type
204  txRead(key_type const& key) const override;
205 
206  //
207  // DigestAwareReadView
208  //
209 
210  boost::optional<digest_type>
211  digest(key_type const& key) const override;
212 
213  //
214  // RawView
215  //
216 
217  void
218  rawErase(std::shared_ptr<SLE> const& sle) override;
219 
220  void
221  rawInsert(std::shared_ptr<SLE> const& sle) override;
222 
223  void
224  rawErase(uint256 const& key);
225 
226  void
227  rawReplace(std::shared_ptr<SLE> const& sle) override;
228 
229  void
230  rawDestroyXRP(XRPAmount const& fee) override
231  {
232  info_.drops -= fee;
233  }
234 
235  //
236  // TxsRawView
237  //
238 
239  void
240  rawTxInsert(
241  uint256 const& key,
243  std::shared_ptr<Serializer const> const& metaData) override;
244 
245  // Insert the transaction, and return the hash of the SHAMap leaf node
246  // holding the transaction. The hash can be used to fetch the transaction
247  // directly, instead of traversing the SHAMap
248  // @param key transaction ID
249  // @param txn transaction
250  // @param metaData transaction metadata
251  // @return hash of SHAMap leaf node that holds the transaction
252  uint256
254  uint256 const& key,
256  std::shared_ptr<Serializer const> const& metaData);
257 
258  //--------------------------------------------------------------------------
259 
260  void
261  setValidated() const
262  {
263  info_.validated = true;
264  }
265 
266  void
267  setAccepted(
268  NetClock::time_point closeTime,
269  NetClock::duration closeResolution,
270  bool correctCloseTime,
271  Config const& config);
272 
273  void
274  setImmutable(Config const& config, bool rehash = true);
275 
276  bool
277  isImmutable() const
278  {
279  return mImmutable;
280  }
281 
282  /* Mark this ledger as "should be full".
283 
284  "Full" is metadata property of the ledger, it indicates
285  that the local server wants all the corresponding nodes
286  in durable storage.
287 
288  This is marked `const` because it reflects metadata
289  and not data that is in common with other nodes on the
290  network.
291  */
292  void
293  setFull() const
294  {
295  txMap_->setFull();
296  stateMap_->setFull();
297  txMap_->setLedgerSeq(info_.seq);
298  stateMap_->setLedgerSeq(info_.seq);
299  }
300 
301  void
303  {
304  info_.drops = totDrops;
305  }
306 
307  SHAMap const&
308  stateMap() const
309  {
310  return *stateMap_;
311  }
312 
313  SHAMap&
315  {
316  return *stateMap_;
317  }
318 
319  SHAMap const&
320  txMap() const
321  {
322  return *txMap_;
323  }
324 
325  SHAMap&
327  {
328  return *txMap_;
329  }
330 
331  // returns false on error
332  bool
333  addSLE(SLE const& sle);
334 
335  //--------------------------------------------------------------------------
336 
337  void
338  updateSkipList();
339 
340  bool
341  walkLedger(beast::Journal j) const;
342 
343  bool
344  assertSensible(beast::Journal ledgerJ) const;
345 
346  void
347  invariants() const;
348  void
349  unshare() const;
350 
357  negativeUNL() const;
358 
364  boost::optional<PublicKey>
365  validatorToDisable() const;
366 
372  boost::optional<PublicKey>
373  validatorToReEnable() const;
374 
380  void
382 
384  bool
385  isFlagLedger() const;
386 
388  bool
389  isVotingLedger() const;
390 
392  peek(Keylet const& k) const;
393 
394 private:
395  class sles_iter_impl;
396  class txs_iter_impl;
397 
398  bool
399  setup(Config const& config);
400 
402 
405 
406  // Protects fee variables
408 
412 };
413 
416 
419 bool
421 
422 //------------------------------------------------------------------------------
423 //
424 // API
425 //
426 //------------------------------------------------------------------------------
427 
428 extern bool
430  Application& app,
431  std::shared_ptr<Ledger const> const& ledger,
432  bool isSynchronous,
433  bool isCurrent);
434 
436 loadByIndex(std::uint32_t ledgerIndex, Application& app, bool acquire = true);
437 
440  std::string const& sqlSuffix,
441  Application& app,
442  bool acquire = true);
443 
445 loadByHash(uint256 const& ledgerHash, Application& app, bool acquire = true);
446 
447 extern uint256
449 
450 extern bool
452  std::uint32_t index,
453  uint256& ledgerHash,
454  uint256& parentHash,
455  Application& app);
456 
459 
460 // Fetch the ledger with the highest sequence contained in the database
463 
464 // *** Reporting Mode Only ***
465 // Fetch all of the transactions contained in ledger from the nodestore.
466 // The transactions are fetched directly as a batch, instead of traversing the
467 // transaction SHAMap. Fetching directly is significantly faster than
468 // traversing, as there are less database reads, and all of the reads can
469 // executed concurrently. This function only works in reporting mode.
470 // @param ledger the ledger for which to fetch the contained transactions
471 // @param app reference to the Application
472 // @return vector of (transaction, metadata) pairs
473 extern std::vector<
475 flatFetchTransactions(ReadView const& ledger, Application& app);
476 
477 // *** Reporting Mode Only ***
478 // For each nodestore hash, fetch the transaction.
479 // The transactions are fetched directly as a batch, instead of traversing the
480 // transaction SHAMap. Fetching directly is significantly faster than
481 // traversing, as there are less database reads, and all of the reads can
482 // executed concurrently. This function only works in reporting mode.
483 // @param nodestoreHashes hashes of the transactions to fetch
484 // @param app reference to the Application
485 // @return vector of (transaction, metadata) pairs
486 extern std::vector<
489 
497 deserializeTx(SHAMapItem const& item);
498 
509 deserializeTxPlusMeta(SHAMapItem const& item);
510 
511 } // namespace ripple
512 
513 #endif
ripple::Ledger::mutex_
std::mutex mutex_
Definition: Ledger.h:407
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:161
ripple::loadLedgerHelper
std::tuple< std::shared_ptr< Ledger >, std::uint32_t, uint256 > loadLedgerHelper(std::string const &sqlSuffix, Application &app, bool acquire)
Definition: Ledger.cpp:1196
ripple::Application
Definition: Application.h:101
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:912
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:403
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:401
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:906
ripple::Ledger::txRead
tx_type txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition: Ledger.cpp:488
std::string
STL class.
std::shared_ptr
STL class.
ripple::loadByIndex
std::shared_ptr< Ledger > loadByIndex(std::uint32_t ledgerIndex, Application &app, bool acquire)
Definition: Ledger.cpp:1565
ripple::Ledger::fees_
Fees fees_
Definition: Ledger.h:409
ripple::Ledger::unshare
void unshare() const
Definition: Ledger.cpp:1173
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:198
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:1463
ripple::FLAG_LEDGER_INTERVAL
constexpr std::uint32_t FLAG_LEDGER_INTERVAL
Definition: Ledger.h:417
ripple::Ledger::exists
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition: Ledger.cpp:407
ripple::Ledger::peek
std::shared_ptr< SLE > peek(Keylet const &k) const
Definition: Ledger.cpp:645
ripple::Ledger::invariants
void invariants() const
Definition: Ledger.cpp:1180
ripple::Ledger::operator=
Ledger & operator=(Ledger const &)=delete
std::tuple
ripple::Ledger::walkLedger
bool walkLedger(beast::Journal j) const
Definition: Ledger.cpp:765
ripple::LedgerInfo::seq
LedgerIndex seq
Definition: ReadView.h:92
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:714
ripple::Ledger::info_
LedgerInfo info_
Definition: Ledger.h:411
ripple::deserializeTx
std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
Definition: Ledger.cpp:381
ripple::Ledger::txMap
SHAMap & txMap()
Definition: Ledger.h:326
ripple::getHashByIndex
uint256 getHashByIndex(std::uint32_t ledgerIndex, Application &app)
Definition: Ledger.cpp:1602
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:73
ripple::Ledger::setLedgerInfo
void setLedgerInfo(LedgerInfo const &info)
Definition: Ledger.h:155
ripple::Ledger::info
LedgerInfo const & info() const override
Returns information about the ledger.
Definition: Ledger.h:149
ripple::Ledger::validatorToReEnable
boost::optional< PublicKey > validatorToReEnable() const
get the to be re-enabled validator's master public key if any
Definition: Ledger.cpp:699
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:1582
ripple::Fees
Reflects the fee settings for a particular ledger.
Definition: ReadView.h:48
ripple::Ledger::rules
Rules const & rules() const override
Returns the tx processing rules.
Definition: Ledger.h:167
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:77
ripple::Ledger::setFull
void setFull() const
Definition: Ledger.h:293
ripple::SHAMapItem
Definition: SHAMapItem.h:35
ripple::Config
Definition: Config.h:67
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:388
ripple::Ledger::stateMap
SHAMap & stateMap()
Definition: Ledger.h:314
ripple::Ledger::rawDestroyXRP
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
Definition: Ledger.h:230
ripple::Ledger::validatorToDisable
boost::optional< PublicKey > validatorToDisable() const
get the to be disabled validator's master public key if any
Definition: Ledger.cpp:684
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:145
ripple::Ledger::stateMap
SHAMap const & stateMap() const
Definition: Ledger.h:308
ripple::DigestAwareReadView
ReadView that associates keys with digests.
Definition: ReadView.h:396
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:45
ripple::Ledger::isFlagLedger
bool isFlagLedger() const
Returns true if the ledger is a flag ledger.
Definition: Ledger.cpp:901
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:813
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:277
std::map
STL class.
ripple::LedgerInfo::drops
XRPAmount drops
Definition: ReadView.h:105
ripple::ReadView::tx_type
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:196
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:431
ripple::Ledger::txMap
SHAMap const & txMap() const
Definition: Ledger.h:320
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:192
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Ledger::setup
bool setup(Config const &config)
Definition: Ledger.cpp:591
ripple::create_genesis
const create_genesis_t create_genesis
Definition: Ledger.cpp:62
ripple::Ledger::stateMap_
std::shared_ptr< SHAMap > stateMap_
Definition: Ledger.h:404
ripple::Ledger::updateSkipList
void updateSkipList()
Definition: Ledger.cpp:837
ripple::Ledger::open
bool open() const override
Returns true if this reflects an open ledger.
Definition: Ledger.h:143
ripple::Ledger::succ
boost::optional< uint256 > succ(uint256 const &key, boost::optional< uint256 > const &last=boost::none) const override
Definition: Ledger.cpp:420
ripple::Ledger::negativeUNL
hash_set< PublicKey > negativeUNL() const
get Negative UNL validators' master public keys
Definition: Ledger.cpp:658
ripple::Rules
Rules controlling protocol behavior.
Definition: ReadView.h:131
mutex
ripple::getHashesByIndex
bool getHashesByIndex(std::uint32_t ledgerIndex, uint256 &ledgerHash, uint256 &parentHash, Application &app)
Definition: Ledger.cpp:1633
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:84
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:302
ripple::TxsRawView
Interface for changing ledger entries with transactions.
Definition: RawView.h:98
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:1123
ripple::LedgerInfo::validated
bool validated
Definition: ReadView.h:110
ripple::Ledger::Ledger
Ledger(Ledger const &)=delete
std::unique_ptr
STL class.
ripple::Ledger::digest
boost::optional< digest_type > digest(key_type const &key) const override
Return the digest associated with the key.
Definition: Ledger.cpp:503
ripple::Ledger::rules_
Rules rules_
Definition: Ledger.h:410
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::Ledger::setValidated
void setValidated() const
Definition: Ledger.h:261
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:1704