xrpld
Loading...
Searching...
No Matches
DepositPreauth.cpp
1#include <xrpl/tx/transactors/payment/DepositPreauth.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Slice.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/core/ServiceRegistry.h>
7#include <xrpl/ledger/helpers/AccountRootHelpers.h>
8#include <xrpl/ledger/helpers/CredentialHelpers.h>
9#include <xrpl/ledger/helpers/DirectoryHelpers.h>
10#include <xrpl/protocol/AccountID.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/Keylet.h>
14#include <xrpl/protocol/Protocol.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STAmount.h>
17#include <xrpl/protocol/STArray.h>
18#include <xrpl/protocol/STLedgerEntry.h>
19#include <xrpl/protocol/STTx.h>
20#include <xrpl/protocol/TER.h>
21#include <xrpl/protocol/XRPAmount.h>
22#include <xrpl/tx/Transactor.h>
23
24#include <cstdint>
25#include <memory>
26#include <optional>
27#include <set>
28#include <utility>
29
30namespace xrpl {
31
32bool
34{
35 bool const authArrPresent = ctx.tx.isFieldPresent(sfAuthorizeCredentials);
36 bool const unauthArrPresent = ctx.tx.isFieldPresent(sfUnauthorizeCredentials);
37 bool const authCredPresent = authArrPresent || unauthArrPresent;
38
39 return !authCredPresent || ctx.rules.enabled(featureCredentials);
40}
41
44{
45 bool const authArrPresent = ctx.tx.isFieldPresent(sfAuthorizeCredentials);
46 bool const unauthArrPresent = ctx.tx.isFieldPresent(sfUnauthorizeCredentials);
47 int const authCredPresent =
48 static_cast<int>(authArrPresent) + static_cast<int>(unauthArrPresent);
49
50 auto const optAuth = ctx.tx[~sfAuthorize];
51 auto const optUnauth = ctx.tx[~sfUnauthorize];
52 int const authPresent =
53 static_cast<int>(optAuth.has_value()) + static_cast<int>(optUnauth.has_value());
54
55 if (authPresent + authCredPresent != 1)
56 {
57 // There can only be 1 field out of 4 or the transaction is malformed.
58 JLOG(ctx.j.trace()) << "Malformed transaction: "
59 "Invalid Authorize and Unauthorize field combination.";
60 return temMALFORMED;
61 }
62
63 if (authPresent != 0)
64 {
65 // Make sure that the passed account is valid.
66 // NOLINTNEXTLINE(bugprone-unchecked-optional-access) authPresent != 0 guarantees one is set
67 AccountID const& target(optAuth ? *optAuth : *optUnauth);
68 if (!target)
69 {
70 JLOG(ctx.j.trace()) << "Malformed transaction: Authorized or Unauthorized "
71 "field zeroed.";
73 }
74
75 // An account may not preauthorize itself.
76 if (optAuth && (target == ctx.tx[sfAccount]))
77 {
78 JLOG(ctx.j.trace()) << "Malformed transaction: Attempting to DepositPreauth self.";
80 }
81 }
82 else
83 {
84 if (auto err = credentials::checkArray(
85 ctx.tx.getFieldArray(
86 authArrPresent ? sfAuthorizeCredentials : sfUnauthorizeCredentials),
88 ctx.j);
89 !isTesSuccess(err))
90 return err;
91 }
92
93 return tesSUCCESS;
94}
95
96TER
98{
99 AccountID const account(ctx.tx[sfAccount]);
100
101 // Determine which operation we're performing: authorizing or unauthorizing.
102 if (ctx.tx.isFieldPresent(sfAuthorize))
103 {
104 // Verify that the Authorize account is present in the ledger.
105 AccountID const auth{ctx.tx[sfAuthorize]};
106 if (!ctx.view.exists(keylet::account(auth)))
107 return tecNO_TARGET;
108
109 // Verify that the Preauth entry they asked to add is not already
110 // in the ledger.
111 if (ctx.view.exists(keylet::depositPreauth(account, auth)))
112 return tecDUPLICATE;
113 }
114 else if (ctx.tx.isFieldPresent(sfUnauthorize))
115 {
116 // Verify that the Preauth entry they asked to remove is in the ledger.
117 if (!ctx.view.exists(keylet::depositPreauth(account, ctx.tx[sfUnauthorize])))
118 return tecNO_ENTRY;
119 }
120 else if (ctx.tx.isFieldPresent(sfAuthorizeCredentials))
121 {
122 STArray const& authCred(ctx.tx.getFieldArray(sfAuthorizeCredentials));
124 for (auto const& o : authCred)
125 {
126 auto const& issuer = o[sfIssuer];
127 if (!ctx.view.exists(keylet::account(issuer)))
128 return tecNO_ISSUER;
129 auto [it, ins] = sorted.emplace(issuer, o[sfCredentialType]);
130 if (!ins)
131 return tefINTERNAL; // LCOV_EXCL_LINE
132 }
133
134 // Verify that the Preauth entry they asked to add is not already
135 // in the ledger.
136 if (ctx.view.exists(keylet::depositPreauth(account, sorted)))
137 return tecDUPLICATE;
138 }
139 else if (ctx.tx.isFieldPresent(sfUnauthorizeCredentials))
140 {
141 // Verify that the Preauth entry is in the ledger.
142 if (!ctx.view.exists(
144 account,
145 credentials::makeSorted(ctx.tx.getFieldArray(sfUnauthorizeCredentials)))))
146 return tecNO_ENTRY;
147 }
148 return tesSUCCESS;
149}
150
151TER
153{
154 if (ctx_.tx.isFieldPresent(sfAuthorize))
155 {
156 auto const sleOwner = view().peek(keylet::account(accountID_));
157 if (!sleOwner)
158 return {tefINTERNAL};
159
160 // A preauth counts against the reserve of the issuing account, but we
161 // check the starting balance because we want to allow dipping into the
162 // reserve to pay fees.
163 {
164 STAmount const reserve{
165 view().fees().accountReserve(sleOwner->getFieldU32(sfOwnerCount) + 1)};
166
167 if (preFeeBalance_ < reserve)
169 }
170
171 // Preclaim already verified that the Preauth entry does not yet exist.
172 // Create and populate the Preauth entry.
173 AccountID const auth{ctx_.tx[sfAuthorize]};
174 Keylet const preauthKeylet = keylet::depositPreauth(accountID_, auth);
175 auto slePreauth = std::make_shared<SLE>(preauthKeylet);
176
177 slePreauth->setAccountID(sfAccount, accountID_);
178 slePreauth->setAccountID(sfAuthorize, auth);
179 view().insert(slePreauth);
180
181 auto const page = view().dirInsert(
183
184 JLOG(j_.trace()) << "Adding DepositPreauth to owner directory "
185 << to_string(preauthKeylet.key) << ": " << (page ? "success" : "failure");
186
187 if (!page)
188 return tecDIR_FULL; // LCOV_EXCL_LINE
189
190 slePreauth->setFieldU64(sfOwnerNode, *page);
191
192 // If we succeeded, the new entry counts against the creator's reserve.
193 adjustOwnerCount(view(), sleOwner, 1, j_);
194 }
195 else if (ctx_.tx.isFieldPresent(sfUnauthorize))
196 {
197 auto const preauth = keylet::depositPreauth(accountID_, ctx_.tx[sfUnauthorize]);
198
199 return DepositPreauth::removeFromLedger(view(), preauth.key, j_);
200 }
201 else if (ctx_.tx.isFieldPresent(sfAuthorizeCredentials))
202 {
203 auto const sleOwner = view().peek(keylet::account(accountID_));
204 if (!sleOwner)
205 return tefINTERNAL; // LCOV_EXCL_LINE
206
207 // A preauth counts against the reserve of the issuing account, but we
208 // check the starting balance because we want to allow dipping into the
209 // reserve to pay fees.
210 {
211 STAmount const reserve{
212 view().fees().accountReserve(sleOwner->getFieldU32(sfOwnerCount) + 1)};
213
214 if (preFeeBalance_ < reserve)
216 }
217
218 // Preclaim already verified that the Preauth entry does not yet exist.
219 // Create and populate the Preauth entry.
220
221 auto const sortedTX =
222 credentials::makeSorted(ctx_.tx.getFieldArray(sfAuthorizeCredentials));
223 STArray sortedLE(sfAuthorizeCredentials, sortedTX.size());
224 for (auto const& p : sortedTX)
225 {
226 auto cred = STObject::makeInnerObject(sfCredential);
227 cred.setAccountID(sfIssuer, p.first);
228 cred.setFieldVL(sfCredentialType, p.second);
229 sortedLE.pushBack(std::move(cred));
230 }
231
232 Keylet const preauthKey = keylet::depositPreauth(accountID_, sortedTX);
233 auto slePreauth = std::make_shared<SLE>(preauthKey);
234 if (!slePreauth)
235 return tefINTERNAL; // LCOV_EXCL_LINE
236
237 slePreauth->setAccountID(sfAccount, accountID_);
238 slePreauth->peekFieldArray(sfAuthorizeCredentials) = std::move(sortedLE);
239
240 view().insert(slePreauth);
241
242 auto const page = view().dirInsert(
244
245 JLOG(j_.trace()) << "Adding DepositPreauth to owner directory " << to_string(preauthKey.key)
246 << ": " << (page ? "success" : "failure");
247
248 if (!page)
249 return tecDIR_FULL; // LCOV_EXCL_LINE
250
251 slePreauth->setFieldU64(sfOwnerNode, *page);
252
253 // If we succeeded, the new entry counts against the creator's reserve.
254 adjustOwnerCount(view(), sleOwner, 1, j_);
255 }
256 else if (ctx_.tx.isFieldPresent(sfUnauthorizeCredentials))
257 {
258 auto const preauthKey = keylet::depositPreauth(
259 accountID_, credentials::makeSorted(ctx_.tx.getFieldArray(sfUnauthorizeCredentials)));
260 return DepositPreauth::removeFromLedger(view(), preauthKey.key, j_);
261 }
262
263 return tesSUCCESS;
264}
265
266TER
268{
269 // Existence already checked in preclaim and AccountDelete
270 auto const slePreauth{view.peek(keylet::depositPreauth(preauthIndex))};
271 if (!slePreauth)
272 {
273 JLOG(j.warn()) << "Selected DepositPreauth does not exist.";
274 return tecNO_ENTRY;
275 }
276
277 AccountID const account{(*slePreauth)[sfAccount]};
278 std::uint64_t const page{(*slePreauth)[sfOwnerNode]};
279 if (!view.dirRemove(keylet::ownerDir(account), page, preauthIndex, false))
280 {
281 // LCOV_EXCL_START
282 JLOG(j.fatal()) << "Unable to delete DepositPreauth from owner.";
283 return tefBAD_LEDGER;
284 // LCOV_EXCL_STOP
285 }
286
287 // If we succeeded, update the DepositPreauth owner's reserve.
288 auto const sleOwner = view.peek(keylet::account(account));
289 if (!sleOwner)
290 return tefINTERNAL; // LCOV_EXCL_LINE
291
292 adjustOwnerCount(view, sleOwner, -1, j);
293
294 // Remove DepositPreauth from ledger.
295 view.erase(slePreauth);
296
297 return tesSUCCESS;
298}
299
300void
302{
303 // No transaction-specific invariants yet (future work).
304}
305
306bool
308 STTx const&,
309 TER,
310 XRPAmount,
311 ReadView const&,
312 beast::Journal const&)
313{
314 // No transaction-specific invariants yet (future work).
315 return true;
316}
317
318} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Stream fatal() const
Definition Journal.h:321
Stream trace() const
Severity stream access functions.
Definition Journal.h:291
Stream warn() const
Definition Journal.h:309
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:118
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void insert(SLE::ref sle)=0
Insert a new state SLE.
std::optional< std::uint64_t > dirInsert(Keylet const &directory, uint256 const &key, std::function< void(SLE::ref)> const &describe)
Insert an entry to a directory.
Definition ApplyView.h:340
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.
static TER removeFromLedger(ApplyView &view, uint256 const &delIndex, beast::Journal j)
static NotTEC preflight(PreflightContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static TER preclaim(PreclaimContext const &ctx)
A view into a ledger.
Definition ReadView.h:31
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:171
void pushBack(STObject const &object)
Definition STArray.h:204
std::shared_ptr< STLedgerEntry const > const & const_ref
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:678
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:454
static STObject makeInnerObject(SField const &name)
Definition STObject.cpp:74
beast::Journal const j_
Definition Transactor.h:118
ApplyView & view()
Definition Transactor.h:136
AccountID const accountID_
Definition Transactor.h:120
XRPAmount preFeeBalance_
Definition Transactor.h:121
ApplyContext & ctx_
Definition Transactor.h:116
T emplace(T... args)
T make_shared(T... args)
std::set< std::pair< AccountID, Slice > > makeSorted(STArray const &credentials)
NotTEC checkArray(STArray const &credentials, unsigned maxSize, beast::Journal j)
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Definition Indexes.cpp:328
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:357
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ tefBAD_LEDGER
Definition TER.h:160
@ tefINTERNAL
Definition TER.h:163
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
constexpr std::size_t kMaxCredentialsArraySize
The maximum number of credentials can be passed in array.
Definition Protocol.h:228
void adjustOwnerCount(ApplyView &view, SLE::ref sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
@ temCANNOT_PREAUTH_SELF
Definition TER.h:106
@ temMALFORMED
Definition TER.h:73
@ temINVALID_ACCOUNT_ID
Definition TER.h:105
bool isTesSuccess(TER x) noexcept
Definition TER.h:663
TERSubset< CanCvtToTER > TER
Definition TER.h:634
@ tecDIR_FULL
Definition TER.h:285
@ tecNO_ENTRY
Definition TER.h:304
@ tecNO_TARGET
Definition TER.h:302
@ tecINSUFFICIENT_RESERVE
Definition TER.h:305
@ tecNO_ISSUER
Definition TER.h:297
@ tecDUPLICATE
Definition TER.h:313
BaseUInt< 256 > uint256
Definition base_uint.h:562
@ tesSUCCESS
Definition TER.h:240
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
uint256 key
Definition Keylet.h:20
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:18
beast::Journal const j
Definition Transactor.h:25