xrpld
Loading...
Searching...
No Matches
VaultSet.cpp
1#include <xrpl/tx/transactors/vault/VaultSet.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/ledger/helpers/VaultHelpers.h>
7#include <xrpl/protocol/Feature.h>
8#include <xrpl/protocol/Indexes.h>
9#include <xrpl/protocol/LedgerFormats.h>
10#include <xrpl/protocol/Protocol.h>
11#include <xrpl/protocol/SField.h>
12#include <xrpl/protocol/STAmount.h>
13#include <xrpl/protocol/STLedgerEntry.h>
14#include <xrpl/protocol/STNumber.h> // IWYU pragma: keep
15#include <xrpl/protocol/STTakesAsset.h>
16#include <xrpl/protocol/STTx.h>
17#include <xrpl/protocol/TER.h>
18#include <xrpl/protocol/XRPAmount.h>
19#include <xrpl/tx/Transactor.h>
20
21namespace xrpl {
22
23bool
25{
26 return !ctx.tx.isFieldPresent(sfDomainID) || ctx.rules.enabled(featurePermissionedDomains);
27}
28
31{
32 if (ctx.tx[sfVaultID] == beast::kZero)
33 {
34 JLOG(ctx.j.debug()) << "VaultSet: zero/empty vault ID.";
35 return temMALFORMED;
36 }
37
38 if (auto const data = ctx.tx[~sfData])
39 {
40 if (data->empty() || data->length() > kMaxDataPayloadLength)
41 {
42 JLOG(ctx.j.debug()) << "VaultSet: invalid data payload size.";
43 return temMALFORMED;
44 }
45 }
46
47 if (auto const assetMax = ctx.tx[~sfAssetsMaximum])
48 {
49 if (*assetMax < beast::kZero)
50 {
51 JLOG(ctx.j.debug()) << "VaultSet: invalid max assets.";
52 return temMALFORMED;
53 }
54 }
55
56 if (!ctx.tx.isFieldPresent(sfDomainID) && !ctx.tx.isFieldPresent(sfAssetsMaximum) &&
57 !ctx.tx.isFieldPresent(sfData))
58 {
59 JLOG(ctx.j.debug()) << "VaultSet: nothing is being updated.";
60 return temMALFORMED;
61 }
62
63 return tesSUCCESS;
64}
65
66TER
68{
69 auto const vault = ctx.view.read(keylet::vault(ctx.tx[sfVaultID]));
70 if (!vault)
71 return tecNO_ENTRY;
72
73 // Assert that submitter is the Owner.
74 if (ctx.tx[sfAccount] != vault->at(sfOwner))
75 {
76 JLOG(ctx.j.debug()) << "VaultSet: account is not an owner.";
77 return tecNO_PERMISSION;
78 }
79
80 // An AssetsMaximum out of range for the vault asset, e.g. XRP above
81 // STAmount::kMaxNativeN, would throw when rounded to the asset.
82 if (auto const assetMax = ctx.tx[~sfAssetsMaximum]; assetMax &&
83 ctx.view.rules().enabled(fixCleanup3_5_0) &&
84 !isRepresentable(vault->at(sfAsset), *assetMax))
85 {
86 JLOG(ctx.j.debug()) << "VaultSet: max assets out of range for the asset.";
87 return tecPRECISION_LOSS;
88 }
89
90 auto const mptIssuanceID = (*vault)[sfShareMPTID];
91 auto const sleIssuance = ctx.view.read(keylet::mptokenIssuance(mptIssuanceID));
92 if (!sleIssuance)
93 {
94 // LCOV_EXCL_START
95 JLOG(ctx.j.error()) << "VaultSet: missing issuance of vault shares.";
96 return tefINTERNAL;
97 // LCOV_EXCL_STOP
98 }
99
100 if (auto const domain = ctx.tx[~sfDomainID])
101 {
102 // We can only set domain if private flag was originally set
103 if (!vault->isFlag(lsfVaultPrivate))
104 {
105 JLOG(ctx.j.debug()) << "VaultSet: vault is not private";
106 return tecNO_PERMISSION;
107 }
108
109 if (*domain != beast::kZero)
110 {
111 auto const sleDomain = ctx.view.read(keylet::permissionedDomain(*domain));
112 if (!sleDomain)
113 return tecOBJECT_NOT_FOUND;
114 }
115
116 // Sanity check only, this should be enforced by VaultCreate
117 if (!sleIssuance->isFlag(lsfMPTRequireAuth))
118 {
119 // LCOV_EXCL_START
120 JLOG(ctx.j.error()) << "VaultSet: issuance of vault shares is not private.";
121 return tefINTERNAL;
122 // LCOV_EXCL_STOP
123 }
124 }
125
126 // With featureLendingProtocolV1_2, AssetsMaximum must be exactly
127 // representable at the Vault's scale (the base grid on FixedPrecision
128 // Vaults, the live scale on existing Legacy/CashBasis Vaults), otherwise
129 // associateAsset would silently round the cap the owner asked for.
130 if (auto const assetMax = ctx.tx[~sfAssetsMaximum];
131 assetMax && ctx.view.rules().enabled(featureLendingProtocolV1_2))
132 {
133 if (auto const ter = checkAssetsMaximum(vault, *assetMax); !isTesSuccess(ter))
134 {
135 JLOG(ctx.j.debug()) << "VaultSet: AssetsMaximum " << *assetMax
136 << " is not representable at the Vault scale.";
137 return ter;
138 }
139 }
140
141 return tesSUCCESS;
142}
143
144TER
146{
147 // All return codes in `doApply` must be `tec`, `ter`, or `tes`.
148 // As we move checks into `preflight` and `preclaim`,
149 // we can consider downgrading them to `tef` or `tem`.
150
151 auto const& tx = ctx_.tx;
152
153 // Update existing object.
154 auto const vault = view().peek(keylet::vault(tx[sfVaultID]));
155 if (!vault)
156 return tefINTERNAL; // LCOV_EXCL_LINE
157
158 auto const vaultAsset = vault->at(sfAsset);
159
160 auto const mptIssuanceID = (*vault)[sfShareMPTID];
161 auto const sleIssuance = view().peek(keylet::mptokenIssuance(mptIssuanceID));
162 if (!sleIssuance)
163 {
164 // LCOV_EXCL_START
165 JLOG(j_.error()) << "VaultSet: missing issuance of vault shares.";
166 return tefINTERNAL;
167 // LCOV_EXCL_STOP
168 }
169
170 // Update mutable flags and fields if given.
171 if (tx.isFieldPresent(sfData))
172 vault->at(sfData) = tx[sfData];
173 if (tx.isFieldPresent(sfAssetsMaximum))
174 {
175 if (tx[sfAssetsMaximum] != 0 && tx[sfAssetsMaximum] < getAssetsTotal(vault))
176 return tecLIMIT_EXCEEDED;
177 vault->at(sfAssetsMaximum) = tx[sfAssetsMaximum];
178 }
179
180 if (auto const domainId = tx[~sfDomainID]; domainId)
181 {
182 if (*domainId != beast::kZero)
183 {
184 // In VaultSet::preclaim we enforce that lsfVaultPrivate must have
185 // been set in the vault. We currently do not support making such a
186 // vault public (i.e. removal of lsfVaultPrivate flag). The
187 // sfDomainID flag must be set in the MPTokenIssuance object and can
188 // be freely updated.
189 sleIssuance->setFieldH256(sfDomainID, *domainId);
190 }
191 else if (sleIssuance->isFieldPresent(sfDomainID))
192 {
193 sleIssuance->makeFieldAbsent(sfDomainID);
194 }
195 view().update(sleIssuance);
196 }
197
198 // Note, we must update Vault object even if only DomainID is being updated
199 // in Issuance object. Otherwise it's really difficult for Vault invariants
200 // to verify the operation.
201 view().update(vault);
202
203 associateAsset(*vault, vaultAsset);
204
205 return tesSUCCESS;
206}
207
208void
210{
211 // No transaction-specific invariants yet (future work).
212}
213
214bool
216{
217 // No transaction-specific invariants yet (future work).
218 return true;
219}
220
221} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream error() const
Definition Journal.h:362
Stream debug() const
Definition Journal.h:344
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void update(SLE::Ref sle)=0
Indicate changes to a peeked SLE.
A view into a ledger.
Definition ReadView.h:41
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual SLE::const_pointer 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:182
std::shared_ptr< STLedgerEntry const > const & ConstRef
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
beast::Journal const j_
Definition Transactor.h:164
ApplyView & view()
Definition Transactor.h:184
ApplyContext & ctx_
Definition Transactor.h:162
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
Definition VaultSet.cpp:215
static bool checkExtraFeatures(PreflightContext const &ctx)
Definition VaultSet.cpp:24
static TER preclaim(PreclaimContext const &ctx)
Definition VaultSet.cpp:67
TER doApply() override
Definition VaultSet.cpp:145
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
Definition VaultSet.cpp:209
static NotTEC preflight(PreflightContext const &ctx)
Definition VaultSet.cpp:30
constexpr Zero kZero
Definition Zero.h:30
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:565
Keylet permissionedDomain(AccountID const &account, SeqProxy const &seq) noexcept
Definition Indexes.cpp:583
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:541
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ tefINTERNAL
Definition TER.h:167
TER checkAssetsMaximum(SLE::ConstRef vault, Number const &amount)
Checks that a requested AssetsMaximum is exactly representable on the Vault's base grid,...
constexpr std::size_t kMaxDataPayloadLength
The maximum length of Data payload.
Definition Protocol.h:303
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
bool isRepresentable(Asset const &asset, Number const &value)
Check whether an arbitrary precision Number can be converted to an STAmount of a given Asset without ...
@ temMALFORMED
Definition TER.h:75
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecNO_ENTRY
Definition TER.h:317
@ tecOBJECT_NOT_FOUND
Definition TER.h:337
@ tecPRECISION_LOSS
Definition TER.h:374
@ tecLIMIT_EXCEEDED
Definition TER.h:372
@ tecNO_PERMISSION
Definition TER.h:316
Number getAssetsTotal(SLE::ConstRef vault)
Return the Vault's current assets total.
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
@ tesSUCCESS
Definition TER.h:253
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:92
ReadView const & view
Definition Transactor.h:95
beast::Journal const j
Definition Transactor.h:100
State information when preflighting a tx.
Definition Transactor.h:39
beast::Journal const j
Definition Transactor.h:46