xrpld
Loading...
Searching...
No Matches
LoanBrokerDelete.cpp
1#include <xrpl/tx/transactors/lending/LoanBrokerDelete.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/ledger/helpers/AccountRootHelpers.h>
7#include <xrpl/ledger/helpers/LendingHelpers.h>
8#include <xrpl/ledger/helpers/TokenHelpers.h>
9#include <xrpl/ledger/helpers/VaultHelpers.h>
10#include <xrpl/protocol/Asset.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/STAmount.h>
15#include <xrpl/protocol/STLedgerEntry.h>
16#include <xrpl/protocol/STTx.h>
17#include <xrpl/protocol/TER.h>
18#include <xrpl/protocol/XRPAmount.h>
19#include <xrpl/tx/Transactor.h>
20
21namespace xrpl {
22
23bool
28
31{
32 if (ctx.tx[sfLoanBrokerID] == beast::kZero)
33 return temINVALID;
34
35 return tesSUCCESS;
36}
37
38TER
40{
41 auto const& tx = ctx.tx;
42
43 auto const account = tx[sfAccount];
44 auto const brokerID = tx[sfLoanBrokerID];
45
46 auto const sleBroker = ctx.view.read(keylet::loanBroker(brokerID));
47 if (!sleBroker)
48 {
49 JLOG(ctx.j.warn()) << "LoanBroker does not exist.";
50 return tecNO_ENTRY;
51 }
52
53 auto const brokerOwner = sleBroker->at(sfOwner);
54
55 if (account != brokerOwner)
56 {
57 JLOG(ctx.j.warn()) << "Account is not the owner of the LoanBroker.";
58 return tecNO_PERMISSION;
59 }
60 if (auto const ownerCount = sleBroker->at(sfOwnerCount); ownerCount != 0)
61 {
62 JLOG(ctx.j.warn()) << "LoanBrokerDelete: Owner count is " << ownerCount;
63 return tecHAS_OBLIGATIONS;
64 }
65
66 auto const vault = ctx.view.read(keylet::vault(sleBroker->at(sfVaultID)));
67 if (!vault)
68 {
69 // LCOV_EXCL_START
70 JLOG(ctx.j.fatal()) << "Vault is missing for Broker " << brokerID;
71 return tefBAD_LEDGER;
72 // LCOV_EXCL_STOP
73 }
74
75 Asset const asset = vault->at(sfAsset);
76
77 if (auto const debtTotal = sleBroker->at(sfDebtTotal); debtTotal != beast::kZero)
78 {
79 // Any remaining debt should have been wiped out by the last Loan
80 // Delete. This check is purely defensive.
81 auto const scale = getVaultScale(vault);
82
83 auto const rounded =
85
86 if (rounded != beast::kZero)
87 {
88 // LCOV_EXCL_START
89 JLOG(ctx.j.warn()) << "LoanBrokerDelete: Debt total is " << debtTotal
90 << ", which rounds to " << rounded;
91 return tecHAS_OBLIGATIONS;
92 // LCOV_EXCL_STOP
93 }
94 }
95
96 auto const coverAvailable = STAmount{asset, sleBroker->at(sfCoverAvailable)};
97 // If there are assets in the cover, broker will receive them on deletion.
98 if (coverAvailable > beast::kZero)
99 {
100 auto const brokerPseudo = sleBroker->at(sfAccount);
101
102 // Pre-fixCleanup3_5_0: only freeze checks apply to the cover payout.
103 // Post-fixCleanup3_5_0: apply the cover-withdraw transfer and authorization checks too.
104 if (ctx.view.rules().enabled(fixCleanup3_5_0))
105 {
106 // Like a cover withdrawal, the payout skips the lsfMPTCanTransfer check so that an
107 // issuer cannot trap the broker's cover.
108 if (auto const ret = canTransfer(
109 ctx.view, asset, brokerPseudo, brokerOwner, WaiveMPTCanTransfer::Yes))
110 return ret;
111
112 if (auto const ret = requireAuth(ctx.view, asset, brokerOwner, AuthType::WeakAuth))
113 return ret;
114
115 if (!holdingExists(ctx.view, brokerOwner, asset))
116 {
117 if (auto const ret = canAddHolding(ctx.view, asset); !isTesSuccess(ret))
118 return ret;
119 }
120 }
121
122 if (auto const ret = checkDeepFrozen(ctx.view, brokerOwner, asset))
123 {
124 JLOG(ctx.j.warn()) << "Broker owner account is frozen.";
125 return ret;
126 }
127
128 if (ctx.view.rules().enabled(fixCleanup3_2_0))
129 {
130 if (auto const ret = checkFrozen(ctx.view, brokerPseudo, asset))
131 {
132 JLOG(ctx.j.warn()) << "Broker pseudo-account is frozen/locked.";
133 return ret;
134 }
135 }
136 }
137
138 return tesSUCCESS;
139}
140
141TER
143{
144 auto const& tx = ctx_.tx;
145
146 auto const brokerID = tx[sfLoanBrokerID];
147
148 // Delete the loan broker
149 auto const broker = view().peek(keylet::loanBroker(brokerID));
150 if (!broker)
151 return tefBAD_LEDGER; // LCOV_EXCL_LINE
152 auto const vaultID = broker->at(sfVaultID);
153 auto const sleVault = view().read(keylet::vault(vaultID));
154 if (!sleVault)
155 return tefBAD_LEDGER; // LCOV_EXCL_LINE
156 auto const vaultPseudoID = sleVault->at(sfAccount);
157 auto const vaultAsset = sleVault->at(sfAsset);
158
159 auto const brokerPseudoID = broker->at(sfAccount);
160
161 if (!view().dirRemove(
162 keylet::ownerDir(accountID_), broker->at(sfOwnerNode), broker->key(), false))
163 {
164 return tefBAD_LEDGER; // LCOV_EXCL_LINE
165 }
166 if (!view().dirRemove(
167 keylet::ownerDir(vaultPseudoID), broker->at(sfVaultNode), broker->key(), false))
168 {
169 return tefBAD_LEDGER; // LCOV_EXCL_LINE
170 }
171
172 auto const owner = view().peek(keylet::account(accountID_));
173 if (!owner)
174 return tefBAD_LEDGER; // LCOV_EXCL_LINE
175
176 // Pre-fixCleanup3_5_0: the owner count drops after the pseudo-account is erased.
177 // Post-fixCleanup3_5_0: it drops first, so the owner's holding created below can use the
178 // reserve that the broker frees.
179 bool const fix350Enabled = view().rules().enabled(fixCleanup3_5_0);
180 if (fix350Enabled)
181 {
182 // Decreases the owner count by two: one for the LoanBroker object, and one
183 // for the pseudo-account.
184 decreaseOwnerCountForObject(view(), owner, broker, 2, j_);
185 }
186
187 {
188 auto const coverAvailable = STAmount{vaultAsset, broker->at(sfCoverAvailable)};
189
190 // Pre-fixCleanup3_5_0: the cover goes straight to accountSend, so an MPT payout to an
191 // owner with no MPToken fails with tecNO_AUTH.
192 // Post-fixCleanup3_5_0: a nonzero cover first gets the owner a holding if it has none, as a
193 // withdrawal to self does.
194 if (fix350Enabled && coverAvailable > beast::kZero &&
195 !holdingExists(view(), accountID_, vaultAsset))
196 {
197 if (auto const ter = addEmptyHolding(
198 ctx_.getApplyViewContext(), accountID_, preFeeBalance_, vaultAsset, j_))
199 return ter;
200 }
201
202 if (auto const ter = accountSend(
203 view(), brokerPseudoID, accountID_, coverAvailable, j_, {}, WaiveTransferFee::Yes))
204 return ter;
205 }
206
207 if (auto ter = removeEmptyHolding(ctx_.getApplyViewContext(), brokerPseudoID, vaultAsset, j_))
208 return ter;
209
210 auto const brokerPseudoSLE = view().peek(keylet::account(brokerPseudoID));
211 if (!brokerPseudoSLE)
212 return tefBAD_LEDGER; // LCOV_EXCL_LINE
213
214 // Making the payment and removing the empty holding should have deleted any
215 // obligations associated with the broker or broker pseudo-account.
216 if (*brokerPseudoSLE->at(sfBalance))
217 {
218 JLOG(j_.warn()) << "LoanBrokerDelete: Pseudo-account has a balance";
219 return tecHAS_OBLIGATIONS; // LCOV_EXCL_LINE
220 }
221 if (brokerPseudoSLE->at(sfOwnerCount) != 0)
222 {
223 JLOG(j_.warn()) << "LoanBrokerDelete: Pseudo-account still owns objects";
224 return tecHAS_OBLIGATIONS; // LCOV_EXCL_LINE
225 }
226 if (auto const directory = keylet::ownerDir(brokerPseudoID); view().read(directory))
227 {
228 JLOG(j_.warn()) << "LoanBrokerDelete: Pseudo-account has a directory";
229 return tecHAS_OBLIGATIONS; // LCOV_EXCL_LINE
230 }
231
232 view().erase(brokerPseudoSLE);
233
234 if (!fix350Enabled)
235 decreaseOwnerCountForObject(view(), owner, broker, 2, j_);
236
237 view().erase(broker);
238
239 return tesSUCCESS;
240}
241
242void
244{
245 // No transaction-specific invariants yet (future work).
246}
247
248bool
250 STTx const&,
251 TER,
252 XRPAmount,
253 ReadView const&,
254 beast::Journal const&)
255{
256 // No transaction-specific invariants yet (future work).
257 return true;
258}
259
260//------------------------------------------------------------------------------
261
262} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream fatal() const
Definition Journal.h:368
Stream warn() const
Definition Journal.h:356
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.
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)
static bool checkExtraFeatures(PreflightContext const &ctx)
A view into a ledger.
Definition ReadView.h:41
virtual Rules const & rules() const =0
Returns the tx processing rules.
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
beast::Journal const j_
Definition Transactor.h:164
ApplyView & view()
Definition Transactor.h:184
AccountID const accountID_
Definition Transactor.h:166
XRPAmount preFeeBalance_
Definition Transactor.h:167
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
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:565
Keylet loanBroker(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:571
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TER checkDeepFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
std::uint32_t ownerCount(SLE::ConstRef sle, beast::Journal j, std::int32_t ownerCountAdj=0)
Return number of the objects which reserve is covered by the account(sle) (so called "ownercount").
TER removeEmptyHolding(ApplyViewContext ctx, AccountID const &accountID, MPTIssue const &mptIssue, beast::Journal journal)
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
Definition STAmount.h:819
@ tefBAD_LEDGER
Definition TER.h:164
TER addEmptyHolding(ApplyViewContext ctx, AccountID const &accountID, XRPAmount priorBalance, MPTIssue const &mptIssue, beast::Journal journal)
int getVaultScale(SLE::ConstRef vault)
Return the Vault's current live exponent.
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to, WaiveMPTCanTransfer waive=WaiveMPTCanTransfer::No, std::uint8_t depth=0)
Check whether to may receive the given MPT from from.
TER canAddHolding(ReadView const &view, MPTIssue const &mptIssue)
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, SLE::Ref sponsorSle={}, WaiveTransferFee waiveFee=WaiveTransferFee::No, AllowMPTOverflow allowOverflow=AllowMPTOverflow::No)
Calls static accountSendIOU if saAmount represents Issue.
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.
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
Definition STAmount.h:745
@ temINVALID
Definition TER.h:98
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, std::uint8_t depth=0)
Check if the account lacks required authorization for MPT.
@ tecNO_ENTRY
Definition TER.h:317
@ tecNO_PERMISSION
Definition TER.h:316
@ tecHAS_OBLIGATIONS
Definition TER.h:328
@ tesSUCCESS
Definition TER.h:253
bool holdingExists(ReadView const &view, AccountID const &account, Asset const &asset)
True if the account already holds this asset (or is the issuer / XRP).
bool checkLendingProtocolDependencies(Rules const &rules, STTx const &tx)
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