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