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[[nodiscard]] bool
158 ReadView const& view,
159 AccountID const& account,
160 Currency const& currency,
161 AccountID const& issuer);
162
163// Returns the amount an account can spend without going into debt.
164//
165// <-- saAmount: amount of currency held by account. May be negative.
166[[nodiscard]] STAmount
168 ReadView const& view,
169 AccountID const& account,
170 Currency const& currency,
171 AccountID const& issuer,
172 FreezeHandling zeroIfFrozen,
174
175[[nodiscard]] STAmount
177 ReadView const& view,
178 AccountID const& account,
179 Issue const& issue,
180 FreezeHandling zeroIfFrozen,
182
183[[nodiscard]] STAmount
185 ReadView const& view,
186 AccountID const& account,
187 MPTIssue const& mptIssue,
188 FreezeHandling zeroIfFrozen,
189 AuthHandling zeroIfUnauthorized,
191
192// Returns the amount an account can spend of the currency type saDefault, or
193// returns saDefault if this account is the issuer of the currency in
194// question. Should be used in favor of accountHolds when questioning how much
195// an account can spend while also allowing currency issuers to spend
196// unlimited amounts of their own currency (since they can always issue more).
197[[nodiscard]] STAmount
199 ReadView const& view,
200 AccountID const& id,
201 STAmount const& saDefault,
202 FreezeHandling freezeHandling,
204
205// Return the account's liquid (not reserved) XRP. Generally prefer
206// calling accountHolds() over this interface. However, this interface
207// allows the caller to temporarily adjust the owner count should that be
208// necessary.
209//
210// @param ownerCountAdj positive to add to count, negative to reduce count.
211[[nodiscard]] XRPAmount
213 ReadView const& view,
214 AccountID const& id,
215 std::int32_t ownerCountAdj,
217
219void
221 ReadView const& view,
222 Keylet const& root,
223 std::function<void(std::shared_ptr<SLE const> const&)> const& f);
224
231bool
233 ReadView const& view,
234 Keylet const& root,
235 uint256 const& after,
236 std::uint64_t const hint,
237 unsigned int limit,
238 std::function<bool(std::shared_ptr<SLE const> const&)> const& f);
239
241inline void
243 ReadView const& view,
244 AccountID const& id,
245 std::function<void(std::shared_ptr<SLE const> const&)> const& f)
246{
247 return forEachItem(view, keylet::ownerDir(id), f);
248}
249
256inline bool
258 ReadView const& view,
259 AccountID const& id,
260 uint256 const& after,
261 std::uint64_t const hint,
262 unsigned int limit,
263 std::function<bool(std::shared_ptr<SLE const> const&)> const& f)
264{
265 return forEachItemAfter(view, keylet::ownerDir(id), after, hint, limit, f);
266}
267
273[[nodiscard]] Rate
274transferRate(ReadView const& view, AccountID const& issuer);
275
281[[nodiscard]] Rate
282transferRate(ReadView const& view, MPTID const& issuanceID);
283
287[[nodiscard]] bool
288dirIsEmpty(ReadView const& view, Keylet const& k);
289
290// Return the list of enabled amendments
291[[nodiscard]] std::set<uint256>
292getEnabledAmendments(ReadView const& view);
293
294// Return a map of amendments that have achieved majority
296[[nodiscard]] majorityAmendments_t
298
308[[nodiscard]] std::optional<uint256>
309hashOfSeq(ReadView const& ledger, LedgerIndex seq, beast::Journal journal);
310
323inline LedgerIndex
325{
326 return (requested + 255) & (~255);
327}
328
334[[nodiscard]] bool
336 ReadView const& validLedger,
337 ReadView const& testLedger,
339 const char* reason);
340
341[[nodiscard]] bool
343 uint256 const& validHash,
344 LedgerIndex validIndex,
345 ReadView const& testLedger,
347 const char* reason);
348
349//------------------------------------------------------------------------------
350//
351// Modifiers
352//
353//------------------------------------------------------------------------------
354
356void
358 ApplyView& view,
359 std::shared_ptr<SLE> const& sle,
360 std::int32_t amount,
362
378bool
380 ReadView const& view,
381 uint256 const& root,
383 unsigned int& index,
384 uint256& entry);
385
386bool
388 ApplyView& view,
389 uint256 const& root,
391 unsigned int& index,
392 uint256& entry);
410bool
412 ReadView const& view,
413 uint256 const& root,
415 unsigned int& index,
416 uint256& entry);
417
418bool
419dirNext(
420 ApplyView& view,
421 uint256 const& root,
423 unsigned int& index,
424 uint256& entry);
427[[nodiscard]] std::function<void(SLE::ref)>
428describeOwnerDir(AccountID const& account);
429
430// VFALCO NOTE Both STAmount parameters should just
431// be "Amount", a unit-less number.
432//
437[[nodiscard]] TER
439 ApplyView& view,
440 const bool bSrcHigh,
441 AccountID const& uSrcAccountID,
442 AccountID const& uDstAccountID,
443 uint256 const& uIndex, // --> ripple state entry
444 SLE::ref sleAccount, // --> the account being set.
445 const bool bAuth, // --> authorize account.
446 const bool bNoRipple, // --> others cannot ripple through
447 const bool bFreeze, // --> funds cannot leave
448 bool bDeepFreeze, // --> can neither receive nor send funds
449 STAmount const& saBalance, // --> balance of account being set.
450 // Issuer should be noAccount()
451 STAmount const& saLimit, // --> limit for account being set.
452 // Issuer should be the account being set.
453 std::uint32_t uSrcQualityIn,
454 std::uint32_t uSrcQualityOut,
456
457[[nodiscard]] TER
459 ApplyView& view,
460 std::shared_ptr<SLE> const& sleRippleState,
461 AccountID const& uLowAccountID,
462 AccountID const& uHighAccountID,
464
471// [[nodiscard]] // nodiscard commented out so Flow, BookTip and others compile.
472TER
473offerDelete(ApplyView& view, std::shared_ptr<SLE> const& sle, beast::Journal j);
474
475//------------------------------------------------------------------------------
476
477//
478// Money Transfers
479//
480
481// Direct send w/o fees:
482// - Redeeming IOUs and/or sending sender's own IOUs.
483// - Create trust line of needed.
484// --> bCheckIssuer : normally require issuer to be involved.
485// [[nodiscard]] // nodiscard commented out so DirectStep.cpp compiles.
486
490TER
492 ApplyView& view,
493 AccountID const& uSenderID,
494 AccountID const& uReceiverID,
495 STAmount const& saAmount,
496 bool bCheckIssuer,
498
502[[nodiscard]] TER
504 ApplyView& view,
505 AccountID const& from,
506 AccountID const& to,
507 STAmount const& saAmount,
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& account,
523 STAmount const& amount,
524 Issue const& issue,
526
527[[nodiscard]] TER
529 ApplyView& view,
530 AccountID const& from,
531 AccountID const& to,
532 STAmount const& amount,
534
539[[nodiscard]] TER
540requireAuth(ReadView const& view, Issue const& issue, AccountID const& account);
541[[nodiscard]] TER
543 ReadView const& view,
544 MPTIssue const& mptIssue,
545 AccountID const& account);
546
551[[nodiscard]] TER
553 ReadView const& view,
554 MPTIssue const& mptIssue,
555 AccountID const& from,
556 AccountID const& to);
557
564 uint256 const&,
573[[nodiscard]] TER
575 ApplyView& view,
576 Keylet const& ownerDirKeylet,
577 EntryDeleter const& deleter,
579 std::optional<std::uint16_t> maxNodesToDelete = std::nullopt);
580
585[[nodiscard]] TER
587 ApplyView& view,
588 std::shared_ptr<SLE> sleState,
591
592} // namespace ripple
593
594#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:140
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:405
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:324
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:2012
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:1861
bool isDeepFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition: View.cpp:271
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Definition: View.cpp:798
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
Definition: View.cpp:612
TER redeemIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1745
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:887
SkipEntry
Definition: View.h:47
TER transferXRP(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &amount, beast::Journal j)
Definition: View.cpp:1818
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
Definition: View.cpp:1054
bool isFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition: View.cpp:238
std::set< uint256 > getEnabledAmendments(ReadView const &view)
Definition: View.cpp:758
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:1914
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:1645
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, bool bDeepFreeze, STAmount const &saBalance, STAmount const &saLimit, std::uint32_t uQualityIn, std::uint32_t uQualityOut, beast::Journal j)
Create a trust line.
Definition: View.cpp:895
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:1014
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
Definition: View.cpp:297
majorityAmendments_t getMajorityAmendments(ReadView const &view)
Definition: View.cpp:775
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:534
TER cleanupOnAccountDelete(ApplyView &view, Keylet const &ownerDirKeylet, EntryDeleter const &deleter, beast::Journal j, std::optional< uint16_t > maxNodesToDelete)
Definition: View.cpp:1934
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:506
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:2061
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:636
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:744
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:1571
XRPAmount xrpLiquid(ReadView const &view, AccountID const &id, std::int32_t ownerCountAdj, beast::Journal j)
Definition: View.cpp:469
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)