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  Fees const&
155  fees() const override
156  {
157  return fees_;
158  }
159 
160  Rules const&
161  rules() const override
162  {
163  return rules_;
164  }
165 
166  bool
167  exists(Keylet const& k) const override;
168 
169  boost::optional<uint256>
170  succ(uint256 const& key, boost::optional<uint256> const& last = boost::none)
171  const override;
172 
174  read(Keylet const& k) const override;
175 
177  slesBegin() const override;
178 
180  slesEnd() const override;
181 
183  slesUpperBound(uint256 const& key) const override;
184 
186  txsBegin() const override;
187 
189  txsEnd() const override;
190 
191  bool
192  txExists(uint256 const& key) const override;
193 
194  tx_type
195  txRead(key_type const& key) const override;
196 
197  //
198  // DigestAwareReadView
199  //
200 
201  boost::optional<digest_type>
202  digest(key_type const& key) const override;
203 
204  //
205  // RawView
206  //
207 
208  void
209  rawErase(std::shared_ptr<SLE> const& sle) override;
210 
211  void
212  rawInsert(std::shared_ptr<SLE> const& sle) override;
213 
214  void
215  rawReplace(std::shared_ptr<SLE> const& sle) override;
216 
217  void
218  rawDestroyXRP(XRPAmount const& fee) override
219  {
220  info_.drops -= fee;
221  }
222 
223  //
224  // TxsRawView
225  //
226 
227  void
228  rawTxInsert(
229  uint256 const& key,
231  std::shared_ptr<Serializer const> const& metaData) override;
232 
233  //--------------------------------------------------------------------------
234 
235  void
236  setValidated() const
237  {
238  info_.validated = true;
239  }
240 
241  void
242  setAccepted(
243  NetClock::time_point closeTime,
244  NetClock::duration closeResolution,
245  bool correctCloseTime,
246  Config const& config);
247 
248  void
249  setImmutable(Config const& config);
250 
251  bool
252  isImmutable() const
253  {
254  return mImmutable;
255  }
256 
257  /* Mark this ledger as "should be full".
258 
259  "Full" is metadata property of the ledger, it indicates
260  that the local server wants all the corresponding nodes
261  in durable storage.
262 
263  This is marked `const` because it reflects metadata
264  and not data that is in common with other nodes on the
265  network.
266  */
267  void
268  setFull() const
269  {
270  txMap_->setFull();
271  stateMap_->setFull();
272  txMap_->setLedgerSeq(info_.seq);
273  stateMap_->setLedgerSeq(info_.seq);
274  }
275 
276  void
278  {
279  info_.drops = totDrops;
280  }
281 
282  SHAMap const&
283  stateMap() const
284  {
285  return *stateMap_;
286  }
287 
288  SHAMap&
290  {
291  return *stateMap_;
292  }
293 
294  SHAMap const&
295  txMap() const
296  {
297  return *txMap_;
298  }
299 
300  SHAMap&
302  {
303  return *txMap_;
304  }
305 
306  // returns false on error
307  bool
308  addSLE(SLE const& sle);
309 
310  //--------------------------------------------------------------------------
311 
312  void
313  updateSkipList();
314 
315  bool
316  walkLedger(beast::Journal j) const;
317 
318  bool
319  assertSensible(beast::Journal ledgerJ) const;
320 
321  void
322  invariants() const;
323  void
324  unshare() const;
325 
332  negativeUNL() const;
333 
339  boost::optional<PublicKey>
340  validatorToDisable() const;
341 
347  boost::optional<PublicKey>
348  validatorToReEnable() const;
349 
355  void
357 
359  bool
360  isFlagLedger() const;
361 
363  bool
364  isVotingLedger() const;
365 
366 private:
367  class sles_iter_impl;
368  class txs_iter_impl;
369 
370  bool
371  setup(Config const& config);
372 
374  peek(Keylet const& k) const;
375 
377 
380 
381  // Protects fee variables
383 
387 };
388 
391 
394 bool
396 
397 //------------------------------------------------------------------------------
398 //
399 // API
400 //
401 //------------------------------------------------------------------------------
402 
403 extern bool
405  Application& app,
406  std::shared_ptr<Ledger const> const& ledger,
407  bool isSynchronous,
408  bool isCurrent);
409 
411 loadByIndex(std::uint32_t ledgerIndex, Application& app, bool acquire = true);
412 
415  std::string const& sqlSuffix,
416  Application& app,
417  bool acquire = true);
418 
420 loadByHash(uint256 const& ledgerHash, Application& app, bool acquire = true);
421 
422 extern uint256
424 
425 extern bool
427  std::uint32_t index,
428  uint256& ledgerHash,
429  uint256& parentHash,
430  Application& app);
431 
434 
442 deserializeTx(SHAMapItem const& item);
443 
454 deserializeTxPlusMeta(SHAMapItem const& item);
455 
456 } // namespace ripple
457 
458 #endif
ripple::Ledger::mutex_
std::mutex mutex_
Definition: Ledger.h:382
ripple::Ledger::slesUpperBound
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition: Ledger.cpp:441
ripple::Ledger::fees
Fees const & fees() const override
Returns the fees for the base ledger.
Definition: Ledger.h:155
ripple::loadLedgerHelper
std::tuple< std::shared_ptr< Ledger >, std::uint32_t, uint256 > loadLedgerHelper(std::string const &sqlSuffix, Application &app, bool acquire)
Definition: Ledger.cpp:1137
ripple::Application
Definition: Application.h:97
ripple::Ledger::slesBegin
std::unique_ptr< sles_type::iter_base > slesBegin() const override
Definition: Ledger.cpp:429
ripple::Ledger::addSLE
bool addSLE(SLE const &sle)
Definition: Ledger.cpp:356
ripple::isFlagLedger
bool isFlagLedger(LedgerIndex seq)
Returns true if the given ledgerIndex is a flag ledgerIndex.
Definition: Ledger.cpp:861
ripple::Ledger::rawReplace
void rawReplace(std::shared_ptr< SLE > const &sle) override
Unconditionally replace a state item.
Definition: Ledger.cpp:511
ripple::Ledger::txMap_
std::shared_ptr< SHAMap > txMap_
Definition: Ledger.h:378
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::Ledger::mImmutable
bool mImmutable
Definition: Ledger.h:376
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:855
ripple::Ledger::txRead
tx_type txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition: Ledger.cpp:466
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:1229
ripple::Ledger::fees_
Fees fees_
Definition: Ledger.h:384
ripple::Ledger::unshare
void unshare() const
Definition: Ledger.cpp:1113
ripple::Ledger::~Ledger
~Ledger()=default
ripple::Ledger::slesEnd
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition: Ledger.cpp:435
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::FLAG_LEDGER_INTERVAL
constexpr std::uint32_t FLAG_LEDGER_INTERVAL
Definition: Ledger.h:392
ripple::Ledger::exists
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition: Ledger.cpp:391
ripple::Ledger::peek
std::shared_ptr< SLE > peek(Keylet const &k) const
Definition: Ledger.cpp:594
ripple::Ledger::invariants
void invariants() const
Definition: Ledger.cpp:1120
ripple::Ledger::operator=
Ledger & operator=(Ledger const &)=delete
std::tuple
ripple::Ledger::walkLedger
bool walkLedger(beast::Journal j) const
Definition: Ledger.cpp:714
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:522
ripple::Ledger::rawErase
void rawErase(std::shared_ptr< SLE > const &sle) override
Delete an existing state item.
Definition: Ledger.cpp:493
ripple::Ledger::updateNegativeUNL
void updateNegativeUNL()
update the Negative UNL ledger component.
Definition: Ledger.cpp:663
ripple::Ledger::info_
LedgerInfo info_
Definition: Ledger.h:386
ripple::deserializeTx
std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
Definition: Ledger.cpp:365
ripple::Ledger::txMap
SHAMap & txMap()
Definition: Ledger.h:301
ripple::Ledger::setImmutable
void setImmutable(Config const &config)
Definition: Ledger.cpp:321
ripple::getHashByIndex
uint256 getHashByIndex(std::uint32_t ledgerIndex, Application &app)
Definition: Ledger.cpp:1262
ripple::Ledger::txsEnd
std::unique_ptr< txs_type::iter_base > txsEnd() const override
Definition: Ledger.cpp:454
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::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:648
ripple::Ledger::txExists
bool txExists(uint256 const &key) const override
Definition: Ledger.cpp:460
ripple::loadByHash
std::shared_ptr< Ledger > loadByHash(uint256 const &ledgerHash, Application &app, bool acquire)
Definition: Ledger.cpp:1244
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:161
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:340
ripple::Ledger
Holds a ledger.
Definition: Ledger.h:77
ripple::Ledger::setFull
void setFull() const
Definition: Ledger.h:268
ripple::SHAMapItem
Definition: SHAMapItem.h:35
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:372
ripple::Ledger::stateMap
SHAMap & stateMap()
Definition: Ledger.h:289
ripple::Ledger::rawDestroyXRP
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
Definition: Ledger.h:218
ripple::Ledger::validatorToDisable
boost::optional< PublicKey > validatorToDisable() const
get the to be disabled validator's master public key if any
Definition: Ledger.cpp:633
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:283
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:850
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:500
std::chrono::time_point
ripple::Ledger::assertSensible
bool assertSensible(beast::Journal ledgerJ) const
Definition: Ledger.cpp:762
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:252
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:409
ripple::Ledger::txMap
SHAMap const & txMap() const
Definition: Ledger.h:295
ripple::Ledger::txsBegin
std::unique_ptr< txs_type::iter_base > txsBegin() const override
Definition: Ledger.cpp:448
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:540
ripple::create_genesis
const create_genesis_t create_genesis
Definition: Ledger.cpp:57
ripple::Ledger::stateMap_
std::shared_ptr< SHAMap > stateMap_
Definition: Ledger.h:379
ripple::Ledger::updateSkipList
void updateSkipList()
Definition: Ledger.cpp:786
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:398
ripple::Ledger::negativeUNL
hash_set< PublicKey > negativeUNL() const
get Negative UNL validators' master public keys
Definition: Ledger.cpp:607
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:1291
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:84
ripple::Ledger::setTotalDrops
void setTotalDrops(std::uint64_t totDrops)
Definition: Ledger.h:277
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:1063
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:480
ripple::Ledger::rules_
Rules rules_
Definition: Ledger.h:385
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::Ledger::setValidated
void setValidated() const
Definition: Ledger.h:236