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
31namespace ripple {
32
33namespace detail {
34
35// VFALCO TODO Inline this implementation
36// into the PaymentSandbox class itself
38{
39public:
41 {
42 Adjustment(STAmount const& d, STAmount const& c, STAmount const& b)
43 : debits(d), credits(c), origBalance(b)
44 {
45 }
49 };
50
51 // Get the adjustments for the balance between main and other.
52 // Returns the debits, credits and the original balance
55 AccountID const& main,
56 AccountID const& other,
57 Currency const& currency) const;
58
59 void
60 credit(
61 AccountID const& sender,
62 AccountID const& receiver,
63 STAmount const& amount,
64 STAmount const& preCreditSenderBalance);
65
66 void
67 ownerCount(AccountID const& id, std::uint32_t cur, std::uint32_t next);
68
69 // Get the adjusted owner count. Since DeferredCredits is meant to be used
70 // in payments, and payments only decrease owner counts, return the max
71 // remembered owner count.
73 ownerCount(AccountID const& id) const;
74
75 void
77
78private:
79 // lowAccount, highAccount
81 struct Value
82 {
83 explicit Value() = default;
84
88 };
89
90 static Key
91 makeKey(AccountID const& a1, AccountID const& a2, Currency const& c);
92
95};
96
97} // namespace detail
98
99//------------------------------------------------------------------------------
100
114{
115public:
116 PaymentSandbox() = delete;
121 operator=(PaymentSandbox const&) = delete;
122
124
126 : ApplyViewBase(base, flags)
127 {
128 }
129
130 PaymentSandbox(ApplyView const* base) : ApplyViewBase(base, base->flags())
131 {
132 }
133
144 // VFALCO If we are constructing on top of a PaymentSandbox,
145 // or a PaymentSandbox-derived class, we MUST go through
146 // one of these constructors or invariants will be broken.
148 explicit PaymentSandbox(PaymentSandbox const* base)
149 : ApplyViewBase(base, base->flags()), ps_(base)
150 {
151 }
152
154 : ApplyViewBase(base, base->flags()), ps_(base)
155 {
156 }
161 AccountID const& account,
162 AccountID const& issuer,
163 STAmount const& amount) const override;
164
165 void
167 AccountID const& from,
168 AccountID const& to,
169 STAmount const& amount,
170 STAmount const& preCreditBalance) override;
171
172 void
174 AccountID const& account,
175 std::uint32_t cur,
176 std::uint32_t next) override;
177
179 ownerCountHook(AccountID const& account, std::uint32_t count)
180 const override;
181
189 void
190 apply(RawView& to);
191
192 void
196 // Return a map of balance changes on trust lines. The low account is the
197 // first account in the key. If the two accounts are equal, the map contains
198 // the total changes in currency regardless of issuer. This is useful to get
199 // the total change in XRP balances.
201 balanceChanges(ReadView const& view) const;
202
204 xrpDestroyed() const;
205
206private:
208 PaymentSandbox const* ps_ = nullptr;
209};
210
211} // namespace ripple
212
213#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:25
ApplyFlags
Definition: ApplyView.h:31
Adjustment(STAmount const &d, STAmount const &c, STAmount const &b)