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 
53 
54 [[nodiscard]] bool
55 isGlobalFrozen(ReadView const& view, AccountID const& issuer);
56 
57 [[nodiscard]] bool
58 isFrozen(
59  ReadView const& view,
60  AccountID const& account,
61  Currency const& currency,
62  AccountID const& issuer);
63 
64 // Returns the amount an account can spend without going into debt.
65 //
66 // <-- saAmount: amount of currency held by account. May be negative.
67 [[nodiscard]] STAmount
69  ReadView const& view,
70  AccountID const& account,
71  Currency const& currency,
72  AccountID const& issuer,
73  FreezeHandling zeroIfFrozen,
74  beast::Journal j);
75 
76 [[nodiscard]] STAmount
78  ReadView const& view,
79  AccountID const& id,
80  STAmount const& saDefault,
81  FreezeHandling freezeHandling,
82  beast::Journal j);
83 
84 // Return the account's liquid (not reserved) XRP. Generally prefer
85 // calling accountHolds() over this interface. However this interface
86 // allows the caller to temporarily adjust the owner count should that be
87 // necessary.
88 //
89 // @param ownerCountAdj positive to add to count, negative to reduce count.
90 [[nodiscard]] XRPAmount
91 xrpLiquid(
92  ReadView const& view,
93  AccountID const& id,
94  std::int32_t ownerCountAdj,
95  beast::Journal j);
96 
98 void
100  ReadView const& view,
101  AccountID const& id,
102  std::function<void(std::shared_ptr<SLE const> const&)> f);
103 
110 bool
112  ReadView const& view,
113  AccountID const& id,
114  uint256 const& after,
115  std::uint64_t const hint,
116  unsigned int limit,
117  std::function<bool(std::shared_ptr<SLE const> const&)> f);
118 
119 [[nodiscard]] Rate
120 transferRate(ReadView const& view, AccountID const& issuer);
121 
125 [[nodiscard]] bool
126 dirIsEmpty(ReadView const& view, Keylet const& k);
127 
128 // Return the first entry and advance uDirEntry.
129 // <-- true, if had a next entry.
130 // VFALCO Fix these clumsy routines with an iterator
131 bool
132 cdirFirst(
133  ReadView const& view,
134  uint256 const& uRootIndex, // --> Root of directory.
135  std::shared_ptr<SLE const>& sleNode, // <-> current node
136  unsigned int& uDirEntry, // <-- next entry
137  uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero.
138  beast::Journal j);
139 
140 // Return the current entry and advance uDirEntry.
141 // <-- true, if had a next entry.
142 // VFALCO Fix these clumsy routines with an iterator
143 bool
144 cdirNext(
145  ReadView const& view,
146  uint256 const& uRootIndex, // --> Root of directory
147  std::shared_ptr<SLE const>& sleNode, // <-> current node
148  unsigned int& uDirEntry, // <-> next entry
149  uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero.
150  beast::Journal j);
151 
152 // Return the list of enabled amendments
153 [[nodiscard]] std::set<uint256>
154 getEnabledAmendments(ReadView const& view);
155 
156 // Return a map of amendments that have achieved majority
158 [[nodiscard]] majorityAmendments_t
159 getMajorityAmendments(ReadView const& view);
160 
170 [[nodiscard]] std::optional<uint256>
171 hashOfSeq(ReadView const& ledger, LedgerIndex seq, beast::Journal journal);
172 
185 inline LedgerIndex
187 {
188  return (requested + 255) & (~255);
189 }
190 
196 [[nodiscard]] bool
198  ReadView const& validLedger,
199  ReadView const& testLedger,
201  const char* reason);
202 
203 [[nodiscard]] bool
205  uint256 const& validHash,
206  LedgerIndex validIndex,
207  ReadView const& testLedger,
209  const char* reason);
210 
211 //------------------------------------------------------------------------------
212 //
213 // Modifiers
214 //
215 //------------------------------------------------------------------------------
216 
218 void
220  ApplyView& view,
221  std::shared_ptr<SLE> const& sle,
222  std::int32_t amount,
223  beast::Journal j);
224 
225 // Return the first entry and advance uDirEntry.
226 // <-- true, if had a next entry.
227 // VFALCO Fix these clumsy routines with an iterator
228 bool
229 dirFirst(
230  ApplyView& view,
231  uint256 const& uRootIndex, // --> Root of directory.
232  std::shared_ptr<SLE>& sleNode, // <-> current node
233  unsigned int& uDirEntry, // <-- next entry
234  uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero.
235  beast::Journal j);
236 
237 // Return the current entry and advance uDirEntry.
238 // <-- true, if had a next entry.
239 // VFALCO Fix these clumsy routines with an iterator
240 bool
241 dirNext(
242  ApplyView& view,
243  uint256 const& uRootIndex, // --> Root of directory
244  std::shared_ptr<SLE>& sleNode, // <-> current node
245  unsigned int& uDirEntry, // <-> next entry
246  uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero.
247  beast::Journal j);
248 
249 [[nodiscard]] std::function<void(SLE::ref)>
250 describeOwnerDir(AccountID const& account);
251 
252 // VFALCO NOTE Both STAmount parameters should just
253 // be "Amount", a unit-less number.
254 //
259 [[nodiscard]] TER
261  ApplyView& view,
262  const bool bSrcHigh,
263  AccountID const& uSrcAccountID,
264  AccountID const& uDstAccountID,
265  uint256 const& uIndex, // --> ripple state entry
266  SLE::ref sleAccount, // --> the account being set.
267  const bool bAuth, // --> authorize account.
268  const bool bNoRipple, // --> others cannot ripple through
269  const bool bFreeze, // --> funds cannot leave
270  STAmount const& saBalance, // --> balance of account being set.
271  // Issuer should be noAccount()
272  STAmount const& saLimit, // --> limit for account being set.
273  // Issuer should be the account being set.
274  std::uint32_t uSrcQualityIn,
275  std::uint32_t uSrcQualityOut,
276  beast::Journal j);
277 
278 [[nodiscard]] TER
280  ApplyView& view,
281  std::shared_ptr<SLE> const& sleRippleState,
282  AccountID const& uLowAccountID,
283  AccountID const& uHighAccountID,
284  beast::Journal j);
285 
292 // [[nodiscard]] // nodiscard commented out so Flow, BookTip and others compile.
293 TER
294 offerDelete(ApplyView& view, std::shared_ptr<SLE> const& sle, beast::Journal j);
295 
296 //------------------------------------------------------------------------------
297 
298 //
299 // Money Transfers
300 //
301 
302 // Direct send w/o fees:
303 // - Redeeming IOUs and/or sending sender's own IOUs.
304 // - Create trust line of needed.
305 // --> bCheckIssuer : normally require issuer to be involved.
306 // [[nodiscard]] // nodiscard commented out so DirectStep.cpp compiles.
307 TER
309  ApplyView& view,
310  AccountID const& uSenderID,
311  AccountID const& uReceiverID,
312  const STAmount& saAmount,
313  bool bCheckIssuer,
314  beast::Journal j);
315 
316 [[nodiscard]] TER
318  ApplyView& view,
319  AccountID const& from,
320  AccountID const& to,
321  const STAmount& saAmount,
322  beast::Journal j);
323 
324 [[nodiscard]] TER
325 issueIOU(
326  ApplyView& view,
327  AccountID const& account,
328  STAmount const& amount,
329  Issue const& issue,
330  beast::Journal j);
331 
332 [[nodiscard]] TER
333 redeemIOU(
334  ApplyView& view,
335  AccountID const& account,
336  STAmount const& amount,
337  Issue const& issue,
338  beast::Journal j);
339 
340 [[nodiscard]] TER
342  ApplyView& view,
343  AccountID const& from,
344  AccountID const& to,
345  STAmount const& amount,
346  beast::Journal j);
347 
348 } // namespace ripple
349 
350 #endif
ripple::rippleCredit
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Definition: View.cpp:919
ripple::transferRate
Rate transferRate(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:351
ripple::getMajorityAmendments
majorityAmendments_t getMajorityAmendments(ReadView const &view)
Definition: View.cpp:554
ripple::LedgerIndex
std::uint32_t LedgerIndex
A ledger index.
Definition: Protocol.h:57
ripple::getEnabledAmendments
std::set< uint256 > getEnabledAmendments(ReadView const &view)
Definition: View.cpp:537
std::shared_ptr
STL class.
ripple::fhZERO_IF_FROZEN
@ fhZERO_IF_FROZEN
Definition: View.h:52
utility
ripple::describeOwnerDir
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:717
functional
ripple::cdirNext
bool cdirNext(ReadView const &view, uint256 const &uRootIndex, std::shared_ptr< SLE const > &sleNode, unsigned int &uDirEntry, uint256 &uEntryIndex, beast::Journal j)
Definition: View.cpp:499
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:96
std::function
ripple::dirNext
bool dirNext(ApplyView &view, uint256 const &uRootIndex, std::shared_ptr< SLE > &sleNode, unsigned int &uDirEntry, uint256 &uEntryIndex, beast::Journal j)
Definition: View.cpp:679
ripple::FreezeHandling
FreezeHandling
Controls the treatment of frozen account balances.
Definition: View.h:52
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:526
ripple::forEachItem
void forEachItem(ReadView const &view, AccountID const &id, std::function< void(std::shared_ptr< SLE const > const &)> f)
Iterate all items in an account's owner directory.
Definition: View.cpp:255
ripple::dirFirst
bool dirFirst(ApplyView &view, uint256 const &uRootIndex, std::shared_ptr< SLE > &sleNode, unsigned int &uDirEntry, uint256 &uEntryIndex, beast::Journal j)
Definition: View.cpp:664
ripple::isGlobalFrozen
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:61
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:646
ripple::offerDelete
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
Definition: View.cpp:879
ripple::TER
TERSubset< CanCvtToTER > TER
Definition: TER.h:552
ripple::accountSend
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j)
Definition: View.cpp:1108
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:219
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:577
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:839
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:362
ripple::accountFunds
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
Definition: View.cpp:140
ripple::transferXRP
TER transferXRP(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &amount, beast::Journal j)
Definition: View.cpp:1433
ripple::issueIOU
TER issueIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1267
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:192
ripple::dirIsEmpty
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
Definition: View.cpp:470
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::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:725
ripple::redeemIOU
TER redeemIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1363
ripple::fhIGNORE_FREEZE
@ fhIGNORE_FREEZE
Definition: View.h:52
ripple::forEachItemAfter
bool forEachItemAfter(ReadView const &view, AccountID const &id, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(std::shared_ptr< SLE const > const &)> f)
Iterate all items after an item in an owner directory.
Definition: View.cpp:278
std::optional
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:186
ripple::STLedgerEntry::ref
const std::shared_ptr< STLedgerEntry > & ref
Definition: STLedgerEntry.h:36
std::set
STL class.
ripple::isFrozen
bool isFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition: View.cpp:73
ripple::AccountID
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:47
ripple::cdirFirst
bool cdirFirst(ReadView const &view, uint256 const &uRootIndex, std::shared_ptr< SLE const > &sleNode, unsigned int &uDirEntry, uint256 &uEntryIndex, beast::Journal j)
Definition: View.cpp:484