rippled
Loading...
Searching...
No Matches
MPTokenIssuanceSet.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/misc/DelegateUtils.h>
21#include <xrpld/app/tx/detail/MPTokenIssuanceSet.h>
22
23#include <xrpl/protocol/Feature.h>
24#include <xrpl/protocol/TxFlags.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
37 auto const txFlags = ctx.tx.getFlags();
38
39 // check flags
40 if (txFlags & tfMPTokenIssuanceSetMask)
41 return temINVALID_FLAG;
42 // fails if both flags are set
43 else if ((txFlags & tfMPTLock) && (txFlags & tfMPTUnlock))
44 return temINVALID_FLAG;
45
46 auto const accountID = ctx.tx[sfAccount];
47 auto const holderID = ctx.tx[~sfHolder];
48 if (holderID && accountID == holderID)
49 return temMALFORMED;
50
51 return preflight2(ctx);
52}
53
54TER
56{
57 auto const delegate = tx[~sfDelegate];
58 if (!delegate)
59 return tesSUCCESS;
60
61 auto const delegateKey = keylet::delegate(tx[sfAccount], *delegate);
62 auto const sle = view.read(delegateKey);
63
64 if (!sle)
65 return tecNO_PERMISSION;
66
67 if (checkTxPermission(sle, tx) == tesSUCCESS)
68 return tesSUCCESS;
69
70 auto const txFlags = tx.getFlags();
71
72 // this is added in case more flags will be added for MPTokenIssuanceSet
73 // in the future. Currently unreachable.
75 return tecNO_PERMISSION; // LCOV_EXCL_LINE
76
78 loadGranularPermission(sle, ttMPTOKEN_ISSUANCE_SET, granularPermissions);
79
80 if (txFlags & tfMPTLock &&
81 !granularPermissions.contains(MPTokenIssuanceLock))
82 return tecNO_PERMISSION;
83
84 if (txFlags & tfMPTUnlock &&
85 !granularPermissions.contains(MPTokenIssuanceUnlock))
86 return tecNO_PERMISSION;
87
88 return tesSUCCESS;
89}
90
91TER
93{
94 // ensure that issuance exists
95 auto const sleMptIssuance =
96 ctx.view.read(keylet::mptIssuance(ctx.tx[sfMPTokenIssuanceID]));
97 if (!sleMptIssuance)
99
100 // if the mpt has disabled locking
101 if (!((*sleMptIssuance)[sfFlags] & lsfMPTCanLock))
102 return tecNO_PERMISSION;
103
104 // ensure it is issued by the tx submitter
105 if ((*sleMptIssuance)[sfIssuer] != ctx.tx[sfAccount])
106 return tecNO_PERMISSION;
107
108 if (auto const holderID = ctx.tx[~sfHolder])
109 {
110 // make sure holder account exists
111 if (!ctx.view.exists(keylet::account(*holderID)))
112 return tecNO_DST;
113
114 // the mptoken must exist
115 if (!ctx.view.exists(
116 keylet::mptoken(ctx.tx[sfMPTokenIssuanceID], *holderID)))
117 return tecOBJECT_NOT_FOUND;
118 }
119
120 return tesSUCCESS;
121}
122
123TER
125{
126 auto const mptIssuanceID = ctx_.tx[sfMPTokenIssuanceID];
127 auto const txFlags = ctx_.tx.getFlags();
128 auto const holderID = ctx_.tx[~sfHolder];
130
131 if (holderID)
132 sle = view().peek(keylet::mptoken(mptIssuanceID, *holderID));
133 else
134 sle = view().peek(keylet::mptIssuance(mptIssuanceID));
135
136 if (!sle)
137 return tecINTERNAL;
138
139 std::uint32_t const flagsIn = sle->getFieldU32(sfFlags);
140 std::uint32_t flagsOut = flagsIn;
141
142 if (txFlags & tfMPTLock)
143 flagsOut |= lsfMPTLocked;
144 else if (txFlags & tfMPTUnlock)
145 flagsOut &= ~lsfMPTLocked;
146
147 if (flagsIn != flagsOut)
148 sle->setFieldU32(sfFlags, flagsOut);
149
150 view().update(sle);
151
152 return tesSUCCESS;
153}
154
155} // namespace ripple
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static TER checkPermission(ReadView const &view, STTx const &tx)
A view into a ledger.
Definition: ReadView.h:52
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
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
T contains(T... args)
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition: Indexes.cpp:532
Keylet delegate(AccountID const &account, AccountID const &authorizedAccount) noexcept
A keylet for Delegate object.
Definition: Indexes.cpp:457
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
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
constexpr std::uint32_t const tfMPTokenIssuanceSetMask
Definition: TxFlags.h:164
constexpr std::uint32_t const tfMPTokenIssuanceSetPermissionMask
Definition: TxFlags.h:166
constexpr std::uint32_t const tfMPTUnlock
Definition: TxFlags.h:163
@ lsfMPTCanLock
@ lsfMPTLocked
bool isTesSuccess(TER x)
Definition: TER.h:656
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:83
void loadGranularPermission(std::shared_ptr< SLE const > const &delegate, TxType const &type, std::unordered_set< GranularPermissionType > &granularPermissions)
Load the granular permissions granted to the delegate account for the specified transaction type.
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:144
TER checkTxPermission(std::shared_ptr< SLE const > const &delegate, STTx const &tx)
Check if the delegate account has permission to execute the transaction.
@ tecNO_DST
Definition: TER.h:277
@ tecOBJECT_NOT_FOUND
Definition: TER.h:313
@ tecINTERNAL
Definition: TER.h:297
@ tecNO_PERMISSION
Definition: TER.h:292
constexpr std::uint32_t const tfMPTLock
Definition: TxFlags.h:162
@ 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
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