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 [[nodiscard]] STAmount
102  ReadView const& view,
103  AccountID const& id,
104  STAmount const& saDefault,
105  FreezeHandling freezeHandling,
106  beast::Journal j);
107 
108 // Return the account's liquid (not reserved) XRP. Generally prefer
109 // calling accountHolds() over this interface. However this interface
110 // allows the caller to temporarily adjust the owner count should that be
111 // necessary.
112 //
113 // @param ownerCountAdj positive to add to count, negative to reduce count.
114 [[nodiscard]] XRPAmount
115 xrpLiquid(
116  ReadView const& view,
117  AccountID const& id,
118  std::int32_t ownerCountAdj,
119  beast::Journal j);
120 
122 void
124  ReadView const& view,
125  Keylet const& root,
126  std::function<void(std::shared_ptr<SLE const> const&)> const& f);
127 
134 bool
136  ReadView const& view,
137  Keylet const& root,
138  uint256 const& after,
139  std::uint64_t const hint,
140  unsigned int limit,
141  std::function<bool(std::shared_ptr<SLE const> const&)> const& f);
142 
144 inline void
146  ReadView const& view,
147  AccountID const& id,
148  std::function<void(std::shared_ptr<SLE const> const&)> const& f)
149 {
150  return forEachItem(view, keylet::ownerDir(id), f);
151 }
152 
159 inline bool
161  ReadView const& view,
162  AccountID const& id,
163  uint256 const& after,
164  std::uint64_t const hint,
165  unsigned int limit,
166  std::function<bool(std::shared_ptr<SLE const> const&)> const& f)
167 {
168  return forEachItemAfter(view, keylet::ownerDir(id), after, hint, limit, f);
169 }
170 
171 [[nodiscard]] Rate
172 transferRate(ReadView const& view, AccountID const& issuer);
173 
177 [[nodiscard]] bool
178 dirIsEmpty(ReadView const& view, Keylet const& k);
179 
180 // Return the list of enabled amendments
181 [[nodiscard]] std::set<uint256>
182 getEnabledAmendments(ReadView const& view);
183 
184 // Return a map of amendments that have achieved majority
186 [[nodiscard]] majorityAmendments_t
187 getMajorityAmendments(ReadView const& view);
188 
198 [[nodiscard]] std::optional<uint256>
199 hashOfSeq(ReadView const& ledger, LedgerIndex seq, beast::Journal journal);
200 
213 inline LedgerIndex
215 {
216  return (requested + 255) & (~255);
217 }
218 
224 [[nodiscard]] bool
226  ReadView const& validLedger,
227  ReadView const& testLedger,
229  const char* reason);
230 
231 [[nodiscard]] bool
233  uint256 const& validHash,
234  LedgerIndex validIndex,
235  ReadView const& testLedger,
237  const char* reason);
238 
239 //------------------------------------------------------------------------------
240 //
241 // Modifiers
242 //
243 //------------------------------------------------------------------------------
244 
246 void
248  ApplyView& view,
249  std::shared_ptr<SLE> const& sle,
250  std::int32_t amount,
251  beast::Journal j);
252 
268 bool
269 cdirFirst(
270  ReadView const& view,
271  uint256 const& root,
273  unsigned int& index,
274  uint256& entry);
275 
276 bool
277 dirFirst(
278  ApplyView& view,
279  uint256 const& root,
280  std::shared_ptr<SLE>& page,
281  unsigned int& index,
282  uint256& entry);
300 bool
301 cdirNext(
302  ReadView const& view,
303  uint256 const& root,
305  unsigned int& index,
306  uint256& entry);
307 
308 bool
309 dirNext(
310  ApplyView& view,
311  uint256 const& root,
312  std::shared_ptr<SLE>& page,
313  unsigned int& index,
314  uint256& entry);
317 [[nodiscard]] std::function<void(SLE::ref)>
318 describeOwnerDir(AccountID const& account);
319 
320 // VFALCO NOTE Both STAmount parameters should just
321 // be "Amount", a unit-less number.
322 //
327 [[nodiscard]] TER
329  ApplyView& view,
330  const bool bSrcHigh,
331  AccountID const& uSrcAccountID,
332  AccountID const& uDstAccountID,
333  uint256 const& uIndex, // --> ripple state entry
334  SLE::ref sleAccount, // --> the account being set.
335  const bool bAuth, // --> authorize account.
336  const bool bNoRipple, // --> others cannot ripple through
337  const bool bFreeze, // --> funds cannot leave
338  STAmount const& saBalance, // --> balance of account being set.
339  // Issuer should be noAccount()
340  STAmount const& saLimit, // --> limit for account being set.
341  // Issuer should be the account being set.
342  std::uint32_t uSrcQualityIn,
343  std::uint32_t uSrcQualityOut,
344  beast::Journal j);
345 
346 [[nodiscard]] TER
348  ApplyView& view,
349  std::shared_ptr<SLE> const& sleRippleState,
350  AccountID const& uLowAccountID,
351  AccountID const& uHighAccountID,
352  beast::Journal j);
353 
360 // [[nodiscard]] // nodiscard commented out so Flow, BookTip and others compile.
361 TER
362 offerDelete(ApplyView& view, std::shared_ptr<SLE> const& sle, beast::Journal j);
363 
364 //------------------------------------------------------------------------------
365 
366 //
367 // Money Transfers
368 //
369 
370 // Direct send w/o fees:
371 // - Redeeming IOUs and/or sending sender's own IOUs.
372 // - Create trust line of needed.
373 // --> bCheckIssuer : normally require issuer to be involved.
374 // [[nodiscard]] // nodiscard commented out so DirectStep.cpp compiles.
375 TER
377  ApplyView& view,
378  AccountID const& uSenderID,
379  AccountID const& uReceiverID,
380  const STAmount& saAmount,
381  bool bCheckIssuer,
382  beast::Journal j);
383 
384 [[nodiscard]] TER
386  ApplyView& view,
387  AccountID const& from,
388  AccountID const& to,
389  const STAmount& saAmount,
390  beast::Journal j);
391 
392 [[nodiscard]] TER
393 issueIOU(
394  ApplyView& view,
395  AccountID const& account,
396  STAmount const& amount,
397  Issue const& issue,
398  beast::Journal j);
399 
400 [[nodiscard]] TER
401 redeemIOU(
402  ApplyView& view,
403  AccountID const& account,
404  STAmount const& amount,
405  Issue const& issue,
406  beast::Journal j);
407 
408 [[nodiscard]] TER
410  ApplyView& view,
411  AccountID const& from,
412  AccountID const& to,
413  STAmount const& amount,
414  beast::Journal j);
415 
416 } // namespace ripple
417 
418 #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:549
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:81
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:565
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:214
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