rippled
Loading...
Searching...
No Matches
DepositPreauth.cpp
1#include <xrpld/app/tx/detail/DepositPreauth.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/ledger/CredentialHelpers.h>
5#include <xrpl/ledger/View.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/Indexes.h>
8#include <xrpl/protocol/TxFlags.h>
9
10#include <optional>
11
12namespace xrpl {
13
14bool
16{
17 bool const authArrPresent = ctx.tx.isFieldPresent(sfAuthorizeCredentials);
18 bool const unauthArrPresent = ctx.tx.isFieldPresent(sfUnauthorizeCredentials);
19 bool const authCredPresent = authArrPresent || unauthArrPresent;
20
21 if (authCredPresent && !ctx.rules.enabled(featureCredentials))
22 return false;
23
24 return true;
25}
26
29{
30 bool const authArrPresent = ctx.tx.isFieldPresent(sfAuthorizeCredentials);
31 bool const unauthArrPresent = ctx.tx.isFieldPresent(sfUnauthorizeCredentials);
32 int const authCredPresent = static_cast<int>(authArrPresent) + static_cast<int>(unauthArrPresent);
33
34 auto const optAuth = ctx.tx[~sfAuthorize];
35 auto const optUnauth = ctx.tx[~sfUnauthorize];
36 int const authPresent = static_cast<int>(optAuth.has_value()) + static_cast<int>(optUnauth.has_value());
37
38 if (authPresent + authCredPresent != 1)
39 {
40 // There can only be 1 field out of 4 or the transaction is malformed.
41 JLOG(ctx.j.trace()) << "Malformed transaction: "
42 "Invalid Authorize and Unauthorize field combination.";
43 return temMALFORMED;
44 }
45
46 if (authPresent)
47 {
48 // Make sure that the passed account is valid.
49 AccountID const& target(optAuth ? *optAuth : *optUnauth);
50 if (!target)
51 {
52 JLOG(ctx.j.trace()) << "Malformed transaction: Authorized or Unauthorized "
53 "field zeroed.";
55 }
56
57 // An account may not preauthorize itself.
58 if (optAuth && (target == ctx.tx[sfAccount]))
59 {
60 JLOG(ctx.j.trace()) << "Malformed transaction: Attempting to DepositPreauth self.";
62 }
63 }
64 else
65 {
66 if (auto err = credentials::checkArray(
67 ctx.tx.getFieldArray(authArrPresent ? sfAuthorizeCredentials : sfUnauthorizeCredentials),
69 ctx.j);
70 !isTesSuccess(err))
71 return err;
72 }
73
74 return tesSUCCESS;
75}
76
77TER
79{
80 AccountID const account(ctx.tx[sfAccount]);
81
82 // Determine which operation we're performing: authorizing or unauthorizing.
83 if (ctx.tx.isFieldPresent(sfAuthorize))
84 {
85 // Verify that the Authorize account is present in the ledger.
86 AccountID const auth{ctx.tx[sfAuthorize]};
87 if (!ctx.view.exists(keylet::account(auth)))
88 return tecNO_TARGET;
89
90 // Verify that the Preauth entry they asked to add is not already
91 // in the ledger.
92 if (ctx.view.exists(keylet::depositPreauth(account, auth)))
93 return tecDUPLICATE;
94 }
95 else if (ctx.tx.isFieldPresent(sfUnauthorize))
96 {
97 // Verify that the Preauth entry they asked to remove is in the ledger.
98 if (!ctx.view.exists(keylet::depositPreauth(account, ctx.tx[sfUnauthorize])))
99 return tecNO_ENTRY;
100 }
101 else if (ctx.tx.isFieldPresent(sfAuthorizeCredentials))
102 {
103 STArray const& authCred(ctx.tx.getFieldArray(sfAuthorizeCredentials));
105 for (auto const& o : authCred)
106 {
107 auto const& issuer = o[sfIssuer];
108 if (!ctx.view.exists(keylet::account(issuer)))
109 return tecNO_ISSUER;
110 auto [it, ins] = sorted.emplace(issuer, o[sfCredentialType]);
111 if (!ins)
112 return tefINTERNAL; // LCOV_EXCL_LINE
113 }
114
115 // Verify that the Preauth entry they asked to add is not already
116 // in the ledger.
117 if (ctx.view.exists(keylet::depositPreauth(account, sorted)))
118 return tecDUPLICATE;
119 }
120 else if (ctx.tx.isFieldPresent(sfUnauthorizeCredentials))
121 {
122 // Verify that the Preauth entry is in the ledger.
124 account, credentials::makeSorted(ctx.tx.getFieldArray(sfUnauthorizeCredentials)))))
125 return tecNO_ENTRY;
126 }
127 return tesSUCCESS;
128}
129
130TER
132{
133 if (ctx_.tx.isFieldPresent(sfAuthorize))
134 {
135 auto const sleOwner = view().peek(keylet::account(account_));
136 if (!sleOwner)
137 return {tefINTERNAL};
138
139 // A preauth counts against the reserve of the issuing account, but we
140 // check the starting balance because we want to allow dipping into the
141 // reserve to pay fees.
142 {
143 STAmount const reserve{view().fees().accountReserve(sleOwner->getFieldU32(sfOwnerCount) + 1)};
144
145 if (mPriorBalance < reserve)
147 }
148
149 // Preclaim already verified that the Preauth entry does not yet exist.
150 // Create and populate the Preauth entry.
151 AccountID const auth{ctx_.tx[sfAuthorize]};
152 Keylet const preauthKeylet = keylet::depositPreauth(account_, auth);
153 auto slePreauth = std::make_shared<SLE>(preauthKeylet);
154
155 slePreauth->setAccountID(sfAccount, account_);
156 slePreauth->setAccountID(sfAuthorize, auth);
157 view().insert(slePreauth);
158
159 auto const page = view().dirInsert(keylet::ownerDir(account_), preauthKeylet, describeOwnerDir(account_));
160
161 JLOG(j_.trace()) << "Adding DepositPreauth to owner directory " << to_string(preauthKeylet.key) << ": "
162 << (page ? "success" : "failure");
163
164 if (!page)
165 return tecDIR_FULL; // LCOV_EXCL_LINE
166
167 slePreauth->setFieldU64(sfOwnerNode, *page);
168
169 // If we succeeded, the new entry counts against the creator's reserve.
170 adjustOwnerCount(view(), sleOwner, 1, j_);
171 }
172 else if (ctx_.tx.isFieldPresent(sfUnauthorize))
173 {
174 auto const preauth = keylet::depositPreauth(account_, ctx_.tx[sfUnauthorize]);
175
176 return DepositPreauth::removeFromLedger(view(), preauth.key, j_);
177 }
178 else if (ctx_.tx.isFieldPresent(sfAuthorizeCredentials))
179 {
180 auto const sleOwner = view().peek(keylet::account(account_));
181 if (!sleOwner)
182 return tefINTERNAL; // LCOV_EXCL_LINE
183
184 // A preauth counts against the reserve of the issuing account, but we
185 // check the starting balance because we want to allow dipping into the
186 // reserve to pay fees.
187 {
188 STAmount const reserve{view().fees().accountReserve(sleOwner->getFieldU32(sfOwnerCount) + 1)};
189
190 if (mPriorBalance < reserve)
192 }
193
194 // Preclaim already verified that the Preauth entry does not yet exist.
195 // Create and populate the Preauth entry.
196
197 auto const sortedTX = credentials::makeSorted(ctx_.tx.getFieldArray(sfAuthorizeCredentials));
198 STArray sortedLE(sfAuthorizeCredentials, sortedTX.size());
199 for (auto const& p : sortedTX)
200 {
201 auto cred = STObject::makeInnerObject(sfCredential);
202 cred.setAccountID(sfIssuer, p.first);
203 cred.setFieldVL(sfCredentialType, p.second);
204 sortedLE.push_back(std::move(cred));
205 }
206
207 Keylet const preauthKey = keylet::depositPreauth(account_, sortedTX);
208 auto slePreauth = std::make_shared<SLE>(preauthKey);
209 if (!slePreauth)
210 return tefINTERNAL; // LCOV_EXCL_LINE
211
212 slePreauth->setAccountID(sfAccount, account_);
213 slePreauth->peekFieldArray(sfAuthorizeCredentials) = std::move(sortedLE);
214
215 view().insert(slePreauth);
216
217 auto const page = view().dirInsert(keylet::ownerDir(account_), preauthKey, describeOwnerDir(account_));
218
219 JLOG(j_.trace()) << "Adding DepositPreauth to owner directory " << to_string(preauthKey.key) << ": "
220 << (page ? "success" : "failure");
221
222 if (!page)
223 return tecDIR_FULL; // LCOV_EXCL_LINE
224
225 slePreauth->setFieldU64(sfOwnerNode, *page);
226
227 // If we succeeded, the new entry counts against the creator's reserve.
228 adjustOwnerCount(view(), sleOwner, 1, j_);
229 }
230 else if (ctx_.tx.isFieldPresent(sfUnauthorizeCredentials))
231 {
232 auto const preauthKey =
234 return DepositPreauth::removeFromLedger(view(), preauthKey.key, j_);
235 }
236
237 return tesSUCCESS;
238}
239
240TER
242{
243 // Existence already checked in preclaim and DeleteAccount
244 auto const slePreauth{view.peek(keylet::depositPreauth(preauthIndex))};
245 if (!slePreauth)
246 {
247 JLOG(j.warn()) << "Selected DepositPreauth does not exist.";
248 return tecNO_ENTRY;
249 }
250
251 AccountID const account{(*slePreauth)[sfAccount]};
252 std::uint64_t const page{(*slePreauth)[sfOwnerNode]};
253 if (!view.dirRemove(keylet::ownerDir(account), page, preauthIndex, false))
254 {
255 // LCOV_EXCL_START
256 JLOG(j.fatal()) << "Unable to delete DepositPreauth from owner.";
257 return tefBAD_LEDGER;
258 // LCOV_EXCL_STOP
259 }
260
261 // If we succeeded, update the DepositPreauth owner's reserve.
262 auto const sleOwner = view.peek(keylet::account(account));
263 if (!sleOwner)
264 return tefINTERNAL; // LCOV_EXCL_LINE
265
266 adjustOwnerCount(view, sleOwner, -1, j);
267
268 // Remove DepositPreauth from ledger.
269 view.erase(slePreauth);
270
271 return tesSUCCESS;
272}
273
274} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:41
Stream fatal() const
Definition Journal.h:325
Stream trace() const
Severity stream access functions.
Definition Journal.h:295
Stream warn() const
Definition Journal.h:313
STTx const & tx
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:115
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
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:284
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static TER removeFromLedger(ApplyView &view, uint256 const &delIndex, beast::Journal j)
static NotTEC preflight(PreflightContext const &ctx)
static bool checkExtraFeatures(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
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:118
void push_back(STObject const &object)
Definition STArray.h:188
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:663
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:439
static STObject makeInnerObject(SField const &name)
Definition STObject.cpp:72
AccountID const account_
Definition Transactor.h:113
beast::Journal const j_
Definition Transactor.h:111
ApplyView & view()
Definition Transactor.h:129
XRPAmount mPriorBalance
Definition Transactor.h:114
ApplyContext & ctx_
Definition Transactor.h:109
T emplace(T... args)
T is_same_v
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:299
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:325
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:6
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
@ tefBAD_LEDGER
Definition TER.h:151
@ tefINTERNAL
Definition TER.h:154
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
Definition View.cpp:941
std::size_t constexpr maxCredentialsArraySize
The maximum number of credentials can be passed in array.
Definition Protocol.h:224
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Definition View.cpp:955
@ temCANNOT_PREAUTH_SELF
Definition TER.h:101
@ temMALFORMED
Definition TER.h:68
@ temINVALID_ACCOUNT_ID
Definition TER.h:100
bool isTesSuccess(TER x) noexcept
Definition TER.h:650
@ tecDIR_FULL
Definition TER.h:269
@ tecNO_ENTRY
Definition TER.h:288
@ tecNO_TARGET
Definition TER.h:286
@ tecINSUFFICIENT_RESERVE
Definition TER.h:289
@ tecNO_ISSUER
Definition TER.h:281
@ tecDUPLICATE
Definition TER.h:297
@ tesSUCCESS
Definition TER.h:226
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:20
uint256 key
Definition Keylet.h:21
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:54
ReadView const & view
Definition Transactor.h:57
State information when preflighting a tx.
Definition Transactor.h:16
beast::Journal const j
Definition Transactor.h:23