rippled
Loading...
Searching...
No Matches
applySteps.cpp
1#include <xrpl/tx/applySteps.h>
2#pragma push_macro("TRANSACTION")
3#undef TRANSACTION
4
5// Do nothing
6#define TRANSACTION(...)
7#define TRANSACTION_INCLUDE 1
8
9#include <xrpl/protocol/detail/transactions.macro>
10
11#undef TRANSACTION
12#pragma pop_macro("TRANSACTION")
13
14// DO NOT INCLUDE TRANSACTOR HEADER FILES HERE.
15// See the instructions at the top of transactions.macro instead.
16
17#include <xrpl/core/ServiceRegistry.h>
18#include <xrpl/protocol/TxFormats.h>
19
20#include <stdexcept>
21
22namespace xrpl {
23
24namespace {
25
26struct UnknownTxnType : std::exception
27{
28 TxType txnType;
29 UnknownTxnType(TxType t) : txnType{t}
30 {
31 }
32};
33
34// Call a lambda with the concrete transaction type as a template parameter
35// throw an "UnknownTxnType" exception on error
36template <class F>
37auto
38with_txn_type(Rules const& rules, TxType txnType, F&& f)
39{
40 // These global updates really should have been for every Transaction
41 // step: preflight, preclaim, calculateBaseFee, and doApply. Unfortunately,
42 // they were only included in doApply (via Transactor::operator()). That may
43 // have been sufficient when the changes were only related to operations
44 // that mutated data, but some features will now change how they read data,
45 // so these need to be more global.
46 //
47 // To prevent unintentional side effects on existing checks, they will be
48 // set for every operation only once SingleAssetVault (or later
49 // LendingProtocol) are enabled.
50 //
51 // See also Transactor::operator().
52 //
53 std::optional<NumberSO> stNumberSO;
56 if (rules.enabled(featureSingleAssetVault) || rules.enabled(featureLendingProtocol))
57 {
58 // raii classes for the current ledger rules.
59 // fixUniversalNumber predates the rulesGuard and should be replaced.
60 stNumberSO.emplace(rules.enabled(fixUniversalNumber));
61 rulesGuard.emplace(rules);
62 }
63 else
64 {
65 // Without those features enabled, always use the old number rules.
66 mantissaScaleGuard.emplace(MantissaRange::small);
67 }
68
69 switch (txnType)
70 {
71#pragma push_macro("TRANSACTION")
72#undef TRANSACTION
73
74#define TRANSACTION(tag, value, name, ...) \
75 case tag: \
76 return f.template operator()<name>();
77
78#include <xrpl/protocol/detail/transactions.macro>
79
80#undef TRANSACTION
81#pragma pop_macro("TRANSACTION")
82 default:
83 throw UnknownTxnType(txnType);
84 }
85}
86} // namespace
87
88// Templates so preflight does the right thing with T::ConsequencesFactory.
89//
90// This could be done more easily using if constexpr, but Visual Studio
91// 2017 doesn't handle if constexpr correctly. So once we're no longer
92// building with Visual Studio 2017 we can consider replacing the four
93// templates with a single template function that uses if constexpr.
94//
95// For Transactor::Normal
96//
97
98// Current formatter for rippled is based on clang-10, which does not handle `requires` clauses
99template <class T>
100 requires(T::ConsequencesFactory == Transactor::Normal)
101TxConsequences
103{
104 return TxConsequences(ctx.tx);
105};
106
107// For Transactor::Blocker
108template <class T>
109 requires(T::ConsequencesFactory == Transactor::Blocker)
110TxConsequences
115
116// For Transactor::Custom
117template <class T>
118 requires(T::ConsequencesFactory == Transactor::Custom)
119TxConsequences
121{
122 return T::makeTxConsequences(ctx);
123};
124
127{
128 try
129 {
130 return with_txn_type(ctx.rules, ctx.tx.getTxnType(), [&]<typename T>() {
131 auto const tec = Transactor::invokePreflight<T>(ctx);
132 return std::make_pair(
133 tec, isTesSuccess(tec) ? consequences_helper<T>(ctx) : TxConsequences{tec});
134 });
135 }
136 catch (UnknownTxnType const& e)
137 {
138 // Should never happen
139 // LCOV_EXCL_START
140 JLOG(ctx.j.fatal()) << "Unknown transaction type in preflight: " << e.txnType;
141 UNREACHABLE("xrpl::invoke_preflight : unknown transaction type");
142 return {temUNKNOWN, TxConsequences{temUNKNOWN}};
143 // LCOV_EXCL_STOP
144 }
145}
146
147static TER
149{
150 try
151 {
152 // use name hiding to accomplish compile-time polymorphism of static
153 // class functions for Transactor and derived classes.
154 return with_txn_type(ctx.view.rules(), ctx.tx.getTxnType(), [&]<typename T>() -> TER {
155 // preclaim functionality is divided into two sections:
156 // 1. Up to and including the signature check: returns NotTEC.
157 // All transaction checks before and including checkSign
158 // MUST return NotTEC, or something more restrictive.
159 // Allowing tec results in these steps risks theft or
160 // destruction of funds, as a fee will be charged before the
161 // signature is checked.
162 // 2. After the signature check: returns TER.
163
164 // If the transactor requires a valid account and the
165 // transaction doesn't list one, preflight will have already
166 // a flagged a failure.
167 auto const id = ctx.tx.getAccountID(sfAccount);
168
169 if (id != beast::zero)
170 {
171 if (NotTEC const preSigResult = [&]() -> NotTEC {
172 if (NotTEC const result = T::checkSeqProxy(ctx.view, ctx.tx, ctx.j))
173 return result;
174
175 if (NotTEC const result = T::checkPriorTxAndLastLedger(ctx))
176 return result;
177
178 if (NotTEC const result = T::checkPermission(ctx.view, ctx.tx))
179 return result;
180
181 if (NotTEC const result = T::checkSign(ctx))
182 return result;
183
184 return tesSUCCESS;
185 }())
186 return preSigResult;
187
188 if (TER const result = T::checkFee(ctx, calculateBaseFee(ctx.view, ctx.tx)))
189 return result;
190 }
191
192 return T::preclaim(ctx);
193 });
194 }
195 catch (UnknownTxnType const& e)
196 {
197 // Should never happen
198 // LCOV_EXCL_START
199 JLOG(ctx.j.fatal()) << "Unknown transaction type in preclaim: " << e.txnType;
200 UNREACHABLE("xrpl::invoke_preclaim : unknown transaction type");
201 return temUNKNOWN;
202 // LCOV_EXCL_STOP
203 }
204}
205
222static XRPAmount
223invoke_calculateBaseFee(ReadView const& view, STTx const& tx)
224{
225 try
226 {
227 return with_txn_type(view.rules(), tx.getTxnType(), [&]<typename T>() {
228 return T::calculateBaseFee(view, tx);
229 });
230 }
231 catch (UnknownTxnType const& e)
232 {
233 // LCOV_EXCL_START
234 UNREACHABLE("xrpl::invoke_calculateBaseFee : unknown transaction type");
235 return XRPAmount{0};
236 // LCOV_EXCL_STOP
237 }
238}
239
240TxConsequences::TxConsequences(NotTEC pfResult)
241 : isBlocker_(false)
242 , fee_(beast::zero)
243 , potentialSpend_(beast::zero)
244 , seqProx_(SeqProxy::sequence(0))
245 , sequencesConsumed_(0)
246{
247 XRPL_ASSERT(
248 !isTesSuccess(pfResult), "xrpl::TxConsequences::TxConsequences : is not tesSUCCESS");
249}
250
252 : isBlocker_(false)
253 , fee_(tx[sfFee].native() && !tx[sfFee].negative() ? tx[sfFee].xrp() : beast::zero)
254 , potentialSpend_(beast::zero)
255 , seqProx_(tx.getSeqProxy())
256 , sequencesConsumed_(tx.getSeqProxy().isSeq() ? 1 : 0)
257{
258}
259
261{
262 isBlocker_ = (category == blocker);
263}
264
269
274
275static ApplyResult
277{
278 try
279 {
280 return with_txn_type(ctx.view().rules(), ctx.tx.getTxnType(), [&]<typename T>() {
281 T p(ctx);
282 return p();
283 });
284 }
285 catch (UnknownTxnType const& e)
286 {
287 // Should never happen
288 // LCOV_EXCL_START
289 JLOG(ctx.journal.fatal()) << "Unknown transaction type in apply: " << e.txnType;
290 UNREACHABLE("xrpl::invoke_apply : unknown transaction type");
291 return {temUNKNOWN, false};
292 // LCOV_EXCL_STOP
293 }
294}
295
296PreflightResult
298 ServiceRegistry& registry,
299 Rules const& rules,
300 STTx const& tx,
301 ApplyFlags flags,
303{
304 PreflightContext const pfCtx(registry, tx, rules, flags, j);
305 try
306 {
307 return {pfCtx, invoke_preflight(pfCtx)};
308 }
309 catch (std::exception const& e)
310 {
311 JLOG(j.fatal()) << "apply (preflight): " << e.what();
312 return {pfCtx, {tefEXCEPTION, TxConsequences{tx}}};
313 }
314}
315
316PreflightResult
318 ServiceRegistry& registry,
319 Rules const& rules,
320 uint256 const& parentBatchId,
321 STTx const& tx,
322 ApplyFlags flags,
324{
325 PreflightContext const pfCtx(registry, tx, parentBatchId, rules, flags, j);
326 try
327 {
328 return {pfCtx, invoke_preflight(pfCtx)};
329 }
330 catch (std::exception const& e)
331 {
332 JLOG(j.fatal()) << "apply (preflight): " << e.what();
333 return {pfCtx, {tefEXCEPTION, TxConsequences{tx}}};
334 }
335}
336
337PreclaimResult
338preclaim(PreflightResult const& preflightResult, ServiceRegistry& registry, OpenView const& view)
339{
341 if (preflightResult.rules != view.rules())
342 {
343 auto secondFlight = [&]() {
344 if (preflightResult.parentBatchId)
345 {
346 return preflight(
347 registry,
348 view.rules(),
349 preflightResult.parentBatchId.value(),
350 preflightResult.tx,
351 preflightResult.flags,
352 preflightResult.j);
353 }
354
355 return preflight(
356 registry,
357 view.rules(),
358 preflightResult.tx,
359 preflightResult.flags,
360 preflightResult.j);
361 }();
362
363 ctx.emplace(
364 registry,
365 view,
366 secondFlight.ter,
367 secondFlight.tx,
368 secondFlight.flags,
369 secondFlight.parentBatchId,
370 secondFlight.j);
371 }
372 else
373 {
374 ctx.emplace(
375 registry,
376 view,
377 preflightResult.ter,
378 preflightResult.tx,
379 preflightResult.flags,
380 preflightResult.parentBatchId,
381 preflightResult.j);
382 }
383
384 try
385 {
386 if (!isTesSuccess(ctx->preflightResult))
387 return {*ctx, ctx->preflightResult};
388 return {*ctx, invoke_preclaim(*ctx)};
389 }
390 catch (std::exception const& e)
391 {
392 JLOG(ctx->j.fatal()) << "apply (preclaim): " << e.what();
393 return {*ctx, tefEXCEPTION};
394 }
395}
396
397XRPAmount
398calculateBaseFee(ReadView const& view, STTx const& tx)
399{
400 return invoke_calculateBaseFee(view, tx);
401}
402
403XRPAmount
404calculateDefaultBaseFee(ReadView const& view, STTx const& tx)
405{
406 return Transactor::calculateBaseFee(view, tx);
407}
408
409ApplyResult
410doApply(PreclaimResult const& preclaimResult, ServiceRegistry& registry, OpenView& view)
411{
412 if (preclaimResult.view.seq() != view.seq())
413 {
414 // Logic error from the caller. Don't have enough
415 // info to recover.
416 return {tefEXCEPTION, false};
417 }
418 try
419 {
420 if (!preclaimResult.likelyToClaimFee)
421 return {preclaimResult.ter, false};
422 ApplyContext ctx(
423 registry,
424 view,
425 preclaimResult.parentBatchId,
426 preclaimResult.tx,
427 preclaimResult.ter,
428 calculateBaseFee(view, preclaimResult.tx),
429 preclaimResult.flags,
430 preclaimResult.j);
431 return invoke_apply(ctx);
432 }
433 catch (std::exception const& e)
434 {
435 JLOG(preclaimResult.j.fatal()) << "apply: " << e.what();
436 return {tefEXCEPTION, false};
437 }
438}
439
440} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream fatal() const
Definition Journal.h:325
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:131
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:18
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
T emplace(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
static XRPAmount invoke_calculateBaseFee(ReadView const &view, STTx const &tx)
Calculates the base fee for a given transaction.
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:39
static ApplyResult invoke_apply(ApplyContext &ctx)
PreclaimResult preclaim(PreflightResult const &preflightResult, ServiceRegistry &registry, OpenView const &view)
Gate a transaction based on static ledger information.
@ tefEXCEPTION
Definition TER.h:152
static std::pair< NotTEC, TxConsequences > invoke_preflight(PreflightContext const &ctx)
static TER invoke_preclaim(PreclaimContext const &ctx)
TERSubset< CanCvtToTER > TER
Definition TER.h:622
TxConsequences consequences_helper(PreflightContext const &ctx)
XRPAmount calculateDefaultBaseFee(ReadView const &view, STTx const &tx)
Return the minimum fee that an "ordinary" transaction would pay.
ApplyFlags
Definition ApplyView.h:10
@ temUNKNOWN
Definition TER.h:104
bool isTesSuccess(TER x) noexcept
Definition TER.h:651
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.
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:57
ReadView const & view
Definition Transactor.h:60
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:14
beast::Journal const j
Definition Transactor.h:21
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)