rippled
Loading...
Searching...
No Matches
CancelCheck.cpp
1#include <xrpld/app/ledger/Ledger.h>
2#include <xrpld/app/tx/detail/CancelCheck.h>
3
4#include <xrpl/basics/Log.h>
5#include <xrpl/ledger/ApplyView.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/Indexes.h>
8#include <xrpl/protocol/TER.h>
9#include <xrpl/protocol/TxFlags.h>
10
11namespace ripple {
12
15{
16 return tesSUCCESS;
17}
18
19TER
21{
22 auto const sleCheck = ctx.view.read(keylet::check(ctx.tx[sfCheckID]));
23 if (!sleCheck)
24 {
25 JLOG(ctx.j.warn()) << "Check does not exist.";
26 return tecNO_ENTRY;
27 }
28
29 using duration = NetClock::duration;
30 using timepoint = NetClock::time_point;
31 auto const optExpiry = (*sleCheck)[~sfExpiration];
32
33 // Expiration is defined in terms of the close time of the parent
34 // ledger, because we definitively know the time that it closed but
35 // we do not know the closing time of the ledger that is under
36 // construction.
37 if (!optExpiry ||
38 (ctx.view.parentCloseTime() < timepoint{duration{*optExpiry}}))
39 {
40 // If the check is not yet expired, then only the creator or the
41 // destination may cancel the check.
42 AccountID const acctId{ctx.tx[sfAccount]};
43 if (acctId != (*sleCheck)[sfAccount] &&
44 acctId != (*sleCheck)[sfDestination])
45 {
46 JLOG(ctx.j.warn()) << "Check is not expired and canceler is "
47 "neither check source nor destination.";
48 return tecNO_PERMISSION;
49 }
50 }
51 return tesSUCCESS;
52}
53
54TER
55CancelCheck::doApply()
56{
57 auto const sleCheck = view().peek(keylet::check(ctx_.tx[sfCheckID]));
58 if (!sleCheck)
59 {
60 // Error should have been caught in preclaim.
61 JLOG(j_.warn()) << "Check does not exist.";
62 return tecNO_ENTRY;
63 }
64
65 AccountID const srcId{sleCheck->getAccountID(sfAccount)};
66 AccountID const dstId{sleCheck->getAccountID(sfDestination)};
67 auto viewJ = ctx_.app.journal("View");
68
69 // If the check is not written to self (and it shouldn't be), remove the
70 // check from the destination account root.
71 if (srcId != dstId)
72 {
73 std::uint64_t const page{(*sleCheck)[sfDestinationNode]};
74 if (!view().dirRemove(
75 keylet::ownerDir(dstId), page, sleCheck->key(), true))
76 {
77 // LCOV_EXCL_START
78 JLOG(j_.fatal()) << "Unable to delete check from destination.";
79 return tefBAD_LEDGER;
80 // LCOV_EXCL_STOP
81 }
82 }
83 {
84 std::uint64_t const page{(*sleCheck)[sfOwnerNode]};
85 if (!view().dirRemove(
86 keylet::ownerDir(srcId), page, sleCheck->key(), true))
87 {
88 // LCOV_EXCL_START
89 JLOG(j_.fatal()) << "Unable to delete check from owner.";
90 return tefBAD_LEDGER;
91 // LCOV_EXCL_STOP
92 }
93 }
94
95 // If we succeeded, update the check owner's reserve.
96 auto const sleSrc = view().peek(keylet::account(srcId));
97 adjustOwnerCount(view(), sleSrc, -1, viewJ);
98
99 // Remove check from ledger.
100 view().erase(sleCheck);
101 return tesSUCCESS;
102}
103
104} // namespace ripple
Stream fatal() const
Definition Journal.h:333
Stream warn() const
Definition Journal.h:321
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
std::chrono::time_point< NetClock > time_point
Definition chrono.h:50
std::chrono::duration< rep, period > duration
Definition chrono.h:49
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:92
Keylet check(AccountID const &id, std::uint32_t seq) noexcept
A Check.
Definition Indexes.cpp:317
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
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:1013
@ tefBAD_LEDGER
Definition TER.h:151
@ tecNO_ENTRY
Definition TER.h:288
@ tecNO_PERMISSION
Definition TER.h:287
@ tesSUCCESS
Definition TER.h:226
TERSubset< CanCvtToTER > TER
Definition TER.h:630
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:590
uint256 key
Definition Keylet.h:21
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
beast::Journal const j
Definition Transactor.h:69
State information when preflighting a tx.
Definition Transactor.h:16