xrpld
Loading...
Searching...
No Matches
applySteps.cpp
1#include <xrpl/tx/applySteps.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/ledger/ApplyView.h>
9#include <xrpl/ledger/OpenView.h>
10#include <xrpl/protocol/Rules.h>
11#include <xrpl/protocol/SField.h>
12#include <xrpl/protocol/SeqProxy.h>
13#include <xrpl/protocol/TER.h>
14#include <xrpl/protocol/XRPAmount.h>
15#include <xrpl/tx/ApplyContext.h>
16#include <xrpl/tx/Transactor.h>
17
18#include <cstdint>
19#include <exception>
20#include <memory>
21#include <optional>
22#include <utility>
23#pragma push_macro("TRANSACTION")
24#undef TRANSACTION
25
26// Do nothing
27#define TRANSACTION(...)
28#define TRANSACTION_INCLUDE 1
29
30#include <xrpl/protocol/detail/transactions.macro>
31
32#undef TRANSACTION
33#pragma pop_macro("TRANSACTION")
34
35// DO NOT INCLUDE TRANSACTOR HEADER FILES HERE.
36// See the instructions at the top of transactions.macro instead.
37
38#include <xrpl/core/ServiceRegistry.h>
39#include <xrpl/protocol/TxFormats.h>
40
41namespace xrpl {
42
43namespace {
44
45struct UnknownTxnType : std::exception
46{
47 TxType txnType;
48 UnknownTxnType(TxType t) : txnType{t}
49 {
50 }
51};
52
53// Call a lambda with the concrete transaction type as a template parameter
54// throw an "UnknownTxnType" exception on error
55template <class F>
56auto
57withTxnType(Rules const& rules, TxType txnType, F&& f)
58{
59 // These global updates really should have been for every Transaction
60 // step: preflight, preclaim, calculateBaseFee, and doApply. Unfortunately,
61 // they were only included in doApply (via Transactor::operator()). That may
62 // have been sufficient when the changes were only related to operations
63 // that mutated data, but some features will now change how they read data,
64 // so these need to be more global.
65 //
66 // To prevent unintentional side effects on existing checks, they will be
67 // set for every operation only once at least one of the relevant amendments
68 // are enabled.
69 //
70 // See also Transactor::operator().
71 //
72 std::optional<CurrentTransactionRulesGuard> rulesGuard;
73 std::optional<NumberMantissaScaleGuard> mantissaScaleGuard;
74 createGuards(rules, rulesGuard, mantissaScaleGuard);
75
76 switch (txnType)
77 {
78#pragma push_macro("TRANSACTION")
79#undef TRANSACTION
80
81#define TRANSACTION(tag, value, name, ...) \
82 case tag: \
83 return f.template operator()<name>();
84
85#include <xrpl/protocol/detail/transactions.macro>
86
87#undef TRANSACTION
88#pragma pop_macro("TRANSACTION")
89 default:
90 throw UnknownTxnType(txnType);
91 }
92}
93} // namespace
94
95// Templates so preflight does the right thing with T::kConsequencesFactory.
96//
97// This could be done more easily using if constexpr, but Visual Studio
98// 2017 doesn't handle if constexpr correctly. So once we're no longer
99// building with Visual Studio 2017 we can consider replacing the four
100// templates with a single template function that uses if constexpr.
101//
102// For ConsequencesFactoryType::Normal
103//
104
105template <class T>
106 requires(T::kConsequencesFactory == Transactor::ConsequencesFactoryType::Normal)
109{
110 return TxConsequences(ctx.tx);
111};
112
113// For ConsequencesFactoryType::Blocker
114template <class T>
115 requires(T::kConsequencesFactory == Transactor::ConsequencesFactoryType::Blocker)
116TxConsequences
117consequencesHelper(PreflightContext const& ctx)
118{
119 return TxConsequences(ctx.tx, TxConsequences::Category::Blocker);
120};
121
122// For ConsequencesFactoryType::Custom
123template <class T>
124 requires(T::kConsequencesFactory == Transactor::ConsequencesFactoryType::Custom)
127{
128 return T::makeTxConsequences(ctx);
129};
130
131static std::pair<NotTEC, TxConsequences>
133{
134 try
135 {
136 return withTxnType(ctx.rules, ctx.tx.getTxnType(), [&]<typename T>() {
137 auto const tec = Transactor::invokePreflight<T>(ctx);
138 return std::make_pair(
139 tec, isTesSuccess(tec) ? consequencesHelper<T>(ctx) : TxConsequences{tec});
140 });
141 }
142 catch (UnknownTxnType const& e)
143 {
144 // Should never happen
145 // LCOV_EXCL_START
146 JLOG(ctx.j.fatal()) << "Unknown transaction type in preflight: " << e.txnType;
147 UNREACHABLE("xrpl::invokePreflight : unknown transaction type");
148 return {temUNKNOWN, TxConsequences{temUNKNOWN}};
149 // LCOV_EXCL_STOP
150 }
151}
152
153static TER
155{
156 try
157 {
158 // use name hiding to accomplish compile-time polymorphism of static
159 // class functions for Transactor and derived classes.
160 return withTxnType(ctx.view.rules(), ctx.tx.getTxnType(), [&]<typename T>() -> TER {
161 // preclaim functionality is divided into two sections:
162 // 1. Up to and including the signature check: returns NotTEC.
163 // All transaction checks before and including checkSign
164 // MUST return NotTEC, or something more restrictive.
165 // Allowing tec results in these steps risks theft or
166 // destruction of funds, as a fee will be charged before the
167 // signature is checked.
168 // 2. After the signature check: returns TER.
169
170 // If the transactor requires a valid account and the
171 // transaction doesn't list one, preflight will have already
172 // a flagged a failure.
173 auto const id = ctx.tx.getAccountID(sfAccount);
174
175 if (id != beast::kZero)
176 {
177 if (NotTEC const preSigResult = [&]() -> NotTEC {
178 if (NotTEC const result = T::checkSeqProxy(ctx.view, ctx.tx, ctx.j))
179 return result;
180
181 if (NotTEC const result = T::checkPriorTxAndLastLedger(ctx))
182 return result;
183
184 if (NotTEC const result =
185 Transactor::invokeCheckPermission<T>(ctx.view, ctx.tx))
186 return result;
187
188 if (NotTEC const result = T::checkSign(ctx))
189 return result;
190
191 return tesSUCCESS;
192 }())
193 return preSigResult;
194
195 if (TER const result = T::checkFee(ctx, calculateBaseFee(ctx.view, ctx.tx)))
196 return result;
197 }
198
199 return T::preclaim(ctx);
200 });
201 }
202 catch (UnknownTxnType const& e)
203 {
204 // Should never happen
205 // LCOV_EXCL_START
206 JLOG(ctx.j.fatal()) << "Unknown transaction type in preclaim: " << e.txnType;
207 UNREACHABLE("xrpl::invokePreclaim : unknown transaction type");
208 return temUNKNOWN;
209 // LCOV_EXCL_STOP
210 }
211}
212
229static XRPAmount
230invokeCalculateBaseFee(ReadView const& view, STTx const& tx)
231{
232 try
233 {
234 return withTxnType(view.rules(), tx.getTxnType(), [&]<typename T>() {
235 return T::calculateBaseFee(view, tx);
236 });
237 }
238 catch (UnknownTxnType const& e)
239 {
240 // LCOV_EXCL_START
241 UNREACHABLE("xrpl::invoke_calculateBaseFee : unknown transaction type");
242 return XRPAmount{0};
243 // LCOV_EXCL_STOP
244 }
245}
246
248 : isBlocker_(false)
249 , fee_(beast::kZero)
250 , potentialSpend_(beast::kZero)
251 , seqProx_(SeqProxy::sequence(0))
253{
254 XRPL_ASSERT(
255 !isTesSuccess(pfResult), "xrpl::TxConsequences::TxConsequences : is not tesSUCCESS");
256}
257
259 : isBlocker_(false)
260 , fee_(tx[sfFee].native() && !tx[sfFee].negative() ? tx[sfFee].xrp() : beast::kZero)
261 , potentialSpend_(beast::kZero)
262 , seqProx_(tx.getSeqProxy())
263 , sequencesConsumed_(tx.getSeqProxy().isSeq() ? 1 : 0)
264{
265}
266
271
276
281
282static ApplyResult
284{
285 try
286 {
287 return withTxnType(ctx.view().rules(), ctx.tx.getTxnType(), [&]<typename T>() {
288 T p(ctx);
289 return p();
290 });
291 }
292 catch (UnknownTxnType const& e)
293 {
294 // Should never happen
295 // LCOV_EXCL_START
296 JLOG(ctx.journal.fatal()) << "Unknown transaction type in apply: " << e.txnType;
297 UNREACHABLE("xrpl::invokeApply : unknown transaction type");
298 return {temUNKNOWN, false};
299 // LCOV_EXCL_STOP
300 }
301}
302
303// Test-only factory — not part of the public API.
304// The returned Transactor holds a raw reference to ctx; the caller must ensure
305// the ApplyContext outlives the Transactor.
308{
309 return withTxnType(
310 ctx.view().rules(), ctx.tx.getTxnType(), [&]<typename T>() -> std::unique_ptr<Transactor> {
311 return std::make_unique<T>(ctx);
312 });
313}
314
315PreflightResult
317 ServiceRegistry& registry,
318 Rules const& rules,
319 STTx const& tx,
320 ApplyFlags flags,
322{
323 PreflightContext const pfCtx(registry, tx, rules, flags, j);
324 try
325 {
326 return {pfCtx, invokePreflight(pfCtx)};
327 }
328 catch (std::exception const& e)
329 {
330 JLOG(j.fatal()) << "apply (preflight): " << e.what();
331 return {pfCtx, {tefEXCEPTION, TxConsequences{tx}}};
332 }
333}
334
335PreflightResult
337 ServiceRegistry& registry,
338 Rules const& rules,
339 uint256 const& parentBatchId,
340 STTx const& tx,
341 ApplyFlags flags,
343{
344 PreflightContext const pfCtx(registry, tx, parentBatchId, rules, flags, j);
345 try
346 {
347 return {pfCtx, invokePreflight(pfCtx)};
348 }
349 catch (std::exception const& e)
350 {
351 JLOG(j.fatal()) << "apply (preflight): " << e.what();
352 return {pfCtx, {tefEXCEPTION, TxConsequences{tx}}};
353 }
354}
355
356PreclaimResult
357preclaim(PreflightResult const& preflightResult, ServiceRegistry& registry, OpenView const& view)
358{
360 if (preflightResult.rules != view.rules())
361 {
362 auto secondFlight = [&]() {
363 if (preflightResult.parentBatchId)
364 {
365 return preflight(
366 registry,
367 view.rules(),
368 preflightResult.parentBatchId.value(),
369 preflightResult.tx,
370 preflightResult.flags,
371 preflightResult.j);
372 }
373
374 return preflight(
375 registry,
376 view.rules(),
377 preflightResult.tx,
378 preflightResult.flags,
379 preflightResult.j);
380 }();
381
382 ctx.emplace(
383 registry,
384 view,
385 secondFlight.ter,
386 secondFlight.tx,
387 secondFlight.flags,
388 secondFlight.parentBatchId,
389 secondFlight.j);
390 }
391 else
392 {
393 ctx.emplace(
394 registry,
395 view,
396 preflightResult.ter,
397 preflightResult.tx,
398 preflightResult.flags,
399 preflightResult.parentBatchId,
400 preflightResult.j);
401 }
402
403 try
404 {
405 if (!isTesSuccess(ctx->preflightResult))
406 return {*ctx, ctx->preflightResult};
407 return {*ctx, invokePreclaim(*ctx)};
408 }
409 catch (std::exception const& e)
410 {
411 JLOG(ctx->j.fatal()) << "apply (preclaim): " << e.what();
412 return {*ctx, tefEXCEPTION};
413 }
414}
415
416XRPAmount
417calculateBaseFee(ReadView const& view, STTx const& tx)
418{
419 return invokeCalculateBaseFee(view, tx);
420}
421
422XRPAmount
423calculateDefaultBaseFee(ReadView const& view, STTx const& tx)
424{
425 return Transactor::calculateBaseFee(view, tx);
426}
427
428ApplyResult
429doApply(PreclaimResult const& preclaimResult, ServiceRegistry& registry, OpenView& view)
430{
431 if (preclaimResult.view.seq() != view.seq())
432 {
433 // Logic error from the caller. Don't have enough
434 // info to recover.
435 return {tefEXCEPTION, false};
436 }
437 try
438 {
439 if (!preclaimResult.likelyToClaimFee)
440 return {preclaimResult.ter, false};
441 ApplyContext ctx(
442 registry,
443 view,
444 preclaimResult.parentBatchId,
445 preclaimResult.tx,
446 preclaimResult.ter,
447 calculateBaseFee(view, preclaimResult.tx),
448 preclaimResult.flags,
449 preclaimResult.j);
450 return invokeApply(ctx);
451 }
452 catch (std::exception const& e)
453 {
454 JLOG(preclaimResult.j.fatal()) << "apply: " << e.what();
455 return {tefEXCEPTION, false};
456 }
457}
458
459} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Stream fatal() const
Definition Journal.h:321
State information when applying a tx.
STTx const & tx
beast::Journal const journal
ApplyView & view()
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:45
Rules const & rules() const override
Returns the tx processing rules.
Definition OpenView.cpp:148
A view into a ledger.
Definition ReadView.h:31
virtual Rules const & rules() const =0
Returns the tx processing rules.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition ReadView.h:97
Rules controlling protocol behavior.
Definition Rules.h:33
TxType getTxnType() const
Definition STTx.h:188
A type that represents either a sequence value or a ticket value.
Definition SeqProxy.h:36
Service registry for dependency injection.
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition applySteps.h:38
TxConsequences(NotTEC pfResult)
std::uint32_t sequencesConsumed_
Number of sequences consumed.
Definition applySteps.h:61
Category
Describes how the transaction affects subsequent transactions.
Definition applySteps.h:42
@ Blocker
Affects the ability of subsequent transactions to claim a fee.
Definition applySteps.h:47
XRPAmount potentialSpend_
Does NOT include the fee.
Definition applySteps.h:57
bool isBlocker_
Describes how the transaction affects subsequent transactions.
Definition applySteps.h:53
std::uint32_t sequencesConsumed() const
Sequences consumed.
Definition applySteps.h:114
XRPAmount const & potentialSpend() const
Potential Spend.
Definition applySteps.h:100
SeqProxy seqProx_
SeqProxy of transaction.
Definition applySteps.h:59
XRPAmount fee_
Transaction fee.
Definition applySteps.h:55
T emplace(T... args)
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:48
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
PreflightResult preflight(ServiceRegistry &registry, Rules const &rules, STTx const &tx, ApplyFlags flags, beast::Journal j)
Gate a transaction based on static information.
TxType
Transaction type identifiers.
Definition TxFormats.h:41
PreclaimResult preclaim(PreflightResult const &preflightResult, ServiceRegistry &registry, OpenView const &view)
Gate a transaction based on static ledger information.
static TER invokePreclaim(PreclaimContext const &ctx)
@ tefEXCEPTION
Definition TER.h:162
void createGuards(Rules const &rules, std::optional< CurrentTransactionRulesGuard > &rulesGuard, std::optional< NumberMantissaScaleGuard > &mantissaScaleGuard)
Definition Rules.cpp:83
static std::pair< NotTEC, TxConsequences > invokePreflight(PreflightContext const &ctx)
static XRPAmount invokeCalculateBaseFee(ReadView const &view, STTx const &tx)
Calculates the base fee for a given transaction.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
std::unique_ptr< Transactor > makeTransactor(ApplyContext &ctx)
XRPAmount calculateDefaultBaseFee(ReadView const &view, STTx const &tx)
Return the minimum fee that an "ordinary" transaction would pay.
TxConsequences consequencesHelper(PreflightContext const &ctx)
ApplyFlags
Definition ApplyView.h:12
@ temUNKNOWN
Definition TER.h:110
bool isTesSuccess(TER x) noexcept
Definition TER.h:663
TERSubset< CanCvtToTER > TER
Definition TER.h:634
static ApplyResult invokeApply(ApplyContext &ctx)
XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Compute only the expected base fee for a transaction.
ApplyResult doApply(PreclaimResult const &preclaimResult, ServiceRegistry &registry, OpenView &view)
Apply a prechecked transaction to an OpenView.
BaseUInt< 256 > uint256
Definition base_uint.h:562
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
Describes the results of the preclaim check.
Definition applySteps.h:187
ReadView const & view
From the input - the ledger view.
Definition applySteps.h:190
std::optional< uint256 const > const parentBatchId
From the input - the batch identifier, if part of a batch.
Definition applySteps.h:194
TER const ter
Intermediate transaction result.
Definition applySteps.h:201
ApplyFlags const flags
From the input - the flags.
Definition applySteps.h:196
STTx const & tx
From the input - the transaction.
Definition applySteps.h:192
beast::Journal const j
From the input - the journal.
Definition applySteps.h:198
bool const likelyToClaimFee
Success flag - whether the transaction is likely to claim a fee.
Definition applySteps.h:205
State information when preflighting a tx.
Definition Transactor.h:18
Describes the results of the preflight check.
Definition applySteps.h:143
beast::Journal const j
From the input - the journal.
Definition applySteps.h:156
ApplyFlags const flags
From the input - the flags.
Definition applySteps.h:154
Rules const rules
From the input - the rules.
Definition applySteps.h:150
NotTEC const ter
Intermediate transaction result.
Definition applySteps.h:159
std::optional< uint256 const > const parentBatchId
From the input - the batch identifier, if part of a batch.
Definition applySteps.h:148
STTx const & tx
From the input - the transaction.
Definition applySteps.h:146
T what(T... args)