rippled
Loading...
Searching...
No Matches
Transactor.h
1#pragma once
2
3#include <xrpld/app/tx/applySteps.h>
4#include <xrpld/app/tx/detail/ApplyContext.h>
5
6#include <xrpl/beast/utility/Journal.h>
7#include <xrpl/beast/utility/WrappedSink.h>
8#include <xrpl/protocol/Permissions.h>
9#include <xrpl/protocol/XRPAmount.h>
10
11namespace xrpl {
12
15{
16public:
18 STTx const& tx;
19 Rules const rules;
23
25 Application& app_,
26 STTx const& tx_,
27 uint256 parentBatchId_,
28 Rules const& rules_,
29 ApplyFlags flags_,
31 : app(app_), tx(tx_), rules(rules_), flags(flags_), parentBatchId(parentBatchId_), j(j_)
32 {
33 XRPL_ASSERT((flags_ & tapBATCH) == tapBATCH, "Batch apply flag should be set");
34 }
35
37 Application& app_,
38 STTx const& tx_,
39 Rules const& rules_,
40 ApplyFlags flags_,
42 : app(app_), tx(tx_), rules(rules_), flags(flags_), j(j_)
43 {
44 XRPL_ASSERT((flags_ & tapBATCH) == 0, "Batch apply flag should not be set");
45 }
46
47 PreflightContext&
48 operator=(PreflightContext const&) = delete;
49};
50
53{
54public:
56 ReadView const& view;
59 STTx const& tx;
62
64 Application& app_,
65 ReadView const& view_,
66 TER preflightResult_,
67 STTx const& tx_,
68 ApplyFlags flags_,
69 std::optional<uint256> parentBatchId_,
71 : app(app_)
72 , view(view_)
73 , preflightResult(preflightResult_)
74 , flags(flags_)
75 , tx(tx_)
76 , parentBatchId(parentBatchId_)
77 , j(j_)
78 {
79 XRPL_ASSERT(
80 parentBatchId.has_value() == ((flags_ & tapBATCH) == tapBATCH),
81 "Parent Batch ID should be set if batch apply flag is set");
82 }
83
85 Application& app_,
86 ReadView const& view_,
87 TER preflightResult_,
88 STTx const& tx_,
89 ApplyFlags flags_,
91 : PreclaimContext(app_, view_, preflightResult_, tx_, flags_, std::nullopt, j_)
92 {
93 XRPL_ASSERT((flags_ & tapBATCH) == 0, "Batch apply flag should not be set");
94 }
95
96 PreclaimContext&
97 operator=(PreclaimContext const&) = delete;
98};
99
100class TxConsequences;
101struct PreflightResult;
102// Needed for preflight specialization
103class Change;
104
106{
107protected:
111
113 XRPAmount mPriorBalance; // Balance before fees.
114 XRPAmount mSourceBalance; // Balance after fees.
115
116 virtual ~Transactor() = default;
117 Transactor(Transactor const&) = delete;
119 operator=(Transactor const&) = delete;
120
121public:
125 operator()();
126
127 ApplyView&
129 {
130 return ctx_.view();
131 }
132
133 ApplyView const&
134 view() const
135 {
136 return ctx_.view();
137 }
138
140 /*
141 These static functions are called from invoke_preclaim<Tx>
142 using name hiding to accomplish compile-time polymorphism,
143 so derived classes can override for different or extra
144 functionality. Use with care, as these are not really
145 virtual and so don't have the compiler-time protection that
146 comes with it.
147 */
148
149 static NotTEC
150 checkSeqProxy(ReadView const& view, STTx const& tx, beast::Journal j);
151
152 static NotTEC
154
155 static TER
156 checkFee(PreclaimContext const& ctx, XRPAmount baseFee);
157
158 static NotTEC
159 checkSign(PreclaimContext const& ctx);
160
161 static NotTEC
163
164 // Returns the fee in fee units, not scaled for load.
165 static XRPAmount
166 calculateBaseFee(ReadView const& view, STTx const& tx);
167
168 /* Do NOT define an invokePreflight function in a derived class.
169 Instead, define:
170
171 // Optional if the transaction is gated on an amendment that
172 // isn't specified in transactions.macro
173 static bool
174 checkExtraFeatures(PreflightContext const& ctx);
175
176 // Optional if the transaction uses any flags other than tfUniversal
177 static std::uint32_t
178 getFlagsMask(PreflightContext const& ctx);
179
180 // Required, even if it just returns tesSUCCESS.
181 static NotTEC
182 preflight(PreflightContext const& ctx);
183
184 // Optional, rarely needed, if the transaction does any expensive
185 // checks after the signature is verified.
186 static NotTEC preflightSigValidated(PreflightContext const& ctx);
187
188 * Do not try to call preflight1 or preflight2 directly.
189 * Do not check whether relevant amendments are enabled in preflight.
190 Instead, define checkExtraFeatures.
191 * Do not check flags in preflight. Instead, define getFlagsMask.
192 */
193 template <class T>
194 static NotTEC
196
197 static TER
199 {
200 // Most transactors do nothing
201 // after checkSeq/Fee/Sign.
202 return tesSUCCESS;
203 }
204
205 static NotTEC
206 checkPermission(ReadView const& view, STTx const& tx);
208
209 // Interface used by DeleteAccount
210 static TER
211 ticketDelete(ApplyView& view, AccountID const& account, uint256 const& ticketIndex, beast::Journal j);
212
213protected:
214 TER
215 apply();
216
217 explicit Transactor(ApplyContext& ctx);
218
219 virtual void
220 preCompute();
221
222 virtual TER
223 doApply() = 0;
224
234 static XRPAmount
235 minimumFee(Application& app, XRPAmount baseFee, Fees const& fees, ApplyFlags flags);
236
237 // Returns the fee in fee units, not scaled for load.
238 static XRPAmount
239 calculateOwnerReserveFee(ReadView const& view, STTx const& tx);
240
241 static NotTEC
242 checkSign(
243 ReadView const& view,
244 ApplyFlags flags,
245 std::optional<uint256 const> const& parentBatchId,
246 AccountID const& idAccount,
247 STObject const& sigObject,
248 beast::Journal const j);
249
250 // Base class always returns true
251 static bool
253
254 // Base class always returns tfUniversalMask
255 static std::uint32_t
256 getFlagsMask(PreflightContext const& ctx);
257
258 // Base class always returns tesSUCCESS
259 static NotTEC
261
262 static bool
263 validDataLength(std::optional<Slice> const& slice, std::size_t maxLength);
264
265 template <class T>
266 static bool
267 validNumericRange(std::optional<T> value, T max, T min = T{});
268
269 template <class T, class Unit>
270 static bool
272 std::optional<T> value,
275
277 template <class T>
278 static bool
279 validNumericMinimum(std::optional<T> value, T min = T{});
280
282 template <class T, class Unit>
283 static bool
284 validNumericMinimum(std::optional<T> value, unit::ValueUnit<Unit, T> min = unit::ValueUnit<Unit, T>{});
285
286private:
288 reset(XRPAmount fee);
289
290 TER
291 consumeSeqProxy(SLE::pointer const& sleAccount);
292 TER
293 payFee();
294 static NotTEC
296 ReadView const& view,
297 AccountID const& idSigner,
298 AccountID const& idAccount,
300 beast::Journal const j);
301 static NotTEC
303 ReadView const& view,
304 ApplyFlags flags,
305 AccountID const& id,
306 STObject const& sigObject,
307 beast::Journal const j);
308
309 void trapTransaction(uint256) const;
310
318 static NotTEC
319 preflight1(PreflightContext const& ctx, std::uint32_t flagMask);
320
326 static NotTEC
327 preflight2(PreflightContext const& ctx);
328};
329
330inline bool
332{
333 return true;
334}
335
337NotTEC
338preflight0(PreflightContext const& ctx, std::uint32_t flagMask);
339
340namespace detail {
341
346NotTEC
348
355} // namespace detail
356
357// Defined in Change.cpp
358template <>
360Transactor::invokePreflight<Change>(PreflightContext const& ctx);
361
362template <class T>
363NotTEC
365{
366 // Using this lookup does NOT require checking the fixDelegateV1_1. The data
367 // exists regardless of whether it is enabled.
368 auto const feature = Permission::getInstance().getTxFeature(ctx.tx.getTxnType());
369
370 if (feature && !ctx.rules.enabled(*feature))
371 return temDISABLED;
372
373 if (!T::checkExtraFeatures(ctx))
374 return temDISABLED;
375
376 if (auto const ret = preflight1(ctx, T::getFlagsMask(ctx)))
377 return ret;
378
379 if (auto const ret = T::preflight(ctx))
380 return ret;
381
382 if (auto const ret = preflight2(ctx))
383 return ret;
384
385 return T::preflightSigValidated(ctx);
386}
387
388template <class T>
389bool
391{
392 if (!value)
393 return true;
394 return value >= min && value <= max;
395}
396
397template <class T, class Unit>
398bool
400{
401 return validNumericRange(value, max.value(), min.value());
402}
403
404template <class T>
405bool
407{
408 if (!value)
409 return true;
410 return value >= min;
411}
412
413template <class T, class Unit>
414bool
419
420} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
static Sink & getNullSink()
Returns a Sink which does nothing.
Wraps a Journal::Sink to prefix its output with a string.
Definition WrappedSink.h:14
State information when applying a tx.
ApplyView & view()
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:114
std::optional< std::reference_wrapper< uint256 const > > const getTxFeature(TxType txType) const
static Permission const & getInstance()
A view into a ledger.
Definition ReadView.h:31
Rules controlling protocol behavior.
Definition Rules.h:18
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:118
std::shared_ptr< STLedgerEntry > pointer
TxType getTxnType() const
Definition STTx.h:180
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:112
void trapTransaction(uint256) const
static TER checkFee(PreclaimContext const &ctx, XRPAmount baseFee)
static NotTEC invokePreflight(PreflightContext const &ctx)
Definition Transactor.h:364
Transactor & operator=(Transactor const &)=delete
beast::WrappedSink sink_
Definition Transactor.h:109
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:406
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:110
static TER preclaim(PreclaimContext const &ctx)
Definition Transactor.h:198
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:331
ApplyView & view()
Definition Transactor.h:128
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:114
static NotTEC checkPriorTxAndLastLedger(PreclaimContext const &ctx)
XRPAmount mPriorBalance
Definition Transactor.h:113
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:108
ApplyView const & view() const
Definition Transactor.h:134
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:390
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:37
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:5
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
base_uint< 256 > uint256
Definition base_uint.h:526
TERSubset< CanCvtToTER > TER
Definition TER.h:620
ApplyFlags
Definition ApplyView.h:10
@ tapBATCH
Definition ApplyView.h:25
@ temDISABLED
Definition TER.h:94
NotTEC preflight0(PreflightContext const &ctx, std::uint32_t flagMask)
Performs early sanity checks on the txid.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:580
@ tesSUCCESS
Definition TER.h:225
Reflects the fee settings for a particular ledger.
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:53
ReadView const & view
Definition Transactor.h:56
PreclaimContext(Application &app_, ReadView const &view_, TER preflightResult_, STTx const &tx_, ApplyFlags flags_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:84
Application & app
Definition Transactor.h:55
PreclaimContext & operator=(PreclaimContext const &)=delete
beast::Journal const j
Definition Transactor.h:61
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:63
std::optional< uint256 const > const parentBatchId
Definition Transactor.h:60
State information when preflighting a tx.
Definition Transactor.h:15
PreflightContext(Application &app_, STTx const &tx_, uint256 parentBatchId_, Rules const &rules_, ApplyFlags flags_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:24
PreflightContext(Application &app_, STTx const &tx_, Rules const &rules_, ApplyFlags flags_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:36
beast::Journal const j
Definition Transactor.h:22
Application & app
Definition Transactor.h:17
PreflightContext & operator=(PreflightContext const &)=delete
std::optional< uint256 const > parentBatchId
Definition Transactor.h:21
Describes the results of the preflight check.
Definition applySteps.h:142