rippled
Loading...
Searching...
No Matches
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 <xrpld/app/ledger/Ledger.h>
21#include <xrpld/app/tx/detail/CancelCheck.h>
22#include <xrpld/ledger/ApplyView.h>
23
24#include <xrpl/basics/Log.h>
25#include <xrpl/protocol/Feature.h>
26#include <xrpl/protocol/Indexes.h>
27#include <xrpl/protocol/TER.h>
28#include <xrpl/protocol/TxFlags.h>
29
30namespace ripple {
31
34{
35 if (!ctx.rules.enabled(featureChecks))
36 return temDISABLED;
37
38 NotTEC const ret{preflight1(ctx)};
39 if (!isTesSuccess(ret))
40 return ret;
41
42 if (ctx.tx.getFlags() & tfUniversalMask)
43 {
44 // There are no flags (other than universal) for CreateCheck yet.
45 JLOG(ctx.j.warn()) << "Malformed transaction: Invalid flags set.";
46 return temINVALID_FLAG;
47 }
48
49 return preflight2(ctx);
50}
51
52TER
54{
55 auto const sleCheck = ctx.view.read(keylet::check(ctx.tx[sfCheckID]));
56 if (!sleCheck)
57 {
58 JLOG(ctx.j.warn()) << "Check does not exist.";
59 return tecNO_ENTRY;
60 }
61
62 using duration = NetClock::duration;
63 using timepoint = NetClock::time_point;
64 auto const optExpiry = (*sleCheck)[~sfExpiration];
65
66 // Expiration is defined in terms of the close time of the parent
67 // ledger, because we definitively know the time that it closed but
68 // we do not know the closing time of the ledger that is under
69 // construction.
70 if (!optExpiry ||
71 (ctx.view.parentCloseTime() < timepoint{duration{*optExpiry}}))
72 {
73 // If the check is not yet expired, then only the creator or the
74 // destination may cancel the check.
75 AccountID const acctId{ctx.tx[sfAccount]};
76 if (acctId != (*sleCheck)[sfAccount] &&
77 acctId != (*sleCheck)[sfDestination])
78 {
79 JLOG(ctx.j.warn()) << "Check is not expired and canceler is "
80 "neither check source nor destination.";
81 return tecNO_PERMISSION;
82 }
83 }
84 return tesSUCCESS;
85}
86
87TER
88CancelCheck::doApply()
89{
90 auto const sleCheck = view().peek(keylet::check(ctx_.tx[sfCheckID]));
91 if (!sleCheck)
92 {
93 // Error should have been caught in preclaim.
94 JLOG(j_.warn()) << "Check does not exist.";
95 return tecNO_ENTRY;
96 }
97
98 AccountID const srcId{sleCheck->getAccountID(sfAccount)};
99 AccountID const dstId{sleCheck->getAccountID(sfDestination)};
100 auto viewJ = ctx_.app.journal("View");
101
102 // If the check is not written to self (and it shouldn't be), remove the
103 // check from the destination account root.
104 if (srcId != dstId)
105 {
106 std::uint64_t const page{(*sleCheck)[sfDestinationNode]};
107 if (!view().dirRemove(
108 keylet::ownerDir(dstId), page, sleCheck->key(), true))
109 {
110 JLOG(j_.fatal()) << "Unable to delete check from destination.";
111 return tefBAD_LEDGER;
112 }
113 }
114 {
115 std::uint64_t const page{(*sleCheck)[sfOwnerNode]};
116 if (!view().dirRemove(
117 keylet::ownerDir(srcId), page, sleCheck->key(), true))
118 {
119 JLOG(j_.fatal()) << "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
Stream fatal() const
Definition: Journal.h:352
Stream warn() const
Definition: Journal.h:340
static TER preclaim(PreclaimContext const &ctx)
Definition: CancelCheck.cpp:53
static NotTEC preflight(PreflightContext const &ctx)
Definition: CancelCheck.cpp:33
std::chrono::time_point< NetClock > time_point
Definition: chrono.h:69
std::chrono::duration< rep, period > duration
Definition: chrono.h:68
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition: ReadView.h:112
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:130
std::uint32_t getFlags() const
Definition: STObject.cpp:537
Keylet check(AccountID const &id, std::uint32_t seq) noexcept
A Check.
Definition: Indexes.cpp:329
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
bool isTesSuccess(TER x)
Definition: TER.h:672
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:83
@ tefBAD_LEDGER
Definition: TER.h:170
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:1019
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:144
@ tecNO_ENTRY
Definition: TER.h:306
@ tecNO_PERMISSION
Definition: TER.h:305
@ tesSUCCESS
Definition: TER.h:244
constexpr std::uint32_t tfUniversalMask
Definition: TxFlags.h:62
TERSubset< CanCvtToTER > TER
Definition: TER.h:643
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:603
@ temINVALID_FLAG
Definition: TER.h:111
@ temDISABLED
Definition: TER.h:114
uint256 key
Definition: Keylet.h:40
State information when determining if a tx is likely to claim a fee.
Definition: Transactor.h:55
ReadView const & view
Definition: Transactor.h:58
beast::Journal const j
Definition: Transactor.h:62
State information when preflighting a tx.
Definition: Transactor.h:34
beast::Journal const j
Definition: Transactor.h:40