xrpld
Loading...
Searching...
No Matches
CheckCancel.cpp
1#include <xrpl/tx/transactors/check/CheckCancel.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/ledger/ApplyView.h>
5#include <xrpl/ledger/View.h>
6#include <xrpl/ledger/helpers/AccountRootHelpers.h>
7#include <xrpl/protocol/AccountID.h>
8#include <xrpl/protocol/Indexes.h>
9#include <xrpl/protocol/SField.h>
10#include <xrpl/protocol/STLedgerEntry.h>
11#include <xrpl/protocol/STTx.h>
12#include <xrpl/protocol/TER.h>
13#include <xrpl/protocol/XRPAmount.h>
14#include <xrpl/tx/Transactor.h>
15
16#include <cstdint>
17namespace xrpl {
18
21{
22 return tesSUCCESS;
23}
24
25TER
27{
28 auto const sleCheck = ctx.view.read(keylet::check(ctx.tx[sfCheckID]));
29 if (!sleCheck)
30 {
31 JLOG(ctx.j.warn()) << "Check does not exist.";
32 return tecNO_ENTRY;
33 }
34
35 // Expiration is defined in terms of the close time of the parent
36 // ledger, because we definitively know the time that it closed but
37 // we do not know the closing time of the ledger that is under
38 // construction.
39 if (!hasExpired(ctx.view, (*sleCheck)[~sfExpiration]))
40 {
41 // If the check is not yet expired, then only the creator or the
42 // destination may cancel the check.
43 AccountID const acctId{ctx.tx[sfAccount]};
44 if (acctId != (*sleCheck)[sfAccount] && 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
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_.registry.get().getJournal("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(keylet::ownerDir(dstId), page, sleCheck->key(), true))
75 {
76 // LCOV_EXCL_START
77 JLOG(j_.fatal()) << "Unable to delete check from destination.";
78 return tefBAD_LEDGER;
79 // LCOV_EXCL_STOP
80 }
81 }
82 {
83 std::uint64_t const page{(*sleCheck)[sfOwnerNode]};
84 if (!view().dirRemove(keylet::ownerDir(srcId), page, sleCheck->key(), true))
85 {
86 // LCOV_EXCL_START
87 JLOG(j_.fatal()) << "Unable to delete check from owner.";
88 return tefBAD_LEDGER;
89 // LCOV_EXCL_STOP
90 }
91 }
92
93 // If we succeeded, update the check owner's reserve.
94 auto const sleSrc = view().peek(keylet::account(srcId));
95 adjustOwnerCount(view(), sleSrc, -1, viewJ);
96
97 // Remove check from ledger.
98 view().erase(sleCheck);
99 return tesSUCCESS;
100}
101
102void
104{
105 // No transaction-specific invariants yet (future work).
106}
107
108bool
110{
111 // No transaction-specific invariants yet (future work).
112 return true;
113}
114
115} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Stream warn() const
Definition Journal.h:309
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void erase(SLE::ref sle)=0
Remove a peeked SLE.
static TER preclaim(PreclaimContext const &ctx)
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
static NotTEC preflight(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
TER doApply() override
A view into a ledger.
Definition ReadView.h:31
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
std::shared_ptr< STLedgerEntry const > const & const_ref
beast::Journal const j_
Definition Transactor.h:118
ApplyView & view()
Definition Transactor.h:136
ApplyContext & ctx_
Definition Transactor.h:116
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:357
Keylet check(AccountID const &id, std::uint32_t seq) noexcept
A Check.
Definition Indexes.cpp:322
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
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:47
@ tefBAD_LEDGER
Definition TER.h:160
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
void adjustOwnerCount(ApplyView &view, SLE::ref sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
TERSubset< CanCvtToTER > TER
Definition TER.h:634
@ tecNO_ENTRY
Definition TER.h:304
@ tecNO_PERMISSION
Definition TER.h:303
@ tesSUCCESS
Definition TER.h:240
uint256 key
Definition Keylet.h:20
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:18