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/entries/DepositPreauthEntry.h>
8#include <xrpl/ledger/helpers/AccountRootHelpers.h>
9#include <xrpl/ledger/helpers/CredentialHelpers.h>
10#include <xrpl/ledger/helpers/DirectoryHelpers.h>
11#include <xrpl/ledger/helpers/SponsorHelpers.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/Feature.h>
14#include <xrpl/protocol/Indexes.h>
15#include <xrpl/protocol/Keylet.h>
16#include <xrpl/protocol/Protocol.h>
17#include <xrpl/protocol/SField.h>
18#include <xrpl/protocol/STArray.h>
19#include <xrpl/protocol/STLedgerEntry.h>
20#include <xrpl/protocol/STTx.h>
21#include <xrpl/protocol/TER.h>
22#include <xrpl/protocol/XRPAmount.h>
23#include <xrpl/tx/Transactor.h>
24
25#include <cstdint>
26#include <memory>
27#include <optional>
28#include <set>
29#include <utility>
30
31namespace xrpl {
32
33bool
35{
36 bool const authArrPresent = ctx.tx.isFieldPresent(sfAuthorizeCredentials);
37 bool const unauthArrPresent = ctx.tx.isFieldPresent(sfUnauthorizeCredentials);
38 bool const authCredPresent = authArrPresent || unauthArrPresent;
39
40 return !authCredPresent || ctx.rules.enabled(featureCredentials);
41}
42
45{
46 bool const authArrPresent = ctx.tx.isFieldPresent(sfAuthorizeCredentials);
47 bool const unauthArrPresent = ctx.tx.isFieldPresent(sfUnauthorizeCredentials);
48 int const authCredPresent =
49 static_cast<int>(authArrPresent) + static_cast<int>(unauthArrPresent);
50
51 auto const optAuth = ctx.tx[~sfAuthorize];
52 auto const optUnauth = ctx.tx[~sfUnauthorize];
53 int const authPresent =
54 static_cast<int>(optAuth.has_value()) + static_cast<int>(optUnauth.has_value());
55
56 if (authPresent + authCredPresent != 1)
57 {
58 // There can only be 1 field out of 4 or the transaction is malformed.
59 JLOG(ctx.j.trace()) << "Malformed transaction: "
60 "Invalid Authorize and Unauthorize field combination.";
61 return temMALFORMED;
62 }
63
64 if (authPresent != 0)
65 {
66 // Make sure that the passed account is valid.
67 // NOLINTNEXTLINE(bugprone-unchecked-optional-access) authPresent != 0 guarantees one is set
68 AccountID const& target(optAuth ? *optAuth : *optUnauth);
69 if (!target)
70 {
71 JLOG(ctx.j.trace()) << "Malformed transaction: Authorized or Unauthorized "
72 "field zeroed.";
74 }
75
76 // An account may not preauthorize itself.
77 if (optAuth && (target == ctx.tx[sfAccount]))
78 {
79 JLOG(ctx.j.trace()) << "Malformed transaction: Attempting to DepositPreauth self.";
81 }
82 }
83 else
84 {
85 if (auto err = credentials::checkArray(
86 ctx.tx.getFieldArray(
87 authArrPresent ? sfAuthorizeCredentials : sfUnauthorizeCredentials),
89 ctx.j);
90 !isTesSuccess(err))
91 return err;
92 }
93
94 return tesSUCCESS;
95}
96
97TER
99{
100 AccountID const account(ctx.tx[sfAccount]);
101
102 // Determine which operation we're performing: authorizing or unauthorizing.
103 if (ctx.tx.isFieldPresent(sfAuthorize))
104 {
105 // Verify that the Authorize account is present in the ledger.
106 AccountID const auth{ctx.tx[sfAuthorize]};
107 auto const sleAuth = ctx.view.read(keylet::account(auth));
108 if (!sleAuth)
109 return tecNO_TARGET;
110
111 if (ctx.view.rules().enabled(fixCleanup3_3_0) && isPseudoAccount(sleAuth))
112 {
113 JLOG(ctx.j.debug()) << "Authorized account is a pseudo-account.";
114 return tecPSEUDO_ACCOUNT;
115 }
116
117 // Verify that the Preauth entry they asked to add is not already
118 // in the ledger.
119 if (DepositPreauthEntryR(account, auth, ctx.view))
120 return tecDUPLICATE;
121 }
122 else if (ctx.tx.isFieldPresent(sfUnauthorize))
123 {
124 // Verify that the Preauth entry they asked to remove is in the ledger.
125 if (!DepositPreauthEntryR(account, ctx.tx[sfUnauthorize], ctx.view))
126 return tecNO_ENTRY;
127 }
128 else if (ctx.tx.isFieldPresent(sfAuthorizeCredentials))
129 {
130 STArray const& authCred(ctx.tx.getFieldArray(sfAuthorizeCredentials));
132 for (auto const& o : authCred)
133 {
134 auto const& issuer = o[sfIssuer];
135 if (!ctx.view.exists(keylet::account(issuer)))
136 return tecNO_ISSUER;
137 auto [it, ins] = sorted.emplace(issuer, o[sfCredentialType]);
138 if (!ins)
139 return tefINTERNAL; // LCOV_EXCL_LINE
140 }
141
142 // Verify that the Preauth entry they asked to add is not already
143 // in the ledger.
144 if (DepositPreauthEntryR(account, sorted, ctx.view))
145 return tecDUPLICATE;
146 }
147 else if (ctx.tx.isFieldPresent(sfUnauthorizeCredentials))
148 {
149 // Verify that the Preauth entry is in the ledger.
151 account,
152 credentials::makeSorted(ctx.tx.getFieldArray(sfUnauthorizeCredentials)),
153 ctx.view))
154 return tecNO_ENTRY;
155 }
156 return tesSUCCESS;
157}
158
159TER
161{
162 auto const applyViewContext = ctx_.getApplyViewContext();
163 if (ctx_.tx.isFieldPresent(sfAuthorize))
164 {
165 auto const sleOwner = view().peek(keylet::account(accountID_));
166 if (!sleOwner)
167 return {tefINTERNAL};
168
169 // A preauth counts against the reserve of the issuing account, but we
170 // check the starting balance because we want to allow dipping into the
171 // reserve to pay fees.
172 if (auto const ret = checkReserve(
173 applyViewContext, sleOwner, preFeeBalance_, {.ownerCountDelta = 1}, j_);
174 !isTesSuccess(ret))
175 return ret;
176
177 // Preclaim already verified that the Preauth entry does not yet exist.
178 // Create and populate the Preauth entry.
179 AccountID const auth{ctx_.tx[sfAuthorize]};
180 DepositPreauthEntryW entryPreauth(accountID_, auth, view(), j_);
181 entryPreauth.newSLE();
182
183 entryPreauth->setAccountID(sfAccount, accountID_);
184 entryPreauth->setAccountID(sfAuthorize, auth);
185 entryPreauth.insert();
186
187 auto const page = view().dirInsert(
189
190 JLOG(j_.trace()) << "Adding DepositPreauth to owner directory "
191 << to_string(entryPreauth.key()) << ": " << (page ? "success" : "failure");
192
193 if (!page)
194 return tecDIR_FULL; // LCOV_EXCL_LINE
195
196 entryPreauth->setFieldU64(sfOwnerNode, *page);
197
198 // If we succeeded, the new entry counts against the creator's reserve.
199 increaseOwnerCount(applyViewContext, sleOwner, 1, j_);
200 addSponsorToLedgerEntry(applyViewContext, entryPreauth.mutableRawSle());
201 }
202 else if (ctx_.tx.isFieldPresent(sfUnauthorize))
203 {
204 auto const preauth = keylet::depositPreauth(accountID_, ctx_.tx[sfUnauthorize]);
205
206 return DepositPreauth::removeFromLedger(view(), preauth.key, j_);
207 }
208 else if (ctx_.tx.isFieldPresent(sfAuthorizeCredentials))
209 {
210 auto const sleOwner = view().peek(keylet::account(accountID_));
211 if (!sleOwner)
212 return tefINTERNAL; // LCOV_EXCL_LINE
213
214 // A preauth counts against the reserve of the issuing account, but we
215 // check the starting balance because we want to allow dipping into the
216 // reserve to pay fees.
217 if (auto const ret = checkReserve(
218 applyViewContext, sleOwner, preFeeBalance_, {.ownerCountDelta = 1}, j_);
219 !isTesSuccess(ret))
220 return ret;
221
222 // Preclaim already verified that the Preauth entry does not yet exist.
223 // Create and populate the Preauth entry.
224
225 auto const sortedTX =
226 credentials::makeSorted(ctx_.tx.getFieldArray(sfAuthorizeCredentials));
227 STArray sortedLE(sfAuthorizeCredentials, sortedTX.size());
228 for (auto const& p : sortedTX)
229 {
230 auto cred = STObject::makeInnerObject(sfCredential);
231 cred.setAccountID(sfIssuer, p.first);
232 cred.setFieldVL(sfCredentialType, p.second);
233 sortedLE.pushBack(std::move(cred));
234 }
235
236 DepositPreauthEntryW entryPreauth(accountID_, sortedTX, view(), j_);
237 entryPreauth.newSLE();
238 if (!entryPreauth)
239 return tefINTERNAL; // LCOV_EXCL_LINE
240
241 entryPreauth->setAccountID(sfAccount, accountID_);
242 entryPreauth->peekFieldArray(sfAuthorizeCredentials) = std::move(sortedLE);
243
244 entryPreauth.insert();
245
246 auto const page = view().dirInsert(
248
249 JLOG(j_.trace()) << "Adding DepositPreauth to owner directory "
250 << to_string(entryPreauth.key()) << ": " << (page ? "success" : "failure");
251
252 if (!page)
253 return tecDIR_FULL; // LCOV_EXCL_LINE
254
255 entryPreauth->setFieldU64(sfOwnerNode, *page);
256
257 // If we succeeded, the new entry counts against the creator's reserve.
258 increaseOwnerCount(applyViewContext, sleOwner, 1, j_);
259 addSponsorToLedgerEntry(applyViewContext, entryPreauth.mutableRawSle());
260 }
261 else if (ctx_.tx.isFieldPresent(sfUnauthorizeCredentials))
262 {
263 auto const preauthKey = keylet::depositPreauth(
264 accountID_, credentials::makeSorted(ctx_.tx.getFieldArray(sfUnauthorizeCredentials)));
265 return DepositPreauth::removeFromLedger(view(), preauthKey.key, j_);
266 }
267
268 return tesSUCCESS;
269}
270
271TER
273{
274 // Existence already checked in preclaim and AccountDelete
275 DepositPreauthEntryW slePreauth(preauthIndex, view, j);
276 if (!slePreauth)
277 {
278 JLOG(j.warn()) << "Selected DepositPreauth does not exist.";
279 return tecNO_ENTRY;
280 }
281
282 AccountID const account{(*slePreauth)[sfAccount]};
283 std::uint64_t const page{(*slePreauth)[sfOwnerNode]};
284 if (!view.dirRemove(keylet::ownerDir(account), page, preauthIndex, false))
285 {
286 // LCOV_EXCL_START
287 JLOG(j.fatal()) << "Unable to delete DepositPreauth from owner.";
288 return tefBAD_LEDGER;
289 // LCOV_EXCL_STOP
290 }
291
292 // If we succeeded, update the DepositPreauth owner's reserve.
293 auto const sleOwner = view.peek(keylet::account(account));
294 if (!sleOwner)
295 return tefINTERNAL; // LCOV_EXCL_LINE
296
297 decreaseOwnerCountForObject(view, sleOwner, slePreauth.mutableRawSle(), 1, j);
298 // Remove DepositPreauth from ledger.
299 slePreauth.erase();
300
301 return tesSUCCESS;
302}
303
304void
306{
307 // No transaction-specific invariants yet (future work).
308}
309
310bool
312 STTx const&,
313 TER,
314 XRPAmount,
315 ReadView const&,
316 beast::Journal const&)
317{
318 // No transaction-specific invariants yet (future work).
319 return true;
320}
321
322} // 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:141
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
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:373
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::ConstRef before, SLE::ConstRef 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:182
void pushBack(STObject const &object)
Definition STArray.h:212
std::shared_ptr< STLedgerEntry const > const & ConstRef
void setFieldU64(SField const &field, std::uint64_t)
Definition STObject.cpp:749
STArray & peekFieldArray(SField const &field)
Definition STObject.cpp:481
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
void setAccountID(SField const &field, AccountID const &)
Definition STObject.cpp:785
beast::Journal const j_
Definition Transactor.h:164
ApplyView & view()
Definition Transactor.h:184
AccountID const accountID_
Definition Transactor.h:166
XRPAmount preFeeBalance_
Definition Transactor.h:167
ApplyContext & ctx_
Definition Transactor.h:162
T emplace(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:386
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:415
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ tefBAD_LEDGER
Definition TER.h:164
@ tefINTERNAL
Definition TER.h:167
TER checkReserve(ApplyViewContext ctx, SLE::ConstRef accSle, XRPAmount accBalance, SLE::ConstRef sponsorSle, Adjustment adj, beast::Journal j, TER insufReserveCode=tecINSUFFICIENT_RESERVE)
Check if an account has sufficient reserve.
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.
void addSponsorToLedgerEntry(SLE::Ref sle, SLE::ConstRef sponsorSle, SF_ACCOUNT const &field=sfSponsor)
Stamp a reserve sponsor onto a ledger entry using an explicit sponsor SLE.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
BaseUInt< 256 > UInt256
Definition base_uint.h:581
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
constexpr std::size_t kMaxCredentialsArraySize
The maximum number of credentials can be passed in array.
Definition Protocol.h:281
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.
DepositPreauthEntry< ReadView > DepositPreauthEntryR
bool isPseudoAccount(SLE::const_pointer sleAcct)
Returns true if and only if sleAcct is a pseudo-account of any kind (i.e.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
@ 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:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecDIR_FULL
Definition TER.h:298
@ tecPSEUDO_ACCOUNT
Definition TER.h:373
@ tecNO_ENTRY
Definition TER.h:317
@ tecNO_TARGET
Definition TER.h:315
@ tecNO_ISSUER
Definition TER.h:310
@ tecDUPLICATE
Definition TER.h:326
std::function< void(SLE::Ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
DepositPreauthEntry< ApplyView > DepositPreauthEntryW
@ tesSUCCESS
Definition TER.h:253
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:92
ReadView const & view
Definition Transactor.h:95
beast::Journal const j
Definition Transactor.h:100
State information when preflighting a tx.
Definition Transactor.h:39
beast::Journal const j
Definition Transactor.h:46