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) {}
46  };
47 
48  // Get the adjustments for the balance between main and other.
49  // Returns the debits, credits and the original balance
50  boost::optional<Adjustment> adjustments (
51  AccountID const& main,
52  AccountID const& other,
53  Currency const& currency) const;
54 
55  void credit (AccountID const& sender,
56  AccountID const& receiver,
57  STAmount const& amount,
58  STAmount const& preCreditSenderBalance);
59 
60  void ownerCount (AccountID const& id,
61  std::uint32_t cur,
62  std::uint32_t next);
63 
64  // Get the adjusted owner count. Since DeferredCredits is meant to be used
65  // in payments, and payments only decrease owner counts, return the max
66  // remembered owner count.
67  boost::optional<std::uint32_t>
68  ownerCount (AccountID const& id) const;
69 
70  void apply (DeferredCredits& to);
71 private:
72  // lowAccount, highAccount
73  using Key = std::tuple<
75  struct Value
76  {
77  explicit Value() = default;
78 
82  };
83 
84  static
85  Key
86  makeKey (AccountID const& a1,
87  AccountID const& a2,
88  Currency const& c);
89 
92 };
93 
94 } // detail
95 
96 //------------------------------------------------------------------------------
97 
110 class PaymentSandbox final
111  : public detail::ApplyViewBase
112 {
113 public:
114  PaymentSandbox() = delete;
115  PaymentSandbox (PaymentSandbox const&) = delete;
117  PaymentSandbox& operator= (PaymentSandbox const&) = delete;
118 
119  PaymentSandbox (PaymentSandbox&&) = default;
120 
122  : ApplyViewBase (base, flags)
123  {
124  }
125 
126  PaymentSandbox (ApplyView const* base)
127  : ApplyViewBase (base, base->flags())
128  {
129  }
130 
141  // VFALCO If we are constructing on top of a PaymentSandbox,
142  // or a PaymentSandbox-derived class, we MUST go through
143  // one of these constructors or invariants will be broken.
145  explicit
147  : ApplyViewBase(base, base->flags())
148  , ps_ (base)
149  {
150  }
151 
152  explicit
154  : ApplyViewBase(base, base->flags())
155  , ps_ (base)
156  {
157  }
160  STAmount
161  balanceHook (AccountID const& account,
162  AccountID const& issuer,
163  STAmount const& amount) const override;
164 
165  void
166  creditHook (AccountID const& from,
167  AccountID const& to,
168  STAmount const& amount,
169  STAmount const& preCreditBalance) override;
170 
171  void
172  adjustOwnerCountHook (AccountID const& account,
173  std::uint32_t cur,
174  std::uint32_t next) override;
175 
177  ownerCountHook (AccountID const& account,
178  std::uint32_t count) const override;
179 
187  void apply (RawView& to);
188 
189  void
190  apply (PaymentSandbox& to);
193  // Return a map of balance changes on trust lines. The low account is the
194  // first account in the key. If the two accounts are equal, the map contains
195  // the total changes in currency regardless of issuer. This is useful to get
196  // the total change in XRP balances.
198  balanceChanges (ReadView const& view) const;
199 
200  XRPAmount xrpDestroyed () const;
201 
202 private:
204  PaymentSandbox const* ps_ = nullptr;
205 };
206 
207 } // ripple
208 
209 #endif
ripple::detail::DeferredCredits::Adjustment::debits
STAmount debits
Definition: PaymentSandbox.h:43
ripple::PaymentSandbox::xrpDestroyed
XRPAmount xrpDestroyed() const
Definition: PaymentSandbox.cpp:379
utility
ripple::PaymentSandbox::PaymentSandbox
PaymentSandbox(ApplyView const *base)
Definition: PaymentSandbox.h:126
ripple::PaymentSandbox
A wrapper which makes credits unavailable to balances.
Definition: PaymentSandbox.h:110
ripple::detail::DeferredCredits::Adjustment
Definition: PaymentSandbox.h:39
ripple::detail::DeferredCredits::adjustments
boost::optional< Adjustment > adjustments(AccountID const &main, AccountID const &other, Currency const &currency) const
Definition: PaymentSandbox.cpp:110
ripple::PaymentSandbox::PaymentSandbox
PaymentSandbox()=delete
ripple::ApplyFlags
ApplyFlags
Definition: ApplyView.h:30
std::tuple
ripple::PaymentSandbox::PaymentSandbox
PaymentSandbox(PaymentSandbox *base)
Definition: PaymentSandbox.h:153
ripple::PaymentSandbox::adjustOwnerCountHook
void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next) override
Definition: PaymentSandbox.cpp:237
ripple::detail::DeferredCredits::Value
Definition: PaymentSandbox.h:75
ripple::PaymentSandbox::balanceChanges
std::map< std::tuple< AccountID, AccountID, Currency >, STAmount > balanceChanges(ReadView const &view) const
Definition: PaymentSandbox.cpp:260
ripple::detail::DeferredCredits::credits_
std::map< Key, Value > credits_
Definition: PaymentSandbox.h:90
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:150
ripple::detail::DeferredCredits::credit
void credit(AccountID const &sender, AccountID const &receiver, STAmount const &amount, STAmount const &preCreditSenderBalance)
Definition: PaymentSandbox.cpp:45
ripple::detail::ApplyViewBase::ApplyViewBase
ApplyViewBase()=delete
ripple::base_uint< 160, detail::AccountIDTag >
ripple::RawView
Interface for ledger entry changes.
Definition: RawView.h:37
ripple::PaymentSandbox::ps_
PaymentSandbox const * ps_
Definition: PaymentSandbox.h:204
ripple::detail::DeferredCredits::Value::highAcctCredits
STAmount highAcctCredits
Definition: PaymentSandbox.h:80
ripple::detail::DeferredCredits::Adjustment::origBalance
STAmount origBalance
Definition: PaymentSandbox.h:45
ripple::detail::DeferredCredits::Adjustment::credits
STAmount credits
Definition: PaymentSandbox.h:44
ripple::STAmount
Definition: STAmount.h:42
std::uint32_t
ripple::detail::DeferredCredits::apply
void apply(DeferredCredits &to)
Definition: PaymentSandbox.cpp:135
map
ripple::PaymentSandbox::PaymentSandbox
PaymentSandbox(PaymentSandbox const *base)
Construct on top of existing PaymentSandbox.
Definition: PaymentSandbox.h:146
ripple::PaymentSandbox::tab_
detail::DeferredCredits tab_
Definition: PaymentSandbox.h:203
ripple::detail::ApplyViewBase::flags
ApplyFlags flags() const override
Returns the tx apply flags.
Definition: ApplyViewBase.cpp:132
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:186
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:245
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:86
ripple::PaymentSandbox::operator=
PaymentSandbox & operator=(PaymentSandbox &&)=delete
ripple::detail::DeferredCredits::Value::lowAcctCredits
STAmount lowAcctCredits
Definition: PaymentSandbox.h:79
ripple::detail::DeferredCredits::Value::Value
Value()=default
ripple::detail::DeferredCredits::Value::lowAcctOrigBalance
STAmount lowAcctOrigBalance
Definition: PaymentSandbox.h:81
ripple::PaymentSandbox::PaymentSandbox
PaymentSandbox(ReadView const *base, ApplyFlags flags)
Definition: PaymentSandbox.h:121
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:215
ripple::detail::ApplyViewBase
Definition: ApplyViewBase.h:33
ripple::PaymentSandbox::creditHook
void creditHook(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance) override
Definition: PaymentSandbox.cpp:228
ripple::detail::DeferredCredits::ownerCounts_
std::map< AccountID, std::uint32_t > ownerCounts_
Definition: PaymentSandbox.h:91
ripple::PaymentSandbox::balanceHook
STAmount balanceHook(AccountID const &account, AccountID const &issuer, STAmount const &amount) const override
Definition: PaymentSandbox.cpp:166
ripple::detail::DeferredCredits
Definition: PaymentSandbox.h:36
ripple::XRPAmount
Definition: XRPAmount.h:46