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#include <xrpl/protocol/Feature.h>
23#include <xrpl/protocol/TxFlags.h>
24#include <xrpl/protocol/st.h>
25
26namespace ripple {
27
30{
31 if (!ctx.rules.enabled(featureMPTokensV1))
32 return temDISABLED;
33
34 if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
35 return ret;
36
38 return temINVALID_FLAG;
39
40 if (ctx.tx[sfAccount] == ctx.tx[~sfHolder])
41 return temMALFORMED;
42
43 return preflight2(ctx);
44}
45
46TER
48{
49 auto const accountID = ctx.tx[sfAccount];
50 auto const holderID = ctx.tx[~sfHolder];
51
52 // if non-issuer account submits this tx, then they are trying either:
53 // 1. Unauthorize/delete MPToken
54 // 2. Use/create MPToken
55 //
56 // Note: `accountID` is holder's account
57 // `holderID` is NOT used
58 if (!holderID)
59 {
61 keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], accountID));
62
63 // There is an edge case where all holders have zero balance, issuance
64 // is legally destroyed, then outstanding MPT(s) are deleted afterwards.
65 // Thus, there is no need to check for the existence of the issuance if
66 // the MPT is being deleted with a zero balance. Check for unauthorize
67 // before fetching the MPTIssuance object.
68
69 // if holder wants to delete/unauthorize a mpt
70 if (ctx.tx.getFlags() & tfMPTUnauthorize)
71 {
72 if (!sleMpt)
74
75 if ((*sleMpt)[sfMPTAmount] != 0)
76 {
77 auto const sleMptIssuance = ctx.view.read(
78 keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
79 if (!sleMptIssuance)
80 return tefINTERNAL;
81
82 return tecHAS_OBLIGATIONS;
83 }
84
85 return tesSUCCESS;
86 }
87
88 // Now test when the holder wants to hold/create/authorize a new MPT
89 auto const sleMptIssuance =
90 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
91
92 if (!sleMptIssuance)
94
95 if (accountID == (*sleMptIssuance)[sfIssuer])
96 return tecNO_PERMISSION;
97
98 // if holder wants to use and create a mpt
99 if (sleMpt)
100 return tecDUPLICATE;
101
102 return tesSUCCESS;
103 }
104
105 if (!ctx.view.exists(keylet::account(*holderID)))
106 return tecNO_DST;
107
108 auto const sleMptIssuance =
109 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
110 if (!sleMptIssuance)
111 return tecOBJECT_NOT_FOUND;
112
113 std::uint32_t const mptIssuanceFlags = sleMptIssuance->getFieldU32(sfFlags);
114
115 // If tx is submitted by issuer, they would either try to do the following
116 // for allowlisting:
117 // 1. authorize an account
118 // 2. unauthorize an account
119 //
120 // Note: `accountID` is issuer's account
121 // `holderID` is holder's account
122 if (accountID != (*sleMptIssuance)[sfIssuer])
123 return tecNO_PERMISSION;
124
125 // If tx is submitted by issuer, it only applies for MPT with
126 // lsfMPTRequireAuth set
127 if (!(mptIssuanceFlags & lsfMPTRequireAuth))
128 return tecNO_AUTH;
129
130 // The holder must create the MPT before the issuer can authorize it.
131 if (!ctx.view.exists(
132 keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], *holderID)))
133 return tecOBJECT_NOT_FOUND;
134
135 return tesSUCCESS;
136}
137
138TER
140 ApplyView& view,
141 beast::Journal journal,
142 MPTAuthorizeArgs const& args)
143{
144 auto const sleAcct = view.peek(keylet::account(args.account));
145 if (!sleAcct)
146 return tecINTERNAL;
147
148 // If the account that submitted the tx is a holder
149 // Note: `account_` is holder's account
150 // `holderID` is NOT used
151 if (!args.holderID)
152 {
153 // When a holder wants to unauthorize/delete a MPT, the ledger must
154 // - delete mptokenKey from owner directory
155 // - delete the MPToken
156 if (args.flags & tfMPTUnauthorize)
157 {
158 auto const mptokenKey =
160 auto const sleMpt = view.peek(mptokenKey);
161 if (!sleMpt || (*sleMpt)[sfMPTAmount] != 0)
162 return tecINTERNAL;
163
164 if (!view.dirRemove(
166 (*sleMpt)[sfOwnerNode],
167 sleMpt->key(),
168 false))
169 return tecINTERNAL;
170
171 adjustOwnerCount(view, sleAcct, -1, journal);
172
173 view.erase(sleMpt);
174 return tesSUCCESS;
175 }
176
177 // A potential holder wants to authorize/hold a mpt, the ledger must:
178 // - add the new mptokenKey to the owner directory
179 // - create the MPToken object for the holder
180
181 // The reserve that is required to create the MPToken. Note
182 // that although the reserve increases with every item
183 // an account owns, in the case of MPTokens we only
184 // *enforce* a reserve if the user owns more than two
185 // items. This is similar to the reserve requirements of trust lines.
186 std::uint32_t const uOwnerCount = sleAcct->getFieldU32(sfOwnerCount);
187 XRPAmount const reserveCreate(
188 (uOwnerCount < 2) ? XRPAmount(beast::zero)
189 : view.fees().accountReserve(uOwnerCount + 1));
190
191 if (args.priorBalance < reserveCreate)
193
194 auto const mptokenKey =
196
197 auto const ownerNode = view.dirInsert(
199 mptokenKey,
201
202 if (!ownerNode)
203 return tecDIR_FULL;
204
205 auto mptoken = std::make_shared<SLE>(mptokenKey);
206 (*mptoken)[sfAccount] = args.account;
207 (*mptoken)[sfMPTokenIssuanceID] = args.mptIssuanceID;
208 (*mptoken)[sfFlags] = 0;
209 (*mptoken)[sfOwnerNode] = *ownerNode;
210 view.insert(mptoken);
211
212 // Update owner count.
213 adjustOwnerCount(view, sleAcct, 1, journal);
214
215 return tesSUCCESS;
216 }
217
218 auto const sleMptIssuance =
220 if (!sleMptIssuance)
221 return tecINTERNAL;
222
223 // If the account that submitted this tx is the issuer of the MPT
224 // Note: `account_` is issuer's account
225 // `holderID` is holder's account
226 if (args.account != (*sleMptIssuance)[sfIssuer])
227 return tecINTERNAL;
228
229 auto const sleMpt =
231 if (!sleMpt)
232 return tecINTERNAL;
233
234 std::uint32_t const flagsIn = sleMpt->getFieldU32(sfFlags);
235 std::uint32_t flagsOut = flagsIn;
236
237 // Issuer wants to unauthorize the holder, unset lsfMPTAuthorized on
238 // their MPToken
239 if (args.flags & tfMPTUnauthorize)
240 flagsOut &= ~lsfMPTAuthorized;
241 // Issuer wants to authorize a holder, set lsfMPTAuthorized on their
242 // MPToken
243 else
244 flagsOut |= lsfMPTAuthorized;
245
246 if (flagsIn != flagsOut)
247 sleMpt->setFieldU32(sfFlags, flagsOut);
248
249 view.update(sleMpt);
250 return tesSUCCESS;
251}
252
253TER
255{
256 auto const& tx = ctx_.tx;
257 return authorize(
258 ctx_.view(),
260 {.priorBalance = mPriorBalance,
261 .mptIssuanceID = tx[sfMPTokenIssuanceID],
262 .account = account_,
263 .flags = tx.getFlags(),
264 .holderID = tx[~sfHolder]});
265}
266
267} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:59
ApplyView & view()
Definition: ApplyContext.h:54
beast::Journal const journal
Definition: ApplyContext.h:51
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:140
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:189
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:314
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:122
std::uint32_t getFlags() const
Definition: STObject.cpp:507
ApplyView & view()
Definition: Transactor.h:107
ApplyContext & ctx_
Definition: Transactor.h:88
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition: Indexes.cpp:508
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition: Indexes.cpp:494
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:160
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:350
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:152
bool isTesSuccess(TER x)
Definition: TER.h:656
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition: View.cpp:925
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:82
@ 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:151
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:134
@ 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:53
ReadView const & view
Definition: Transactor.h:56
State information when preflighting a tx.
Definition: Transactor.h:32