rippled
Loading...
Searching...
No Matches
Transactor.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_APP_TX_TRANSACTOR_H_INCLUDED
21#define RIPPLE_APP_TX_TRANSACTOR_H_INCLUDED
22
23#include <xrpld/app/tx/applySteps.h>
24#include <xrpld/app/tx/detail/ApplyContext.h>
25
26#include <xrpl/beast/utility/Journal.h>
27#include <xrpl/beast/utility/WrappedSink.h>
28#include <xrpl/protocol/Permissions.h>
29#include <xrpl/protocol/XRPAmount.h>
30
31namespace ripple {
32
35{
36public:
38 STTx const& tx;
39 Rules const rules;
43
45 Application& app_,
46 STTx const& tx_,
47 uint256 parentBatchId_,
48 Rules const& rules_,
49 ApplyFlags flags_,
51 : app(app_)
52 , tx(tx_)
53 , rules(rules_)
54 , flags(flags_)
55 , parentBatchId(parentBatchId_)
56 , j(j_)
57 {
58 XRPL_ASSERT(
59 (flags_ & tapBATCH) == tapBATCH, "Batch apply flag should be set");
60 }
61
63 Application& app_,
64 STTx const& tx_,
65 Rules const& rules_,
66 ApplyFlags flags_,
68 : app(app_), tx(tx_), rules(rules_), flags(flags_), j(j_)
69 {
70 XRPL_ASSERT(
71 (flags_ & tapBATCH) == 0, "Batch apply flag should not be set");
72 }
73
74 PreflightContext&
75 operator=(PreflightContext const&) = delete;
76};
77
80{
81public:
83 ReadView const& view;
86 STTx const& tx;
89
91 Application& app_,
92 ReadView const& view_,
93 TER preflightResult_,
94 STTx const& tx_,
95 ApplyFlags flags_,
96 std::optional<uint256> parentBatchId_,
98 : app(app_)
99 , view(view_)
100 , preflightResult(preflightResult_)
101 , flags(flags_)
102 , tx(tx_)
103 , parentBatchId(parentBatchId_)
104 , j(j_)
105 {
106 XRPL_ASSERT(
107 parentBatchId.has_value() == ((flags_ & tapBATCH) == tapBATCH),
108 "Parent Batch ID should be set if batch apply flag is set");
109 }
110
112 Application& app_,
113 ReadView const& view_,
114 TER preflightResult_,
115 STTx const& tx_,
116 ApplyFlags flags_,
119 app_,
120 view_,
121 preflightResult_,
122 tx_,
123 flags_,
124 std::nullopt,
125 j_)
126 {
127 XRPL_ASSERT(
128 (flags_ & tapBATCH) == 0, "Batch apply flag should not be set");
129 }
130
131 PreclaimContext&
132 operator=(PreclaimContext const&) = delete;
133};
134
135class TxConsequences;
136struct PreflightResult;
137// Needed for preflight specialization
138class Change;
139
141{
142protected:
146
148 XRPAmount mPriorBalance; // Balance before fees.
149 XRPAmount mSourceBalance; // Balance after fees.
150
151 virtual ~Transactor() = default;
152 Transactor(Transactor const&) = delete;
154 operator=(Transactor const&) = delete;
155
156public:
160 operator()();
161
162 ApplyView&
164 {
165 return ctx_.view();
166 }
167
168 ApplyView const&
169 view() const
170 {
171 return ctx_.view();
172 }
173
175 /*
176 These static functions are called from invoke_preclaim<Tx>
177 using name hiding to accomplish compile-time polymorphism,
178 so derived classes can override for different or extra
179 functionality. Use with care, as these are not really
180 virtual and so don't have the compiler-time protection that
181 comes with it.
182 */
183
184 static NotTEC
185 checkSeqProxy(ReadView const& view, STTx const& tx, beast::Journal j);
186
187 static NotTEC
189
190 static TER
191 checkFee(PreclaimContext const& ctx, XRPAmount baseFee);
192
193 static NotTEC
194 checkSign(PreclaimContext const& ctx);
195
196 static NotTEC
198
199 // Returns the fee in fee units, not scaled for load.
200 static XRPAmount
201 calculateBaseFee(ReadView const& view, STTx const& tx);
202
203 /* Do NOT define an invokePreflight function in a derived class.
204 Instead, define:
205
206 // Optional if the transaction is gated on an amendment that
207 // isn't specified in transactions.macro
208 static bool
209 checkExtraFeatures(PreflightContext const& ctx);
210
211 // Optional if the transaction uses any flags other than tfUniversal
212 static std::uint32_t
213 getFlagsMask(PreflightContext const& ctx);
214
215 // Required, even if it just returns tesSUCCESS.
216 static NotTEC
217 preflight(PreflightContext const& ctx);
218
219 // Optional, rarely needed, if the transaction does any expensive
220 // checks after the signature is verified.
221 static NotTEC preflightSigValidated(PreflightContext const& ctx);
222
223 * Do not try to call preflight1 or preflight2 directly.
224 * Do not check whether relevant amendments are enabled in preflight.
225 Instead, define checkExtraFeatures.
226 * Do not check flags in preflight. Instead, define getFlagsMask.
227 */
228 template <class T>
229 static NotTEC
231
232 static TER
234 {
235 // Most transactors do nothing
236 // after checkSeq/Fee/Sign.
237 return tesSUCCESS;
238 }
239
240 static NotTEC
241 checkPermission(ReadView const& view, STTx const& tx);
243
244 // Interface used by DeleteAccount
245 static TER
248 AccountID const& account,
249 uint256 const& ticketIndex,
251
252protected:
253 TER
254 apply();
255
256 explicit Transactor(ApplyContext& ctx);
257
258 virtual void
259 preCompute();
260
261 virtual TER
262 doApply() = 0;
263
273 static XRPAmount
275 Application& app,
276 XRPAmount baseFee,
277 Fees const& fees,
278 ApplyFlags flags);
279
280 // Returns the fee in fee units, not scaled for load.
281 static XRPAmount
282 calculateOwnerReserveFee(ReadView const& view, STTx const& tx);
283
284 static NotTEC
285 checkSign(
286 ReadView const& view,
287 ApplyFlags flags,
288 AccountID const& idAccount,
289 STObject const& sigObject,
290 beast::Journal const j);
291
292 // Base class always returns true
293 static bool
295
296 // Base class always returns tfUniversalMask
297 static std::uint32_t
298 getFlagsMask(PreflightContext const& ctx);
299
300 // Base class always returns tesSUCCESS
301 static NotTEC
303
304 static bool
305 validDataLength(std::optional<Slice> const& slice, std::size_t maxLength);
306
307 template <class T>
308 static bool
309 validNumericRange(std::optional<T> value, T max, T min = {});
310
311 template <class T, class Unit>
312 static bool
314 std::optional<T> value,
316 unit::ValueUnit<Unit, T> min = {});
317
318private:
320 reset(XRPAmount fee);
321
322 TER
323 consumeSeqProxy(SLE::pointer const& sleAccount);
324 TER
325 payFee();
326 static NotTEC
328 ReadView const& view,
329 AccountID const& idSigner,
330 AccountID const& idAccount,
332 beast::Journal const j);
333 static NotTEC
335 ReadView const& view,
336 ApplyFlags flags,
337 AccountID const& id,
338 STObject const& sigObject,
339 beast::Journal const j);
340
341 void trapTransaction(uint256) const;
342
350 static NotTEC
351 preflight1(PreflightContext const& ctx, std::uint32_t flagMask);
352
358 static NotTEC
359 preflight2(PreflightContext const& ctx);
360};
361
362inline bool
364{
365 return true;
366}
367
369NotTEC
370preflight0(PreflightContext const& ctx, std::uint32_t flagMask);
371
372namespace detail {
373
378NotTEC
380
387 ApplyFlags flags,
388 STObject const& sigObject,
390} // namespace detail
391
392// Defined in Change.cpp
393template <>
395Transactor::invokePreflight<Change>(PreflightContext const& ctx);
396
397template <class T>
398NotTEC
400{
401 // Using this lookup does NOT require checking the fixDelegateV1_1. The data
402 // exists regardless of whether it is enabled.
403 auto const feature =
405
406 if (feature && !ctx.rules.enabled(*feature))
407 return temDISABLED;
408
409 if (!T::checkExtraFeatures(ctx))
410 return temDISABLED;
411
412 if (auto const ret = preflight1(ctx, T::getFlagsMask(ctx)))
413 return ret;
414
415 if (auto const ret = T::preflight(ctx))
416 return ret;
417
418 if (auto const ret = preflight2(ctx))
419 return ret;
420
421 return T::preflightSigValidated(ctx);
422}
423
424template <class T>
425bool
427{
428 if (!value)
429 return true;
430 return value >= min && value <= max;
431}
432
433template <class T, class Unit>
434bool
436 std::optional<T> value,
439{
440 return validNumericRange(value, max.value(), min.value());
441}
442
443} // namespace ripple
444
445#endif
A generic endpoint for log messages.
Definition Journal.h:60
static Sink & getNullSink()
Returns a Sink which does nothing.
Wraps a Journal::Sink to prefix its output with a string.
Definition WrappedSink.h:34
State information when applying a tx.
ApplyView & view()
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:143
std::optional< std::reference_wrapper< uint256 const > > const getTxFeature(TxType txType) const
static Permission const & getInstance()
A view into a ledger.
Definition ReadView.h:51
Rules controlling protocol behavior.
Definition Rules.h:38
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:130
std::shared_ptr< STLedgerEntry > pointer
TxType getTxnType() const
Definition STTx.h:237
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:363
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:147
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:163
beast::WrappedSink sink_
Definition Transactor.h:144
static TER preclaim(PreclaimContext const &ctx)
Definition Transactor.h:233
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:426
beast::Journal const j_
Definition Transactor.h:145
XRPAmount mPriorBalance
Definition Transactor.h:148
virtual void preCompute()
static TER ticketDelete(ApplyView &view, AccountID const &account, uint256 const &ticketIndex, beast::Journal j)
XRPAmount mSourceBalance
Definition Transactor.h:149
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:143
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:169
static NotTEC invokePreflight(PreflightContext const &ctx)
Definition Transactor.h:399
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition applySteps.h:58
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:25
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:48
base_uint< 256 > uint256
Definition base_uint.h:558
@ tesSUCCESS
Definition TER.h:245
@ tapBATCH
Definition ApplyView.h:45
TERSubset< CanCvtToTER > TER
Definition TER.h:649
NotTEC preflight0(PreflightContext const &ctx, std::uint32_t flagMask)
Performs early sanity checks on the txid.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:609
@ temDISABLED
Definition TER.h:114
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:80
ReadView const & view
Definition Transactor.h:83
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:111
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:90
std::optional< uint256 const > const parentBatchId
Definition Transactor.h:87
beast::Journal const j
Definition Transactor.h:88
State information when preflighting a tx.
Definition Transactor.h:35
PreflightContext(Application &app_, STTx const &tx_, uint256 parentBatchId_, Rules const &rules_, ApplyFlags flags_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:44
std::optional< uint256 const > parentBatchId
Definition Transactor.h:41
beast::Journal const j
Definition Transactor.h:42
PreflightContext(Application &app_, STTx const &tx_, Rules const &rules_, ApplyFlags flags_, beast::Journal j_=beast::Journal{beast::Journal::getNullSink()})
Definition Transactor.h:62
PreflightContext & operator=(PreflightContext const &)=delete
Describes the results of the preflight check.
Definition applySteps.h:163