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 {
39 std::shared_ptr<SLE const> sleMpt = ctx.view.read(keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], accountID));
40
41 // There is an edge case where all holders have zero balance, issuance
42 // is legally destroyed, then outstanding MPT(s) are deleted afterwards.
43 // Thus, there is no need to check for the existence of the issuance if
44 // the MPT is being deleted with a zero balance. Check for unauthorize
45 // before fetching the MPTIssuance object.
46
47 // if holder wants to delete/unauthorize a mpt
48 if (ctx.tx.getFlags() & tfMPTUnauthorize)
49 {
50 if (!sleMpt)
52
53 if ((*sleMpt)[sfMPTAmount] != 0)
54 {
55 auto const sleMptIssuance = ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
56 if (!sleMptIssuance)
57 return tefINTERNAL; // LCOV_EXCL_LINE
58
59 return tecHAS_OBLIGATIONS;
60 }
61
62 if ((*sleMpt)[~sfLockedAmount].value_or(0) != 0)
63 {
64 auto const sleMptIssuance = ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
65 if (!sleMptIssuance)
66 return tefINTERNAL; // LCOV_EXCL_LINE
67
68 return tecHAS_OBLIGATIONS;
69 }
70 if (ctx.view.rules().enabled(featureSingleAssetVault) && sleMpt->isFlag(lsfMPTLocked))
71 return tecNO_PERMISSION;
72
73 return tesSUCCESS;
74 }
75
76 // Now test when the holder wants to hold/create/authorize a new MPT
77 auto const sleMptIssuance = ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
78
79 if (!sleMptIssuance)
81
82 if (accountID == (*sleMptIssuance)[sfIssuer])
83 return tecNO_PERMISSION;
84
85 // if holder wants to use and create a mpt
86 if (sleMpt)
87 return tecDUPLICATE;
88
89 return tesSUCCESS;
90 }
91
92 auto const sleHolder = ctx.view.read(keylet::account(*holderID));
93 if (!sleHolder)
94 return tecNO_DST;
95
96 auto const sleMptIssuance = ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
97 if (!sleMptIssuance)
99
100 std::uint32_t const mptIssuanceFlags = sleMptIssuance->getFieldU32(sfFlags);
101
102 // If tx is submitted by issuer, they would either try to do the following
103 // for allowlisting:
104 // 1. authorize an account
105 // 2. unauthorize an account
106 //
107 // Note: `accountID` is issuer's account
108 // `holderID` is holder's account
109 if (accountID != (*sleMptIssuance)[sfIssuer])
110 return tecNO_PERMISSION;
111
112 // If tx is submitted by issuer, it only applies for MPT with
113 // lsfMPTRequireAuth set
114 if (!(mptIssuanceFlags & lsfMPTRequireAuth))
115 return tecNO_AUTH;
116
117 // The holder must create the MPT before the issuer can authorize it.
118 if (!ctx.view.exists(keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], *holderID)))
119 return tecOBJECT_NOT_FOUND;
120
121 // Can't unauthorize the pseudo-accounts because they are implicitly
122 // always authorized. No need to amendment gate since Vault and LoanBroker
123 // can only be created if the Vault amendment is enabled.
124 if (isPseudoAccount(ctx.view, *holderID, {&sfVaultID, &sfLoanBrokerID}))
125 return tecNO_PERMISSION;
126
127 return tesSUCCESS;
128}
129
130TER
132 ApplyView& view,
133 MPTID const& mptIssuanceID,
134 AccountID const& account,
135 std::uint32_t const flags)
136{
137 auto const mptokenKey = keylet::mptoken(mptIssuanceID, account);
138
139 auto const ownerNode = view.dirInsert(keylet::ownerDir(account), mptokenKey, describeOwnerDir(account));
140
141 if (!ownerNode)
142 return tecDIR_FULL; // LCOV_EXCL_LINE
143
144 auto mptoken = std::make_shared<SLE>(mptokenKey);
145 (*mptoken)[sfAccount] = account;
146 (*mptoken)[sfMPTokenIssuanceID] = mptIssuanceID;
147 (*mptoken)[sfFlags] = flags;
148 (*mptoken)[sfOwnerNode] = *ownerNode;
149
150 view.insert(mptoken);
151
152 return tesSUCCESS;
153}
154
155TER
157{
158 auto const& tx = ctx_.tx;
159 return authorizeMPToken(
160 ctx_.view(), mPriorBalance, tx[sfMPTokenIssuanceID], account_, ctx_.journal, tx.getFlags(), tx[~sfHolder]);
161}
162
163} // namespace xrpl
STTx const & tx
beast::Journal const journal
ApplyView & view()
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:114
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:283
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:118
std::uint32_t getFlags() const
Definition STObject.cpp:492
AccountID const account_
Definition Transactor.h:112
ApplyView & view()
Definition Transactor.h:128
XRPAmount mPriorBalance
Definition Transactor.h:113
ApplyContext & ctx_
Definition Transactor.h:108
T is_same_v
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:325
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:462
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:474
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:160
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ tefINTERNAL
Definition TER.h:153
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:1326
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
Definition View.cpp:1020
constexpr std::uint32_t const tfMPTokenAuthorizeMask
Definition TxFlags.h:153
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition View.cpp:955
constexpr std::uint32_t const tfMPTUnauthorize
Definition TxFlags.h:152
@ temMALFORMED
Definition TER.h:67
@ tecDIR_FULL
Definition TER.h:268
@ tecOBJECT_NOT_FOUND
Definition TER.h:307
@ tecNO_AUTH
Definition TER.h:281
@ tecNO_PERMISSION
Definition TER.h:286
@ tecDUPLICATE
Definition TER.h:296
@ tecHAS_OBLIGATIONS
Definition TER.h:298
@ tecNO_DST
Definition TER.h:271
@ lsfMPTRequireAuth
@ lsfMPTLocked
@ tesSUCCESS
Definition TER.h:225
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:15