rippled
Loading...
Searching...
No Matches
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 <xrpld/core/Config.h>
24#include <xrpld/ledger/ApplyView.h>
25#include <xrpld/ledger/OpenView.h>
26#include <xrpld/ledger/RawView.h>
27#include <xrpld/ledger/ReadView.h>
28#include <xrpl/beast/utility/Journal.h>
29#include <xrpl/protocol/MPTIssue.h>
30#include <xrpl/protocol/Protocol.h>
31#include <xrpl/protocol/Rate.h>
32#include <xrpl/protocol/STLedgerEntry.h>
33#include <xrpl/protocol/STObject.h>
34#include <xrpl/protocol/STTx.h>
35#include <xrpl/protocol/Serializer.h>
36#include <xrpl/protocol/TER.h>
37#include <functional>
38#include <map>
39#include <memory>
40#include <utility>
41
42#include <vector>
43
44namespace ripple {
45
46enum class WaiveTransferFee : bool { No = false, Yes };
47enum class SkipEntry : bool { No = false, Yes };
48
49//------------------------------------------------------------------------------
50//
51// Observers
52//
53//------------------------------------------------------------------------------
54
76[[nodiscard]] bool
77hasExpired(ReadView const& view, std::optional<std::uint32_t> const& exp);
78
81
84
85[[nodiscard]] bool
86isGlobalFrozen(ReadView const& view, AccountID const& issuer);
87
88[[nodiscard]] bool
89isGlobalFrozen(ReadView const& view, MPTIssue const& mptIssue);
90
91[[nodiscard]] bool
92isGlobalFrozen(ReadView const& view, Asset const& asset);
93
94[[nodiscard]] bool
96 ReadView const& view,
97 AccountID const& account,
98 Currency const& currency,
99 AccountID const& issuer);
100
101[[nodiscard]] inline bool
103 ReadView const& view,
104 AccountID const& account,
105 Issue const& issue)
106{
107 return isIndividualFrozen(view, account, issue.currency, issue.account);
108}
109
110[[nodiscard]] bool
112 ReadView const& view,
113 AccountID const& account,
114 MPTIssue const& mptIssue);
115
116[[nodiscard]] inline bool
118 ReadView const& view,
119 AccountID const& account,
120 Asset const& asset)
121{
122 return std::visit(
123 [&](auto const& issue) {
124 return isIndividualFrozen(view, account, issue);
125 },
126 asset.value());
127}
128
129[[nodiscard]] bool
131 ReadView const& view,
132 AccountID const& account,
133 Currency const& currency,
134 AccountID const& issuer);
135
136[[nodiscard]] inline bool
137isFrozen(ReadView const& view, AccountID const& account, Issue const& issue)
138{
139 return isFrozen(view, account, issue.currency, issue.account);
140}
141
142[[nodiscard]] bool
144 ReadView const& view,
145 AccountID const& account,
146 MPTIssue const& mptIssue);
147
148[[nodiscard]] inline bool
149isFrozen(ReadView const& view, AccountID const& account, Asset const& asset)
150{
151 return std::visit(
152 [&](auto const& issue) { return isFrozen(view, account, issue); },
153 asset.value());
154}
155
156// Returns the amount an account can spend without going into debt.
157//
158// <-- saAmount: amount of currency held by account. May be negative.
159[[nodiscard]] STAmount
161 ReadView const& view,
162 AccountID const& account,
163 Currency const& currency,
164 AccountID const& issuer,
165 FreezeHandling zeroIfFrozen,
167
168[[nodiscard]] STAmount
170 ReadView const& view,
171 AccountID const& account,
172 Issue const& issue,
173 FreezeHandling zeroIfFrozen,
175
176[[nodiscard]] STAmount
178 ReadView const& view,
179 AccountID const& account,
180 MPTIssue const& mptIssue,
181 FreezeHandling zeroIfFrozen,
182 AuthHandling zeroIfUnauthorized,
184
185// Returns the amount an account can spend of the currency type saDefault, or
186// returns saDefault if this account is the issuer of the currency in
187// question. Should be used in favor of accountHolds when questioning how much
188// an account can spend while also allowing currency issuers to spend
189// unlimited amounts of their own currency (since they can always issue more).
190[[nodiscard]] STAmount
192 ReadView const& view,
193 AccountID const& id,
194 STAmount const& saDefault,
195 FreezeHandling freezeHandling,
197
198// Return the account's liquid (not reserved) XRP. Generally prefer
199// calling accountHolds() over this interface. However, this interface
200// allows the caller to temporarily adjust the owner count should that be
201// necessary.
202//
203// @param ownerCountAdj positive to add to count, negative to reduce count.
204[[nodiscard]] XRPAmount
206 ReadView const& view,
207 AccountID const& id,
208 std::int32_t ownerCountAdj,
210
212void
214 ReadView const& view,
215 Keylet const& root,
216 std::function<void(std::shared_ptr<SLE const> const&)> const& f);
217
224bool
226 ReadView const& view,
227 Keylet const& root,
228 uint256 const& after,
229 std::uint64_t const hint,
230 unsigned int limit,
231 std::function<bool(std::shared_ptr<SLE const> const&)> const& f);
232
234inline void
236 ReadView const& view,
237 AccountID const& id,
238 std::function<void(std::shared_ptr<SLE const> const&)> const& f)
239{
240 return forEachItem(view, keylet::ownerDir(id), f);
241}
242
249inline bool
251 ReadView const& view,
252 AccountID const& id,
253 uint256 const& after,
254 std::uint64_t const hint,
255 unsigned int limit,
256 std::function<bool(std::shared_ptr<SLE const> const&)> const& f)
257{
258 return forEachItemAfter(view, keylet::ownerDir(id), after, hint, limit, f);
259}
260
266[[nodiscard]] Rate
267transferRate(ReadView const& view, AccountID const& issuer);
268
274[[nodiscard]] Rate
275transferRate(ReadView const& view, MPTID const& issuanceID);
276
280[[nodiscard]] bool
281dirIsEmpty(ReadView const& view, Keylet const& k);
282
283// Return the list of enabled amendments
284[[nodiscard]] std::set<uint256>
285getEnabledAmendments(ReadView const& view);
286
287// Return a map of amendments that have achieved majority
289[[nodiscard]] majorityAmendments_t
291
301[[nodiscard]] std::optional<uint256>
302hashOfSeq(ReadView const& ledger, LedgerIndex seq, beast::Journal journal);
303
316inline LedgerIndex
318{
319 return (requested + 255) & (~255);
320}
321
327[[nodiscard]] bool
329 ReadView const& validLedger,
330 ReadView const& testLedger,
332 const char* reason);
333
334[[nodiscard]] bool
336 uint256 const& validHash,
337 LedgerIndex validIndex,
338 ReadView const& testLedger,
340 const char* reason);
341
342//------------------------------------------------------------------------------
343//
344// Modifiers
345//
346//------------------------------------------------------------------------------
347
349void
351 ApplyView& view,
352 std::shared_ptr<SLE> const& sle,
353 std::int32_t amount,
355
371bool
373 ReadView const& view,
374 uint256 const& root,
376 unsigned int& index,
377 uint256& entry);
378
379bool
381 ApplyView& view,
382 uint256 const& root,
384 unsigned int& index,
385 uint256& entry);
403bool
405 ReadView const& view,
406 uint256 const& root,
408 unsigned int& index,
409 uint256& entry);
410
411bool
412dirNext(
413 ApplyView& view,
414 uint256 const& root,
416 unsigned int& index,
417 uint256& entry);
420[[nodiscard]] std::function<void(SLE::ref)>
421describeOwnerDir(AccountID const& account);
422
423// VFALCO NOTE Both STAmount parameters should just
424// be "Amount", a unit-less number.
425//
430[[nodiscard]] TER
432 ApplyView& view,
433 const bool bSrcHigh,
434 AccountID const& uSrcAccountID,
435 AccountID const& uDstAccountID,
436 uint256 const& uIndex, // --> ripple state entry
437 SLE::ref sleAccount, // --> the account being set.
438 const bool bAuth, // --> authorize account.
439 const bool bNoRipple, // --> others cannot ripple through
440 const bool bFreeze, // --> funds cannot leave
441 STAmount const& saBalance, // --> balance of account being set.
442 // Issuer should be noAccount()
443 STAmount const& saLimit, // --> limit for account being set.
444 // Issuer should be the account being set.
445 std::uint32_t uSrcQualityIn,
446 std::uint32_t uSrcQualityOut,
448
449[[nodiscard]] TER
451 ApplyView& view,
452 std::shared_ptr<SLE> const& sleRippleState,
453 AccountID const& uLowAccountID,
454 AccountID const& uHighAccountID,
456
463// [[nodiscard]] // nodiscard commented out so Flow, BookTip and others compile.
464TER
465offerDelete(ApplyView& view, std::shared_ptr<SLE> const& sle, beast::Journal j);
466
467//------------------------------------------------------------------------------
468
469//
470// Money Transfers
471//
472
473// Direct send w/o fees:
474// - Redeeming IOUs and/or sending sender's own IOUs.
475// - Create trust line of needed.
476// --> bCheckIssuer : normally require issuer to be involved.
477// [[nodiscard]] // nodiscard commented out so DirectStep.cpp compiles.
478
482TER
484 ApplyView& view,
485 AccountID const& uSenderID,
486 AccountID const& uReceiverID,
487 STAmount const& saAmount,
488 bool bCheckIssuer,
490
494[[nodiscard]] TER
496 ApplyView& view,
497 AccountID const& from,
498 AccountID const& to,
499 STAmount const& saAmount,
502
503[[nodiscard]] TER
505 ApplyView& view,
506 AccountID const& account,
507 STAmount const& amount,
508 Issue const& issue,
510
511[[nodiscard]] TER
513 ApplyView& view,
514 AccountID const& account,
515 STAmount const& amount,
516 Issue const& issue,
518
519[[nodiscard]] TER
521 ApplyView& view,
522 AccountID const& from,
523 AccountID const& to,
524 STAmount const& amount,
526
531[[nodiscard]] TER
532requireAuth(ReadView const& view, Issue const& issue, AccountID const& account);
533[[nodiscard]] TER
535 ReadView const& view,
536 MPTIssue const& mptIssue,
537 AccountID const& account);
538
543[[nodiscard]] TER
545 ReadView const& view,
546 MPTIssue const& mptIssue,
547 AccountID const& from,
548 AccountID const& to);
549
556 uint256 const&,
565[[nodiscard]] TER
567 ApplyView& view,
568 Keylet const& ownerDirKeylet,
569 EntryDeleter const& deleter,
571 std::optional<std::uint16_t> maxNodesToDelete = std::nullopt);
572
577[[nodiscard]] TER
579 ApplyView& view,
580 std::shared_ptr<SLE> sleState,
583
584} // namespace ripple
585
586#endif
Provide a light-weight way to check active() before string formatting.
Definition: Journal.h:194
A generic endpoint for log messages.
Definition: Journal.h:59
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:136
constexpr value_type const & value() const
Definition: Asset.h:143
A currency issued by an account.
Definition: Issue.h:36
AccountID account
Definition: Issue.h:39
Currency currency
Definition: Issue.h:38
A view into a ledger.
Definition: ReadView.h:55
const std::shared_ptr< STLedgerEntry > & ref
Definition: STLedgerEntry.h:38
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:350
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:49
std::uint32_t LedgerIndex
A ledger index.
Definition: Protocol.h:119
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
Definition: View.cpp:366
FreezeHandling
Controls the treatment of frozen account balances.
Definition: View.h:80
@ fhZERO_IF_FROZEN
Definition: View.h:80
@ fhIGNORE_FREEZE
Definition: View.h:80
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:136
LedgerIndex getCandidateLedger(LedgerIndex requested)
Find a ledger index from which we could easily get the requested ledger.
Definition: View.h:317
bool isIndividualFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition: View.cpp:204
TER deleteAMMTrustLine(ApplyView &view, std::shared_ptr< SLE > sleState, std::optional< AccountID > const &ammAccountID, beast::Journal j)
Delete trustline to AMM.
Definition: View.cpp:1966
bool dirFirst(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
Definition: View.cpp:114
base_uint< 256 > uint256
Definition: base_uint.h:557
bool dirNext(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
Definition: View.cpp:125
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account)
Check if the account lacks required authorization.
Definition: View.cpp:1815
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Definition: View.cpp:759
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
Definition: View.cpp:573
TER redeemIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1699
base_uint< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
Definition: UintTypes.h:64
AuthHandling
Controls the treatment of unauthorized MPT balances.
Definition: View.h:83
@ ahZERO_IF_UNAUTHORIZED
Definition: View.h:83
@ ahIGNORE_AUTH
Definition: View.h:83
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:848
SkipEntry
Definition: View.h:47
TER transferXRP(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &amount, beast::Journal j)
Definition: View.cpp:1772
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
Definition: View.cpp:1010
bool isFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition: View.cpp:238
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:856
std::set< uint256 > getEnabledAmendments(ReadView const &view)
Definition: View.cpp:719
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to)
Check if the destination account is allowed to receive MPT.
Definition: View.cpp:1868
AccountID ammAccountID(std::uint16_t prefix, uint256 const &parentHash, uint256 const &ammID)
Calculate AMM account ID.
Definition: AMMCore.cpp:30
static bool adjustOwnerCount(ApplyContext &ctx, int count)
Definition: SetOracle.cpp:186
base_uint< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition: UintTypes.h:56
TER issueIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1600
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:147
WaiveTransferFee
Definition: View.h:46
TER trustDelete(ApplyView &view, std::shared_ptr< SLE > const &sleRippleState, AccountID const &uLowAccountID, AccountID const &uHighAccountID, beast::Journal j)
Definition: View.cpp:970
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
Definition: View.cpp:271
majorityAmendments_t getMajorityAmendments(ReadView const &view)
Definition: View.cpp:736
LedgerEntryType
Identifiers for on-ledger objects.
Definition: LedgerFormats.h:54
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:495
TER cleanupOnAccountDelete(ApplyView &view, Keylet const &ownerDirKeylet, EntryDeleter const &deleter, beast::Journal j, std::optional< uint16_t > maxNodesToDelete)
Definition: View.cpp:1888
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:467
Number root(Number f, unsigned d)
Definition: Number.cpp:630
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
Definition: View.cpp:164
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Calls static rippleCreditIOU if saAmount represents Issue.
Definition: View.cpp:2015
TERSubset< CanCvtToTER > TER
Definition: TER.h:627
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:597
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition: Escrow.cpp:89
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
Definition: View.cpp:705
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee)
Calls static accountSendIOU if saAmount represents Issue.
Definition: View.cpp:1526
XRPAmount xrpLiquid(ReadView const &view, AccountID const &id, std::int32_t ownerCountAdj, beast::Journal j)
Definition: View.cpp:430
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:173
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:39
T visit(T... args)