rippled
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 <ripple/ledger/RawView.h>
24 #include <ripple/ledger/Sandbox.h>
25 #include <ripple/ledger/detail/ApplyViewBase.h>
26 #include <ripple/protocol/AccountID.h>
27 #include <map>
28 #include <utility>
29 
30 namespace ripple {
31 
32 namespace detail {
33 
34 // VFALCO TODO Inline this implementation
35 // into the PaymentSandbox class itself
37 {
38 public:
39  struct Adjustment
40  {
41  Adjustment(STAmount const& d, STAmount const& c, STAmount const& b)
42  : debits(d), credits(c), origBalance(b)
43  {
44  }
48  };
49 
50  // Get the adjustments for the balance between main and other.
51  // Returns the debits, credits and the original balance
54  AccountID const& main,
55  AccountID const& other,
56  Currency const& currency) const;
57 
58  void
59  credit(
60  AccountID const& sender,
61  AccountID const& receiver,
62  STAmount const& amount,
63  STAmount const& preCreditSenderBalance);
64 
65  void
66  ownerCount(AccountID const& id, std::uint32_t cur, std::uint32_t next);
67 
68  // Get the adjusted owner count. Since DeferredCredits is meant to be used
69  // in payments, and payments only decrease owner counts, return the max
70  // remembered owner count.
72  ownerCount(AccountID const& id) const;
73 
74  void
76 
77 private:
78  // lowAccount, highAccount
80  struct Value
81  {
82  explicit Value() = default;
83 
87  };
88 
89  static Key
90  makeKey(AccountID const& a1, AccountID const& a2, Currency const& c);
91 
94 };
95 
96 } // namespace detail
97 
98 //------------------------------------------------------------------------------
99 
113 {
114 public:
115  PaymentSandbox() = delete;
116  PaymentSandbox(PaymentSandbox const&) = delete;
118  operator=(PaymentSandbox&&) = delete;
120  operator=(PaymentSandbox const&) = delete;
121 
122  PaymentSandbox(PaymentSandbox&&) = default;
123 
125  : ApplyViewBase(base, flags)
126  {
127  }
128 
129  PaymentSandbox(ApplyView const* base) : ApplyViewBase(base, base->flags())
130  {
131  }
132 
143  // VFALCO If we are constructing on top of a PaymentSandbox,
144  // or a PaymentSandbox-derived class, we MUST go through
145  // one of these constructors or invariants will be broken.
147  explicit PaymentSandbox(PaymentSandbox const* base)
148  : ApplyViewBase(base, base->flags()), ps_(base)
149  {
150  }
151 
153  : ApplyViewBase(base, base->flags()), ps_(base)
154  {
155  }
158  STAmount
159  balanceHook(
160  AccountID const& account,
161  AccountID const& issuer,
162  STAmount const& amount) const override;
163 
164  void
165  creditHook(
166  AccountID const& from,
167  AccountID const& to,
168  STAmount const& amount,
169  STAmount const& preCreditBalance) override;
170 
171  void
173  AccountID const& account,
174  std::uint32_t cur,
175  std::uint32_t next) override;
176 
178  ownerCountHook(AccountID const& account, std::uint32_t count)
179  const override;
180 
188  void
189  apply(RawView& to);
190 
191  void
192  apply(PaymentSandbox& to);
195  // Return a map of balance changes on trust lines. The low account is the
196  // first account in the key. If the two accounts are equal, the map contains
197  // the total changes in currency regardless of issuer. This is useful to get
198  // the total change in XRP balances.
200  balanceChanges(ReadView const& view) const;
201 
202  XRPAmount
203  xrpDestroyed() const;
204 
205 private:
207  PaymentSandbox const* ps_ = nullptr;
208 };
209 
210 } // namespace ripple
211 
212 #endif
ripple::detail::DeferredCredits::Adjustment::debits
STAmount debits
Definition: PaymentSandbox.h:45
ripple::PaymentSandbox::xrpDestroyed
XRPAmount xrpDestroyed() const
Definition: PaymentSandbox.cpp:391
utility
ripple::PaymentSandbox::PaymentSandbox
PaymentSandbox(ApplyView const *base)
Definition: PaymentSandbox.h:129
ripple::PaymentSandbox
A wrapper which makes credits unavailable to balances.
Definition: PaymentSandbox.h:112
ripple::detail::DeferredCredits::Adjustment
Definition: PaymentSandbox.h:39
ripple::detail::DeferredCredits::adjustments
std::optional< Adjustment > adjustments(AccountID const &main, AccountID const &other, Currency const &currency) const
Definition: PaymentSandbox.cpp:113
ripple::PaymentSandbox::PaymentSandbox
PaymentSandbox()=delete
ripple::ApplyFlags
ApplyFlags
Definition: ApplyView.h:29
std::tuple
ripple::PaymentSandbox::PaymentSandbox
PaymentSandbox(PaymentSandbox *base)
Definition: PaymentSandbox.h:152
ripple::PaymentSandbox::adjustOwnerCountHook
void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next) override
Definition: PaymentSandbox.cpp:245
ripple::detail::DeferredCredits::Value
Definition: PaymentSandbox.h:80
ripple::PaymentSandbox::balanceChanges
std::map< std::tuple< AccountID, AccountID, Currency >, STAmount > balanceChanges(ReadView const &view) const
Definition: PaymentSandbox.cpp:269
ripple::detail::DeferredCredits::credits_
std::map< Key, Value > credits_
Definition: PaymentSandbox.h:92
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:134
ripple::detail::DeferredCredits::credit
void credit(AccountID const &sender, AccountID const &receiver, STAmount const &amount, STAmount const &preCreditSenderBalance)
Definition: PaymentSandbox.cpp:46
ripple::detail::ApplyViewBase::ApplyViewBase
ApplyViewBase()=delete
ripple::base_uint< 160, detail::AccountIDTag >
ripple::RawView
Interface for ledger entry changes.
Definition: RawView.h:36
ripple::PaymentSandbox::ps_
PaymentSandbox const * ps_
Definition: PaymentSandbox.h:207
ripple::detail::DeferredCredits::Value::highAcctCredits
STAmount highAcctCredits
Definition: PaymentSandbox.h:85
ripple::detail::DeferredCredits::Adjustment::origBalance
STAmount origBalance
Definition: PaymentSandbox.h:47
ripple::detail::DeferredCredits::Adjustment::credits
STAmount credits
Definition: PaymentSandbox.h:46
ripple::STAmount
Definition: STAmount.h:45
std::uint32_t
ripple::detail::DeferredCredits::apply
void apply(DeferredCredits &to)
Definition: PaymentSandbox.cpp:142
map
ripple::PaymentSandbox::PaymentSandbox
PaymentSandbox(PaymentSandbox const *base)
Construct on top of existing PaymentSandbox.
Definition: PaymentSandbox.h:147
ripple::PaymentSandbox::tab_
detail::DeferredCredits tab_
Definition: PaymentSandbox.h:206
ripple::detail::ApplyViewBase::flags
ApplyFlags flags() const override
Returns the tx apply flags.
Definition: ApplyViewBase.cpp:122
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:125
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PaymentSandbox::apply
void apply(RawView &to)
Apply changes to base view.
Definition: PaymentSandbox.cpp:254
ripple::detail::DeferredCredits::Adjustment::Adjustment
Adjustment(STAmount const &d, STAmount const &c, STAmount const &b)
Definition: PaymentSandbox.h:41
ripple::detail::DeferredCredits::ownerCount
void ownerCount(AccountID const &id, std::uint32_t cur, std::uint32_t next)
Definition: PaymentSandbox.cpp:88
ripple::PaymentSandbox::operator=
PaymentSandbox & operator=(PaymentSandbox &&)=delete
ripple::detail::DeferredCredits::Value::lowAcctCredits
STAmount lowAcctCredits
Definition: PaymentSandbox.h:84
std::optional
ripple::detail::DeferredCredits::Value::Value
Value()=default
ripple::detail::DeferredCredits::Value::lowAcctOrigBalance
STAmount lowAcctOrigBalance
Definition: PaymentSandbox.h:86
ripple::PaymentSandbox::PaymentSandbox
PaymentSandbox(ReadView const *base, ApplyFlags flags)
Definition: PaymentSandbox.h:124
ripple::detail::DeferredCredits::makeKey
static Key makeKey(AccountID const &a1, AccountID const &a2, Currency const &c)
Definition: PaymentSandbox.cpp:34
ripple::PaymentSandbox::ownerCountHook
std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t count) const override
Definition: PaymentSandbox.cpp:222
ripple::detail::ApplyViewBase
Definition: ApplyViewBase.h:32
ripple::PaymentSandbox::creditHook
void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance) override
Definition: PaymentSandbox.cpp:235
ripple::detail::DeferredCredits::ownerCounts_
std::map< AccountID, std::uint32_t > ownerCounts_
Definition: PaymentSandbox.h:93
ripple::PaymentSandbox::balanceHook
STAmount balanceHook(AccountID const &account, AccountID const &issuer, STAmount const &amount) const override
Definition: PaymentSandbox.cpp:172
ripple::detail::DeferredCredits
Definition: PaymentSandbox.h:36
ripple::XRPAmount
Definition: XRPAmount.h:46