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 //------------------------------------------------------------------------------
46 //
47 // Observers
48 //
49 //------------------------------------------------------------------------------
50 
72 [[nodiscard]] bool
73 hasExpired(ReadView const& view, std::optional<std::uint32_t> const& exp);
74 
77 
78 [[nodiscard]] bool
79 isGlobalFrozen(ReadView const& view, AccountID const& issuer);
80 
81 [[nodiscard]] bool
82 isFrozen(
83  ReadView const& view,
84  AccountID const& account,
85  Currency const& currency,
86  AccountID const& issuer);
87 
88 // Returns the amount an account can spend without going into debt.
89 //
90 // <-- saAmount: amount of currency held by account. May be negative.
91 [[nodiscard]] STAmount
93  ReadView const& view,
94  AccountID const& account,
95  Currency const& currency,
96  AccountID const& issuer,
97  FreezeHandling zeroIfFrozen,
98  beast::Journal j);
99 
100 // Returns the amount an account can spend of the currency type saDefault, or
101 // returns saDefault if this account is the issuer of the the currency in
102 // question. Should be used in favor of accountHolds when questioning how much
103 // an account can spend while also allowing currency issuers to spend
104 // unlimited amounts of their own currency (since they can always issue more).
105 [[nodiscard]] STAmount
107  ReadView const& view,
108  AccountID const& id,
109  STAmount const& saDefault,
110  FreezeHandling freezeHandling,
111  beast::Journal j);
112 
113 // Return the account's liquid (not reserved) XRP. Generally prefer
114 // calling accountHolds() over this interface. However this interface
115 // allows the caller to temporarily adjust the owner count should that be
116 // necessary.
117 //
118 // @param ownerCountAdj positive to add to count, negative to reduce count.
119 [[nodiscard]] XRPAmount
120 xrpLiquid(
121  ReadView const& view,
122  AccountID const& id,
123  std::int32_t ownerCountAdj,
124  beast::Journal j);
125 
127 void
129  ReadView const& view,
130  Keylet const& root,
131  std::function<void(std::shared_ptr<SLE const> const&)> const& f);
132 
139 bool
141  ReadView const& view,
142  Keylet const& root,
143  uint256 const& after,
144  std::uint64_t const hint,
145  unsigned int limit,
146  std::function<bool(std::shared_ptr<SLE const> const&)> const& f);
147 
149 inline void
151  ReadView const& view,
152  AccountID const& id,
153  std::function<void(std::shared_ptr<SLE const> const&)> const& f)
154 {
155  return forEachItem(view, keylet::ownerDir(id), f);
156 }
157 
164 inline bool
166  ReadView const& view,
167  AccountID const& id,
168  uint256 const& after,
169  std::uint64_t const hint,
170  unsigned int limit,
171  std::function<bool(std::shared_ptr<SLE const> const&)> const& f)
172 {
173  return forEachItemAfter(view, keylet::ownerDir(id), after, hint, limit, f);
174 }
175 
176 [[nodiscard]] Rate
177 transferRate(ReadView const& view, AccountID const& issuer);
178 
182 [[nodiscard]] bool
183 dirIsEmpty(ReadView const& view, Keylet const& k);
184 
185 // Return the list of enabled amendments
186 [[nodiscard]] std::set<uint256>
187 getEnabledAmendments(ReadView const& view);
188 
189 // Return a map of amendments that have achieved majority
191 [[nodiscard]] majorityAmendments_t
192 getMajorityAmendments(ReadView const& view);
193 
203 [[nodiscard]] std::optional<uint256>
204 hashOfSeq(ReadView const& ledger, LedgerIndex seq, beast::Journal journal);
205 
218 inline LedgerIndex
220 {
221  return (requested + 255) & (~255);
222 }
223 
229 [[nodiscard]] bool
231  ReadView const& validLedger,
232  ReadView const& testLedger,
234  const char* reason);
235 
236 [[nodiscard]] bool
238  uint256 const& validHash,
239  LedgerIndex validIndex,
240  ReadView const& testLedger,
242  const char* reason);
243 
244 //------------------------------------------------------------------------------
245 //
246 // Modifiers
247 //
248 //------------------------------------------------------------------------------
249 
251 void
253  ApplyView& view,
254  std::shared_ptr<SLE> const& sle,
255  std::int32_t amount,
256  beast::Journal j);
257 
273 bool
274 cdirFirst(
275  ReadView const& view,
276  uint256 const& root,
278  unsigned int& index,
279  uint256& entry);
280 
281 bool
282 dirFirst(
283  ApplyView& view,
284  uint256 const& root,
285  std::shared_ptr<SLE>& page,
286  unsigned int& index,
287  uint256& entry);
305 bool
306 cdirNext(
307  ReadView const& view,
308  uint256 const& root,
310  unsigned int& index,
311  uint256& entry);
312 
313 bool
314 dirNext(
315  ApplyView& view,
316  uint256 const& root,
317  std::shared_ptr<SLE>& page,
318  unsigned int& index,
319  uint256& entry);
322 [[nodiscard]] std::function<void(SLE::ref)>
323 describeOwnerDir(AccountID const& account);
324 
325 // VFALCO NOTE Both STAmount parameters should just
326 // be "Amount", a unit-less number.
327 //
332 [[nodiscard]] TER
334  ApplyView& view,
335  const bool bSrcHigh,
336  AccountID const& uSrcAccountID,
337  AccountID const& uDstAccountID,
338  uint256 const& uIndex, // --> ripple state entry
339  SLE::ref sleAccount, // --> the account being set.
340  const bool bAuth, // --> authorize account.
341  const bool bNoRipple, // --> others cannot ripple through
342  const bool bFreeze, // --> funds cannot leave
343  STAmount const& saBalance, // --> balance of account being set.
344  // Issuer should be noAccount()
345  STAmount const& saLimit, // --> limit for account being set.
346  // Issuer should be the account being set.
347  std::uint32_t uSrcQualityIn,
348  std::uint32_t uSrcQualityOut,
349  beast::Journal j);
350 
351 [[nodiscard]] TER
353  ApplyView& view,
354  std::shared_ptr<SLE> const& sleRippleState,
355  AccountID const& uLowAccountID,
356  AccountID const& uHighAccountID,
357  beast::Journal j);
358 
365 // [[nodiscard]] // nodiscard commented out so Flow, BookTip and others compile.
366 TER
367 offerDelete(ApplyView& view, std::shared_ptr<SLE> const& sle, beast::Journal j);
368 
369 //------------------------------------------------------------------------------
370 
371 //
372 // Money Transfers
373 //
374 
375 // Direct send w/o fees:
376 // - Redeeming IOUs and/or sending sender's own IOUs.
377 // - Create trust line of needed.
378 // --> bCheckIssuer : normally require issuer to be involved.
379 // [[nodiscard]] // nodiscard commented out so DirectStep.cpp compiles.
380 TER
382  ApplyView& view,
383  AccountID const& uSenderID,
384  AccountID const& uReceiverID,
385  const STAmount& saAmount,
386  bool bCheckIssuer,
387  beast::Journal j);
388 
389 [[nodiscard]] TER
391  ApplyView& view,
392  AccountID const& from,
393  AccountID const& to,
394  const STAmount& saAmount,
395  beast::Journal j);
396 
397 [[nodiscard]] TER
398 issueIOU(
399  ApplyView& view,
400  AccountID const& account,
401  STAmount const& amount,
402  Issue const& issue,
403  beast::Journal j);
404 
405 [[nodiscard]] TER
406 redeemIOU(
407  ApplyView& view,
408  AccountID const& account,
409  STAmount const& amount,
410  Issue const& issue,
411  beast::Journal j);
412 
413 [[nodiscard]] TER
415  ApplyView& view,
416  AccountID const& from,
417  AccountID const& to,
418  STAmount const& amount,
419  beast::Journal j);
420 
421 } // namespace ripple
422 
423 #endif
ripple::rippleCredit
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Definition: View.cpp:933
ripple::keylet::ownerDir
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:303
ripple::transferRate
Rate transferRate(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:471
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:621
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:604
std::shared_ptr
STL class.
ripple::fhZERO_IF_FROZEN
@ fhZERO_IF_FROZEN
Definition: View.h:76
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:394
ripple::describeOwnerDir
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:731
functional
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:223
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:179
std::function
ripple::FreezeHandling
FreezeHandling
Controls the treatment of frozen account balances.
Definition: View.h:76
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:188
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:713
ripple::offerDelete
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
Definition: View.cpp:893
ripple::TER
TERSubset< CanCvtToTER > TER
Definition: TER.h:568
ripple::accountSend
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j)
Definition: View.cpp:1122
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:331
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:644
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:853
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:482
ripple::accountFunds
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
Definition: View.cpp:267
ripple::transferXRP
TER transferXRP(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &amount, beast::Journal j)
Definition: View.cpp:1447
ripple::issueIOU
TER issueIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1281
ripple::dirIsEmpty
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
Definition: View.cpp:590
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:125
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::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:739
ripple::redeemIOU
TER redeemIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1377
ripple::fhIGNORE_FREEZE
@ fhIGNORE_FREEZE
Definition: View.h:76
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:367
ripple::after
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition: Escrow.cpp:88
ripple::getCandidateLedger
LedgerIndex getCandidateLedger(LedgerIndex requested)
Find a ledger index from which we could easily get the requested ledger.
Definition: View.h:219
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:200
ripple::AccountID
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:47
ripple::root
Number root(Number f, unsigned d)
Definition: Number.cpp:624