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 xrpl {
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 || (ctx.view.parentCloseTime() < timepoint{duration{*optExpiry}}))
38 {
39 // If the check is not yet expired, then only the creator or the
40 // destination may cancel the check.
41 AccountID const acctId{ctx.tx[sfAccount]};
42 if (acctId != (*sleCheck)[sfAccount] && acctId != (*sleCheck)[sfDestination])
43 {
44 JLOG(ctx.j.warn()) << "Check is not expired and canceler is "
45 "neither check source nor destination.";
46 return tecNO_PERMISSION;
47 }
48 }
49 return tesSUCCESS;
50}
51
52TER
53CancelCheck::doApply()
54{
55 auto const sleCheck = view().peek(keylet::check(ctx_.tx[sfCheckID]));
56 if (!sleCheck)
57 {
58 // Error should have been caught in preclaim.
59 JLOG(j_.warn()) << "Check does not exist.";
60 return tecNO_ENTRY;
61 }
62
63 AccountID const srcId{sleCheck->getAccountID(sfAccount)};
64 AccountID const dstId{sleCheck->getAccountID(sfDestination)};
65 auto viewJ = ctx_.app.journal("View");
66
67 // If the check is not written to self (and it shouldn't be), remove the
68 // check from the destination account root.
69 if (srcId != dstId)
70 {
71 std::uint64_t const page{(*sleCheck)[sfDestinationNode]};
72 if (!view().dirRemove(keylet::ownerDir(dstId), page, sleCheck->key(), true))
73 {
74 // LCOV_EXCL_START
75 JLOG(j_.fatal()) << "Unable to delete check from destination.";
76 return tefBAD_LEDGER;
77 // LCOV_EXCL_STOP
78 }
79 }
80 {
81 std::uint64_t const page{(*sleCheck)[sfOwnerNode]};
82 if (!view().dirRemove(keylet::ownerDir(srcId), page, sleCheck->key(), true))
83 {
84 // LCOV_EXCL_START
85 JLOG(j_.fatal()) << "Unable to delete check from owner.";
86 return tefBAD_LEDGER;
87 // LCOV_EXCL_STOP
88 }
89 }
90
91 // If we succeeded, update the check owner's reserve.
92 auto const sleSrc = view().peek(keylet::account(srcId));
93 adjustOwnerCount(view(), sleSrc, -1, viewJ);
94
95 // Remove check from ledger.
96 view().erase(sleCheck);
97 return tesSUCCESS;
98}
99
100} // namespace xrpl
Stream warn() const
Definition Journal.h:313
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
std::chrono::time_point< NetClock > time_point
Definition chrono.h:46
std::chrono::duration< rep, period > duration
Definition chrono.h:45
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition ReadView.h:91
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
Keylet check(AccountID const &id, std::uint32_t seq) noexcept
A Check.
Definition Indexes.cpp:293
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ tefBAD_LEDGER
Definition TER.h:151
TERSubset< CanCvtToTER > TER
Definition TER.h:621
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:941
@ tecNO_ENTRY
Definition TER.h:288
@ tecNO_PERMISSION
Definition TER.h:287
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:581
@ tesSUCCESS
Definition TER.h:226
uint256 key
Definition Keylet.h:21
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:54
ReadView const & view
Definition Transactor.h:57
beast::Journal const j
Definition Transactor.h:62
State information when preflighting a tx.
Definition Transactor.h:16