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/ledger/ApplyView.h>
24 #include <ripple/ledger/OpenView.h>
25 #include <ripple/ledger/RawView.h>
26 #include <ripple/ledger/ReadView.h>
27 #include <ripple/protocol/Protocol.h>
28 #include <ripple/protocol/Rate.h>
29 #include <ripple/protocol/Serializer.h>
30 #include <ripple/protocol/STLedgerEntry.h>
31 #include <ripple/protocol/STObject.h>
32 #include <ripple/protocol/STTx.h>
33 #include <ripple/protocol/TER.h>
34 #include <ripple/core/Config.h>
35 #include <ripple/beast/utility/Journal.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 {
57 };
58 
59 [[nodiscard]] bool
60 isGlobalFrozen (ReadView const& view,
61  AccountID const& issuer);
62 
63 [[nodiscard]] bool
64 isFrozen (ReadView const& view, AccountID const& account,
65  Currency const& currency, AccountID const& issuer);
66 
67 // Returns the amount an account can spend without going into debt.
68 //
69 // <-- saAmount: amount of currency held by account. May be negative.
70 [[nodiscard]] STAmount
71 accountHolds (ReadView const& view,
72  AccountID const& account, Currency const& currency,
73  AccountID const& issuer, FreezeHandling zeroIfFrozen,
74  beast::Journal j);
75 
76 [[nodiscard]] STAmount
77 accountFunds (ReadView const& view, AccountID const& id,
78  STAmount const& saDefault, FreezeHandling freezeHandling,
79  beast::Journal j);
80 
81 // Return the account's liquid (not reserved) XRP. Generally prefer
82 // calling accountHolds() over this interface. However this interface
83 // allows the caller to temporarily adjust the owner count should that be
84 // necessary.
85 //
86 // @param ownerCountAdj positive to add to count, negative to reduce count.
87 [[nodiscard]] XRPAmount
88 xrpLiquid (ReadView const& view, AccountID const& id,
89  std::int32_t ownerCountAdj, beast::Journal j);
90 
92 void
93 forEachItem (ReadView const& view, AccountID const& id,
94  std::function<void (std::shared_ptr<SLE const> const&)> f);
95 
102 bool
103 forEachItemAfter (ReadView const& view, AccountID const& id,
104  uint256 const& after, std::uint64_t const hint,
105  unsigned int limit, std::function<
106  bool (std::shared_ptr<SLE const> const&)> f);
107 
108 [[nodiscard]] Rate
109 transferRate (ReadView const& view,
110  AccountID const& issuer);
111 
115 [[nodiscard]] bool
116 dirIsEmpty (ReadView const& view,
117  Keylet const& k);
118 
119 // Return the first entry and advance uDirEntry.
120 // <-- true, if had a next entry.
121 // VFALCO Fix these clumsy routines with an iterator
122 bool
123 cdirFirst (ReadView const& view,
124  uint256 const& uRootIndex, // --> Root of directory.
125  std::shared_ptr<SLE const>& sleNode, // <-> current node
126  unsigned int& uDirEntry, // <-- next entry
127  uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero.
128  beast::Journal j);
129 
130 // Return the current entry and advance uDirEntry.
131 // <-- true, if had a next entry.
132 // VFALCO Fix these clumsy routines with an iterator
133 bool
134 cdirNext (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 list of enabled amendments
142 [[nodiscard]] std::set <uint256>
143 getEnabledAmendments (ReadView const& view);
144 
145 // Return a map of amendments that have achieved majority
147 [[nodiscard]] majorityAmendments_t
148 getMajorityAmendments (ReadView const& view);
149 
159 [[nodiscard]] boost::optional<uint256>
160 hashOfSeq (ReadView const& ledger, LedgerIndex seq,
161  beast::Journal journal);
162 
175 inline
178 {
179  return (requested + 255) & (~255);
180 }
181 
187 [[nodiscard]] bool
188 areCompatible (ReadView const& validLedger, ReadView const& testLedger,
189  beast::Journal::Stream& s, const char* reason);
190 
191 [[nodiscard]] bool
192 areCompatible (uint256 const& validHash, LedgerIndex validIndex,
193  ReadView const& testLedger, beast::Journal::Stream& s, const char* reason);
194 
195 //------------------------------------------------------------------------------
196 //
197 // Modifiers
198 //
199 //------------------------------------------------------------------------------
200 
202 void
203 adjustOwnerCount (ApplyView& view,
204  std::shared_ptr<SLE> const& sle,
205  std::int32_t amount, beast::Journal j);
206 
207 // Return the first entry and advance uDirEntry.
208 // <-- true, if had a next entry.
209 // VFALCO Fix these clumsy routines with an iterator
210 bool
211 dirFirst (ApplyView& view,
212  uint256 const& uRootIndex, // --> Root of directory.
213  std::shared_ptr<SLE>& sleNode, // <-> current node
214  unsigned int& uDirEntry, // <-- next entry
215  uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero.
216  beast::Journal j);
217 
218 // Return the current entry and advance uDirEntry.
219 // <-- true, if had a next entry.
220 // VFALCO Fix these clumsy routines with an iterator
221 bool
222 dirNext (ApplyView& view,
223  uint256 const& uRootIndex, // --> Root of directory
224  std::shared_ptr<SLE>& sleNode, // <-> current node
225  unsigned int& uDirEntry, // <-> next entry
226  uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero.
227  beast::Journal j);
228 
229 [[nodiscard]] std::function<void (SLE::ref)>
230 describeOwnerDir(AccountID const& account);
231 
232 // deprecated
233 boost::optional<std::uint64_t>
234 dirAdd (ApplyView& view,
235  Keylet const& uRootIndex,
236  uint256 const& uLedgerIndex,
237  bool strictOrder,
238  std::function<void (SLE::ref)> fDescriber,
239  beast::Journal j);
240 
241 // VFALCO NOTE Both STAmount parameters should just
242 // be "Amount", a unit-less number.
243 //
248 [[nodiscard]] TER
249 trustCreate (ApplyView& view,
250  const bool bSrcHigh,
251  AccountID const& uSrcAccountID,
252  AccountID const& uDstAccountID,
253  uint256 const& uIndex, // --> ripple state entry
254  SLE::ref sleAccount, // --> the account being set.
255  const bool bAuth, // --> authorize account.
256  const bool bNoRipple, // --> others cannot ripple through
257  const bool bFreeze, // --> funds cannot leave
258  STAmount const& saBalance, // --> balance of account being set.
259  // Issuer should be noAccount()
260  STAmount const& saLimit, // --> limit for account being set.
261  // Issuer should be the account being set.
262  std::uint32_t uSrcQualityIn,
263  std::uint32_t uSrcQualityOut,
264  beast::Journal j);
265 
266 [[nodiscard]] TER
267 trustDelete (ApplyView& view,
268  std::shared_ptr<SLE> const& sleRippleState,
269  AccountID const& uLowAccountID,
270  AccountID const& uHighAccountID,
271  beast::Journal j);
272 
279 // [[nodiscard]] // nodiscard commented out so Flow, BookTip and others compile.
280 TER
281 offerDelete (ApplyView& view,
282  std::shared_ptr<SLE> const& sle,
283  beast::Journal j);
284 
285 //------------------------------------------------------------------------------
286 
287 //
288 // Money Transfers
289 //
290 
291 // Direct send w/o fees:
292 // - Redeeming IOUs and/or sending sender's own IOUs.
293 // - Create trust line of needed.
294 // --> bCheckIssuer : normally require issuer to be involved.
295 // [[nodiscard]] // nodiscard commented out so DirectStep.cpp compiles.
296 TER
297 rippleCredit (ApplyView& view,
298  AccountID const& uSenderID, AccountID const& uReceiverID,
299  const STAmount & saAmount, bool bCheckIssuer,
300  beast::Journal j);
301 
302 [[nodiscard]]
303 TER
304 accountSend (ApplyView& view,
305  AccountID const& from,
306  AccountID const& to,
307  const STAmount & saAmount,
308  beast::Journal j);
309 
310 [[nodiscard]] TER
311 issueIOU (ApplyView& view,
312  AccountID const& account,
313  STAmount const& amount,
314  Issue const& issue,
315  beast::Journal j);
316 
317 [[nodiscard]] TER
318 redeemIOU (ApplyView& view,
319  AccountID const& account,
320  STAmount const& amount,
321  Issue const& issue,
322  beast::Journal j);
323 
324 [[nodiscard]] TER
325 transferXRP (ApplyView& view,
326  AccountID const& from,
327  AccountID const& to,
328  STAmount const& amount,
329  beast::Journal j);
330 
331 } // ripple
332 
333 #endif
ripple::rippleCredit
TER rippleCredit(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Definition: View.cpp:914
ripple::transferRate
Rate transferRate(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:338
ripple::getMajorityAmendments
majorityAmendments_t getMajorityAmendments(ReadView const &view)
Definition: View.cpp:530
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:513
std::shared_ptr
STL class.
ripple::fhZERO_IF_FROZEN
@ fhZERO_IF_FROZEN
Definition: View.h:56
utility
ripple::describeOwnerDir
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:700
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:472
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:91
ripple::AccountID
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:48
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:659
ripple::FreezeHandling
FreezeHandling
Controls the treatment of frozen account balances.
Definition: View.h:53
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:436
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:244
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:645
ripple::isGlobalFrozen
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
Definition: View.cpp:56
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:629
ripple::offerDelete
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
Definition: View.cpp:871
ripple::TER
TERSubset< CanCvtToTER > TER
Definition: TER.h:477
ripple::accountSend
TER accountSend(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, beast::Journal j)
Definition: View.cpp:1101
beast::Journal::Stream
Provide a light-weight way to check active() before string formatting.
Definition: Journal.h:179
ripple::xrpLiquid
XRPAmount xrpLiquid(ReadView const &view, AccountID const &id, std::int32_t ownerCountAdj, beast::Journal j)
Definition: View.cpp:207
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
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:830
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:350
ripple::accountFunds
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
Definition: View.cpp:133
ripple::transferXRP
TER transferXRP(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &amount, beast::Journal j)
Definition: View.cpp:1405
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:709
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:553
ripple::issueIOU
TER issueIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1262
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:186
ripple::dirIsEmpty
bool dirIsEmpty(ReadView const &view, Keylet const &k)
Returns true if the directory is empty.
Definition: View.cpp:443
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:723
ripple::redeemIOU
TER redeemIOU(ApplyView &view, AccountID const &account, STAmount const &amount, Issue const &issue, beast::Journal j)
Definition: View.cpp:1335
ripple::fhIGNORE_FREEZE
@ fhIGNORE_FREEZE
Definition: View.h:55
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:266
ripple::after
static bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition: Escrow.cpp:87
ripple::getCandidateLedger
LedgerIndex getCandidateLedger(LedgerIndex requested)
Find a ledger index from which we could easily get the requested ledger.
Definition: View.h:177
ripple::STLedgerEntry::ref
const std::shared_ptr< STLedgerEntry > & ref
Definition: STLedgerEntry.h:40
std::set
STL class.
ripple::isFrozen
bool isFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition: View.cpp:68
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:458