rippled
Loading...
Searching...
No Matches
CheckCancel.cpp
1#include <xrpl/ledger/ApplyView.h>
2#include <xrpl/ledger/View.h>
3#include <xrpl/ledger/helpers/AccountRootHelpers.h>
4#include <xrpl/protocol/Feature.h>
5#include <xrpl/protocol/Indexes.h>
6#include <xrpl/protocol/TER.h>
7#include <xrpl/protocol/TxFlags.h>
8#include <xrpl/tx/transactors/check/CheckCancel.h>
9
10namespace xrpl {
11
14{
15 return tesSUCCESS;
16}
17
18TER
20{
21 auto const sleCheck = ctx.view.read(keylet::check(ctx.tx[sfCheckID]));
22 if (!sleCheck)
23 {
24 JLOG(ctx.j.warn()) << "Check does not exist.";
25 return tecNO_ENTRY;
26 }
27
28 // Expiration is defined in terms of the close time of the parent
29 // ledger, because we definitively know the time that it closed but
30 // we do not know the closing time of the ledger that is under
31 // construction.
32 if (!hasExpired(ctx.view, (*sleCheck)[~sfExpiration]))
33 {
34 // If the check is not yet expired, then only the creator or the
35 // destination may cancel the check.
36 AccountID const acctId{ctx.tx[sfAccount]};
37 if (acctId != (*sleCheck)[sfAccount] && acctId != (*sleCheck)[sfDestination])
38 {
39 JLOG(ctx.j.warn()) << "Check is not expired and canceler is "
40 "neither check source nor destination.";
41 return tecNO_PERMISSION;
42 }
43 }
44 return tesSUCCESS;
45}
46
47TER
49{
50 auto const sleCheck = view().peek(keylet::check(ctx_.tx[sfCheckID]));
51 if (!sleCheck)
52 {
53 // Error should have been caught in preclaim.
54 JLOG(j_.warn()) << "Check does not exist.";
55 return tecNO_ENTRY;
56 }
57
58 AccountID const srcId{sleCheck->getAccountID(sfAccount)};
59 AccountID const dstId{sleCheck->getAccountID(sfDestination)};
60 auto viewJ = ctx_.registry.get().getJournal("View");
61
62 // If the check is not written to self (and it shouldn't be), remove the
63 // check from the destination account root.
64 if (srcId != dstId)
65 {
66 std::uint64_t const page{(*sleCheck)[sfDestinationNode]};
67 if (!view().dirRemove(keylet::ownerDir(dstId), page, sleCheck->key(), true))
68 {
69 // LCOV_EXCL_START
70 JLOG(j_.fatal()) << "Unable to delete check from destination.";
71 return tefBAD_LEDGER;
72 // LCOV_EXCL_STOP
73 }
74 }
75 {
76 std::uint64_t const page{(*sleCheck)[sfOwnerNode]};
77 if (!view().dirRemove(keylet::ownerDir(srcId), page, sleCheck->key(), true))
78 {
79 // LCOV_EXCL_START
80 JLOG(j_.fatal()) << "Unable to delete check from owner.";
81 return tefBAD_LEDGER;
82 // LCOV_EXCL_STOP
83 }
84 }
85
86 // If we succeeded, update the check owner's reserve.
87 auto const sleSrc = view().peek(keylet::account(srcId));
88 adjustOwnerCount(view(), sleSrc, -1, viewJ);
89
90 // Remove check from ledger.
91 view().erase(sleCheck);
92 return tesSUCCESS;
93}
94
95} // namespace xrpl
Stream fatal() const
Definition Journal.h:325
Stream warn() const
Definition Journal.h:313
STTx const & tx
std::reference_wrapper< ServiceRegistry > registry
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
TER doApply() override
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
beast::Journal const j_
Definition Transactor.h:114
ApplyView & view()
Definition Transactor.h:132
ApplyContext & ctx_
Definition Transactor.h:112
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:336
Keylet check(AccountID const &id, std::uint32_t seq) noexcept
A Check.
Definition Indexes.cpp:301
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool hasExpired(ReadView const &view, std::optional< std::uint32_t > const &exp)
Determines whether the given expiration time has passed.
Definition View.cpp:34
@ tefBAD_LEDGER
Definition TER.h:150
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
@ tecNO_ENTRY
Definition TER.h:287
@ tecNO_PERMISSION
Definition TER.h:286
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:582
@ tesSUCCESS
Definition TER.h:225
uint256 key
Definition Keylet.h:20
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:57
ReadView const & view
Definition Transactor.h:60
beast::Journal const j
Definition Transactor.h:65
State information when preflighting a tx.
Definition Transactor.h:14