rippled
Loading...
Searching...
No Matches
applySteps.cpp
1#include <xrpld/app/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/protocol/TxFormats.h>
18
19#include <stdexcept>
20
21namespace ripple {
22
23namespace {
24
25struct UnknownTxnType : std::exception
26{
27 TxType txnType;
28 UnknownTxnType(TxType t) : txnType{t}
29 {
30 }
31};
32
33// Call a lambda with the concrete transaction type as a template parameter
34// throw an "UnknownTxnType" exception on error
35template <class F>
36auto
37with_txn_type(TxType txnType, F&& f)
38{
39 switch (txnType)
40 {
41#pragma push_macro("TRANSACTION")
42#undef TRANSACTION
43
44#define TRANSACTION(tag, value, name, ...) \
45 case tag: \
46 return f.template operator()<name>();
47
48#include <xrpl/protocol/detail/transactions.macro>
49
50#undef TRANSACTION
51#pragma pop_macro("TRANSACTION")
52 default:
53 throw UnknownTxnType(txnType);
54 }
55}
56} // namespace
57
58// Templates so preflight does the right thing with T::ConsequencesFactory.
59//
60// This could be done more easily using if constexpr, but Visual Studio
61// 2017 doesn't handle if constexpr correctly. So once we're no longer
62// building with Visual Studio 2017 we can consider replacing the four
63// templates with a single template function that uses if constexpr.
64//
65// For Transactor::Normal
66//
67
68// clang-format off
69// Current formatter for rippled is based on clang-10, which does not handle `requires` clauses
70template <class T>
71requires(T::ConsequencesFactory == Transactor::Normal)
72TxConsequences
74{
75 return TxConsequences(ctx.tx);
76};
77
78// For Transactor::Blocker
79template <class T>
80requires(T::ConsequencesFactory == Transactor::Blocker)
81TxConsequences
86
87// For Transactor::Custom
88template <class T>
89requires(T::ConsequencesFactory == Transactor::Custom)
90TxConsequences
92{
93 return T::makeTxConsequences(ctx);
94};
95// clang-format on
96
99{
100 try
101 {
102 return with_txn_type(ctx.tx.getTxnType(), [&]<typename T>() {
103 auto const tec = Transactor::invokePreflight<T>(ctx);
104 return std::make_pair(
105 tec,
106 isTesSuccess(tec) ? consequences_helper<T>(ctx)
107 : TxConsequences{tec});
108 });
109 }
110 catch (UnknownTxnType const& e)
111 {
112 // Should never happen
113 // LCOV_EXCL_START
114 JLOG(ctx.j.fatal())
115 << "Unknown transaction type in preflight: " << e.txnType;
116 UNREACHABLE("ripple::invoke_preflight : unknown transaction type");
117 return {temUNKNOWN, TxConsequences{temUNKNOWN}};
118 // LCOV_EXCL_STOP
119 }
120}
121
122static TER
124{
125 try
126 {
127 // use name hiding to accomplish compile-time polymorphism of static
128 // class functions for Transactor and derived classes.
129 return with_txn_type(ctx.tx.getTxnType(), [&]<typename T>() -> TER {
130 // preclaim functionality is divided into two sections:
131 // 1. Up to and including the signature check: returns NotTEC.
132 // All transaction checks before and including checkSign
133 // MUST return NotTEC, or something more restrictive.
134 // Allowing tec results in these steps risks theft or
135 // destruction of funds, as a fee will be charged before the
136 // signature is checked.
137 // 2. After the signature check: returns TER.
138
139 // If the transactor requires a valid account and the
140 // transaction doesn't list one, preflight will have already
141 // a flagged a failure.
142 auto const id = ctx.tx.getAccountID(sfAccount);
143
144 if (id != beast::zero)
145 {
146 if (NotTEC const preSigResult = [&]() -> NotTEC {
147 if (NotTEC const result =
148 T::checkSeqProxy(ctx.view, ctx.tx, ctx.j))
149 return result;
150
151 if (NotTEC const result =
152 T::checkPriorTxAndLastLedger(ctx))
153 return result;
154
155 if (NotTEC const result =
156 T::checkPermission(ctx.view, ctx.tx))
157 return result;
158
159 if (NotTEC const result = T::checkSign(ctx))
160 return result;
161
162 return tesSUCCESS;
163 }())
164 return preSigResult;
165
166 if (TER const result =
167 T::checkFee(ctx, calculateBaseFee(ctx.view, ctx.tx)))
168 return result;
169 }
170
171 return T::preclaim(ctx);
172 });
173 }
174 catch (UnknownTxnType const& e)
175 {
176 // Should never happen
177 // LCOV_EXCL_START
178 JLOG(ctx.j.fatal())
179 << "Unknown transaction type in preclaim: " << e.txnType;
180 UNREACHABLE("ripple::invoke_preclaim : unknown transaction type");
181 return temUNKNOWN;
182 // LCOV_EXCL_STOP
183 }
184}
185
202static XRPAmount
203invoke_calculateBaseFee(ReadView const& view, STTx const& tx)
204{
205 try
206 {
207 return with_txn_type(tx.getTxnType(), [&]<typename T>() {
208 return T::calculateBaseFee(view, tx);
209 });
210 }
211 catch (UnknownTxnType const& e)
212 {
213 // LCOV_EXCL_START
214 UNREACHABLE(
215 "ripple::invoke_calculateBaseFee : unknown transaction type");
216 return XRPAmount{0};
217 // LCOV_EXCL_STOP
218 }
219}
220
221TxConsequences::TxConsequences(NotTEC pfresult)
222 : isBlocker_(false)
223 , fee_(beast::zero)
224 , potentialSpend_(beast::zero)
225 , seqProx_(SeqProxy::sequence(0))
226 , sequencesConsumed_(0)
227{
228 XRPL_ASSERT(
229 !isTesSuccess(pfresult),
230 "ripple::TxConsequences::TxConsequences : is not tesSUCCESS");
231}
232
234 : isBlocker_(false)
235 , fee_(
236 tx[sfFee].native() && !tx[sfFee].negative() ? tx[sfFee].xrp()
237 : beast::zero)
238 , potentialSpend_(beast::zero)
239 , seqProx_(tx.getSeqProxy())
240 , sequencesConsumed_(tx.getSeqProxy().isSeq() ? 1 : 0)
241{
242}
243
245 : TxConsequences(tx)
246{
247 isBlocker_ = (category == blocker);
248}
249
251 : TxConsequences(tx)
252{
254}
255
261
262static ApplyResult
264{
265 try
266 {
267 return with_txn_type(ctx.tx.getTxnType(), [&]<typename T>() {
268 T p(ctx);
269 return p();
270 });
271 }
272 catch (UnknownTxnType const& e)
273 {
274 // Should never happen
275 // LCOV_EXCL_START
276 JLOG(ctx.journal.fatal())
277 << "Unknown transaction type in apply: " << e.txnType;
278 UNREACHABLE("ripple::invoke_apply : unknown transaction type");
279 return {temUNKNOWN, false};
280 // LCOV_EXCL_STOP
281 }
282}
283
284PreflightResult
286 Application& app,
287 Rules const& rules,
288 STTx const& tx,
289 ApplyFlags flags,
291{
292 PreflightContext const pfctx(app, tx, rules, flags, j);
293 try
294 {
295 return {pfctx, invoke_preflight(pfctx)};
296 }
297 catch (std::exception const& e)
298 {
299 JLOG(j.fatal()) << "apply (preflight): " << e.what();
300 return {pfctx, {tefEXCEPTION, TxConsequences{tx}}};
301 }
302}
303
304PreflightResult
306 Application& app,
307 Rules const& rules,
308 uint256 const& parentBatchId,
309 STTx const& tx,
310 ApplyFlags flags,
312{
313 PreflightContext const pfctx(app, tx, parentBatchId, rules, flags, j);
314 try
315 {
316 return {pfctx, invoke_preflight(pfctx)};
317 }
318 catch (std::exception const& e)
319 {
320 JLOG(j.fatal()) << "apply (preflight): " << e.what();
321 return {pfctx, {tefEXCEPTION, TxConsequences{tx}}};
322 }
323}
324
325PreclaimResult
327 PreflightResult const& preflightResult,
328 Application& app,
329 OpenView const& view)
330{
332 if (preflightResult.rules != view.rules())
333 {
334 auto secondFlight = [&]() {
335 if (preflightResult.parentBatchId)
336 return preflight(
337 app,
338 view.rules(),
339 preflightResult.parentBatchId.value(),
340 preflightResult.tx,
341 preflightResult.flags,
342 preflightResult.j);
343
344 return preflight(
345 app,
346 view.rules(),
347 preflightResult.tx,
348 preflightResult.flags,
349 preflightResult.j);
350 }();
351
352 ctx.emplace(
353 app,
354 view,
355 secondFlight.ter,
356 secondFlight.tx,
357 secondFlight.flags,
358 secondFlight.parentBatchId,
359 secondFlight.j);
360 }
361 else
362 {
363 ctx.emplace(
364 app,
365 view,
366 preflightResult.ter,
367 preflightResult.tx,
368 preflightResult.flags,
369 preflightResult.parentBatchId,
370 preflightResult.j);
371 }
372
373 try
374 {
375 if (ctx->preflightResult != tesSUCCESS)
376 return {*ctx, ctx->preflightResult};
377 return {*ctx, invoke_preclaim(*ctx)};
378 }
379 catch (std::exception const& e)
380 {
381 JLOG(ctx->j.fatal()) << "apply (preclaim): " << e.what();
382 return {*ctx, tefEXCEPTION};
383 }
384}
385
386XRPAmount
387calculateBaseFee(ReadView const& view, STTx const& tx)
388{
389 return invoke_calculateBaseFee(view, tx);
390}
391
392XRPAmount
393calculateDefaultBaseFee(ReadView const& view, STTx const& tx)
394{
395 return Transactor::calculateBaseFee(view, tx);
396}
397
398ApplyResult
399doApply(PreclaimResult const& preclaimResult, Application& app, OpenView& view)
400{
401 if (preclaimResult.view.seq() != view.seq())
402 {
403 // Logic error from the caller. Don't have enough
404 // info to recover.
405 return {tefEXCEPTION, false};
406 }
407 try
408 {
409 if (!preclaimResult.likelyToClaimFee)
410 return {preclaimResult.ter, false};
411 ApplyContext ctx(
412 app,
413 view,
414 preclaimResult.parentBatchId,
415 preclaimResult.tx,
416 preclaimResult.ter,
417 calculateBaseFee(view, preclaimResult.tx),
418 preclaimResult.flags,
419 preclaimResult.j);
420 return invoke_apply(ctx);
421 }
422 catch (std::exception const& e)
423 {
424 JLOG(preclaimResult.j.fatal()) << "apply: " << e.what();
425 return {tefEXCEPTION, false};
426 }
427}
428
429} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:41
Stream fatal() const
Definition Journal.h:333
State information when applying a tx.
beast::Journal const journal
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:46
Rules const & rules() const override
Returns the tx processing rules.
Definition OpenView.cpp:131
A view into a ledger.
Definition ReadView.h:32
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition ReadView.h:99
Rules controlling protocol behavior.
Definition Rules.h:19
TxType getTxnType() const
Definition STTx.h:209
A type that represents either a sequence value or a ticket value.
Definition SeqProxy.h:37
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:39
std::uint32_t sequencesConsumed() const
Sequences consumed.
Definition applySteps.h:115
TxConsequences(NotTEC pfresult)
std::uint32_t sequencesConsumed_
Number of sequences consumed.
Definition applySteps.h:62
XRPAmount const & potentialSpend() const
Potential Spend.
Definition applySteps.h:101
bool isBlocker_
Describes how the transaction affects subsequent transactions.
Definition applySteps.h:54
Category
Describes how the transaction affects subsequent transactions.
Definition applySteps.h:43
@ blocker
Affects the ability of subsequent transactions to claim a fee.
Definition applySteps.h:48
XRPAmount potentialSpend_
Does NOT include the fee.
Definition applySteps.h:58
T emplace(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
PreflightResult preflight(Application &app, Rules const &rules, STTx const &tx, ApplyFlags flags, beast::Journal j)
Gate a transaction based on static information.
ApplyResult doApply(PreclaimResult const &preclaimResult, Application &app, OpenView &view)
Apply a prechecked transaction to an OpenView.
TxType
Transaction type identifiers.
Definition TxFormats.h:38
XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Compute only the expected base fee for a transaction.
static XRPAmount invoke_calculateBaseFee(ReadView const &view, STTx const &tx)
Calculates the base fee for a given transaction.
PreclaimResult preclaim(PreflightResult const &preflightResult, Application &app, OpenView const &view)
Gate a transaction based on static ledger information.
static std::pair< NotTEC, TxConsequences > invoke_preflight(PreflightContext const &ctx)
static TER invoke_preclaim(PreclaimContext const &ctx)
@ tefEXCEPTION
Definition TER.h:153
@ tesSUCCESS
Definition TER.h:226
bool isTesSuccess(TER x) noexcept
Definition TER.h:659
static ApplyResult invoke_apply(ApplyContext &ctx)
TERSubset< CanCvtToTER > TER
Definition TER.h:630
TxConsequences consequences_helper(PreflightContext const &ctx)
XRPAmount calculateDefaultBaseFee(ReadView const &view, STTx const &tx)
Return the minimum fee that an "ordinary" transaction would pay.
@ temUNKNOWN
Definition TER.h:105
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
Describes the results of the preclaim check.
Definition applySteps.h:190
ApplyFlags const flags
From the input - the flags.
Definition applySteps.h:199
TER const ter
Intermediate transaction result.
Definition applySteps.h:204
bool const likelyToClaimFee
Success flag - whether the transaction is likely to claim a fee.
Definition applySteps.h:208
ReadView const & view
From the input - the ledger view.
Definition applySteps.h:193
beast::Journal const j
From the input - the journal.
Definition applySteps.h:201
std::optional< uint256 const > const parentBatchId
From the input - the batch identifier, if part of a batch.
Definition applySteps.h:197
STTx const & tx
From the input - the transaction.
Definition applySteps.h:195
State information when preflighting a tx.
Definition Transactor.h:16
beast::Journal const j
Definition Transactor.h:23
Describes the results of the preflight check.
Definition applySteps.h:144
Rules const rules
From the input - the rules.
Definition applySteps.h:151
ApplyFlags const flags
From the input - the flags.
Definition applySteps.h:155
beast::Journal const j
From the input - the journal.
Definition applySteps.h:157
NotTEC const ter
Intermediate transaction result.
Definition applySteps.h:160
std::optional< uint256 const > const parentBatchId
From the input - the batch identifier, if part of a batch.
Definition applySteps.h:149
STTx const & tx
From the input - the transaction.
Definition applySteps.h:147
T what(T... args)