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 return tesSUCCESS;
87 }
88
89 // Now test when the holder wants to hold/create/authorize a new MPT
90 auto const sleMptIssuance =
91 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
92
93 if (!sleMptIssuance)
95
96 if (accountID == (*sleMptIssuance)[sfIssuer])
97 return tecNO_PERMISSION;
98
99 // if holder wants to use and create a mpt
100 if (sleMpt)
101 return tecDUPLICATE;
102
103 return tesSUCCESS;
104 }
105
106 if (!ctx.view.exists(keylet::account(*holderID)))
107 return tecNO_DST;
108
109 auto const sleMptIssuance =
110 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
111 if (!sleMptIssuance)
112 return tecOBJECT_NOT_FOUND;
113
114 std::uint32_t const mptIssuanceFlags = sleMptIssuance->getFieldU32(sfFlags);
115
116 // If tx is submitted by issuer, they would either try to do the following
117 // for allowlisting:
118 // 1. authorize an account
119 // 2. unauthorize an account
120 //
121 // Note: `accountID` is issuer's account
122 // `holderID` is holder's account
123 if (accountID != (*sleMptIssuance)[sfIssuer])
124 return tecNO_PERMISSION;
125
126 // If tx is submitted by issuer, it only applies for MPT with
127 // lsfMPTRequireAuth set
128 if (!(mptIssuanceFlags & lsfMPTRequireAuth))
129 return tecNO_AUTH;
130
131 // The holder must create the MPT before the issuer can authorize it.
132 if (!ctx.view.exists(
133 keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], *holderID)))
134 return tecOBJECT_NOT_FOUND;
135
136 return tesSUCCESS;
137}
138
139TER
141 ApplyView& view,
142 beast::Journal journal,
143 MPTAuthorizeArgs const& args)
144{
145 auto const sleAcct = view.peek(keylet::account(args.account));
146 if (!sleAcct)
147 return tecINTERNAL;
148
149 // If the account that submitted the tx is a holder
150 // Note: `account_` is holder's account
151 // `holderID` is NOT used
152 if (!args.holderID)
153 {
154 // When a holder wants to unauthorize/delete a MPT, the ledger must
155 // - delete mptokenKey from owner directory
156 // - delete the MPToken
157 if (args.flags & tfMPTUnauthorize)
158 {
159 auto const mptokenKey =
161 auto const sleMpt = view.peek(mptokenKey);
162 if (!sleMpt || (*sleMpt)[sfMPTAmount] != 0)
163 return tecINTERNAL;
164
165 if (!view.dirRemove(
167 (*sleMpt)[sfOwnerNode],
168 sleMpt->key(),
169 false))
170 return tecINTERNAL;
171
172 adjustOwnerCount(view, sleAcct, -1, journal);
173
174 view.erase(sleMpt);
175 return tesSUCCESS;
176 }
177
178 // A potential holder wants to authorize/hold a mpt, the ledger must:
179 // - add the new mptokenKey to the owner directory
180 // - create the MPToken object for the holder
181
182 // The reserve that is required to create the MPToken. Note
183 // that although the reserve increases with every item
184 // an account owns, in the case of MPTokens we only
185 // *enforce* a reserve if the user owns more than two
186 // items. This is similar to the reserve requirements of trust lines.
187 std::uint32_t const uOwnerCount = sleAcct->getFieldU32(sfOwnerCount);
188 XRPAmount const reserveCreate(
189 (uOwnerCount < 2) ? XRPAmount(beast::zero)
190 : view.fees().accountReserve(uOwnerCount + 1));
191
192 if (args.priorBalance < reserveCreate)
194
195 auto const mptokenKey =
197
198 auto const ownerNode = view.dirInsert(
200 mptokenKey,
202
203 if (!ownerNode)
204 return tecDIR_FULL;
205
206 auto mptoken = std::make_shared<SLE>(mptokenKey);
207 (*mptoken)[sfAccount] = args.account;
208 (*mptoken)[sfMPTokenIssuanceID] = args.mptIssuanceID;
209 (*mptoken)[sfFlags] = 0;
210 (*mptoken)[sfOwnerNode] = *ownerNode;
211 view.insert(mptoken);
212
213 // Update owner count.
214 adjustOwnerCount(view, sleAcct, 1, journal);
215
216 return tesSUCCESS;
217 }
218
219 auto const sleMptIssuance =
221 if (!sleMptIssuance)
222 return tecINTERNAL;
223
224 // If the account that submitted this tx is the issuer of the MPT
225 // Note: `account_` is issuer's account
226 // `holderID` is holder's account
227 if (args.account != (*sleMptIssuance)[sfIssuer])
228 return tecINTERNAL;
229
230 auto const sleMpt =
232 if (!sleMpt)
233 return tecINTERNAL;
234
235 std::uint32_t const flagsIn = sleMpt->getFieldU32(sfFlags);
236 std::uint32_t flagsOut = flagsIn;
237
238 // Issuer wants to unauthorize the holder, unset lsfMPTAuthorized on
239 // their MPToken
240 if (args.flags & tfMPTUnauthorize)
241 flagsOut &= ~lsfMPTAuthorized;
242 // Issuer wants to authorize a holder, set lsfMPTAuthorized on their
243 // MPToken
244 else
245 flagsOut |= lsfMPTAuthorized;
246
247 if (flagsIn != flagsOut)
248 sleMpt->setFieldU32(sfFlags, flagsOut);
249
250 view.update(sleMpt);
251 return tesSUCCESS;
252}
253
254TER
256{
257 auto const& tx = ctx_.tx;
258 return authorize(
259 ctx_.view(),
261 {.priorBalance = mPriorBalance,
262 .mptIssuanceID = tx[sfMPTokenIssuanceID],
263 .account = account_,
264 .flags = tx.getFlags(),
265 .holderID = tx[~sfHolder]});
266}
267
268} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:60
ApplyView & view()
Definition: ApplyContext.h:55
beast::Journal const journal
Definition: ApplyContext.h:52
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:141
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.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(std::shared_ptr< SLE > const &)> const &describe)
Insert an entry to a directory.
Definition: ApplyView.h:315
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.
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:109
ApplyContext & ctx_
Definition: Transactor.h:90
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition: Indexes.cpp:532
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition: Indexes.cpp:518
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:176
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:366
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
@ lsfMPTRequireAuth
@ lsfMPTAuthorized
constexpr std::uint32_t const tfMPTokenAuthorizeMask
Definition: TxFlags.h:159
bool isTesSuccess(TER x)
Definition: TER.h:656
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:927
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:83
@ 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:158
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:144
@ tecNO_DST
Definition: TER.h:277
@ tecOBJECT_NOT_FOUND
Definition: TER.h:313
@ tecDIR_FULL
Definition: TER.h:274
@ tecDUPLICATE
Definition: TER.h:302
@ tecINTERNAL
Definition: TER.h:297
@ tecNO_PERMISSION
Definition: TER.h:292
@ tecHAS_OBLIGATIONS
Definition: TER.h:304
@ tecINSUFFICIENT_RESERVE
Definition: TER.h:294
@ tecNO_AUTH
Definition: TER.h:287
@ tesSUCCESS
Definition: TER.h:242
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:587
@ 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
uint192 const & mptIssuanceID
XRPAmount const & priorBalance
State information when determining if a tx is likely to claim a fee.
Definition: Transactor.h:55
ReadView const & view
Definition: Transactor.h:58
State information when preflighting a tx.
Definition: Transactor.h:34