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/ledger/TxMeta.h>
24 #include <ripple/ledger/View.h>
25 #include <ripple/ledger/CachedView.h>
26 #include <ripple/basics/CountedObject.h>
27 #include <ripple/core/TimeKeeper.h>
28 #include <ripple/protocol/Indexes.h>
29 #include <ripple/protocol/STLedgerEntry.h>
30 #include <ripple/protocol/Serializer.h>
31 #include <ripple/protocol/Book.h>
32 #include <ripple/shamap/SHAMap.h>
33 #include <ripple/beast/utility/Journal.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
78  : public std::enable_shared_from_this <Ledger>
79  , public DigestAwareReadView
80  , public TxsRawView
81  , public CountedObject <Ledger>
82 {
83 public:
84  static char const* getCountedObjectName () { return "Ledger"; }
85 
86  Ledger (Ledger const&) = delete;
87  Ledger& operator= (Ledger const&) = delete;
88 
103  Ledger (
105  Config const& config,
106  std::vector<uint256> const& amendments,
107  Family& family);
108 
109  Ledger (
110  LedgerInfo const& info,
111  Config const& config,
112  Family& family);
113 
118  Ledger (
119  LedgerInfo const& info,
120  bool& loaded,
121  bool acquire,
122  Config const& config,
123  Family& family,
124  beast::Journal j);
125 
132  Ledger (Ledger const& previous,
133  NetClock::time_point closeTime);
134 
135  // used for database ledgers
136  Ledger (std::uint32_t ledgerSeq,
137  NetClock::time_point closeTime, Config const& config,
138  Family& family);
139 
140  ~Ledger() = default;
141 
142  //
143  // ReadView
144  //
145 
146  bool
147  open() const override
148  {
149  return false;
150  }
151 
152  LedgerInfo const&
153  info() const override
154  {
155  return info_;
156  }
157 
158  Fees const&
159  fees() const override
160  {
161  return fees_;
162  }
163 
164  Rules const&
165  rules() const override
166  {
167  return rules_;
168  }
169 
170  bool
171  exists (Keylet const& k) const override;
172 
173  boost::optional<uint256>
174  succ (uint256 const& key, boost::optional<
175  uint256> const& last = boost::none) const override;
176 
178  read (Keylet const& k) const override;
179 
181  slesBegin() const override;
182 
184  slesEnd() const override;
185 
187  slesUpperBound(uint256 const& key) const override;
188 
190  txsBegin() const override;
191 
193  txsEnd() const override;
194 
195  bool
196  txExists (uint256 const& key) const override;
197 
198  tx_type
199  txRead (key_type const& key) const override;
200 
201  //
202  // DigestAwareReadView
203  //
204 
205  boost::optional<digest_type>
206  digest (key_type const& key) const override;
207 
208  //
209  // RawView
210  //
211 
212  void
214  SLE> const& sle) override;
215 
216  void
218  SLE> const& sle) override;
219 
220  void
222  SLE> const& sle) override;
223 
224  void
225  rawDestroyXRP (XRPAmount const& fee) override
226  {
227  info_.drops -= fee;
228  }
229 
230  //
231  // TxsRawView
232  //
233 
234  void
235  rawTxInsert (uint256 const& key,
237  > const& txn, std::shared_ptr<
238  Serializer const> const& metaData) override;
239 
240  //--------------------------------------------------------------------------
241 
242  void setValidated() const
243  {
244  info_.validated = true;
245  }
246 
247  void setAccepted (NetClock::time_point closeTime,
248  NetClock::duration closeResolution, bool correctCloseTime,
249  Config const& config);
250 
251  void setImmutable (Config const& config);
252 
253  bool isImmutable () const
254  {
255  return mImmutable;
256  }
257 
258  /* Mark this ledger as "should be full".
259 
260  "Full" is metadata property of the ledger, it indicates
261  that the local server wants all the corresponding nodes
262  in durable storage.
263 
264  This is marked `const` because it reflects metadata
265  and not data that is in common with other nodes on the
266  network.
267  */
268  void
269  setFull() const
270  {
271  txMap_->setFull();
272  stateMap_->setFull();
273  txMap_->setLedgerSeq(info_.seq);
274  stateMap_->setLedgerSeq(info_.seq);
275  }
276 
277  void setTotalDrops (std::uint64_t totDrops)
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 addSLE (SLE const& sle);
308 
309  //--------------------------------------------------------------------------
310 
311  void updateSkipList ();
312 
313  bool walkLedger (beast::Journal j) const;
314 
315  bool assertSane (beast::Journal ledgerJ) const;
316 
317  void invariants() const;
318  void unshare() const;
319 private:
320  class sles_iter_impl;
321  class txs_iter_impl;
322 
323  bool
324  setup (Config const& config);
325 
327  peek (Keylet const& k) const;
328 
330 
333 
334  // Protects fee variables
336 
340 };
341 
344 
345 //------------------------------------------------------------------------------
346 //
347 // API
348 //
349 //------------------------------------------------------------------------------
350 
351 extern
352 bool
354  Application& app,
355  std::shared_ptr<Ledger const> const& ledger,
356  bool isSynchronous,
357  bool isCurrent);
358 
359 extern
361 loadByIndex (std::uint32_t ledgerIndex,
362  Application& app, bool acquire = true);
363 
364 extern
366 loadLedgerHelper(std::string const& sqlSuffix,
367  Application& app, bool acquire = true);
368 
369 extern
371 loadByHash (uint256 const& ledgerHash,
372  Application& app, bool acquire = true);
373 
374 extern
375 uint256
377 
378 extern
379 bool
381  uint256 &ledgerHash, uint256& parentHash,
382  Application& app);
383 
384 extern
387  Application& app);
388 
396 deserializeTx (SHAMapItem const& item);
397 
408  STTx const>, std::shared_ptr<
409  STObject const>>
410 deserializeTxPlusMeta (SHAMapItem const& item);
411 
412 } // ripple
413 
414 #endif
ripple::Ledger::mutex_
std::mutex mutex_
Definition: Ledger.h:335
ripple::Ledger::slesUpperBound
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition: Ledger.cpp:456
ripple::Ledger::fees
Fees const & fees() const override
Returns the fees for the base ledger.
Definition: Ledger.h:159
ripple::loadLedgerHelper
std::tuple< std::shared_ptr< Ledger >, std::uint32_t, uint256 > loadLedgerHelper(std::string const &sqlSuffix, Application &app, bool acquire)
Definition: Ledger.cpp:1056
ripple::Application
Definition: Application.h:85
ripple::Ledger::slesBegin
std::unique_ptr< sles_type::iter_base > slesBegin() const override
Definition: Ledger.cpp:442
ripple::Ledger::addSLE
bool addSLE(SLE const &sle)
Definition: Ledger.cpp:359
ripple::Ledger::rawReplace
void rawReplace(std::shared_ptr< SLE > const &sle) override
Unconditionally replace a state item.
Definition: Ledger.cpp:537
ripple::Ledger::txMap_
std::shared_ptr< SHAMap > txMap_
Definition: Ledger.h:331
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::Ledger::mImmutable
bool mImmutable
Definition: Ledger.h:329
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:102
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:483
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:1159
ripple::Ledger::fees_
Fees fees_
Definition: Ledger.h:337
ripple::Ledger::unshare
void unshare() const
Definition: Ledger.cpp:1032
ripple::Ledger::~Ledger
~Ledger()=default
ripple::Ledger::slesEnd
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition: Ledger.cpp:449
std::pair
ripple::ReadView::key_type
uint256 key_type
Definition: ReadView.h:193
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:401
ripple::Ledger::peek
std::shared_ptr< SLE > peek(Keylet const &k) const
Definition: Ledger.cpp:625
ripple::Ledger::invariants
void invariants() const
Definition: Ledger.cpp:1039
ripple::Ledger::operator=
Ledger & operator=(Ledger const &)=delete
std::tuple
ripple::Ledger::walkLedger
bool walkLedger(beast::Journal j) const
Definition: Ledger.cpp:639
ripple::LedgerInfo::seq
LedgerIndex seq
Definition: ReadView.h:87
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:551
ripple::Ledger::rawErase
void rawErase(std::shared_ptr< SLE > const &sle) override
Delete an existing state item.
Definition: Ledger.cpp:516
ripple::Ledger::info_
LedgerInfo info_
Definition: Ledger.h:339
ripple::deserializeTx
std::shared_ptr< STTx const > deserializeTx(SHAMapItem const &item)
Deserialize a SHAMapItem containing a single STTx.
Definition: Ledger.cpp:368
ripple::Ledger::txMap
SHAMap & txMap()
Definition: Ledger.h:301
ripple::Ledger::setImmutable
void setImmutable(Config const &config)
Definition: Ledger.cpp:327
ripple::getHashByIndex
uint256 getHashByIndex(std::uint32_t ledgerIndex, Application &app)
Definition: Ledger.cpp:1196
ripple::Ledger::txsEnd
std::unique_ptr< txs_type::iter_base > txsEnd() const override
Definition: Ledger.cpp:470
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:153
ripple::Ledger::txExists
bool txExists(uint256 const &key) const override
Returns true if a tx exists in the tx map.
Definition: Ledger.cpp:477
ripple::Ledger::assertSane
bool assertSane(beast::Journal ledgerJ) const
Definition: Ledger.cpp:686
ripple::loadByHash
std::shared_ptr< Ledger > loadByHash(uint256 const &ledgerHash, Application &app, bool acquire)
Definition: Ledger.cpp:1176
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:165
ripple::CachedView
Wraps a DigestAwareReadView to provide caching.
Definition: CachedView.h:160
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:346
ripple::Ledger
Holds a ledger.
Definition: Ledger.h:77
ripple::Ledger::setFull
void setFull() const
Definition: Ledger.h:269
ripple::SHAMapItem
Definition: SHAMapItem.h:34
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:377
ripple::Ledger::stateMap
SHAMap & stateMap()
Definition: Ledger.h:289
ripple::Ledger::rawDestroyXRP
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
Definition: Ledger.h:225
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:407
ripple::SHAMap
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition: SHAMap.h:79
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:523
std::chrono::time_point
ripple::STTx
Definition: STTx.h:43
ripple::Family
Definition: Family.h:32
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
std::uint32_t
ripple::Ledger::isImmutable
bool isImmutable() const
Definition: Ledger.h:253
std::map
STL class.
ripple::LedgerInfo::drops
XRPAmount drops
Definition: ReadView.h:100
ripple::ReadView::tx_type
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > tx_type
Definition: ReadView.h:191
ripple::Serializer
Definition: Serializer.h:43
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:420
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:463
ripple::STObject
Definition: STObject.h:51
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:571
ripple::create_genesis
const create_genesis_t create_genesis
Definition: Ledger.cpp:56
ripple::Ledger::stateMap_
std::shared_ptr< SHAMap > stateMap_
Definition: Ledger.h:332
ripple::Ledger::updateSkipList
void updateSkipList()
Definition: Ledger.cpp:712
ripple::Ledger::open
bool open() const override
Returns true if this reflects an open ledger.
Definition: Ledger.h:147
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:408
ripple::Rules
Rules controlling protocol behavior.
Definition: ReadView.h:126
mutex
ripple::getHashesByIndex
bool getHashesByIndex(std::uint32_t ledgerIndex, uint256 &ledgerHash, uint256 &parentHash, Application &app)
Definition: Ledger.cpp:1226
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:79
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:101
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:979
ripple::LedgerInfo::validated
bool validated
Definition: ReadView.h:105
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:502
ripple::Ledger::rules_
Rules rules_
Definition: Ledger.h:338
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::Ledger::setValidated
void setValidated() const
Definition: Ledger.h:242