rippled
CancelCheck.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2017 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 #include <ripple/app/tx/impl/CancelCheck.h>
21 
22 #include <ripple/app/ledger/Ledger.h>
23 #include <ripple/basics/Log.h>
24 #include <ripple/ledger/ApplyView.h>
25 #include <ripple/protocol/Feature.h>
26 #include <ripple/protocol/Indexes.h>
27 #include <ripple/protocol/STAccount.h>
28 #include <ripple/protocol/TER.h>
29 #include <ripple/protocol/TxFlags.h>
30 
31 namespace ripple {
32 
33 NotTEC
35 {
36  if (! ctx.rules.enabled (featureChecks))
37  return temDISABLED;
38 
39  NotTEC const ret {preflight1 (ctx)};
40  if (! isTesSuccess (ret))
41  return ret;
42 
43  if (ctx.tx.getFlags() & tfUniversalMask)
44  {
45  // There are no flags (other than universal) for CreateCheck yet.
46  JLOG(ctx.j.warn()) << "Malformed transaction: Invalid flags set.";
47  return temINVALID_FLAG;
48  }
49 
50  return preflight2 (ctx);
51 }
52 
53 TER
55 {
56  auto const sleCheck = ctx.view.read (keylet::check (ctx.tx[sfCheckID]));
57  if (! sleCheck)
58  {
59  JLOG(ctx.j.warn()) << "Check does not exist.";
60  return tecNO_ENTRY;
61  }
62 
63  using duration = NetClock::duration;
64  using timepoint = NetClock::time_point;
65  auto const optExpiry = (*sleCheck)[~sfExpiration];
66 
67  // Expiration is defined in terms of the close time of the parent
68  // ledger, because we definitively know the time that it closed but
69  // we do not know the closing time of the ledger that is under
70  // construction.
71  if (! optExpiry ||
72  (ctx.view.parentCloseTime() < timepoint {duration {*optExpiry}}))
73  {
74  // If the check is not yet expired, then only the creator or the
75  // destination may cancel the check.
76  AccountID const acctId {ctx.tx[sfAccount]};
77  if (acctId != (*sleCheck)[sfAccount] &&
78  acctId != (*sleCheck)[sfDestination])
79  {
80  JLOG(ctx.j.warn()) << "Check is not expired and canceler is "
81  "neither check source nor destination.";
82  return tecNO_PERMISSION;
83  }
84  }
85  return tesSUCCESS;
86 }
87 
88 TER
89 CancelCheck::doApply ()
90 {
91  uint256 const checkId {ctx_.tx[sfCheckID]};
92  auto const sleCheck = view().peek (keylet::check (checkId));
93  if (! sleCheck)
94  {
95  // Error should have been caught in preclaim.
96  JLOG(j_.warn()) << "Check does not exist.";
97  return tecNO_ENTRY;
98  }
99 
100  AccountID const srcId {sleCheck->getAccountID (sfAccount)};
101  AccountID const dstId {sleCheck->getAccountID (sfDestination)};
102  auto viewJ = ctx_.app.journal ("View");
103 
104  // If the check is not written to self (and it shouldn't be), remove the
105  // check from the destination account root.
106  if (srcId != dstId)
107  {
108  std::uint64_t const page {(*sleCheck)[sfDestinationNode]};
109  if (! view().dirRemove (keylet::ownerDir(dstId), page, checkId, true))
110  {
111  JLOG(j_.warn()) << "Unable to delete check from destination.";
112  return tefBAD_LEDGER;
113  }
114  }
115  {
116  std::uint64_t const page {(*sleCheck)[sfOwnerNode]};
117  if (! view().dirRemove (keylet::ownerDir(srcId), page, checkId, true))
118  {
119  JLOG(j_.warn()) << "Unable to delete check from owner.";
120  return tefBAD_LEDGER;
121  }
122  }
123 
124  // If we succeeded, update the check owner's reserve.
125  auto const sleSrc = view().peek (keylet::account (srcId));
126  adjustOwnerCount (view(), sleSrc, -1, viewJ);
127 
128  // Remove check from ledger.
129  view().erase (sleCheck);
130  return tesSUCCESS;
131 }
132 
133 } // namespace ripple
ripple::CancelCheck::preclaim
static TER preclaim(PreclaimContext const &ctx)
Definition: CancelCheck.cpp:54
ripple::preflight2
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:90
ripple::PreclaimContext::view
ReadView const & view
Definition: Transactor.h:53
ripple::PreclaimContext::j
const beast::Journal j
Definition: Transactor.h:57
ripple::isTesSuccess
bool isTesSuccess(TER x)
Definition: TER.h:501
ripple::sfAccount
const SF_Account sfAccount(access, STI_ACCOUNT, 1, "Account")
Definition: SField.h:460
beast::Journal::warn
Stream warn() const
Definition: Journal.h:302
ripple::keylet::check
static const check_t check
Definition: Indexes.h:254
ripple::sfOwnerNode
const SF_U64 sfOwnerNode(access, STI_UINT64, 4, "OwnerNode")
Definition: SField.h:382
ripple::ReadView::parentCloseTime
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition: ReadView.h:251
ripple::PreflightContext::j
const beast::Journal j
Definition: Transactor.h:39
ripple::preflight1
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:56
ripple::base_uint
Definition: base_uint.h:65
ripple::temINVALID_FLAG
@ temINVALID_FLAG
Definition: TER.h:109
ripple::tefBAD_LEDGER
@ tefBAD_LEDGER
Definition: TER.h:150
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::TERSubset
Definition: TER.h:311
ripple::sfDestinationNode
const SF_U64 sfDestinationNode(access, STI_UINT64, 9, "DestinationNode")
Definition: SField.h:387
ripple::TER
TERSubset< CanCvtToTER > TER
Definition: TER.h:477
ripple::STObject::getFlags
std::uint32_t getFlags() const
Definition: STObject.cpp:439
std::uint64_t
ripple::Rules::enabled
bool enabled(uint256 const &id) const
Returns true if a feature is enabled.
Definition: ReadView.cpp:107
ripple::featureChecks
const uint256 featureChecks
Definition: Feature.cpp:165
ripple::sfExpiration
const SF_U32 sfExpiration(access, STI_UINT32, 10, "Expiration")
Definition: SField.h:346
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::PreclaimContext::tx
STTx const & tx
Definition: Transactor.h:55
ripple::PreclaimContext
State information when determining if a tx is likely to claim a fee.
Definition: Transactor.h:49
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::temDISABLED
@ temDISABLED
Definition: TER.h:112
ripple::tecNO_PERMISSION
@ tecNO_PERMISSION
Definition: TER.h:270
ripple::sfDestination
const SF_Account sfDestination(access, STI_ACCOUNT, 3, "Destination")
Definition: SField.h:462
ripple::sfCheckID
const SF_U256 sfCheckID(access, STI_HASH256, 24, "CheckID")
Definition: SField.h:420
ripple::tecNO_ENTRY
@ tecNO_ENTRY
Definition: TER.h:271
ripple::NetClock::duration
std::chrono::duration< rep, period > duration
Definition: chrono.h:52
ripple::tfUniversalMask
const std::uint32_t tfUniversalMask
Definition: TxFlags.h:50
ripple::PreflightContext::tx
STTx const & tx
Definition: Transactor.h:36
ripple::PreflightContext
State information when preflighting a tx.
Definition: Transactor.h:32
ripple::PreflightContext::rules
const Rules rules
Definition: Transactor.h:37
ripple::NetClock::time_point
std::chrono::time_point< NetClock > time_point
Definition: chrono.h:53
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:219
ripple::CancelCheck::preflight
static NotTEC preflight(PreflightContext const &ctx)
Definition: CancelCheck.cpp:34
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:461