rippled
Loading...
Searching...
No Matches
MPTokenAuthorize.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/tx/detail/MPTokenAuthorize.h>
21#include <xrpld/ledger/View.h>
22
23#include <xrpl/protocol/Feature.h>
24#include <xrpl/protocol/TxFlags.h>
25#include <xrpl/protocol/st.h>
26
27namespace ripple {
28
31{
32 if (!ctx.rules.enabled(featureMPTokensV1))
33 return temDISABLED;
34
35 if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
36 return ret;
37
39 return temINVALID_FLAG;
40
41 if (ctx.tx[sfAccount] == ctx.tx[~sfHolder])
42 return temMALFORMED;
43
44 return preflight2(ctx);
45}
46
47TER
49{
50 auto const accountID = ctx.tx[sfAccount];
51 auto const holderID = ctx.tx[~sfHolder];
52
53 // if non-issuer account submits this tx, then they are trying either:
54 // 1. Unauthorize/delete MPToken
55 // 2. Use/create MPToken
56 //
57 // Note: `accountID` is holder's account
58 // `holderID` is NOT used
59 if (!holderID)
60 {
62 keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], accountID));
63
64 // There is an edge case where all holders have zero balance, issuance
65 // is legally destroyed, then outstanding MPT(s) are deleted afterwards.
66 // Thus, there is no need to check for the existence of the issuance if
67 // the MPT is being deleted with a zero balance. Check for unauthorize
68 // before fetching the MPTIssuance object.
69
70 // if holder wants to delete/unauthorize a mpt
71 if (ctx.tx.getFlags() & tfMPTUnauthorize)
72 {
73 if (!sleMpt)
75
76 if ((*sleMpt)[sfMPTAmount] != 0)
77 {
78 auto const sleMptIssuance = ctx.view.read(
79 keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
80 if (!sleMptIssuance)
81 return tefINTERNAL;
82
83 return tecHAS_OBLIGATIONS;
84 }
85
86 if (ctx.view.rules().enabled(featureSingleAssetVault) &&
87 sleMpt->isFlag(lsfMPTLocked))
88 return tecNO_PERMISSION;
89
90 return tesSUCCESS;
91 }
92
93 // Now test when the holder wants to hold/create/authorize a new MPT
94 auto const sleMptIssuance =
95 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
96
97 if (!sleMptIssuance)
99
100 if (accountID == (*sleMptIssuance)[sfIssuer])
101 return tecNO_PERMISSION;
102
103 // if holder wants to use and create a mpt
104 if (sleMpt)
105 return tecDUPLICATE;
106
107 return tesSUCCESS;
108 }
109
110 if (!ctx.view.exists(keylet::account(*holderID)))
111 return tecNO_DST;
112
113 auto const sleMptIssuance =
114 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
115 if (!sleMptIssuance)
116 return tecOBJECT_NOT_FOUND;
117
118 std::uint32_t const mptIssuanceFlags = sleMptIssuance->getFieldU32(sfFlags);
119
120 // If tx is submitted by issuer, they would either try to do the following
121 // for allowlisting:
122 // 1. authorize an account
123 // 2. unauthorize an account
124 //
125 // Note: `accountID` is issuer's account
126 // `holderID` is holder's account
127 if (accountID != (*sleMptIssuance)[sfIssuer])
128 return tecNO_PERMISSION;
129
130 // If tx is submitted by issuer, it only applies for MPT with
131 // lsfMPTRequireAuth set
132 if (!(mptIssuanceFlags & lsfMPTRequireAuth))
133 return tecNO_AUTH;
134
135 // The holder must create the MPT before the issuer can authorize it.
136 if (!ctx.view.exists(
137 keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], *holderID)))
138 return tecOBJECT_NOT_FOUND;
139
140 return tesSUCCESS;
141}
142
143TER
145 ApplyView& view,
146 beast::Journal journal,
147 MPTAuthorizeArgs const& args)
148{
149 auto const sleAcct = view.peek(keylet::account(args.account));
150 if (!sleAcct)
151 return tecINTERNAL;
152
153 // If the account that submitted the tx is a holder
154 // Note: `account_` is holder's account
155 // `holderID` is NOT used
156 if (!args.holderID)
157 {
158 // When a holder wants to unauthorize/delete a MPT, the ledger must
159 // - delete mptokenKey from owner directory
160 // - delete the MPToken
161 if (args.flags & tfMPTUnauthorize)
162 {
163 auto const mptokenKey =
165 auto const sleMpt = view.peek(mptokenKey);
166 if (!sleMpt || (*sleMpt)[sfMPTAmount] != 0)
167 return tecINTERNAL; // LCOV_EXCL_LINE
168
169 if (!view.dirRemove(
171 (*sleMpt)[sfOwnerNode],
172 sleMpt->key(),
173 false))
174 return tecINTERNAL; // LCOV_EXCL_LINE
175
176 adjustOwnerCount(view, sleAcct, -1, journal);
177
178 view.erase(sleMpt);
179 return tesSUCCESS;
180 }
181
182 // A potential holder wants to authorize/hold a mpt, the ledger must:
183 // - add the new mptokenKey to the owner directory
184 // - create the MPToken object for the holder
185
186 // The reserve that is required to create the MPToken. Note
187 // that although the reserve increases with every item
188 // an account owns, in the case of MPTokens we only
189 // *enforce* a reserve if the user owns more than two
190 // items. This is similar to the reserve requirements of trust lines.
191 std::uint32_t const uOwnerCount = sleAcct->getFieldU32(sfOwnerCount);
192 XRPAmount const reserveCreate(
193 (uOwnerCount < 2) ? XRPAmount(beast::zero)
194 : view.fees().accountReserve(uOwnerCount + 1));
195
196 if (args.priorBalance < reserveCreate)
198
199 auto const mptokenKey =
201 auto mptoken = std::make_shared<SLE>(mptokenKey);
202 if (auto ter = dirLink(view, args.account, mptoken))
203 return ter; // LCOV_EXCL_LINE
204
205 (*mptoken)[sfAccount] = args.account;
206 (*mptoken)[sfMPTokenIssuanceID] = args.mptIssuanceID;
207 (*mptoken)[sfFlags] = 0;
208 view.insert(mptoken);
209
210 // Update owner count.
211 adjustOwnerCount(view, sleAcct, 1, journal);
212
213 return tesSUCCESS;
214 }
215
216 auto const sleMptIssuance =
218 if (!sleMptIssuance)
219 return tecINTERNAL;
220
221 // If the account that submitted this tx is the issuer of the MPT
222 // Note: `account_` is issuer's account
223 // `holderID` is holder's account
224 if (args.account != (*sleMptIssuance)[sfIssuer])
225 return tecINTERNAL;
226
227 auto const sleMpt =
229 if (!sleMpt)
230 return tecINTERNAL;
231
232 std::uint32_t const flagsIn = sleMpt->getFieldU32(sfFlags);
233 std::uint32_t flagsOut = flagsIn;
234
235 // Issuer wants to unauthorize the holder, unset lsfMPTAuthorized on
236 // their MPToken
237 if (args.flags & tfMPTUnauthorize)
238 flagsOut &= ~lsfMPTAuthorized;
239 // Issuer wants to authorize a holder, set lsfMPTAuthorized on their
240 // MPToken
241 else
242 flagsOut |= lsfMPTAuthorized;
243
244 if (flagsIn != flagsOut)
245 sleMpt->setFieldU32(sfFlags, flagsOut);
246
247 view.update(sleMpt);
248 return tesSUCCESS;
249}
250
251TER
253{
254 auto const& tx = ctx_.tx;
255 return authorize(
256 ctx_.view(),
258 {.priorBalance = mPriorBalance,
259 .mptIssuanceID = tx[sfMPTokenIssuanceID],
260 .account = account_,
261 .flags = tx.getFlags(),
262 .holderID = tx[~sfHolder]});
263}
264
265} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:60
ApplyView & view()
Definition: ApplyContext.h:78
beast::Journal const journal
Definition: ApplyContext.h:75
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:144
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
Definition: ApplyView.cpp:190
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static TER authorize(ApplyView &view, beast::Journal journal, MPTAuthorizeArgs const &args)
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:130
std::uint32_t getFlags() const
Definition: STObject.cpp:537
ApplyView & view()
Definition: Transactor.h:159
ApplyContext & ctx_
Definition: Transactor.h:140
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition: Indexes.cpp:533
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition: Indexes.cpp:519
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:177
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:367
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
@ lsfMPTRequireAuth
@ lsfMPTAuthorized
@ lsfMPTLocked
constexpr std::uint32_t const tfMPTokenAuthorizeMask
Definition: TxFlags.h:161
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:91
@ tefINTERNAL
Definition: TER.h:173
static bool adjustOwnerCount(ApplyContext &ctx, int count)
Definition: SetOracle.cpp:186
constexpr std::uint32_t const tfMPTUnauthorize
Definition: TxFlags.h:160
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:160
@ tecNO_DST
Definition: TER.h:290
@ tecOBJECT_NOT_FOUND
Definition: TER.h:326
@ tecDUPLICATE
Definition: TER.h:315
@ tecINTERNAL
Definition: TER.h:310
@ tecNO_PERMISSION
Definition: TER.h:305
@ tecHAS_OBLIGATIONS
Definition: TER.h:317
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:307
@ tecNO_AUTH
Definition: TER.h:300
@ tesSUCCESS
Definition: TER.h:244
bool isTesSuccess(TER x) noexcept
Definition: TER.h:672
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:603
TER dirLink(ApplyView &view, AccountID const &owner, std::shared_ptr< SLE > &object)
Definition: View.cpp:1045
@ temMALFORMED
Definition: TER.h:87
@ temINVALID_FLAG
Definition: TER.h:111
@ temDISABLED
Definition: TER.h:114
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
Definition: protocol/Fees.h:49
uint256 key
Definition: Keylet.h:40
AccountID const & account
std::optional< AccountID > holderID
XRPAmount const & priorBalance
State information when determining if a tx is likely to claim a fee.
Definition: Transactor.h:79
ReadView const & view
Definition: Transactor.h:82
State information when preflighting a tx.
Definition: Transactor.h:34