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 ripple {
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 AccountID const& idAccount,
270 STObject const& sigObject,
271 beast::Journal const j);
272
273 // Base class always returns true
274 static bool
276
277 // Base class always returns tfUniversalMask
278 static std::uint32_t
279 getFlagsMask(PreflightContext const& ctx);
280
281 // Base class always returns tesSUCCESS
282 static NotTEC
284
285 static bool
286 validDataLength(std::optional<Slice> const& slice, std::size_t maxLength);
287
288 template <class T>
289 static bool
290 validNumericRange(std::optional<T> value, T max, T min = {});
291
292 template <class T, class Unit>
293 static bool
295 std::optional<T> value,
297 unit::ValueUnit<Unit, T> min = {});
298
299private:
301 reset(XRPAmount fee);
302
303 TER
304 consumeSeqProxy(SLE::pointer const& sleAccount);
305 TER
306 payFee();
307 static NotTEC
309 ReadView const& view,
310 AccountID const& idSigner,
311 AccountID const& idAccount,
313 beast::Journal const j);
314 static NotTEC
316 ReadView const& view,
317 ApplyFlags flags,
318 AccountID const& id,
319 STObject const& sigObject,
320 beast::Journal const j);
321
322 void trapTransaction(uint256) const;
323
331 static NotTEC
332 preflight1(PreflightContext const& ctx, std::uint32_t flagMask);
333
339 static NotTEC
340 preflight2(PreflightContext const& ctx);
341};
342
343inline bool
345{
346 return true;
347}
348
350NotTEC
351preflight0(PreflightContext const& ctx, std::uint32_t flagMask);
352
353namespace detail {
354
359NotTEC
361
368 ApplyFlags flags,
369 STObject const& sigObject,
371} // namespace detail
372
373// Defined in Change.cpp
374template <>
376Transactor::invokePreflight<Change>(PreflightContext const& ctx);
377
378template <class T>
379NotTEC
381{
382 // Using this lookup does NOT require checking the fixDelegateV1_1. The data
383 // exists regardless of whether it is enabled.
384 auto const feature =
386
387 if (feature && !ctx.rules.enabled(*feature))
388 return temDISABLED;
389
390 if (!T::checkExtraFeatures(ctx))
391 return temDISABLED;
392
393 if (auto const ret = preflight1(ctx, T::getFlagsMask(ctx)))
394 return ret;
395
396 if (auto const ret = T::preflight(ctx))
397 return ret;
398
399 if (auto const ret = preflight2(ctx))
400 return ret;
401
402 return T::preflightSigValidated(ctx);
403}
404
405template <class T>
406bool
408{
409 if (!value)
410 return true;
411 return value >= min && value <= max;
412}
413
414template <class T, class Unit>
415bool
417 std::optional<T> value,
420{
421 return validNumericRange(value, max.value(), min.value());
422}
423
424} // namespace ripple
425
426#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:218
TER consumeSeqProxy(SLE::pointer const &sleAccount)
ApplyResult operator()()
Process the transaction.
static NotTEC preflightSigValidated(PreflightContext const &ctx)
Transactor & operator=(Transactor const &)=delete
static NotTEC checkPriorTxAndLastLedger(PreclaimContext const &ctx)
static NotTEC checkMultiSign(ReadView const &view, ApplyFlags flags, AccountID const &id, STObject const &sigObject, beast::Journal const j)
static TER checkFee(PreclaimContext const &ctx, XRPAmount baseFee)
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
static NotTEC checkSeqProxy(ReadView const &view, STTx const &tx, beast::Journal j)
static NotTEC checkSign(PreclaimContext const &ctx)
void trapTransaction(uint256) const
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...
virtual ~Transactor()=default
static bool checkExtraFeatures(PreflightContext const &ctx)
Definition Transactor.h:344
static NotTEC preflight1(PreflightContext const &ctx, std::uint32_t flagMask)
Performs early sanity checks on the account and fee fields.
AccountID const account_
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)
static NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
ApplyView & view()
Definition Transactor.h:144
beast::WrappedSink sink_
Definition Transactor.h:125
static TER preclaim(PreclaimContext const &ctx)
Definition Transactor.h:214
static NotTEC checkBatchSign(PreclaimContext const &ctx)
static XRPAmount calculateOwnerReserveFee(ReadView const &view, STTx const &tx)
static bool validNumericRange(std::optional< T > value, T max, T min={})
Definition Transactor.h:407
beast::Journal const j_
Definition Transactor.h:126
XRPAmount mPriorBalance
Definition Transactor.h:129
virtual void preCompute()
static TER ticketDelete(ApplyView &view, AccountID const &account, uint256 const &ticketIndex, beast::Journal j)
XRPAmount mSourceBalance
Definition Transactor.h:130
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static bool validDataLength(std::optional< Slice > const &slice, std::size_t maxLength)
ApplyContext & ctx_
Definition Transactor.h:124
virtual TER doApply()=0
std::pair< TER, XRPAmount > reset(XRPAmount fee)
Reset the context, discarding any changes made and adjust the fee.
Transactor(Transactor const &)=delete
static NotTEC checkPermission(ReadView const &view, STTx const &tx)
ApplyView const & view() const
Definition Transactor.h:150
static NotTEC invokePreflight(PreflightContext const &ctx)
Definition Transactor.h:380
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition applySteps.h:39
std::optional< NotTEC > preflightCheckSimulateKeys(ApplyFlags flags, STObject const &sigObject, beast::Journal j)
Checks the special signing key state needed for simulation.
NotTEC preflightCheckSigningKey(STObject const &sigObject, beast::Journal j)
Checks the validity of the transactor signing key.
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
@ tesSUCCESS
Definition TER.h:226
@ tapBATCH
Definition ApplyView.h:26
TERSubset< CanCvtToTER > TER
Definition TER.h:630
NotTEC preflight0(PreflightContext const &ctx, std::uint32_t flagMask)
Performs early sanity checks on the txid.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:590
@ temDISABLED
Definition TER.h:95
STL namespace.
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 & operator=(PreclaimContext const &)=delete
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
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
beast::Journal const j
Definition Transactor.h:69
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
std::optional< uint256 const > parentBatchId
Definition Transactor.h:22
beast::Journal const j
Definition Transactor.h:23
PreflightContext(Application &app_, STTx const &tx_, Rules const &rules_, ApplyFlags flags_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:43
PreflightContext & operator=(PreflightContext const &)=delete
Describes the results of the preflight check.
Definition applySteps.h:144