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/beast/utility/Zero.h>
5#include <xrpl/ledger/ApplyView.h>
6#include <xrpl/ledger/ReadView.h>
7#include <xrpl/ledger/View.h>
8#include <xrpl/ledger/entries/CheckEntry.h>
9#include <xrpl/ledger/helpers/AccountRootHelpers.h>
10#include <xrpl/protocol/AccountID.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/STLedgerEntry.h>
15#include <xrpl/protocol/STTx.h>
16#include <xrpl/protocol/TER.h>
17#include <xrpl/protocol/XRPAmount.h>
18#include <xrpl/tx/Transactor.h>
19
20#include <cstdint>
21namespace xrpl {
22
25{
26 if (ctx.rules.enabled(fixCleanup3_3_0) && ctx.tx[sfCheckID] == beast::kZero)
27 return temMALFORMED;
28
29 return tesSUCCESS;
30}
31
32TER
34{
35 CheckEntryR const sleCheck(ctx.tx[sfCheckID], ctx.view);
36 if (!sleCheck)
37 {
38 JLOG(ctx.j.warn()) << "Check does not exist.";
39 return tecNO_ENTRY;
40 }
41
42 // Expiration is defined in terms of the close time of the parent
43 // ledger, because we definitively know the time that it closed but
44 // we do not know the closing time of the ledger that is under
45 // construction.
46 if (!hasExpired(ctx.view, (*sleCheck)[~sfExpiration]))
47 {
48 // If the check is not yet expired, then only the creator or the
49 // destination may cancel the check.
50 AccountID const acctId{ctx.tx[sfAccount]};
51 if (acctId != (*sleCheck)[sfAccount] && acctId != (*sleCheck)[sfDestination])
52 {
53 JLOG(ctx.j.warn()) << "Check is not expired and canceler is "
54 "neither check source nor destination.";
55 return tecNO_PERMISSION;
56 }
57 }
58 return tesSUCCESS;
59}
60
61TER
63{
64 CheckEntryW sleCheck(ctx_.tx[sfCheckID], view(), j_);
65 if (!sleCheck)
66 {
67 // Error should have been caught in preclaim.
68 JLOG(j_.warn()) << "Check does not exist.";
69 return tecNO_ENTRY;
70 }
71
72 AccountID const srcId{sleCheck->getAccountID(sfAccount)};
73 AccountID const dstId{sleCheck->getAccountID(sfDestination)};
74 auto const viewJ = ctx_.registry.get().getJournal("View");
75
76 // If the check is not written to self (and it shouldn't be), remove the
77 // check from the destination account root.
78 if (srcId != dstId)
79 {
80 std::uint64_t const page{(*sleCheck)[sfDestinationNode]};
81 if (!view().dirRemove(keylet::ownerDir(dstId), page, sleCheck->key(), true))
82 {
83 // LCOV_EXCL_START
84 JLOG(j_.fatal()) << "Unable to delete check from destination.";
85 return tefBAD_LEDGER;
86 // LCOV_EXCL_STOP
87 }
88 }
89 {
90 std::uint64_t const page{(*sleCheck)[sfOwnerNode]};
91 if (!view().dirRemove(keylet::ownerDir(srcId), page, sleCheck->key(), true))
92 {
93 // LCOV_EXCL_START
94 JLOG(j_.fatal()) << "Unable to delete check from owner.";
95 return tefBAD_LEDGER;
96 // LCOV_EXCL_STOP
97 }
98 }
99
100 // If we succeeded, update the check owner's reserve.
101 decreaseOwnerCountForObject(view(), srcId, sleCheck.mutableRawSle(), 1, viewJ);
102
103 // Remove check from ledger.
104 sleCheck.erase();
105 return tesSUCCESS;
106}
107
108void
110{
111 // No transaction-specific invariants yet (future work).
112}
113
114bool
116{
117 // No transaction-specific invariants yet (future work).
118 return true;
119}
120
121} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream warn() const
Definition Journal.h:356
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::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
TER doApply() override
A view into a ledger.
Definition ReadView.h:41
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
SlePtrType const & mutableRawSle()
Definition SLEBase.h:361
UInt256 const & key() const
Returns the 'key' (or 'index') of this item.
std::shared_ptr< STLedgerEntry const > const & ConstRef
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:643
beast::Journal const j_
Definition Transactor.h:164
ApplyView & view()
Definition Transactor.h:184
ApplyContext & ctx_
Definition Transactor.h:162
constexpr Zero kZero
Definition Zero.h:30
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:415
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, ExpiryComparison comparison=ExpiryComparison::Inclusive)
Determines whether the given expiration time has passed.
Definition View.cpp:52
CheckEntry< ReadView > CheckEntryR
Definition CheckEntry.h:43
@ tefBAD_LEDGER
Definition TER.h:164
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
void decreaseOwnerCountForObject(ApplyView &view, SLE::Ref accountSle, SLE::Ref objectSle, std::uint32_t count, beast::Journal j)
Decrease owner-count fields for an existing ledger object.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
@ temMALFORMED
Definition TER.h:75
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecNO_ENTRY
Definition TER.h:317
@ tecNO_PERMISSION
Definition TER.h:316
@ tesSUCCESS
Definition TER.h:253
CheckEntry< ApplyView > CheckEntryW
Definition CheckEntry.h:44
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:92
ReadView const & view
Definition Transactor.h:95
beast::Journal const j
Definition Transactor.h:100
State information when preflighting a tx.
Definition Transactor.h:39