rippled
Loading...
Searching...
No Matches
Transactor.h
1#ifndef XRPL_APP_TX_TRANSACTOR_H_INCLUDED
2#define XRPL_APP_TX_TRANSACTOR_H_INCLUDED
3
4#include <xrpld/app/tx/applySteps.h>
5#include <xrpld/app/tx/detail/ApplyContext.h>
6
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/beast/utility/WrappedSink.h>
9#include <xrpl/protocol/Permissions.h>
10#include <xrpl/protocol/XRPAmount.h>
11
12namespace xrpl {
13
16{
17public:
19 STTx const& tx;
20 Rules const rules;
24
26 Application& app_,
27 STTx const& tx_,
28 uint256 parentBatchId_,
29 Rules const& rules_,
30 ApplyFlags flags_,
32 : app(app_)
33 , tx(tx_)
34 , rules(rules_)
35 , flags(flags_)
36 , parentBatchId(parentBatchId_)
37 , j(j_)
38 {
39 XRPL_ASSERT(
40 (flags_ & tapBATCH) == tapBATCH, "Batch apply flag should be set");
41 }
42
44 Application& app_,
45 STTx const& tx_,
46 Rules const& rules_,
47 ApplyFlags flags_,
49 : app(app_), tx(tx_), rules(rules_), flags(flags_), j(j_)
50 {
51 XRPL_ASSERT(
52 (flags_ & tapBATCH) == 0, "Batch apply flag should not be set");
53 }
54
55 PreflightContext&
56 operator=(PreflightContext const&) = delete;
57};
58
61{
62public:
64 ReadView const& view;
67 STTx const& tx;
70
72 Application& app_,
73 ReadView const& view_,
74 TER preflightResult_,
75 STTx const& tx_,
76 ApplyFlags flags_,
77 std::optional<uint256> parentBatchId_,
79 : app(app_)
80 , view(view_)
81 , preflightResult(preflightResult_)
82 , flags(flags_)
83 , tx(tx_)
84 , parentBatchId(parentBatchId_)
85 , j(j_)
86 {
87 XRPL_ASSERT(
88 parentBatchId.has_value() == ((flags_ & tapBATCH) == tapBATCH),
89 "Parent Batch ID should be set if batch apply flag is set");
90 }
91
93 Application& app_,
94 ReadView const& view_,
95 TER preflightResult_,
96 STTx const& tx_,
97 ApplyFlags flags_,
100 app_,
101 view_,
102 preflightResult_,
103 tx_,
104 flags_,
105 std::nullopt,
106 j_)
107 {
108 XRPL_ASSERT(
109 (flags_ & tapBATCH) == 0, "Batch apply flag should not be set");
110 }
111
112 PreclaimContext&
113 operator=(PreclaimContext const&) = delete;
114};
115
116class TxConsequences;
117struct PreflightResult;
118// Needed for preflight specialization
119class Change;
120
122{
123protected:
127
129 XRPAmount mPriorBalance; // Balance before fees.
130 XRPAmount mSourceBalance; // Balance after fees.
131
132 virtual ~Transactor() = default;
133 Transactor(Transactor const&) = delete;
135 operator=(Transactor const&) = delete;
136
137public:
141 operator()();
142
143 ApplyView&
145 {
146 return ctx_.view();
147 }
148
149 ApplyView const&
150 view() const
151 {
152 return ctx_.view();
153 }
154
156 /*
157 These static functions are called from invoke_preclaim<Tx>
158 using name hiding to accomplish compile-time polymorphism,
159 so derived classes can override for different or extra
160 functionality. Use with care, as these are not really
161 virtual and so don't have the compiler-time protection that
162 comes with it.
163 */
164
165 static NotTEC
166 checkSeqProxy(ReadView const& view, STTx const& tx, beast::Journal j);
167
168 static NotTEC
170
171 static TER
172 checkFee(PreclaimContext const& ctx, XRPAmount baseFee);
173
174 static NotTEC
175 checkSign(PreclaimContext const& ctx);
176
177 static NotTEC
179
180 // Returns the fee in fee units, not scaled for load.
181 static XRPAmount
182 calculateBaseFee(ReadView const& view, STTx const& tx);
183
184 /* Do NOT define an invokePreflight function in a derived class.
185 Instead, define:
186
187 // Optional if the transaction is gated on an amendment that
188 // isn't specified in transactions.macro
189 static bool
190 checkExtraFeatures(PreflightContext const& ctx);
191
192 // Optional if the transaction uses any flags other than tfUniversal
193 static std::uint32_t
194 getFlagsMask(PreflightContext const& ctx);
195
196 // Required, even if it just returns tesSUCCESS.
197 static NotTEC
198 preflight(PreflightContext const& ctx);
199
200 // Optional, rarely needed, if the transaction does any expensive
201 // checks after the signature is verified.
202 static NotTEC preflightSigValidated(PreflightContext const& ctx);
203
204 * Do not try to call preflight1 or preflight2 directly.
205 * Do not check whether relevant amendments are enabled in preflight.
206 Instead, define checkExtraFeatures.
207 * Do not check flags in preflight. Instead, define getFlagsMask.
208 */
209 template <class T>
210 static NotTEC
212
213 static TER
215 {
216 // Most transactors do nothing
217 // after checkSeq/Fee/Sign.
218 return tesSUCCESS;
219 }
220
221 static NotTEC
222 checkPermission(ReadView const& view, STTx const& tx);
224
225 // Interface used by DeleteAccount
226 static TER
229 AccountID const& account,
230 uint256 const& ticketIndex,
232
233protected:
234 TER
235 apply();
236
237 explicit Transactor(ApplyContext& ctx);
238
239 virtual void
240 preCompute();
241
242 virtual TER
243 doApply() = 0;
244
254 static XRPAmount
256 Application& app,
257 XRPAmount baseFee,
258 Fees const& fees,
259 ApplyFlags flags);
260
261 // Returns the fee in fee units, not scaled for load.
262 static XRPAmount
263 calculateOwnerReserveFee(ReadView const& view, STTx const& tx);
264
265 static NotTEC
266 checkSign(
267 ReadView const& view,
268 ApplyFlags flags,
269 std::optional<uint256 const> const& parentBatchId,
270 AccountID const& idAccount,
271 STObject const& sigObject,
272 beast::Journal const j);
273
274 // Base class always returns true
275 static bool
277
278 // Base class always returns tfUniversalMask
279 static std::uint32_t
280 getFlagsMask(PreflightContext const& ctx);
281
282 // Base class always returns tesSUCCESS
283 static NotTEC
285
286 static bool
287 validDataLength(std::optional<Slice> const& slice, std::size_t maxLength);
288
289 template <class T>
290 static bool
291 validNumericRange(std::optional<T> value, T max, T min = T{});
292
293 template <class T, class Unit>
294 static bool
296 std::optional<T> value,
299
301 template <class T>
302 static bool
303 validNumericMinimum(std::optional<T> value, T min = T{});
304
306 template <class T, class Unit>
307 static bool
309 std::optional<T> value,
310 unit::ValueUnit<Unit, T> min = unit::ValueUnit<Unit, T>{});
311
312private:
314 reset(XRPAmount fee);
315
316 TER
317 consumeSeqProxy(SLE::pointer const& sleAccount);
318 TER
319 payFee();
320 static NotTEC
322 ReadView const& view,
323 AccountID const& idSigner,
324 AccountID const& idAccount,
326 beast::Journal const j);
327 static NotTEC
329 ReadView const& view,
330 ApplyFlags flags,
331 AccountID const& id,
332 STObject const& sigObject,
333 beast::Journal const j);
334
335 void trapTransaction(uint256) const;
336
344 static NotTEC
345 preflight1(PreflightContext const& ctx, std::uint32_t flagMask);
346
352 static NotTEC
353 preflight2(PreflightContext const& ctx);
354};
355
356inline bool
358{
359 return true;
360}
361
363NotTEC
364preflight0(PreflightContext const& ctx, std::uint32_t flagMask);
365
366namespace detail {
367
372NotTEC
374
381 ApplyFlags flags,
382 STObject const& sigObject,
384} // namespace detail
385
386// Defined in Change.cpp
387template <>
389Transactor::invokePreflight<Change>(PreflightContext const& ctx);
390
391template <class T>
392NotTEC
394{
395 // Using this lookup does NOT require checking the fixDelegateV1_1. The data
396 // exists regardless of whether it is enabled.
397 auto const feature =
399
400 if (feature && !ctx.rules.enabled(*feature))
401 return temDISABLED;
402
403 if (!T::checkExtraFeatures(ctx))
404 return temDISABLED;
405
406 if (auto const ret = preflight1(ctx, T::getFlagsMask(ctx)))
407 return ret;
408
409 if (auto const ret = T::preflight(ctx))
410 return ret;
411
412 if (auto const ret = preflight2(ctx))
413 return ret;
414
415 return T::preflightSigValidated(ctx);
416}
417
418template <class T>
419bool
421{
422 if (!value)
423 return true;
424 return value >= min && value <= max;
425}
426
427template <class T, class Unit>
428bool
430 std::optional<T> value,
433{
434 return validNumericRange(value, max.value(), min.value());
435}
436
437template <class T>
438bool
440{
441 if (!value)
442 return true;
443 return value >= min;
444}
445
446template <class T, class Unit>
447bool
449 std::optional<T> value,
451{
452 return validNumericMinimum(value, min.value());
453}
454
455} // namespace xrpl
456
457#endif
A generic endpoint for log messages.
Definition Journal.h:41
static Sink & getNullSink()
Returns a Sink which does nothing.
Wraps a Journal::Sink to prefix its output with a string.
Definition WrappedSink.h:15
State information when applying a tx.
ApplyView & view()
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:124
std::optional< std::reference_wrapper< uint256 const > > const getTxFeature(TxType txType) const
static Permission const & getInstance()
A view into a ledger.
Definition ReadView.h:32
Rules controlling protocol behavior.
Definition Rules.h:19
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:111
std::shared_ptr< STLedgerEntry > pointer
TxType getTxnType() const
Definition STTx.h:187
static NotTEC preflight1(PreflightContext const &ctx, std::uint32_t flagMask)
Performs early sanity checks on the account and fee fields.
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
TER consumeSeqProxy(SLE::pointer const &sleAccount)
AccountID const account_
Definition Transactor.h:128
void trapTransaction(uint256) const
static TER checkFee(PreclaimContext const &ctx, XRPAmount baseFee)
static NotTEC invokePreflight(PreflightContext const &ctx)
Definition Transactor.h:393
Transactor & operator=(Transactor const &)=delete
beast::WrappedSink sink_
Definition Transactor.h:125
static NotTEC checkSign(PreclaimContext const &ctx)
static XRPAmount calculateOwnerReserveFee(ReadView const &view, STTx const &tx)
ApplyResult operator()()
Process the transaction.
static NotTEC checkPermission(ReadView const &view, STTx const &tx)
static XRPAmount minimumFee(Application &app, XRPAmount baseFee, Fees const &fees, ApplyFlags flags)
Compute the minimum fee required to process a transaction with a given baseFee based on the current s...
static bool validNumericMinimum(std::optional< T > value, T min=T{})
Minimum will usually be zero.
Definition Transactor.h:439
static NotTEC preflightSigValidated(PreflightContext const &ctx)
static NotTEC checkBatchSign(PreclaimContext const &ctx)
static NotTEC checkSeqProxy(ReadView const &view, STTx const &tx, beast::Journal j)
beast::Journal const j_
Definition Transactor.h:126
static TER preclaim(PreclaimContext const &ctx)
Definition Transactor.h:214
virtual ~Transactor()=default
virtual TER doApply()=0
static NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
static bool checkExtraFeatures(PreflightContext const &ctx)
Definition Transactor.h:357
ApplyView & view()
Definition Transactor.h:144
static NotTEC checkSingleSign(ReadView const &view, AccountID const &idSigner, AccountID const &idAccount, std::shared_ptr< SLE const > sleAccount, beast::Journal const j)
Transactor(Transactor const &)=delete
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
XRPAmount mSourceBalance
Definition Transactor.h:130
static NotTEC checkPriorTxAndLastLedger(PreclaimContext const &ctx)
XRPAmount mPriorBalance
Definition Transactor.h:129
static NotTEC checkMultiSign(ReadView const &view, ApplyFlags flags, AccountID const &id, STObject const &sigObject, beast::Journal const j)
static bool validDataLength(std::optional< Slice > const &slice, std::size_t maxLength)
virtual void preCompute()
ApplyContext & ctx_
Definition Transactor.h:124
ApplyView const & view() const
Definition Transactor.h:150
std::pair< TER, XRPAmount > reset(XRPAmount fee)
Reset the context, discarding any changes made and adjust the fee.
static bool validNumericRange(std::optional< T > value, T max, T min=T{})
Definition Transactor.h:420
static TER ticketDelete(ApplyView &view, AccountID const &account, uint256 const &ticketIndex, beast::Journal j)
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition applySteps.h:39
STL namespace.
NotTEC preflightCheckSigningKey(STObject const &sigObject, beast::Journal j)
Checks the validity of the transactor signing key.
std::optional< NotTEC > preflightCheckSimulateKeys(ApplyFlags flags, STObject const &sigObject, beast::Journal j)
Checks the special signing key state needed for simulation.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:29
base_uint< 256 > uint256
Definition base_uint.h:539
TERSubset< CanCvtToTER > TER
Definition TER.h:630
ApplyFlags
Definition ApplyView.h:11
@ tapBATCH
Definition ApplyView.h:26
@ temDISABLED
Definition TER.h:95
NotTEC preflight0(PreflightContext const &ctx, std::uint32_t flagMask)
Performs early sanity checks on the txid.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:590
@ tesSUCCESS
Definition TER.h:226
Reflects the fee settings for a particular ledger.
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
PreclaimContext(Application &app_, ReadView const &view_, TER preflightResult_, STTx const &tx_, ApplyFlags flags_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:92
Application & app
Definition Transactor.h:63
PreclaimContext & operator=(PreclaimContext const &)=delete
beast::Journal const j
Definition Transactor.h:69
PreclaimContext(Application &app_, ReadView const &view_, TER preflightResult_, STTx const &tx_, ApplyFlags flags_, std::optional< uint256 > parentBatchId_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:71
std::optional< uint256 const > const parentBatchId
Definition Transactor.h:68
State information when preflighting a tx.
Definition Transactor.h:16
PreflightContext(Application &app_, STTx const &tx_, uint256 parentBatchId_, Rules const &rules_, ApplyFlags flags_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:25
PreflightContext(Application &app_, STTx const &tx_, Rules const &rules_, ApplyFlags flags_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:43
beast::Journal const j
Definition Transactor.h:23
Application & app
Definition Transactor.h:18
PreflightContext & operator=(PreflightContext const &)=delete
std::optional< uint256 const > parentBatchId
Definition Transactor.h:22
Describes the results of the preflight check.
Definition applySteps.h:144