xrpld
Loading...
Searching...
No Matches
VaultDelete.cpp
1#include <xrpl/tx/transactors/vault/VaultDelete.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/ledger/helpers/AccountRootHelpers.h>
7#include <xrpl/ledger/helpers/MPTokenHelpers.h>
8#include <xrpl/ledger/helpers/TokenHelpers.h>
9#include <xrpl/ledger/helpers/VaultHelpers.h>
10#include <xrpl/protocol/AccountID.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/MPTIssue.h>
14#include <xrpl/protocol/Protocol.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STLedgerEntry.h>
17#include <xrpl/protocol/STNumber.h> // IWYU pragma: keep
18#include <xrpl/protocol/STTx.h>
19#include <xrpl/protocol/TER.h>
20#include <xrpl/protocol/XRPAmount.h>
21#include <xrpl/tx/Transactor.h>
22
23namespace xrpl {
24
27{
28 if (ctx.tx[sfVaultID] == beast::kZero)
29 {
30 JLOG(ctx.j.debug()) << "VaultDelete: zero/empty vault ID.";
31 return temMALFORMED;
32 }
33
34 if (ctx.tx.isFieldPresent(sfMemoData) && !ctx.rules.enabled(featureLendingProtocolV1_1))
35 return temDISABLED;
36
37 // The sfMemoData field is an optional field used to record the deletion reason.
38 if (!validDataLength(ctx.tx[~sfMemoData], kMaxDataPayloadLength))
39 return temMALFORMED;
40
41 return tesSUCCESS;
42}
43
44TER
46{
47 auto const vault = ctx.view.read(keylet::vault(ctx.tx[sfVaultID]));
48 if (!vault)
49 return tecNO_ENTRY;
50
51 if (vault->at(sfOwner) != ctx.tx[sfAccount])
52 {
53 JLOG(ctx.j.debug()) << "VaultDelete: account is not an owner.";
54 return tecNO_PERMISSION;
55 }
56
57 if (vault->at(sfAssetsAvailable) != 0)
58 {
59 JLOG(ctx.j.debug()) << "VaultDelete: nonzero assets available.";
60 return tecHAS_OBLIGATIONS;
61 }
62
63 if (getAssetsTotal(vault) != 0)
64 {
65 JLOG(ctx.j.debug()) << "VaultDelete: nonzero assets total.";
66 return tecHAS_OBLIGATIONS;
67 }
68
69 // Verify we can destroy MPTokenIssuance
70 auto const sleMPT = ctx.view.read(keylet::mptokenIssuance(vault->at(sfShareMPTID)));
71
72 if (!sleMPT)
73 {
74 // LCOV_EXCL_START
75 JLOG(ctx.j.error()) << "VaultDelete: missing issuance of vault shares.";
77 // LCOV_EXCL_STOP
78 }
79
80 if (sleMPT->at(sfIssuer) != vault->getAccountID(sfAccount))
81 {
82 // LCOV_EXCL_START
83 JLOG(ctx.j.error()) << "VaultDelete: invalid owner of vault shares.";
84 return tecNO_PERMISSION;
85 // LCOV_EXCL_STOP
86 }
87
88 if (sleMPT->at(sfOutstandingAmount) != 0)
89 {
90 JLOG(ctx.j.debug()) << "VaultDelete: nonzero outstanding shares.";
91 return tecHAS_OBLIGATIONS;
92 }
93
94 return tesSUCCESS;
95}
96
97TER
99{
100 auto const vault = view().peek(keylet::vault(ctx_.tx[sfVaultID]));
101 auto const applyViewContext = ctx_.getApplyViewContext();
102 if (!vault)
103 return tefINTERNAL; // LCOV_EXCL_LINE
104
105 // Destroy the asset holding.
106 auto const asset = vault->at(sfAsset);
107
108 if (auto ter = removeEmptyHolding(applyViewContext, vault->at(sfAccount), asset, j_);
109 !isTesSuccess(ter))
110 return ter;
111
112 auto const& pseudoID = vault->at(sfAccount);
113 auto const pseudoAcct = view().peek(keylet::account(pseudoID));
114 if (!pseudoAcct)
115 {
116 // LCOV_EXCL_START
117 JLOG(j_.error()) << "VaultDelete: missing vault pseudo-account.";
118 return tefBAD_LEDGER;
119 // LCOV_EXCL_STOP
120 }
121
122 // Destroy the share issuance. Do not use MPTokenIssuanceDestroy for this,
123 // no special logic needed. First run few checks, duplicated from preclaim.
124 auto const shareMPTID = *vault->at(sfShareMPTID);
125 auto const mpt = view().peek(keylet::mptokenIssuance(shareMPTID));
126 if (!mpt)
127 {
128 // LCOV_EXCL_START
129 JLOG(j_.error()) << "VaultDelete: missing issuance of vault shares.";
130 return tefINTERNAL;
131 // LCOV_EXCL_STOP
132 }
133
134 // Try to remove MPToken for vault shares for the vault owner if it exists.
135 if (auto const mptoken = view().peek(keylet::mptoken(shareMPTID, accountID_)))
136 {
137 if (auto const ter =
138 removeEmptyHolding(applyViewContext, accountID_, MPTIssue(shareMPTID), j_);
139 !isTesSuccess(ter))
140 {
141 // LCOV_EXCL_START
142 JLOG(j_.error()) //
143 << "VaultDelete: failed to remove vault owner's MPToken"
144 << " MPTID=" << to_string(shareMPTID) //
145 << " account=" << toBase58(accountID_) //
146 << " with result: " << transToken(ter);
147 return ter;
148 // LCOV_EXCL_STOP
149 }
150 }
151
152 if (!view().dirRemove(keylet::ownerDir(pseudoID), (*mpt)[sfOwnerNode], mpt->key(), false))
153 {
154 // LCOV_EXCL_START
155 JLOG(j_.error()) << "VaultDelete: failed to delete issuance object.";
156 return tefBAD_LEDGER;
157 // LCOV_EXCL_STOP
158 }
159 decreaseOwnerCountForObject(view(), pseudoAcct, mpt, 1, j_);
160
161 view().erase(mpt);
162
163 // The pseudo-account's directory should have been deleted already.
164 if (view().peek(keylet::ownerDir(pseudoID)))
165 return tecHAS_OBLIGATIONS; // LCOV_EXCL_LINE
166
167 // Destroy the pseudo-account.
168 auto const vaultPseudoSLE = view().peek(keylet::account(pseudoID));
169 if (!vaultPseudoSLE || vaultPseudoSLE->at(~sfVaultID) != vault->key())
170 return tefBAD_LEDGER; // LCOV_EXCL_LINE
171
172 // Making the payment and removing the empty holding should have deleted any
173 // obligations associated with the vault or vault pseudo-account.
174 if (*vaultPseudoSLE->at(sfBalance))
175 {
176 // LCOV_EXCL_START
177 JLOG(j_.error()) << "VaultDelete: pseudo-account has a balance";
178 return tecHAS_OBLIGATIONS;
179 // LCOV_EXCL_STOP
180 }
181 if (vaultPseudoSLE->at(sfOwnerCount) != 0)
182 {
183 // LCOV_EXCL_START
184 JLOG(j_.error()) << "VaultDelete: pseudo-account still owns objects";
185 return tecHAS_OBLIGATIONS;
186 // LCOV_EXCL_STOP
187 }
188 if (view().exists(keylet::ownerDir(pseudoID)))
189 {
190 // LCOV_EXCL_START
191 JLOG(j_.error()) << "VaultDelete: pseudo-account has a directory";
192 return tecHAS_OBLIGATIONS;
193 // LCOV_EXCL_STOP
194 }
195
196 view().erase(vaultPseudoSLE);
197
198 // Remove the vault from its owner's directory.
199 auto const ownerID = vault->at(sfOwner);
200 if (!view().dirRemove(keylet::ownerDir(ownerID), vault->at(sfOwnerNode), vault->key(), false))
201 {
202 // LCOV_EXCL_START
203 JLOG(j_.error()) << "VaultDelete: failed to delete vault object.";
204 return tefBAD_LEDGER;
205 // LCOV_EXCL_STOP
206 }
207
208 auto const owner = view().peek(keylet::account(ownerID));
209 if (!owner)
210 {
211 // LCOV_EXCL_START
212 JLOG(j_.error()) << "VaultDelete: missing vault owner account.";
213 return tefBAD_LEDGER;
214 // LCOV_EXCL_STOP
215 }
216
217 // We are destroying Vault and PseudoAccount, hence decrease by 2
218 decreaseOwnerCountForObject(view(), owner, vault, 2, j_);
219
220 // Destroy the vault.
221 view().erase(vault);
222
223 return tesSUCCESS;
224}
225
226void
228{
229 // No transaction-specific invariants yet (future work).
230}
231
232bool
234{
235 // No transaction-specific invariants yet (future work).
236 return true;
237}
238
239} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream error() const
Definition Journal.h:362
Stream debug() const
Definition Journal.h:344
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.
A view into a ledger.
Definition ReadView.h:41
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
std::shared_ptr< STLedgerEntry const > const & ConstRef
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
beast::Journal const j_
Definition Transactor.h:164
ApplyView & view()
Definition Transactor.h:184
AccountID const accountID_
Definition Transactor.h:166
static bool validDataLength(std::optional< Slice > const &slice, std::size_t maxLength)
ApplyContext & ctx_
Definition Transactor.h:162
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 TER preclaim(PreclaimContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
static NotTEC preflight(PreflightContext const &ctx)
TER doApply() override
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
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:565
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:547
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:541
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TER removeEmptyHolding(ApplyViewContext ctx, AccountID const &accountID, MPTIssue const &mptIssue, beast::Journal journal)
@ tefBAD_LEDGER
Definition TER.h:164
@ tefINTERNAL
Definition TER.h:167
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
std::string transToken(TER code)
Definition TER.cpp:259
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
constexpr std::size_t kMaxDataPayloadLength
The maximum length of Data payload.
Definition Protocol.h:303
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.
@ temMALFORMED
Definition TER.h:75
@ temDISABLED
Definition TER.h:102
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecNO_ENTRY
Definition TER.h:317
@ tecOBJECT_NOT_FOUND
Definition TER.h:337
@ tecNO_PERMISSION
Definition TER.h:316
@ tecHAS_OBLIGATIONS
Definition TER.h:328
Number getAssetsTotal(SLE::ConstRef vault)
Return the Vault's current assets total.
@ tesSUCCESS
Definition TER.h:253
UInt256 key
Definition Keylet.h:21
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
beast::Journal const j
Definition Transactor.h:46