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 
47 //------------------------------------------------------------------------------
48 //
49 // Observers
50 //
51 //------------------------------------------------------------------------------
52 
74 [[nodiscard]] bool
75 hasExpired(ReadView const& view, std::optional<std::uint32_t> const& exp);
76 
79 
80 [[nodiscard]] bool
81 isGlobalFrozen(ReadView const& view, AccountID const& issuer);
82 
83 [[nodiscard]] bool
85  ReadView const& view,
86  AccountID const& account,
87  Currency const& currency,
88  AccountID const& issuer);
89 
90 [[nodiscard]] inline bool
92  ReadView const& view,
93  AccountID const& account,
94  Issue const& issue)
95 {
96  return isIndividualFrozen(view, account, issue.currency, issue.account);
97 }
98 
99 [[nodiscard]] bool
100 isFrozen(
101  ReadView const& view,
102  AccountID const& account,
103  Currency const& currency,
104  AccountID const& issuer);
105 
106 [[nodiscard]] inline bool
107 isFrozen(ReadView const& view, AccountID const& account, Issue const& issue)
108 {
109  return isFrozen(view, account, issue.currency, issue.account);
110 }
111 
112 // Returns the amount an account can spend without going into debt.
113 //
114 // <-- saAmount: amount of currency held by account. May be negative.
115 [[nodiscard]] STAmount
117  ReadView const& view,
118  AccountID const& account,
119  Currency const& currency,
120  AccountID const& issuer,
121  FreezeHandling zeroIfFrozen,
122  beast::Journal j);
123 
124 [[nodiscard]] STAmount
126  ReadView const& view,
127  AccountID const& account,
128  Issue const& issue,
129  FreezeHandling zeroIfFrozen,
130  beast::Journal j);
131 
132 // Returns the amount an account can spend of the currency type saDefault, or
133 // returns saDefault if this account is the issuer of the currency in
134 // question. Should be used in favor of accountHolds when questioning how much
135 // an account can spend while also allowing currency issuers to spend
136 // unlimited amounts of their own currency (since they can always issue more).
137 [[nodiscard]] STAmount
139  ReadView const& view,
140  AccountID const& id,
141  STAmount const& saDefault,
142  FreezeHandling freezeHandling,
143  beast::Journal j);
144 
145 // Return the account's liquid (not reserved) XRP. Generally prefer
146 // calling accountHolds() over this interface. However, this interface
147 // allows the caller to temporarily adjust the owner count should that be
148 // necessary.
149 //
150 // @param ownerCountAdj positive to add to count, negative to reduce count.
151 [[nodiscard]] XRPAmount
152 xrpLiquid(
153  ReadView const& view,
154  AccountID const& id,
155  std::int32_t ownerCountAdj,
156  beast::Journal j);
157 
159 void
161  ReadView const& view,
162  Keylet const& root,
163  std::function<void(std::shared_ptr<SLE const> const&)> const& f);
164 
171 bool
173  ReadView const& view,
174  Keylet const& root,
175  uint256 const& after,
176  std::uint64_t const hint,
177  unsigned int limit,
178  std::function<bool(std::shared_ptr<SLE const> const&)> const& f);
179 
181 inline void
183  ReadView const& view,
184  AccountID const& id,
185  std::function<void(std::shared_ptr<SLE const> const&)> const& f)
186 {
187  return forEachItem(view, keylet::ownerDir(id), f);
188 }
189 
196 inline bool
198  ReadView const& view,
199  AccountID const& id,
200  uint256 const& after,
201  std::uint64_t const hint,
202  unsigned int limit,
203  std::function<bool(std::shared_ptr<SLE const> const&)> const& f)
204 {
205  return forEachItemAfter(view, keylet::ownerDir(id), after, hint, limit, f);
206 }
207 
208 [[nodiscard]] Rate
209 transferRate(ReadView const& view, AccountID const& issuer);
210 
214 [[nodiscard]] bool
215 dirIsEmpty(ReadView const& view, Keylet const& k);
216 
217 // Return the list of enabled amendments
218 [[nodiscard]] std::set<uint256>
219 getEnabledAmendments(ReadView const& view);
220 
221 // Return a map of amendments that have achieved majority
223 [[nodiscard]] majorityAmendments_t
224 getMajorityAmendments(ReadView const& view);
225 
235 [[nodiscard]] std::optional<uint256>
236 hashOfSeq(ReadView const& ledger, LedgerIndex seq, beast::Journal journal);
237 
250 inline LedgerIndex
252 {
253  return (requested + 255) & (~255);
254 }
255 
261 [[nodiscard]] bool
263  ReadView const& validLedger,
264  ReadView const& testLedger,
266  const char* reason);
267 
268 [[nodiscard]] bool
270  uint256 const& validHash,
271  LedgerIndex validIndex,
272  ReadView const& testLedger,
274  const char* reason);
275 
276 //------------------------------------------------------------------------------
277 //
278 // Modifiers
279 //
280 //------------------------------------------------------------------------------
281 
283 void
285  ApplyView& view,
286  std::shared_ptr<SLE> const& sle,
287  std::int32_t amount,
288  beast::Journal j);
289 
305 bool
306 cdirFirst(
307  ReadView const& view,
308  uint256 const& root,
310  unsigned int& index,
311  uint256& entry);
312 
313 bool
314 dirFirst(
315  ApplyView& view,
316  uint256 const& root,
317  std::shared_ptr<SLE>& page,
318  unsigned int& index,
319  uint256& entry);
337 bool
338 cdirNext(
339  ReadView const& view,
340  uint256 const& root,
342  unsigned int& index,
343  uint256& entry);
344 
345 bool
346 dirNext(
347  ApplyView& view,
348  uint256 const& root,
349  std::shared_ptr<SLE>& page,
350  unsigned int& index,
351  uint256& entry);
354 [[nodiscard]] std::function<void(SLE::ref)>
355 describeOwnerDir(AccountID const& account);
356 
357 // VFALCO NOTE Both STAmount parameters should just
358 // be "Amount", a unit-less number.
359 //
364 [[nodiscard]] TER
366  ApplyView& view,
367  const bool bSrcHigh,
368  AccountID const& uSrcAccountID,
369  AccountID const& uDstAccountID,
370  uint256 const& uIndex, // --> ripple state entry
371  SLE::ref sleAccount, // --> the account being set.
372  const bool bAuth, // --> authorize account.
373  const bool bNoRipple, // --> others cannot ripple through
374  const bool bFreeze, // --> funds cannot leave
375  STAmount const& saBalance, // --> balance of account being set.
376  // Issuer should be noAccount()
377  STAmount const& saLimit, // --> limit for account being set.
378  // Issuer should be the account being set.
379  std::uint32_t uSrcQualityIn,
380  std::uint32_t uSrcQualityOut,
381  beast::Journal j);
382 
383 [[nodiscard]] TER
385  ApplyView& view,
386  std::shared_ptr<SLE> const& sleRippleState,
387  AccountID const& uLowAccountID,
388  AccountID const& uHighAccountID,
389  beast::Journal j);
390 
397 // [[nodiscard]] // nodiscard commented out so Flow, BookTip and others compile.
398 TER
399 offerDelete(ApplyView& view, std::shared_ptr<SLE> const& sle, beast::Journal j);
400 
401 //------------------------------------------------------------------------------
402 
403 //
404 // Money Transfers
405 //
406 
407 // Direct send w/o fees:
408 // - Redeeming IOUs and/or sending sender's own IOUs.
409 // - Create trust line of needed.
410 // --> bCheckIssuer : normally require issuer to be involved.
411 // [[nodiscard]] // nodiscard commented out so DirectStep.cpp compiles.
412 TER
414  ApplyView& view,
415  AccountID const& uSenderID,
416  AccountID const& uReceiverID,
417  const STAmount& saAmount,
418  bool bCheckIssuer,
419  beast::Journal j);
420 
421 [[nodiscard]] TER
423  ApplyView& view,
424  AccountID const& from,
425  AccountID const& to,
426  const STAmount& saAmount,
427  beast::Journal j,
429 
430 [[nodiscard]] TER
431 issueIOU(
432  ApplyView& view,
433  AccountID const& account,
434  STAmount const& amount,
435  Issue const& issue,
436  beast::Journal j);
437 
438 [[nodiscard]] TER
439 redeemIOU(
440  ApplyView& view,
441  AccountID const& account,
442  STAmount const& amount,
443  Issue const& issue,
444  beast::Journal j);
445 
446 [[nodiscard]] TER
448  ApplyView& view,
449  AccountID const& from,
450  AccountID const& to,
451  STAmount const& amount,
452  beast::Journal j);
453 
458 [[nodiscard]] TER
459 requireAuth(ReadView const& view, Issue const& issue, AccountID const& account);
460 
468 [[nodiscard]] TER
470  ApplyView& view,
471  Keylet const& ownerDirKeylet,
473  deleter,
474  beast::Journal j,
475  std::optional<std::uint16_t> maxNodesToDelete = std::nullopt);
476 
481 [[nodiscard]] TER
483  ApplyView& view,
484  std::shared_ptr<SLE> sleState,
486  beast::Journal j);
487 
488 } // namespace ripple
489 
490 #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::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::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:78
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:78
ripple::ammAccountID
AccountID ammAccountID(std::uint16_t prefix, uint256 const &parentHash, uint256 const &ammID)
Calculate AMM account ID.
Definition: AMMCore.cpp:30
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::offerDelete
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
Definition: View.cpp:910
ripple::TER
TERSubset< CanCvtToTER > TER
Definition: TER.h:580
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
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::cleanupOnAccountDelete
TER cleanupOnAccountDelete(ApplyView &view, Keylet const &ownerDirKeylet, std::function< TER(LedgerEntryType, uint256 const &, std::shared_ptr< SLE > &)> deleter, beast::Journal j, std::optional< uint16_t > maxNodesToDelete)
Definition: View.cpp:1531
ripple::redeemIOU
TER redeemIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1399
ripple::fhIGNORE_FREEZE
@ fhIGNORE_FREEZE
Definition: View.h:78
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:251
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