rippled
Loading...
Searching...
No Matches
DepositPreauth.cpp
1#include <xrpl/basics/Log.h>
2#include <xrpl/ledger/View.h>
3#include <xrpl/ledger/helpers/AccountRootHelpers.h>
4#include <xrpl/ledger/helpers/CredentialHelpers.h>
5#include <xrpl/ledger/helpers/DirectoryHelpers.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/Indexes.h>
8#include <xrpl/protocol/TxFlags.h>
9#include <xrpl/tx/transactors/payment/DepositPreauth.h>
10
11#include <optional>
12
13namespace xrpl {
14
15bool
17{
18 bool const authArrPresent = ctx.tx.isFieldPresent(sfAuthorizeCredentials);
19 bool const unauthArrPresent = ctx.tx.isFieldPresent(sfUnauthorizeCredentials);
20 bool const authCredPresent = authArrPresent || unauthArrPresent;
21
22 return !authCredPresent || ctx.rules.enabled(featureCredentials);
23}
24
27{
28 bool const authArrPresent = ctx.tx.isFieldPresent(sfAuthorizeCredentials);
29 bool const unauthArrPresent = ctx.tx.isFieldPresent(sfUnauthorizeCredentials);
30 int const authCredPresent =
31 static_cast<int>(authArrPresent) + static_cast<int>(unauthArrPresent);
32
33 auto const optAuth = ctx.tx[~sfAuthorize];
34 auto const optUnauth = ctx.tx[~sfUnauthorize];
35 int const authPresent =
36 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 != 0)
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(
68 authArrPresent ? sfAuthorizeCredentials : sfUnauthorizeCredentials),
70 ctx.j);
71 !isTesSuccess(err))
72 return err;
73 }
74
75 return tesSUCCESS;
76}
77
78TER
80{
81 AccountID const account(ctx.tx[sfAccount]);
82
83 // Determine which operation we're performing: authorizing or unauthorizing.
84 if (ctx.tx.isFieldPresent(sfAuthorize))
85 {
86 // Verify that the Authorize account is present in the ledger.
87 AccountID const auth{ctx.tx[sfAuthorize]};
88 if (!ctx.view.exists(keylet::account(auth)))
89 return tecNO_TARGET;
90
91 // Verify that the Preauth entry they asked to add is not already
92 // in the ledger.
93 if (ctx.view.exists(keylet::depositPreauth(account, auth)))
94 return tecDUPLICATE;
95 }
96 else if (ctx.tx.isFieldPresent(sfUnauthorize))
97 {
98 // Verify that the Preauth entry they asked to remove is in the ledger.
99 if (!ctx.view.exists(keylet::depositPreauth(account, ctx.tx[sfUnauthorize])))
100 return tecNO_ENTRY;
101 }
102 else if (ctx.tx.isFieldPresent(sfAuthorizeCredentials))
103 {
104 STArray const& authCred(ctx.tx.getFieldArray(sfAuthorizeCredentials));
106 for (auto const& o : authCred)
107 {
108 auto const& issuer = o[sfIssuer];
109 if (!ctx.view.exists(keylet::account(issuer)))
110 return tecNO_ISSUER;
111 auto [it, ins] = sorted.emplace(issuer, o[sfCredentialType]);
112 if (!ins)
113 return tefINTERNAL; // LCOV_EXCL_LINE
114 }
115
116 // Verify that the Preauth entry they asked to add is not already
117 // in the ledger.
118 if (ctx.view.exists(keylet::depositPreauth(account, sorted)))
119 return tecDUPLICATE;
120 }
121 else if (ctx.tx.isFieldPresent(sfUnauthorizeCredentials))
122 {
123 // Verify that the Preauth entry is in the ledger.
124 if (!ctx.view.exists(
126 account,
127 credentials::makeSorted(ctx.tx.getFieldArray(sfUnauthorizeCredentials)))))
128 return tecNO_ENTRY;
129 }
130 return tesSUCCESS;
131}
132
133TER
135{
136 if (ctx_.tx.isFieldPresent(sfAuthorize))
137 {
138 auto const sleOwner = view().peek(keylet::account(account_));
139 if (!sleOwner)
140 return {tefINTERNAL};
141
142 // A preauth counts against the reserve of the issuing account, but we
143 // check the starting balance because we want to allow dipping into the
144 // reserve to pay fees.
145 {
146 STAmount const reserve{
147 view().fees().accountReserve(sleOwner->getFieldU32(sfOwnerCount) + 1)};
148
149 if (preFeeBalance_ < reserve)
151 }
152
153 // Preclaim already verified that the Preauth entry does not yet exist.
154 // Create and populate the Preauth entry.
155 AccountID const auth{ctx_.tx[sfAuthorize]};
156 Keylet const preauthKeylet = keylet::depositPreauth(account_, auth);
157 auto slePreauth = std::make_shared<SLE>(preauthKeylet);
158
159 slePreauth->setAccountID(sfAccount, account_);
160 slePreauth->setAccountID(sfAuthorize, auth);
161 view().insert(slePreauth);
162
163 auto const page =
165
166 JLOG(j_.trace()) << "Adding DepositPreauth to owner directory "
167 << to_string(preauthKeylet.key) << ": " << (page ? "success" : "failure");
168
169 if (!page)
170 return tecDIR_FULL; // LCOV_EXCL_LINE
171
172 slePreauth->setFieldU64(sfOwnerNode, *page);
173
174 // If we succeeded, the new entry counts against the creator's reserve.
175 adjustOwnerCount(view(), sleOwner, 1, j_);
176 }
177 else if (ctx_.tx.isFieldPresent(sfUnauthorize))
178 {
179 auto const preauth = keylet::depositPreauth(account_, ctx_.tx[sfUnauthorize]);
180
181 return DepositPreauth::removeFromLedger(view(), preauth.key, j_);
182 }
183 else if (ctx_.tx.isFieldPresent(sfAuthorizeCredentials))
184 {
185 auto const sleOwner = view().peek(keylet::account(account_));
186 if (!sleOwner)
187 return tefINTERNAL; // LCOV_EXCL_LINE
188
189 // A preauth counts against the reserve of the issuing account, but we
190 // check the starting balance because we want to allow dipping into the
191 // reserve to pay fees.
192 {
193 STAmount const reserve{
194 view().fees().accountReserve(sleOwner->getFieldU32(sfOwnerCount) + 1)};
195
196 if (preFeeBalance_ < reserve)
198 }
199
200 // Preclaim already verified that the Preauth entry does not yet exist.
201 // Create and populate the Preauth entry.
202
203 auto const sortedTX =
204 credentials::makeSorted(ctx_.tx.getFieldArray(sfAuthorizeCredentials));
205 STArray sortedLE(sfAuthorizeCredentials, sortedTX.size());
206 for (auto const& p : sortedTX)
207 {
208 auto cred = STObject::makeInnerObject(sfCredential);
209 cred.setAccountID(sfIssuer, p.first);
210 cred.setFieldVL(sfCredentialType, p.second);
211 sortedLE.push_back(std::move(cred));
212 }
213
214 Keylet const preauthKey = keylet::depositPreauth(account_, sortedTX);
215 auto slePreauth = std::make_shared<SLE>(preauthKey);
216 if (!slePreauth)
217 return tefINTERNAL; // LCOV_EXCL_LINE
218
219 slePreauth->setAccountID(sfAccount, account_);
220 slePreauth->peekFieldArray(sfAuthorizeCredentials) = std::move(sortedLE);
221
222 view().insert(slePreauth);
223
224 auto const page =
226
227 JLOG(j_.trace()) << "Adding DepositPreauth to owner directory " << to_string(preauthKey.key)
228 << ": " << (page ? "success" : "failure");
229
230 if (!page)
231 return tecDIR_FULL; // LCOV_EXCL_LINE
232
233 slePreauth->setFieldU64(sfOwnerNode, *page);
234
235 // If we succeeded, the new entry counts against the creator's reserve.
236 adjustOwnerCount(view(), sleOwner, 1, j_);
237 }
238 else if (ctx_.tx.isFieldPresent(sfUnauthorizeCredentials))
239 {
240 auto const preauthKey = keylet::depositPreauth(
241 account_, credentials::makeSorted(ctx_.tx.getFieldArray(sfUnauthorizeCredentials)));
242 return DepositPreauth::removeFromLedger(view(), preauthKey.key, j_);
243 }
244
245 return tesSUCCESS;
246}
247
248TER
250{
251 // Existence already checked in preclaim and AccountDelete
252 auto const slePreauth{view.peek(keylet::depositPreauth(preauthIndex))};
253 if (!slePreauth)
254 {
255 JLOG(j.warn()) << "Selected DepositPreauth does not exist.";
256 return tecNO_ENTRY;
257 }
258
259 AccountID const account{(*slePreauth)[sfAccount]};
260 std::uint64_t const page{(*slePreauth)[sfOwnerNode]};
261 if (!view.dirRemove(keylet::ownerDir(account), page, preauthIndex, false))
262 {
263 // LCOV_EXCL_START
264 JLOG(j.fatal()) << "Unable to delete DepositPreauth from owner.";
265 return tefBAD_LEDGER;
266 // LCOV_EXCL_STOP
267 }
268
269 // If we succeeded, update the DepositPreauth owner's reserve.
270 auto const sleOwner = view.peek(keylet::account(account));
271 if (!sleOwner)
272 return tefINTERNAL; // LCOV_EXCL_LINE
273
274 adjustOwnerCount(view, sleOwner, -1, j);
275
276 // Remove DepositPreauth from ledger.
277 view.erase(slePreauth);
278
279 return tesSUCCESS;
280}
281
282} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
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:116
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:289
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:120
void push_back(STObject const &object)
Definition STArray.h:189
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:680
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:456
static STObject makeInnerObject(SField const &name)
Definition STObject.cpp:73
AccountID const account_
Definition Transactor.h:116
beast::Journal const j_
Definition Transactor.h:114
ApplyView & view()
Definition Transactor.h:132
XRPAmount preFeeBalance_
Definition Transactor.h:117
ApplyContext & ctx_
Definition Transactor.h:112
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:307
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:336
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
@ tefBAD_LEDGER
Definition TER.h:150
@ tefINTERNAL
Definition TER.h:153
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
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)
Returns a function that sets the owner on a directory SLE.
@ temCANNOT_PREAUTH_SELF
Definition TER.h:100
@ temMALFORMED
Definition TER.h:67
@ temINVALID_ACCOUNT_ID
Definition TER.h:99
bool isTesSuccess(TER x) noexcept
Definition TER.h:651
@ tecDIR_FULL
Definition TER.h:268
@ tecNO_ENTRY
Definition TER.h:287
@ tecNO_TARGET
Definition TER.h:285
@ tecINSUFFICIENT_RESERVE
Definition TER.h:288
@ tecNO_ISSUER
Definition TER.h:280
@ tecDUPLICATE
Definition TER.h:296
@ tesSUCCESS
Definition TER.h:225
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:57
ReadView const & view
Definition Transactor.h:60
State information when preflighting a tx.
Definition Transactor.h:14
beast::Journal const j
Definition Transactor.h:21