rippled
View.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_LEDGER_VIEW_H_INCLUDED
21 #define RIPPLE_LEDGER_VIEW_H_INCLUDED
22 
23 #include <ripple/beast/utility/Journal.h>
24 #include <ripple/core/Config.h>
25 #include <ripple/ledger/ApplyView.h>
26 #include <ripple/ledger/OpenView.h>
27 #include <ripple/ledger/RawView.h>
28 #include <ripple/ledger/ReadView.h>
29 #include <ripple/protocol/Protocol.h>
30 #include <ripple/protocol/Rate.h>
31 #include <ripple/protocol/STLedgerEntry.h>
32 #include <ripple/protocol/STObject.h>
33 #include <ripple/protocol/STTx.h>
34 #include <ripple/protocol/Serializer.h>
35 #include <ripple/protocol/TER.h>
36 #include <functional>
37 #include <map>
38 #include <memory>
39 #include <utility>
40 
41 #include <vector>
42 
43 namespace ripple {
44 
45 enum class WaiveTransferFee : bool { No = false, Yes };
46 enum class SkipEntry : bool { No = false, Yes };
47 
48 //------------------------------------------------------------------------------
49 //
50 // Observers
51 //
52 //------------------------------------------------------------------------------
53 
75 [[nodiscard]] bool
76 hasExpired(ReadView const& view, std::optional<std::uint32_t> const& exp);
77 
80 
81 [[nodiscard]] bool
82 isGlobalFrozen(ReadView const& view, AccountID const& issuer);
83 
84 [[nodiscard]] bool
86  ReadView const& view,
87  AccountID const& account,
88  Currency const& currency,
89  AccountID const& issuer);
90 
91 [[nodiscard]] inline bool
93  ReadView const& view,
94  AccountID const& account,
95  Issue const& issue)
96 {
97  return isIndividualFrozen(view, account, issue.currency, issue.account);
98 }
99 
100 [[nodiscard]] bool
101 isFrozen(
102  ReadView const& view,
103  AccountID const& account,
104  Currency const& currency,
105  AccountID const& issuer);
106 
107 [[nodiscard]] inline bool
108 isFrozen(ReadView const& view, AccountID const& account, Issue const& issue)
109 {
110  return isFrozen(view, account, issue.currency, issue.account);
111 }
112 
113 // Returns the amount an account can spend without going into debt.
114 //
115 // <-- saAmount: amount of currency held by account. May be negative.
116 [[nodiscard]] STAmount
118  ReadView const& view,
119  AccountID const& account,
120  Currency const& currency,
121  AccountID const& issuer,
122  FreezeHandling zeroIfFrozen,
123  beast::Journal j);
124 
125 [[nodiscard]] STAmount
127  ReadView const& view,
128  AccountID const& account,
129  Issue const& issue,
130  FreezeHandling zeroIfFrozen,
131  beast::Journal j);
132 
133 // Returns the amount an account can spend of the currency type saDefault, or
134 // returns saDefault if this account is the issuer of the currency in
135 // question. Should be used in favor of accountHolds when questioning how much
136 // an account can spend while also allowing currency issuers to spend
137 // unlimited amounts of their own currency (since they can always issue more).
138 [[nodiscard]] STAmount
140  ReadView const& view,
141  AccountID const& id,
142  STAmount const& saDefault,
143  FreezeHandling freezeHandling,
144  beast::Journal j);
145 
146 // Return the account's liquid (not reserved) XRP. Generally prefer
147 // calling accountHolds() over this interface. However, this interface
148 // allows the caller to temporarily adjust the owner count should that be
149 // necessary.
150 //
151 // @param ownerCountAdj positive to add to count, negative to reduce count.
152 [[nodiscard]] XRPAmount
153 xrpLiquid(
154  ReadView const& view,
155  AccountID const& id,
156  std::int32_t ownerCountAdj,
157  beast::Journal j);
158 
160 void
162  ReadView const& view,
163  Keylet const& root,
164  std::function<void(std::shared_ptr<SLE const> const&)> const& f);
165 
172 bool
174  ReadView const& view,
175  Keylet const& root,
176  uint256 const& after,
177  std::uint64_t const hint,
178  unsigned int limit,
179  std::function<bool(std::shared_ptr<SLE const> const&)> const& f);
180 
182 inline void
184  ReadView const& view,
185  AccountID const& id,
186  std::function<void(std::shared_ptr<SLE const> const&)> const& f)
187 {
188  return forEachItem(view, keylet::ownerDir(id), f);
189 }
190 
197 inline bool
199  ReadView const& view,
200  AccountID const& id,
201  uint256 const& after,
202  std::uint64_t const hint,
203  unsigned int limit,
204  std::function<bool(std::shared_ptr<SLE const> const&)> const& f)
205 {
206  return forEachItemAfter(view, keylet::ownerDir(id), after, hint, limit, f);
207 }
208 
209 [[nodiscard]] Rate
210 transferRate(ReadView const& view, AccountID const& issuer);
211 
215 [[nodiscard]] bool
216 dirIsEmpty(ReadView const& view, Keylet const& k);
217 
218 // Return the list of enabled amendments
219 [[nodiscard]] std::set<uint256>
220 getEnabledAmendments(ReadView const& view);
221 
222 // Return a map of amendments that have achieved majority
224 [[nodiscard]] majorityAmendments_t
225 getMajorityAmendments(ReadView const& view);
226 
236 [[nodiscard]] std::optional<uint256>
237 hashOfSeq(ReadView const& ledger, LedgerIndex seq, beast::Journal journal);
238 
251 inline LedgerIndex
253 {
254  return (requested + 255) & (~255);
255 }
256 
262 [[nodiscard]] bool
264  ReadView const& validLedger,
265  ReadView const& testLedger,
267  const char* reason);
268 
269 [[nodiscard]] bool
271  uint256 const& validHash,
272  LedgerIndex validIndex,
273  ReadView const& testLedger,
275  const char* reason);
276 
277 //------------------------------------------------------------------------------
278 //
279 // Modifiers
280 //
281 //------------------------------------------------------------------------------
282 
284 void
286  ApplyView& view,
287  std::shared_ptr<SLE> const& sle,
288  std::int32_t amount,
289  beast::Journal j);
290 
306 bool
307 cdirFirst(
308  ReadView const& view,
309  uint256 const& root,
311  unsigned int& index,
312  uint256& entry);
313 
314 bool
315 dirFirst(
316  ApplyView& view,
317  uint256 const& root,
318  std::shared_ptr<SLE>& page,
319  unsigned int& index,
320  uint256& entry);
338 bool
339 cdirNext(
340  ReadView const& view,
341  uint256 const& root,
343  unsigned int& index,
344  uint256& entry);
345 
346 bool
347 dirNext(
348  ApplyView& view,
349  uint256 const& root,
350  std::shared_ptr<SLE>& page,
351  unsigned int& index,
352  uint256& entry);
355 [[nodiscard]] std::function<void(SLE::ref)>
356 describeOwnerDir(AccountID const& account);
357 
358 // VFALCO NOTE Both STAmount parameters should just
359 // be "Amount", a unit-less number.
360 //
365 [[nodiscard]] TER
367  ApplyView& view,
368  const bool bSrcHigh,
369  AccountID const& uSrcAccountID,
370  AccountID const& uDstAccountID,
371  uint256 const& uIndex, // --> ripple state entry
372  SLE::ref sleAccount, // --> the account being set.
373  const bool bAuth, // --> authorize account.
374  const bool bNoRipple, // --> others cannot ripple through
375  const bool bFreeze, // --> funds cannot leave
376  STAmount const& saBalance, // --> balance of account being set.
377  // Issuer should be noAccount()
378  STAmount const& saLimit, // --> limit for account being set.
379  // Issuer should be the account being set.
380  std::uint32_t uSrcQualityIn,
381  std::uint32_t uSrcQualityOut,
382  beast::Journal j);
383 
384 [[nodiscard]] TER
386  ApplyView& view,
387  std::shared_ptr<SLE> const& sleRippleState,
388  AccountID const& uLowAccountID,
389  AccountID const& uHighAccountID,
390  beast::Journal j);
391 
398 // [[nodiscard]] // nodiscard commented out so Flow, BookTip and others compile.
399 TER
400 offerDelete(ApplyView& view, std::shared_ptr<SLE> const& sle, beast::Journal j);
401 
402 //------------------------------------------------------------------------------
403 
404 //
405 // Money Transfers
406 //
407 
408 // Direct send w/o fees:
409 // - Redeeming IOUs and/or sending sender's own IOUs.
410 // - Create trust line of needed.
411 // --> bCheckIssuer : normally require issuer to be involved.
412 // [[nodiscard]] // nodiscard commented out so DirectStep.cpp compiles.
413 TER
415  ApplyView& view,
416  AccountID const& uSenderID,
417  AccountID const& uReceiverID,
418  const STAmount& saAmount,
419  bool bCheckIssuer,
420  beast::Journal j);
421 
422 [[nodiscard]] TER
424  ApplyView& view,
425  AccountID const& from,
426  AccountID const& to,
427  const STAmount& saAmount,
428  beast::Journal j,
430 
431 [[nodiscard]] TER
432 issueIOU(
433  ApplyView& view,
434  AccountID const& account,
435  STAmount const& amount,
436  Issue const& issue,
437  beast::Journal j);
438 
439 [[nodiscard]] TER
440 redeemIOU(
441  ApplyView& view,
442  AccountID const& account,
443  STAmount const& amount,
444  Issue const& issue,
445  beast::Journal j);
446 
447 [[nodiscard]] TER
449  ApplyView& view,
450  AccountID const& from,
451  AccountID const& to,
452  STAmount const& amount,
453  beast::Journal j);
454 
459 [[nodiscard]] TER
460 requireAuth(ReadView const& view, Issue const& issue, AccountID const& account);
461 
468  uint256 const&,
477 [[nodiscard]] TER
479  ApplyView& view,
480  Keylet const& ownerDirKeylet,
481  EntryDeleter const& deleter,
482  beast::Journal j,
483  std::optional<std::uint16_t> maxNodesToDelete = std::nullopt);
484 
489 [[nodiscard]] TER
491  ApplyView& view,
492  std::shared_ptr<SLE> sleState,
494  beast::Journal j);
495 
496 } // namespace ripple
497 
498 #endif
ripple::rippleCredit
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Definition: View.cpp:950
ripple::keylet::ownerDir
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:304
ripple::EntryDeleter
std::function< std::pair< TER, SkipEntry >(LedgerEntryType, uint256 const &, std::shared_ptr< SLE > &)> EntryDeleter
Deleter function prototype.
Definition: View.h:469
ripple::transferRate
Rate transferRate(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:488
ripple::cdirNext
bool cdirNext(ReadView const &view, uint256 const &root, std::shared_ptr< SLE const > &page, unsigned int &index, uint256 &entry)
Returns the next entry in the directory, advancing the index.
Definition: View.cpp:145
ripple::dirNext
bool dirNext(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
Definition: View.cpp:123
ripple::getMajorityAmendments
majorityAmendments_t getMajorityAmendments(ReadView const &view)
Definition: View.cpp:638
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::LedgerIndex
std::uint32_t LedgerIndex
A ledger index.
Definition: Protocol.h:90
ripple::getEnabledAmendments
std::set< uint256 > getEnabledAmendments(ReadView const &view)
Definition: View.cpp:621
ripple::Issue
A currency issued by an account.
Definition: Issue.h:35
std::shared_ptr
STL class.
ripple::fhZERO_IF_FROZEN
@ fhZERO_IF_FROZEN
Definition: View.h:79
utility
ripple::forEachItemAfter
bool forEachItemAfter(ReadView const &view, Keylet const &root, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items after an item in the given directory.
Definition: View.cpp:411
ripple::accountSend
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee)
Definition: View.cpp:1142
ripple::describeOwnerDir
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:748
functional
ripple::WaiveTransferFee::Yes
@ Yes
ripple::WaiveTransferFee
WaiveTransferFee
Definition: View.h:45
vector
ripple::accountHolds
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
Definition: View.cpp:226
ripple::Issue::currency
Currency currency
Definition: Issue.h:38
ripple::hasExpired
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
Definition: View.cpp:162
ripple::deleteAMMTrustLine
TER deleteAMMTrustLine(ApplyView &view, std::shared_ptr< SLE > sleState, std::optional< AccountID > const &ammAccountID, beast::Journal j)
Delete trustline to AMM.
Definition: View.cpp:1607
std::function
ripple::FreezeHandling
FreezeHandling
Controls the treatment of frozen account balances.
Definition: View.h:79
ripple::ammAccountID
AccountID ammAccountID(std::uint16_t prefix, uint256 const &parentHash, uint256 const &ammID)
Calculate AMM account ID.
Definition: AMMCore.cpp:30
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:134
ripple::requireAuth
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account)
Check if the account requires authorization.
Definition: View.cpp:1511
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
ripple::isGlobalFrozen
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:171
ripple::adjustOwnerCount
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
Definition: View.cpp:730
ripple::SkipEntry::Yes
@ Yes
ripple::offerDelete
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
Definition: View.cpp:910
ripple::TERSubset< CanCvtToTER >
ripple::TER
TERSubset< CanCvtToTER > TER
Definition: TER.h:579
beast::Journal::Stream
Provide a light-weight way to check active() before string formatting.
Definition: Journal.h:194
ripple::xrpLiquid
XRPAmount xrpLiquid(ReadView const &view, AccountID const &id, std::int32_t ownerCountAdj, beast::Journal j)
Definition: View.cpp:346
ripple::hashOfSeq
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Definition: View.cpp:661
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::int32_t
map
ripple::trustDelete
TER trustDelete(ApplyView &view, std::shared_ptr< SLE > const &sleRippleState, AccountID const &uLowAccountID, AccountID const &uHighAccountID, beast::Journal j)
Definition: View.cpp:870
ripple::cleanupOnAccountDelete
TER cleanupOnAccountDelete(ApplyView &view, Keylet const &ownerDirKeylet, EntryDeleter const &deleter, beast::Journal j, std::optional< uint16_t > maxNodesToDelete)
Definition: View.cpp:1531
ripple::SkipEntry
SkipEntry
Definition: View.h:46
memory
ripple::areCompatible
bool areCompatible(ReadView const &validLedger, ReadView const &testLedger, beast::Journal::Stream &s, const char *reason)
Return false if the test ledger is provably incompatible with the valid ledger, that is,...
Definition: View.cpp:499
ripple::accountFunds
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
Definition: View.cpp:282
ripple::transferXRP
TER transferXRP(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &amount, beast::Journal j)
Definition: View.cpp:1469
ripple::issueIOU
TER issueIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1303
ripple::dirIsEmpty
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
Definition: View.cpp:607
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:54
ripple::Currency
base_uint< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition: UintTypes.h:56
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::dirFirst
bool dirFirst(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
Definition: View.cpp:112
ripple::isIndividualFrozen
bool isIndividualFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition: View.cpp:181
ripple::trustCreate
TER trustCreate(ApplyView &view, const bool bSrcHigh, AccountID const &uSrcAccountID, AccountID const &uDstAccountID, uint256 const &uIndex, SLE::ref sleAccount, const bool bAuth, const bool bNoRipple, const bool bFreeze, STAmount const &saBalance, STAmount const &saLimit, std::uint32_t uQualityIn, std::uint32_t uQualityOut, beast::Journal j)
Create a trust line.
Definition: View.cpp:756
ripple::LedgerEntryType
LedgerEntryType
Identifiers for on-ledger objects.
Definition: LedgerFormats.h:53
ripple::redeemIOU
TER redeemIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1399
ripple::SkipEntry::No
@ No
ripple::fhIGNORE_FREEZE
@ fhIGNORE_FREEZE
Definition: View.h:79
std::optional< std::uint32_t >
ripple::forEachItem
void forEachItem(ReadView const &view, Keylet const &root, std::function< void(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items in the given directory.
Definition: View.cpp:384
ripple::after
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition: Escrow.cpp:88
ripple::WaiveTransferFee::No
@ No
ripple::getCandidateLedger
LedgerIndex getCandidateLedger(LedgerIndex requested)
Find a ledger index from which we could easily get the requested ledger.
Definition: View.h:252
ripple::cdirFirst
bool cdirFirst(ReadView const &view, uint256 const &root, std::shared_ptr< SLE const > &page, unsigned int &index, uint256 &entry)
Returns the first entry in the directory, advancing the index.
Definition: View.cpp:134
ripple::STLedgerEntry::ref
const std::shared_ptr< STLedgerEntry > & ref
Definition: STLedgerEntry.h:37
std::set
STL class.
ripple::isFrozen
bool isFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition: View.cpp:203
ripple::AccountID
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:47
ripple::Issue::account
AccountID account
Definition: Issue.h:39
ripple::root
Number root(Number f, unsigned d)
Definition: Number.cpp:624