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/ledger/helpers/SponsorHelpers.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/Feature.h>
13#include <xrpl/protocol/Indexes.h>
14#include <xrpl/protocol/Keylet.h>
15#include <xrpl/protocol/Protocol.h>
16#include <xrpl/protocol/SField.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 auto const sleAuth = ctx.view.read(keylet::account(auth));
107 if (!sleAuth)
108 return tecNO_TARGET;
109
110 if (ctx.view.rules().enabled(fixCleanup3_3_0) && isPseudoAccount(sleAuth))
111 {
112 JLOG(ctx.j.debug()) << "Authorized account is a pseudo-account.";
113 return tecPSEUDO_ACCOUNT;
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, auth)))
119 return tecDUPLICATE;
120 }
121 else if (ctx.tx.isFieldPresent(sfUnauthorize))
122 {
123 // Verify that the Preauth entry they asked to remove is in the ledger.
124 if (!ctx.view.exists(keylet::depositPreauth(account, ctx.tx[sfUnauthorize])))
125 return tecNO_ENTRY;
126 }
127 else if (ctx.tx.isFieldPresent(sfAuthorizeCredentials))
128 {
129 STArray const& authCred(ctx.tx.getFieldArray(sfAuthorizeCredentials));
131 for (auto const& o : authCred)
132 {
133 auto const& issuer = o[sfIssuer];
134 if (!ctx.view.exists(keylet::account(issuer)))
135 return tecNO_ISSUER;
136 auto [it, ins] = sorted.emplace(issuer, o[sfCredentialType]);
137 if (!ins)
138 return tefINTERNAL; // LCOV_EXCL_LINE
139 }
140
141 // Verify that the Preauth entry they asked to add is not already
142 // in the ledger.
143 if (ctx.view.exists(keylet::depositPreauth(account, sorted)))
144 return tecDUPLICATE;
145 }
146 else if (ctx.tx.isFieldPresent(sfUnauthorizeCredentials))
147 {
148 // Verify that the Preauth entry is in the ledger.
149 if (!ctx.view.exists(
151 account,
152 credentials::makeSorted(ctx.tx.getFieldArray(sfUnauthorizeCredentials)))))
153 return tecNO_ENTRY;
154 }
155 return tesSUCCESS;
156}
157
158TER
160{
161 auto applyViewContext = ctx_.getApplyViewContext();
162 if (ctx_.tx.isFieldPresent(sfAuthorize))
163 {
164 auto const sleOwner = view().peek(keylet::account(accountID_));
165 if (!sleOwner)
166 return {tefINTERNAL};
167
168 // A preauth counts against the reserve of the issuing account, but we
169 // check the starting balance because we want to allow dipping into the
170 // reserve to pay fees.
171 if (auto const ret = checkReserve(
172 applyViewContext, sleOwner, preFeeBalance_, {.ownerCountDelta = 1}, j_);
173 !isTesSuccess(ret))
174 return ret;
175
176 // Preclaim already verified that the Preauth entry does not yet exist.
177 // Create and populate the Preauth entry.
178 AccountID const auth{ctx_.tx[sfAuthorize]};
179 Keylet const preauthKeylet = keylet::depositPreauth(accountID_, auth);
180 auto slePreauth = std::make_shared<SLE>(preauthKeylet);
181
182 slePreauth->setAccountID(sfAccount, accountID_);
183 slePreauth->setAccountID(sfAuthorize, auth);
184 view().insert(slePreauth);
185
186 auto const page = view().dirInsert(
188
189 JLOG(j_.trace()) << "Adding DepositPreauth to owner directory "
190 << to_string(preauthKeylet.key) << ": " << (page ? "success" : "failure");
191
192 if (!page)
193 return tecDIR_FULL; // LCOV_EXCL_LINE
194
195 slePreauth->setFieldU64(sfOwnerNode, *page);
196
197 // If we succeeded, the new entry counts against the creator's reserve.
198 increaseOwnerCount(applyViewContext, sleOwner, 1, j_);
199 addSponsorToLedgerEntry(applyViewContext, slePreauth);
200 }
201 else if (ctx_.tx.isFieldPresent(sfUnauthorize))
202 {
203 auto const preauth = keylet::depositPreauth(accountID_, ctx_.tx[sfUnauthorize]);
204
205 return DepositPreauth::removeFromLedger(view(), preauth.key, j_);
206 }
207 else if (ctx_.tx.isFieldPresent(sfAuthorizeCredentials))
208 {
209 auto const sleOwner = view().peek(keylet::account(accountID_));
210 if (!sleOwner)
211 return tefINTERNAL; // LCOV_EXCL_LINE
212
213 // A preauth counts against the reserve of the issuing account, but we
214 // check the starting balance because we want to allow dipping into the
215 // reserve to pay fees.
216 if (auto const ret = checkReserve(
217 applyViewContext, sleOwner, preFeeBalance_, {.ownerCountDelta = 1}, j_);
218 !isTesSuccess(ret))
219 return ret;
220
221 // Preclaim already verified that the Preauth entry does not yet exist.
222 // Create and populate the Preauth entry.
223
224 auto const sortedTX =
225 credentials::makeSorted(ctx_.tx.getFieldArray(sfAuthorizeCredentials));
226 STArray sortedLE(sfAuthorizeCredentials, sortedTX.size());
227 for (auto const& p : sortedTX)
228 {
229 auto cred = STObject::makeInnerObject(sfCredential);
230 cred.setAccountID(sfIssuer, p.first);
231 cred.setFieldVL(sfCredentialType, p.second);
232 sortedLE.pushBack(std::move(cred));
233 }
234
235 Keylet const preauthKey = keylet::depositPreauth(accountID_, sortedTX);
236 auto slePreauth = std::make_shared<SLE>(preauthKey);
237 if (!slePreauth)
238 return tefINTERNAL; // LCOV_EXCL_LINE
239
240 slePreauth->setAccountID(sfAccount, accountID_);
241 slePreauth->peekFieldArray(sfAuthorizeCredentials) = std::move(sortedLE);
242
243 view().insert(slePreauth);
244
245 auto const page = view().dirInsert(
247
248 JLOG(j_.trace()) << "Adding DepositPreauth to owner directory " << to_string(preauthKey.key)
249 << ": " << (page ? "success" : "failure");
250
251 if (!page)
252 return tecDIR_FULL; // LCOV_EXCL_LINE
253
254 slePreauth->setFieldU64(sfOwnerNode, *page);
255
256 // If we succeeded, the new entry counts against the creator's reserve.
257 increaseOwnerCount(applyViewContext, sleOwner, 1, j_);
258 addSponsorToLedgerEntry(applyViewContext, slePreauth);
259 }
260 else if (ctx_.tx.isFieldPresent(sfUnauthorizeCredentials))
261 {
262 auto const preauthKey = keylet::depositPreauth(
263 accountID_, credentials::makeSorted(ctx_.tx.getFieldArray(sfUnauthorizeCredentials)));
264 return DepositPreauth::removeFromLedger(view(), preauthKey.key, j_);
265 }
266
267 return tesSUCCESS;
268}
269
270TER
272{
273 // Existence already checked in preclaim and AccountDelete
274 auto const slePreauth{view.peek(keylet::depositPreauth(preauthIndex))};
275 if (!slePreauth)
276 {
277 JLOG(j.warn()) << "Selected DepositPreauth does not exist.";
278 return tecNO_ENTRY;
279 }
280
281 AccountID const account{(*slePreauth)[sfAccount]};
282 std::uint64_t const page{(*slePreauth)[sfOwnerNode]};
283 if (!view.dirRemove(keylet::ownerDir(account), page, preauthIndex, false))
284 {
285 // LCOV_EXCL_START
286 JLOG(j.fatal()) << "Unable to delete DepositPreauth from owner.";
287 return tefBAD_LEDGER;
288 // LCOV_EXCL_STOP
289 }
290
291 // If we succeeded, update the DepositPreauth owner's reserve.
292 auto const sleOwner = view.peek(keylet::account(account));
293 if (!sleOwner)
294 return tefINTERNAL; // LCOV_EXCL_LINE
295
296 decreaseOwnerCountForObject(view, sleOwner, slePreauth, 1, j);
297 // Remove DepositPreauth from ledger.
298 view.erase(slePreauth);
299
300 return tesSUCCESS;
301}
302
303void
305{
306 // No transaction-specific invariants yet (future work).
307}
308
309bool
311 STTx const&,
312 TER,
313 XRPAmount,
314 ReadView const&,
315 beast::Journal const&)
316{
317 // No transaction-specific invariants yet (future work).
318 return true;
319}
320
321} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream fatal() const
Definition Journal.h:368
Stream debug() const
Definition Journal.h:344
Stream trace() const
Severity stream access functions.
Definition Journal.h:338
Stream warn() const
Definition Journal.h:356
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:134
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:366
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:41
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
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:180
void pushBack(STObject const &object)
Definition STArray.h:212
std::shared_ptr< STLedgerEntry const > const & const_ref
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:688
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
static STObject makeInnerObject(SField const &name)
Definition STObject.cpp:79
beast::Journal const j_
Definition Transactor.h:155
ApplyView & view()
Definition Transactor.h:175
AccountID const accountID_
Definition Transactor.h:157
XRPAmount preFeeBalance_
Definition Transactor.h:158
ApplyContext & ctx_
Definition Transactor.h:153
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:344
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:373
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:198
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void decreaseOwnerCountForObject(ApplyView &view, SLE::ref accountSle, SLE::ref objectSle, std::uint32_t count, beast::Journal j)
Decrease owner-count fields for an existing ledger object.
void increaseOwnerCount(ApplyView &view, SLE::ref accountSle, SLE::ref sponsorSle, std::uint32_t count, beast::Journal j)
Increase owner-count fields when the caller supplies the sponsor.
@ tefBAD_LEDGER
Definition TER.h:162
@ tefINTERNAL
Definition TER.h:165
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
void addSponsorToLedgerEntry(SLE::ref sle, SLE::const_ref sponsorSle, SF_ACCOUNT const &field=sfSponsor)
Stamp a reserve sponsor onto a ledger entry using an explicit sponsor SLE.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:607
constexpr std::size_t kMaxCredentialsArraySize
The maximum number of credentials can be passed in array.
Definition Protocol.h:280
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:34
TER checkReserve(ApplyViewContext ctx, SLE::const_ref accSle, XRPAmount accBalance, SLE::const_ref sponsorSle, Adjustment adj, beast::Journal j, TER insufReserveCode=tecINSUFFICIENT_RESERVE)
Check if an account has sufficient reserve.
@ temCANNOT_PREAUTH_SELF
Definition TER.h:108
@ temMALFORMED
Definition TER.h:75
@ temINVALID_ACCOUNT_ID
Definition TER.h:107
bool isTesSuccess(TER x) noexcept
Definition TER.h:676
TERSubset< CanCvtToTER > TER
Definition TER.h:647
@ tecDIR_FULL
Definition TER.h:290
@ tecPSEUDO_ACCOUNT
Definition TER.h:365
@ tecNO_ENTRY
Definition TER.h:309
@ tecNO_TARGET
Definition TER.h:307
@ tecNO_ISSUER
Definition TER.h:302
@ tecDUPLICATE
Definition TER.h:318
bool isPseudoAccount(SLE::const_pointer sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
Returns true if and only if sleAcct is a pseudo-account or specific pseudo-accounts in pseudoFieldFil...
BaseUInt< 256 > uint256
Definition base_uint.h:580
@ tesSUCCESS
Definition TER.h:245
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:83
ReadView const & view
Definition Transactor.h:86
beast::Journal const j
Definition Transactor.h:91
State information when preflighting a tx.
Definition Transactor.h:38
beast::Journal const j
Definition Transactor.h:45