rippled
Loading...
Searching...
No Matches
PaymentSandbox.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_PAYMENTSANDBOX_H_INCLUDED
21#define RIPPLE_LEDGER_PAYMENTSANDBOX_H_INCLUDED
22
23#include <xrpld/ledger/RawView.h>
24#include <xrpld/ledger/Sandbox.h>
25#include <xrpld/ledger/detail/ApplyViewBase.h>
26
27#include <xrpl/protocol/AccountID.h>
28
29#include <map>
30#include <utility>
31
32namespace ripple {
33
34namespace detail {
35
36// VFALCO TODO Inline this implementation
37// into the PaymentSandbox class itself
39{
40public:
42 {
43 Adjustment(STAmount const& d, STAmount const& c, STAmount const& b)
44 : debits(d), credits(c), origBalance(b)
45 {
46 }
50 };
51
52 // Get the adjustments for the balance between main and other.
53 // Returns the debits, credits and the original balance
56 AccountID const& main,
57 AccountID const& other,
58 Currency const& currency) const;
59
60 void
61 credit(
62 AccountID const& sender,
63 AccountID const& receiver,
64 STAmount const& amount,
65 STAmount const& preCreditSenderBalance);
66
67 void
68 ownerCount(AccountID const& id, std::uint32_t cur, std::uint32_t next);
69
70 // Get the adjusted owner count. Since DeferredCredits is meant to be used
71 // in payments, and payments only decrease owner counts, return the max
72 // remembered owner count.
74 ownerCount(AccountID const& id) const;
75
76 void
78
79private:
80 // lowAccount, highAccount
82 struct Value
83 {
84 explicit Value() = default;
85
89 };
90
91 static Key
92 makeKey(AccountID const& a1, AccountID const& a2, Currency const& c);
93
96};
97
98} // namespace detail
99
100//------------------------------------------------------------------------------
101
115{
116public:
117 PaymentSandbox() = delete;
122 operator=(PaymentSandbox const&) = delete;
123
125
127 : ApplyViewBase(base, flags)
128 {
129 }
130
131 PaymentSandbox(ApplyView const* base) : ApplyViewBase(base, base->flags())
132 {
133 }
134
145 // VFALCO If we are constructing on top of a PaymentSandbox,
146 // or a PaymentSandbox-derived class, we MUST go through
147 // one of these constructors or invariants will be broken.
149 explicit PaymentSandbox(PaymentSandbox const* base)
150 : ApplyViewBase(base, base->flags()), ps_(base)
151 {
152 }
153
155 : ApplyViewBase(base, base->flags()), ps_(base)
156 {
157 }
162 AccountID const& account,
163 AccountID const& issuer,
164 STAmount const& amount) const override;
165
166 void
168 AccountID const& from,
169 AccountID const& to,
170 STAmount const& amount,
171 STAmount const& preCreditBalance) override;
172
173 void
175 AccountID const& account,
176 std::uint32_t cur,
177 std::uint32_t next) override;
178
180 ownerCountHook(AccountID const& account, std::uint32_t count)
181 const override;
182
190 void
191 apply(RawView& to);
192
193 void
197 // Return a map of balance changes on trust lines. The low account is the
198 // first account in the key. If the two accounts are equal, the map contains
199 // the total changes in currency regardless of issuer. This is useful to get
200 // the total change in XRP balances.
202 balanceChanges(ReadView const& view) const;
203
205 xrpDestroyed() const;
206
207private:
209 PaymentSandbox const* ps_ = nullptr;
210};
211
212} // namespace ripple
213
214#endif
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:144
A wrapper which makes credits unavailable to balances.
XRPAmount xrpDestroyed() const
std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const override
std::map< std::tuple< AccountID, AccountID, Currency >, STAmount > balanceChanges(ReadView const &view) const
void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance) override
PaymentSandbox(ApplyView const *base)
void apply(RawView &to)
Apply changes to base view.
detail::DeferredCredits tab_
PaymentSandbox & operator=(PaymentSandbox &&)=delete
STAmount balanceHook(AccountID const &account, AccountID const &issuer, STAmount const &amount) const override
PaymentSandbox(ReadView const *base, ApplyFlags flags)
void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next) override
PaymentSandbox(PaymentSandbox *base)
PaymentSandbox(PaymentSandbox const &)=delete
PaymentSandbox(PaymentSandbox const *base)
Construct on top of existing PaymentSandbox.
PaymentSandbox const * ps_
PaymentSandbox & operator=(PaymentSandbox const &)=delete
PaymentSandbox(PaymentSandbox &&)=default
Interface for ledger entry changes.
Definition: RawView.h:35
A view into a ledger.
Definition: ReadView.h:52
ApplyFlags flags() const override
Returns the tx apply flags.
std::optional< Adjustment > adjustments(AccountID const &main, AccountID const &other, Currency const &currency) const
void credit(AccountID const &sender, AccountID const &receiver, STAmount const &amount, STAmount const &preCreditSenderBalance)
void apply(DeferredCredits &to)
std::map< AccountID, std::uint32_t > ownerCounts_
void ownerCount(AccountID const &id, std::uint32_t cur, std::uint32_t next)
std::map< Key, Value > credits_
static Key makeKey(AccountID const &a1, AccountID const &a2, Currency const &c)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
ApplyFlags
Definition: ApplyView.h:31
Adjustment(STAmount const &d, STAmount const &c, STAmount const &b)