rippled
Loading...
Searching...
No Matches
MPTokenAuthorize.cpp
1#include <xrpld/app/tx/detail/MPTokenAuthorize.h>
2
3#include <xrpl/ledger/View.h>
4#include <xrpl/protocol/Feature.h>
5#include <xrpl/protocol/TxFlags.h>
6#include <xrpl/protocol/st.h>
7
8namespace xrpl {
9
15
18{
19 if (ctx.tx[sfAccount] == ctx.tx[~sfHolder])
20 return temMALFORMED;
21
22 return tesSUCCESS;
23}
24
25TER
27{
28 auto const accountID = ctx.tx[sfAccount];
29 auto const holderID = ctx.tx[~sfHolder];
30
31 // if non-issuer account submits this tx, then they are trying either:
32 // 1. Unauthorize/delete MPToken
33 // 2. Use/create MPToken
34 //
35 // Note: `accountID` is holder's account
36 // `holderID` is NOT used
37 if (!holderID)
38 {
40 keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], accountID));
41
42 // There is an edge case where all holders have zero balance, issuance
43 // is legally destroyed, then outstanding MPT(s) are deleted afterwards.
44 // Thus, there is no need to check for the existence of the issuance if
45 // the MPT is being deleted with a zero balance. Check for unauthorize
46 // before fetching the MPTIssuance object.
47
48 // if holder wants to delete/unauthorize a mpt
49 if (ctx.tx.getFlags() & tfMPTUnauthorize)
50 {
51 if (!sleMpt)
53
54 if ((*sleMpt)[sfMPTAmount] != 0)
55 {
56 auto const sleMptIssuance = ctx.view.read(
57 keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
58 if (!sleMptIssuance)
59 return tefINTERNAL; // LCOV_EXCL_LINE
60
61 return tecHAS_OBLIGATIONS;
62 }
63
64 if ((*sleMpt)[~sfLockedAmount].value_or(0) != 0)
65 {
66 auto const sleMptIssuance = ctx.view.read(
67 keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
68 if (!sleMptIssuance)
69 return tefINTERNAL; // LCOV_EXCL_LINE
70
71 return tecHAS_OBLIGATIONS;
72 }
73 if (ctx.view.rules().enabled(featureSingleAssetVault) &&
74 sleMpt->isFlag(lsfMPTLocked))
75 return tecNO_PERMISSION;
76
77 return tesSUCCESS;
78 }
79
80 // Now test when the holder wants to hold/create/authorize a new MPT
81 auto const sleMptIssuance =
82 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
83
84 if (!sleMptIssuance)
86
87 if (accountID == (*sleMptIssuance)[sfIssuer])
88 return tecNO_PERMISSION;
89
90 // if holder wants to use and create a mpt
91 if (sleMpt)
92 return tecDUPLICATE;
93
94 return tesSUCCESS;
95 }
96
97 auto const sleHolder = ctx.view.read(keylet::account(*holderID));
98 if (!sleHolder)
99 return tecNO_DST;
100
101 auto const sleMptIssuance =
102 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
103 if (!sleMptIssuance)
104 return tecOBJECT_NOT_FOUND;
105
106 std::uint32_t const mptIssuanceFlags = sleMptIssuance->getFieldU32(sfFlags);
107
108 // If tx is submitted by issuer, they would either try to do the following
109 // for allowlisting:
110 // 1. authorize an account
111 // 2. unauthorize an account
112 //
113 // Note: `accountID` is issuer's account
114 // `holderID` is holder's account
115 if (accountID != (*sleMptIssuance)[sfIssuer])
116 return tecNO_PERMISSION;
117
118 // If tx is submitted by issuer, it only applies for MPT with
119 // lsfMPTRequireAuth set
120 if (!(mptIssuanceFlags & lsfMPTRequireAuth))
121 return tecNO_AUTH;
122
123 // The holder must create the MPT before the issuer can authorize it.
124 if (!ctx.view.exists(
125 keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], *holderID)))
126 return tecOBJECT_NOT_FOUND;
127
128 // Can't unauthorize the pseudo-accounts because they are implicitly
129 // always authorized. No need to amendment gate since Vault and LoanBroker
130 // can only be created if the Vault amendment is enabled.
131 if (isPseudoAccount(ctx.view, *holderID, {&sfVaultID, &sfLoanBrokerID}))
132 return tecNO_PERMISSION;
133
134 return tesSUCCESS;
135}
136
137TER
139 ApplyView& view,
140 MPTID const& mptIssuanceID,
141 AccountID const& account,
142 std::uint32_t const flags)
143{
144 auto const mptokenKey = keylet::mptoken(mptIssuanceID, account);
145
146 auto const ownerNode = view.dirInsert(
147 keylet::ownerDir(account), mptokenKey, describeOwnerDir(account));
148
149 if (!ownerNode)
150 return tecDIR_FULL; // LCOV_EXCL_LINE
151
152 auto mptoken = std::make_shared<SLE>(mptokenKey);
153 (*mptoken)[sfAccount] = account;
154 (*mptoken)[sfMPTokenIssuanceID] = mptIssuanceID;
155 (*mptoken)[sfFlags] = flags;
156 (*mptoken)[sfOwnerNode] = *ownerNode;
157
158 view.insert(mptoken);
159
160 return tesSUCCESS;
161}
162
163TER
165{
166 auto const& tx = ctx_.tx;
167 return authorizeMPToken(
168 ctx_.view(),
170 tx[sfMPTokenIssuanceID],
171 account_,
173 tx.getFlags(),
174 tx[~sfHolder]);
175}
176
177} // namespace xrpl
STTx const & tx
beast::Journal const journal
ApplyView & view()
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:124
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:300
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static TER createMPToken(ApplyView &view, MPTID const &mptIssuanceID, AccountID const &account, std::uint32_t const flags)
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual std::shared_ptr< SLE const > 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:111
std::uint32_t getFlags() const
Definition STObject.cpp:518
AccountID const account_
Definition Transactor.h:128
ApplyView & view()
Definition Transactor.h:144
XRPAmount mPriorBalance
Definition Transactor.h:129
ApplyContext & ctx_
Definition Transactor.h:124
T is_same_v
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:356
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:508
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:522
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:166
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ tefINTERNAL
Definition TER.h:154
TER authorizeMPToken(ApplyView &view, XRPAmount const &priorBalance, MPTID const &mptIssuanceID, AccountID const &account, beast::Journal journal, std::uint32_t flags=0, std::optional< AccountID > holderID=std::nullopt)
Definition View.cpp:1515
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
Definition View.cpp:1226
constexpr std::uint32_t const tfMPTokenAuthorizeMask
Definition TxFlags.h:154
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition View.cpp:1152
constexpr std::uint32_t const tfMPTUnauthorize
Definition TxFlags.h:153
@ temMALFORMED
Definition TER.h:68
@ tecDIR_FULL
Definition TER.h:269
@ tecOBJECT_NOT_FOUND
Definition TER.h:308
@ tecNO_AUTH
Definition TER.h:282
@ tecNO_PERMISSION
Definition TER.h:287
@ tecDUPLICATE
Definition TER.h:297
@ tecHAS_OBLIGATIONS
Definition TER.h:299
@ tecNO_DST
Definition TER.h:272
@ lsfMPTRequireAuth
@ lsfMPTLocked
@ tesSUCCESS
Definition TER.h:226
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
State information when preflighting a tx.
Definition Transactor.h:16