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  static char const*
85  {
86  return "Ledger";
87  }
88 
89  Ledger(Ledger const&) = delete;
90  Ledger&
91  operator=(Ledger const&) = delete;
92 
107  Ledger(
109  Config const& config,
110  std::vector<uint256> const& amendments,
111  Family& family);
112 
113  Ledger(LedgerInfo const& info, Config const& config, Family& family);
114 
119  Ledger(
120  LedgerInfo const& info,
121  bool& loaded,
122  bool acquire,
123  Config const& config,
124  Family& family,
125  beast::Journal j);
126 
133  Ledger(Ledger const& previous, NetClock::time_point closeTime);
134 
135  // used for database ledgers
136  Ledger(
137  std::uint32_t ledgerSeq,
138  NetClock::time_point closeTime,
139  Config const& config,
140  Family& family);
141 
142  ~Ledger() = default;
143 
144  //
145  // ReadView
146  //
147 
148  bool
149  open() const override
150  {
151  return false;
152  }
153 
154  LedgerInfo const&
155  info() const override
156  {
157  return 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  boost::optional<uint256>
176  succ(uint256 const& key, boost::optional<uint256> const& last = boost::none)
177  const override;
178 
180  read(Keylet const& k) const override;
181 
183  slesBegin() const override;
184 
186  slesEnd() const override;
187 
189  slesUpperBound(uint256 const& key) const override;
190 
192  txsBegin() const override;
193 
195  txsEnd() const override;
196 
197  bool
198  txExists(uint256 const& key) const override;
199 
200  tx_type
201  txRead(key_type const& key) const override;
202 
203  //
204  // DigestAwareReadView
205  //
206 
207  boost::optional<digest_type>
208  digest(key_type const& key) const override;
209 
210  //
211  // RawView
212  //
213 
214  void
215  rawErase(std::shared_ptr<SLE> const& sle) override;
216 
217  void
218  rawInsert(std::shared_ptr<SLE> const& sle) override;
219 
220  void
221  rawReplace(std::shared_ptr<SLE> const& sle) override;
222 
223  void
224  rawDestroyXRP(XRPAmount const& fee) override
225  {
226  info_.drops -= fee;
227  }
228 
229  //
230  // TxsRawView
231  //
232 
233  void
234  rawTxInsert(
235  uint256 const& key,
237  std::shared_ptr<Serializer const> const& metaData) override;
238 
239  //--------------------------------------------------------------------------
240 
241  void
242  setValidated() const
243  {
244  info_.validated = true;
245  }
246 
247  void
248  setAccepted(
249  NetClock::time_point closeTime,
250  NetClock::duration closeResolution,
251  bool correctCloseTime,
252  Config const& config);
253 
254  void
255  setImmutable(Config const& config);
256 
257  bool
258  isImmutable() const
259  {
260  return mImmutable;
261  }
262 
263  /* Mark this ledger as "should be full".
264 
265  "Full" is metadata property of the ledger, it indicates
266  that the local server wants all the corresponding nodes
267  in durable storage.
268 
269  This is marked `const` because it reflects metadata
270  and not data that is in common with other nodes on the
271  network.
272  */
273  void
274  setFull() const
275  {
276  txMap_->setFull();
277  stateMap_->setFull();
278  txMap_->setLedgerSeq(info_.seq);
279  stateMap_->setLedgerSeq(info_.seq);
280  }
281 
282  void
284  {
285  info_.drops = totDrops;
286  }
287 
288  SHAMap const&
289  stateMap() const
290  {
291  return *stateMap_;
292  }
293 
294  SHAMap&
296  {
297  return *stateMap_;
298  }
299 
300  SHAMap const&
301  txMap() const
302  {
303  return *txMap_;
304  }
305 
306  SHAMap&
308  {
309  return *txMap_;
310  }
311 
312  // returns false on error
313  bool
314  addSLE(SLE const& sle);
315 
316  //--------------------------------------------------------------------------
317 
318  void
319  updateSkipList();
320 
321  bool
322  walkLedger(beast::Journal j) const;
323 
324  bool
325  assertSane(beast::Journal ledgerJ) const;
326 
327  void
328  invariants() const;
329  void
330  unshare() const;
331 
332 private:
333  class sles_iter_impl;
334  class txs_iter_impl;
335 
336  bool
337  setup(Config const& config);
338 
340  peek(Keylet const& k) const;
341 
343 
346 
347  // Protects fee variables
349 
353 };
354 
357 
358 //------------------------------------------------------------------------------
359 //
360 // API
361 //
362 //------------------------------------------------------------------------------
363 
364 extern bool
366  Application& app,
367  std::shared_ptr<Ledger const> const& ledger,
368  bool isSynchronous,
369  bool isCurrent);
370 
372 loadByIndex(std::uint32_t ledgerIndex, Application& app, bool acquire = true);
373 
376  std::string const& sqlSuffix,
377  Application& app,
378  bool acquire = true);
379 
381 loadByHash(uint256 const& ledgerHash, Application& app, bool acquire = true);
382 
383 extern uint256
385 
386 extern bool
388  std::uint32_t index,
389  uint256& ledgerHash,
390  uint256& parentHash,
391  Application& app);
392 
395 
403 deserializeTx(SHAMapItem const& item);
404 
415 deserializeTxPlusMeta(SHAMapItem const& item);
416 
417 } // namespace ripple
418 
419 #endif
ripple::Ledger::mutex_
std::mutex mutex_
Definition: Ledger.h:348
ripple::Ledger::slesUpperBound
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition: Ledger.cpp:438
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:1009
ripple::Application
Definition: Application.h:97
ripple::Ledger::slesBegin
std::unique_ptr< sles_type::iter_base > slesBegin() const override
Definition: Ledger.cpp:426
ripple::Ledger::addSLE
bool addSLE(SLE const &sle)
Definition: Ledger.cpp:353
ripple::Ledger::rawReplace
void rawReplace(std::shared_ptr< SLE > const &sle) override
Unconditionally replace a state item.
Definition: Ledger.cpp:507
ripple::Ledger::txMap_
std::shared_ptr< SHAMap > txMap_
Definition: Ledger.h:344
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::Ledger::mImmutable
bool mImmutable
Definition: Ledger.h:342
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:110
ripple::STLedgerEntry
Definition: STLedgerEntry.h:30
ripple::Ledger::txRead
tx_type txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition: Ledger.cpp:463
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:1101
ripple::Ledger::fees_
Fees fees_
Definition: Ledger.h:350
ripple::Ledger::unshare
void unshare() const
Definition: Ledger.cpp:985
ripple::Ledger::~Ledger
~Ledger()=default
ripple::Ledger::slesEnd
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition: Ledger.cpp:432
std::pair
ripple::ReadView::key_type
uint256 key_type
Definition: ReadView.h:194
std::vector
STL class.
std::chrono::duration
ripple::Ledger::exists
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition: Ledger.cpp:388
ripple::Ledger::peek
std::shared_ptr< SLE > peek(Keylet const &k) const
Definition: Ledger.cpp:589
ripple::Ledger::invariants
void invariants() const
Definition: Ledger.cpp:992
ripple::Ledger::operator=
Ledger & operator=(Ledger const &)=delete
std::tuple
ripple::Ledger::walkLedger
bool walkLedger(beast::Journal j) const
Definition: Ledger.cpp:603
ripple::LedgerInfo::seq
LedgerIndex seq
Definition: ReadView.h:88
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:518
ripple::Ledger::rawErase
void rawErase(std::shared_ptr< SLE > const &sle) override
Delete an existing state item.
Definition: Ledger.cpp:490
ripple::Ledger::info_
LedgerInfo info_
Definition: Ledger.h:352
ripple::deserializeTx
std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
Definition: Ledger.cpp:362
ripple::Ledger::txMap
SHAMap & txMap()
Definition: Ledger.h:307
ripple::Ledger::setImmutable
void setImmutable(Config const &config)
Definition: Ledger.cpp:318
ripple::getHashByIndex
uint256 getHashByIndex(std::uint32_t ledgerIndex, Application &app)
Definition: Ledger.cpp:1134
ripple::Ledger::txsEnd
std::unique_ptr< txs_type::iter_base > txsEnd() const override
Definition: Ledger.cpp:451
ripple::create_genesis_t::create_genesis_t
create_genesis_t()=default
ripple::base_uint< 256 >
ripple::Ledger::info
LedgerInfo const & info() const override
Returns information about the ledger.
Definition: Ledger.h:155
ripple::Ledger::txExists
bool txExists(uint256 const &key) const override
Returns true if a tx exists in the tx map.
Definition: Ledger.cpp:457
ripple::Ledger::assertSane
bool assertSane(beast::Journal ledgerJ) const
Definition: Ledger.cpp:651
ripple::loadByHash
std::shared_ptr< Ledger > loadByHash(uint256 const &ledgerHash, Application &app, bool acquire)
Definition: Ledger.cpp:1116
ripple::Fees
Reflects the fee settings for a particular ledger.
Definition: ReadView.h:47
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::getCountedObjectName
static char const * getCountedObjectName()
Definition: Ledger.h:84
ripple::Ledger::setAccepted
void setAccepted(NetClock::time_point closeTime, NetClock::duration closeResolution, bool correctCloseTime, Config const &config)
Definition: Ledger.cpp:337
ripple::Ledger
Holds a ledger.
Definition: Ledger.h:77
ripple::Ledger::setFull
void setFull() const
Definition: Ledger.h:274
ripple::SHAMapItem
Definition: SHAMapItem.h:34
ripple::Config
Definition: Config.h:66
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:369
ripple::Ledger::stateMap
SHAMap & stateMap()
Definition: Ledger.h:295
ripple::Ledger::rawDestroyXRP
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
Definition: Ledger.h:224
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:289
ripple::DigestAwareReadView
ReadView that associates keys with digests.
Definition: ReadView.h:392
ripple::SHAMap
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition: SHAMap.h:81
ripple::create_genesis_t
Definition: Ledger.h:45
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:497
std::chrono::time_point
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:258
std::map
STL class.
ripple::LedgerInfo::drops
XRPAmount drops
Definition: ReadView.h:101
ripple::ReadView::tx_type
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:192
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:406
ripple::Ledger::txMap
SHAMap const & txMap() const
Definition: Ledger.h:301
ripple::Ledger::txsBegin
std::unique_ptr< txs_type::iter_base > txsBegin() const override
Definition: Ledger.cpp:445
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:535
ripple::create_genesis
const create_genesis_t create_genesis
Definition: Ledger.cpp:56
ripple::Ledger::stateMap_
std::shared_ptr< SHAMap > stateMap_
Definition: Ledger.h:345
ripple::Ledger::updateSkipList
void updateSkipList()
Definition: Ledger.cpp:675
ripple::Ledger::open
bool open() const override
Returns true if this reflects an open ledger.
Definition: Ledger.h:149
ripple::Ledger::succ
boost::optional< uint256 > succ(uint256 const &key, boost::optional< uint256 > const &last=boost::none) const override
Return the key of the next state item.
Definition: Ledger.cpp:395
ripple::Rules
Rules controlling protocol behavior.
Definition: ReadView.h:127
mutex
ripple::getHashesByIndex
bool getHashesByIndex(std::uint32_t ledgerIndex, uint256 &ledgerHash, uint256 &parentHash, Application &app)
Definition: Ledger.cpp:1163
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:80
ripple::Ledger::setTotalDrops
void setTotalDrops(std::uint64_t totDrops)
Definition: Ledger.h:283
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:935
ripple::LedgerInfo::validated
bool validated
Definition: ReadView.h:106
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:477
ripple::Ledger::rules_
Rules rules_
Definition: Ledger.h:351
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::Ledger::setValidated
void setValidated() const
Definition: Ledger.h:242