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 <boost/optional.hpp>
37 #include <functional>
38 #include <map>
39 #include <memory>
40 #include <utility>
41 
42 #include <vector>
43 
44 namespace ripple {
45 
46 //------------------------------------------------------------------------------
47 //
48 // Observers
49 //
50 //------------------------------------------------------------------------------
51 
54 
55 [[nodiscard]] bool
56 isGlobalFrozen(ReadView const& view, AccountID const& issuer);
57 
58 [[nodiscard]] bool
59 isFrozen(
60  ReadView const& view,
61  AccountID const& account,
62  Currency const& currency,
63  AccountID const& issuer);
64 
65 // Returns the amount an account can spend without going into debt.
66 //
67 // <-- saAmount: amount of currency held by account. May be negative.
68 [[nodiscard]] STAmount
70  ReadView const& view,
71  AccountID const& account,
72  Currency const& currency,
73  AccountID const& issuer,
74  FreezeHandling zeroIfFrozen,
75  beast::Journal j);
76 
77 [[nodiscard]] STAmount
79  ReadView const& view,
80  AccountID const& id,
81  STAmount const& saDefault,
82  FreezeHandling freezeHandling,
83  beast::Journal j);
84 
85 // Return the account's liquid (not reserved) XRP. Generally prefer
86 // calling accountHolds() over this interface. However this interface
87 // allows the caller to temporarily adjust the owner count should that be
88 // necessary.
89 //
90 // @param ownerCountAdj positive to add to count, negative to reduce count.
91 [[nodiscard]] XRPAmount
92 xrpLiquid(
93  ReadView const& view,
94  AccountID const& id,
95  std::int32_t ownerCountAdj,
96  beast::Journal j);
97 
99 void
101  ReadView const& view,
102  AccountID const& id,
103  std::function<void(std::shared_ptr<SLE const> const&)> f);
104 
111 bool
113  ReadView const& view,
114  AccountID const& id,
115  uint256 const& after,
116  std::uint64_t const hint,
117  unsigned int limit,
118  std::function<bool(std::shared_ptr<SLE const> const&)> f);
119 
120 [[nodiscard]] Rate
121 transferRate(ReadView const& view, AccountID const& issuer);
122 
126 [[nodiscard]] bool
127 dirIsEmpty(ReadView const& view, Keylet const& k);
128 
129 // Return the first entry and advance uDirEntry.
130 // <-- true, if had a next entry.
131 // VFALCO Fix these clumsy routines with an iterator
132 bool
133 cdirFirst(
134  ReadView const& view,
135  uint256 const& uRootIndex, // --> Root of directory.
136  std::shared_ptr<SLE const>& sleNode, // <-> current node
137  unsigned int& uDirEntry, // <-- next entry
138  uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero.
139  beast::Journal j);
140 
141 // Return the current entry and advance uDirEntry.
142 // <-- true, if had a next entry.
143 // VFALCO Fix these clumsy routines with an iterator
144 bool
145 cdirNext(
146  ReadView const& view,
147  uint256 const& uRootIndex, // --> Root of directory
148  std::shared_ptr<SLE const>& sleNode, // <-> current node
149  unsigned int& uDirEntry, // <-> next entry
150  uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero.
151  beast::Journal j);
152 
153 // Return the list of enabled amendments
154 [[nodiscard]] std::set<uint256>
155 getEnabledAmendments(ReadView const& view);
156 
157 // Return a map of amendments that have achieved majority
159 [[nodiscard]] majorityAmendments_t
160 getMajorityAmendments(ReadView const& view);
161 
171 [[nodiscard]] boost::optional<uint256>
172 hashOfSeq(ReadView const& ledger, LedgerIndex seq, beast::Journal journal);
173 
186 inline LedgerIndex
188 {
189  return (requested + 255) & (~255);
190 }
191 
197 [[nodiscard]] bool
199  ReadView const& validLedger,
200  ReadView const& testLedger,
202  const char* reason);
203 
204 [[nodiscard]] bool
206  uint256 const& validHash,
207  LedgerIndex validIndex,
208  ReadView const& testLedger,
210  const char* reason);
211 
212 //------------------------------------------------------------------------------
213 //
214 // Modifiers
215 //
216 //------------------------------------------------------------------------------
217 
219 void
221  ApplyView& view,
222  std::shared_ptr<SLE> const& sle,
223  std::int32_t amount,
224  beast::Journal j);
225 
226 // Return the first entry and advance uDirEntry.
227 // <-- true, if had a next entry.
228 // VFALCO Fix these clumsy routines with an iterator
229 bool
230 dirFirst(
231  ApplyView& view,
232  uint256 const& uRootIndex, // --> Root of directory.
233  std::shared_ptr<SLE>& sleNode, // <-> current node
234  unsigned int& uDirEntry, // <-- next entry
235  uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero.
236  beast::Journal j);
237 
238 // Return the current entry and advance uDirEntry.
239 // <-- true, if had a next entry.
240 // VFALCO Fix these clumsy routines with an iterator
241 bool
242 dirNext(
243  ApplyView& view,
244  uint256 const& uRootIndex, // --> Root of directory
245  std::shared_ptr<SLE>& sleNode, // <-> current node
246  unsigned int& uDirEntry, // <-> next entry
247  uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero.
248  beast::Journal j);
249 
250 [[nodiscard]] std::function<void(SLE::ref)>
251 describeOwnerDir(AccountID const& account);
252 
253 // deprecated
254 boost::optional<std::uint64_t>
255 dirAdd(
256  ApplyView& view,
257  Keylet const& uRootIndex,
258  uint256 const& uLedgerIndex,
259  bool strictOrder,
260  std::function<void(SLE::ref)> fDescriber,
261  beast::Journal j);
262 
263 // VFALCO NOTE Both STAmount parameters should just
264 // be "Amount", a unit-less number.
265 //
270 [[nodiscard]] TER
272  ApplyView& view,
273  const bool bSrcHigh,
274  AccountID const& uSrcAccountID,
275  AccountID const& uDstAccountID,
276  uint256 const& uIndex, // --> ripple state entry
277  SLE::ref sleAccount, // --> the account being set.
278  const bool bAuth, // --> authorize account.
279  const bool bNoRipple, // --> others cannot ripple through
280  const bool bFreeze, // --> funds cannot leave
281  STAmount const& saBalance, // --> balance of account being set.
282  // Issuer should be noAccount()
283  STAmount const& saLimit, // --> limit for account being set.
284  // Issuer should be the account being set.
285  std::uint32_t uSrcQualityIn,
286  std::uint32_t uSrcQualityOut,
287  beast::Journal j);
288 
289 [[nodiscard]] TER
291  ApplyView& view,
292  std::shared_ptr<SLE> const& sleRippleState,
293  AccountID const& uLowAccountID,
294  AccountID const& uHighAccountID,
295  beast::Journal j);
296 
303 // [[nodiscard]] // nodiscard commented out so Flow, BookTip and others compile.
304 TER
305 offerDelete(ApplyView& view, std::shared_ptr<SLE> const& sle, beast::Journal j);
306 
307 //------------------------------------------------------------------------------
308 
309 //
310 // Money Transfers
311 //
312 
313 // Direct send w/o fees:
314 // - Redeeming IOUs and/or sending sender's own IOUs.
315 // - Create trust line of needed.
316 // --> bCheckIssuer : normally require issuer to be involved.
317 // [[nodiscard]] // nodiscard commented out so DirectStep.cpp compiles.
318 TER
320  ApplyView& view,
321  AccountID const& uSenderID,
322  AccountID const& uReceiverID,
323  const STAmount& saAmount,
324  bool bCheckIssuer,
325  beast::Journal j);
326 
327 [[nodiscard]] TER
329  ApplyView& view,
330  AccountID const& from,
331  AccountID const& to,
332  const STAmount& saAmount,
333  beast::Journal j);
334 
335 [[nodiscard]] TER
336 issueIOU(
337  ApplyView& view,
338  AccountID const& account,
339  STAmount const& amount,
340  Issue const& issue,
341  beast::Journal j);
342 
343 [[nodiscard]] TER
344 redeemIOU(
345  ApplyView& view,
346  AccountID const& account,
347  STAmount const& amount,
348  Issue const& issue,
349  beast::Journal j);
350 
351 [[nodiscard]] TER
353  ApplyView& view,
354  AccountID const& from,
355  AccountID const& to,
356  STAmount const& amount,
357  beast::Journal j);
358 
359 } // namespace ripple
360 
361 #endif
ripple::rippleCredit
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Definition: View.cpp:936
ripple::transferRate
Rate transferRate(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:347
ripple::getMajorityAmendments
majorityAmendments_t getMajorityAmendments(ReadView const &view)
Definition: View.cpp:550
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:533
std::shared_ptr
STL class.
ripple::fhZERO_IF_FROZEN
@ fhZERO_IF_FROZEN
Definition: View.h:53
utility
ripple::describeOwnerDir
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:713
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:495
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:92
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:675
ripple::FreezeHandling
FreezeHandling
Controls the treatment of frozen account balances.
Definition: View.h:53
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:493
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:251
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:660
ripple::isGlobalFrozen
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:57
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:642
ripple::offerDelete
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
Definition: View.cpp:896
ripple::TER
TERSubset< CanCvtToTER > TER
Definition: TER.h:547
ripple::accountSend
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j)
Definition: View.cpp:1125
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:215
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:856
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:358
ripple::accountFunds
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
Definition: View.cpp:136
ripple::transferXRP
TER transferXRP(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &amount, beast::Journal j)
Definition: View.cpp:1450
ripple::dirAdd
boost::optional< std::uint64_t > dirAdd(ApplyView &view, Keylet const &dir, uint256 const &uLedgerIndex, bool strictOrder, std::function< void(SLE::ref)> fDescriber, beast::Journal j)
Definition: View.cpp:721
ripple::hashOfSeq
boost::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Definition: View.cpp:573
ripple::issueIOU
TER issueIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1284
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:188
ripple::dirIsEmpty
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
Definition: View.cpp:466
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:736
ripple::redeemIOU
TER redeemIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1380
ripple::fhIGNORE_FREEZE
@ fhIGNORE_FREEZE
Definition: View.h:53
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:274
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:187
ripple::STLedgerEntry::ref
const std::shared_ptr< STLedgerEntry > & ref
Definition: STLedgerEntry.h:42
std::set
STL class.
ripple::isFrozen
bool isFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition: View.cpp:69
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:480