rippled
Loading...
Searching...
No Matches
Transactor.h
1#pragma once
2
3#include <xrpl/beast/utility/Journal.h>
4#include <xrpl/beast/utility/WrappedSink.h>
5#include <xrpl/protocol/Permissions.h>
6#include <xrpl/protocol/XRPAmount.h>
7#include <xrpl/tx/ApplyContext.h>
8#include <xrpl/tx/applySteps.h>
9
10namespace xrpl {
11
14{
15public:
17 STTx const& tx;
18 Rules const rules;
22
24 ServiceRegistry& registry_,
25 STTx const& tx_,
26 uint256 parentBatchId_,
27 Rules const& rules_,
28 ApplyFlags flags_,
30 : registry(registry_)
31 , tx(tx_)
32 , rules(rules_)
33 , flags(flags_)
34 , parentBatchId(parentBatchId_)
35 , j(j_)
36 {
37 XRPL_ASSERT((flags_ & tapBATCH) == tapBATCH, "Batch apply flag should be set");
38 }
39
41 ServiceRegistry& registry_,
42 STTx const& tx_,
43 Rules const& rules_,
44 ApplyFlags flags_,
46 : registry(registry_), tx(tx_), rules(rules_), flags(flags_), j(j_)
47 {
48 XRPL_ASSERT((flags_ & tapBATCH) == 0, "Batch apply flag should not be set");
49 }
50
51 PreflightContext&
52 operator=(PreflightContext const&) = delete;
53};
54
57{
58public:
60 ReadView const& view;
63 STTx const& tx;
66
68 ServiceRegistry& registry_,
69 ReadView const& view_,
70 TER preflightResult_,
71 STTx const& tx_,
72 ApplyFlags flags_,
73 std::optional<uint256> parentBatchId_,
75 : registry(registry_)
76 , view(view_)
77 , preflightResult(preflightResult_)
78 , flags(flags_)
79 , tx(tx_)
80 , parentBatchId(parentBatchId_)
81 , j(j_)
82 {
83 XRPL_ASSERT(
84 parentBatchId.has_value() == ((flags_ & tapBATCH) == tapBATCH),
85 "Parent Batch ID should be set if batch apply flag is set");
86 }
87
89 ServiceRegistry& registry_,
90 ReadView const& view_,
91 TER preflightResult_,
92 STTx const& tx_,
93 ApplyFlags flags_,
95 : PreclaimContext(registry_, view_, preflightResult_, tx_, flags_, std::nullopt, j_)
96 {
97 XRPL_ASSERT((flags_ & tapBATCH) == 0, "Batch apply flag should not be set");
98 }
99
100 PreclaimContext&
101 operator=(PreclaimContext const&) = delete;
102};
103
104class TxConsequences;
105struct PreflightResult;
106// Needed for preflight specialization
107class Change;
108
110{
111protected:
115
117 XRPAmount preFeeBalance_{}; // Balance before fees.
118
119 Transactor(Transactor const&) = delete;
121 operator=(Transactor const&) = delete;
122
123public:
124 virtual ~Transactor() = default;
125
129 operator()();
130
131 ApplyView&
133 {
134 return ctx_.view();
135 }
136
137 ApplyView const&
138 view() const
139 {
140 return ctx_.view();
141 }
142
144 /*
145 These static functions are called from invoke_preclaim<Tx>
146 using name hiding to accomplish compile-time polymorphism,
147 so derived classes can override for different or extra
148 functionality. Use with care, as these are not really
149 virtual and so don't have the compiler-time protection that
150 comes with it.
151 */
152
153 static NotTEC
154 checkSeqProxy(ReadView const& view, STTx const& tx, beast::Journal j);
155
156 static NotTEC
158
159 static TER
160 checkFee(PreclaimContext const& ctx, XRPAmount baseFee);
161
162 static NotTEC
163 checkSign(PreclaimContext const& ctx);
164
165 static NotTEC
167
168 // Returns the fee in fee units, not scaled for load.
169 static XRPAmount
170 calculateBaseFee(ReadView const& view, STTx const& tx);
171
172 /* Do NOT define an invokePreflight function in a derived class.
173 Instead, define:
174
175 // Optional if the transaction is gated on an amendment that
176 // isn't specified in transactions.macro
177 static bool
178 checkExtraFeatures(PreflightContext const& ctx);
179
180 // Optional if the transaction uses any flags other than tfUniversal
181 static std::uint32_t
182 getFlagsMask(PreflightContext const& ctx);
183
184 // Required, even if it just returns tesSUCCESS.
185 static NotTEC
186 preflight(PreflightContext const& ctx);
187
188 // Optional, rarely needed, if the transaction does any expensive
189 // checks after the signature is verified.
190 static NotTEC preflightSigValidated(PreflightContext const& ctx);
191
192 * Do not try to call preflight1 or preflight2 directly.
193 * Do not check whether relevant amendments are enabled in preflight.
194 Instead, define checkExtraFeatures.
195 * Do not check flags in preflight. Instead, define getFlagsMask.
196 */
197 template <class T>
198 static NotTEC
200
201 static TER
203 {
204 // Most transactors do nothing
205 // after checkSeq/Fee/Sign.
206 return tesSUCCESS;
207 }
208
209 static NotTEC
210 checkPermission(ReadView const& view, STTx const& tx);
212
213 // Interface used by AccountDelete
214 static TER
217 AccountID const& account,
218 uint256 const& ticketIndex,
220
221protected:
222 TER
223 apply();
224
225 explicit Transactor(ApplyContext& ctx);
226
227 virtual void
228 preCompute();
229
230 virtual TER
231 doApply() = 0;
232
242 static XRPAmount
243 minimumFee(ServiceRegistry& registry, XRPAmount baseFee, Fees const& fees, ApplyFlags flags);
244
245 // Returns the fee in fee units, not scaled for load.
246 static XRPAmount
247 calculateOwnerReserveFee(ReadView const& view, STTx const& tx);
248
249 static NotTEC
250 checkSign(
251 ReadView const& view,
252 ApplyFlags flags,
253 std::optional<uint256 const> const& parentBatchId,
254 AccountID const& idAccount,
255 STObject const& sigObject,
256 beast::Journal const j);
257
258 // Base class always returns true
259 static bool
261
262 // Base class always returns tfUniversalMask
263 static std::uint32_t
264 getFlagsMask(PreflightContext const& ctx);
265
266 // Base class always returns tesSUCCESS
267 static NotTEC
269
270 static bool
271 validDataLength(std::optional<Slice> const& slice, std::size_t maxLength);
272
273 template <class T>
274 static bool
275 validNumericRange(std::optional<T> value, T max, T min = T{});
276
277 template <class T, class Unit>
278 static bool
280 std::optional<T> value,
283
285 template <class T>
286 static bool
287 validNumericMinimum(std::optional<T> value, T min = T{});
288
290 template <class T, class Unit>
291 static bool
293 std::optional<T> value,
294 unit::ValueUnit<Unit, T> min = unit::ValueUnit<Unit, T>{});
295
296private:
298 reset(XRPAmount fee);
299
300 TER
301 consumeSeqProxy(SLE::pointer const& sleAccount);
302 TER
303 payFee();
304 static NotTEC
306 ReadView const& view,
307 AccountID const& idSigner,
308 AccountID const& idAccount,
310 beast::Journal const j);
311 static NotTEC
313 ReadView const& view,
314 ApplyFlags flags,
315 AccountID const& id,
316 STObject const& sigObject,
317 beast::Journal const j);
318
319 void trapTransaction(uint256) const;
320
328 static NotTEC
329 preflight1(PreflightContext const& ctx, std::uint32_t flagMask);
330
336 static NotTEC
337 preflight2(PreflightContext const& ctx);
338};
339
340inline bool
342{
343 return true;
344}
345
347NotTEC
348preflight0(PreflightContext const& ctx, std::uint32_t flagMask);
349
350namespace detail {
351
356NotTEC
358
365} // namespace detail
366
367// Defined in Change.cpp
368template <>
370Transactor::invokePreflight<Change>(PreflightContext const& ctx);
371
372template <class T>
373NotTEC
375{
376 // Using this lookup does NOT require checking the fixDelegateV1_1. The data
377 // exists regardless of whether it is enabled.
378 auto const feature = Permission::getInstance().getTxFeature(ctx.tx.getTxnType());
379
380 if (feature && !ctx.rules.enabled(*feature))
381 return temDISABLED;
382
383 if (!T::checkExtraFeatures(ctx))
384 return temDISABLED;
385
386 if (auto const ret = preflight1(ctx, T::getFlagsMask(ctx)))
387 return ret;
388
389 if (auto const ret = T::preflight(ctx))
390 return ret;
391
392 if (auto const ret = preflight2(ctx))
393 return ret;
394
395 return T::preflightSigValidated(ctx);
396}
397
398template <class T>
399bool
401{
402 if (!value)
403 return true;
404 return value >= min && value <= max;
405}
406
407template <class T, class Unit>
408bool
410 std::optional<T> value,
413{
414 return validNumericRange(value, max.value(), min.value());
415}
416
417template <class T>
418bool
420{
421 if (!value)
422 return true;
423 return value >= min;
424}
425
426template <class T, class Unit>
427bool
432
433} // 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:116
std::optional< std::reference_wrapper< uint256 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:120
std::shared_ptr< STLedgerEntry > pointer
TxType getTxnType() const
Definition STTx.h:188
Service registry for dependency injection.
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:116
void trapTransaction(uint256) const
static TER checkFee(PreclaimContext const &ctx, XRPAmount baseFee)
static NotTEC invokePreflight(PreflightContext const &ctx)
Definition Transactor.h:374
Transactor & operator=(Transactor const &)=delete
beast::WrappedSink sink_
Definition Transactor.h:113
static XRPAmount minimumFee(ServiceRegistry &registry, 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 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 bool validNumericMinimum(std::optional< T > value, T min=T{})
Minimum will usually be zero.
Definition Transactor.h:419
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:114
static TER preclaim(PreclaimContext const &ctx)
Definition Transactor.h:202
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:341
ApplyView & view()
Definition Transactor.h:132
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)
static NotTEC checkPriorTxAndLastLedger(PreclaimContext const &ctx)
XRPAmount preFeeBalance_
Definition Transactor.h:117
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:112
ApplyView const & view() const
Definition Transactor.h:138
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:400
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:38
STL namespace.
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: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:531
TERSubset< CanCvtToTER > TER
Definition TER.h:622
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 and flags.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:582
@ 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:57
ReadView const & view
Definition Transactor.h:60
PreclaimContext(ServiceRegistry &registry_, 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:67
std::reference_wrapper< ServiceRegistry > registry
Definition Transactor.h:59
PreclaimContext(ServiceRegistry &registry_, ReadView const &view_, TER preflightResult_, STTx const &tx_, ApplyFlags flags_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:88
PreclaimContext & operator=(PreclaimContext const &)=delete
beast::Journal const j
Definition Transactor.h:65
std::optional< uint256 const > const parentBatchId
Definition Transactor.h:64
State information when preflighting a tx.
Definition Transactor.h:14
beast::Journal const j
Definition Transactor.h:21
PreflightContext(ServiceRegistry &registry_, STTx const &tx_, Rules const &rules_, ApplyFlags flags_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:40
PreflightContext & operator=(PreflightContext const &)=delete
std::optional< uint256 const > parentBatchId
Definition Transactor.h:20
PreflightContext(ServiceRegistry &registry_, STTx const &tx_, uint256 parentBatchId_, Rules const &rules_, ApplyFlags flags_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:23
std::reference_wrapper< ServiceRegistry > registry
Definition Transactor.h:16
Describes the results of the preflight check.
Definition applySteps.h:143