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 TER
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 PreclaimContext const& ctx,
287 AccountID const& id,
288 STObject const& sigObject);
289
290 // Base class always returns true
291 static bool
293
294 // Base class always returns tfUniversalMask
295 static std::uint32_t
296 getFlagsMask(PreflightContext const& ctx);
297
298 // Base class always returns tesSUCCESS
299 static NotTEC
301
302 static bool
303 validDataLength(std::optional<Slice> const& slice, std::size_t maxLength);
304
305 template <class T>
306 static bool
307 validNumericRange(std::optional<T> value, T max, T min = {});
308
309 template <class T, class Unit>
310 static bool
312 std::optional<T> value,
314 unit::ValueUnit<Unit, T> min = {});
315
316private:
318 reset(XRPAmount fee);
319
320 TER
321 consumeSeqProxy(SLE::pointer const& sleAccount);
322 TER
323 payFee();
324 static NotTEC
326 PreclaimContext const& ctx,
327 AccountID const& idSigner,
328 AccountID const& idAccount,
329 std::shared_ptr<SLE const> sleAccount);
330 static NotTEC
332 PreclaimContext const& ctx,
333 AccountID const& id,
334 STObject const& sigObject);
335
336 void trapTransaction(uint256) const;
337
345 static NotTEC
346 preflight1(PreflightContext const& ctx, std::uint32_t flagMask);
347
353 static NotTEC
354 preflight2(PreflightContext const& ctx);
355};
356
357inline bool
359{
360 return true;
361}
362
364NotTEC
365preflight0(PreflightContext const& ctx, std::uint32_t flagMask);
366
367namespace detail {
368
373NotTEC
375
382 ApplyFlags flags,
383 STObject const& sigObject,
385} // namespace detail
386
387// Defined in Change.cpp
388template <>
390Transactor::invokePreflight<Change>(PreflightContext const& ctx);
391
392template <class T>
393NotTEC
395{
396 // Using this lookup does NOT require checking the fixDelegateV1_1. The data
397 // exists regardless of whether it is enabled.
398 auto const feature =
400
401 if (feature && !ctx.rules.enabled(*feature))
402 return temDISABLED;
403
404 if (!T::checkExtraFeatures(ctx))
405 return temDISABLED;
406
407 if (auto const ret = preflight1(ctx, T::getFlagsMask(ctx)))
408 return ret;
409
410 if (auto const ret = T::preflight(ctx))
411 return ret;
412
413 if (auto const ret = preflight2(ctx))
414 return ret;
415
416 return T::preflightSigValidated(ctx);
417}
418
419template <class T>
420bool
422{
423 if (!value)
424 return true;
425 return value >= min && value <= max;
426}
427
428template <class T, class Unit>
429bool
431 std::optional<T> value,
434{
435 return validNumericRange(value, max.value(), min.value());
436}
437
438} // namespace ripple
439
440#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:207
static NotTEC checkMultiSign(PreclaimContext const &ctx, AccountID const &id, STObject const &sigObject)
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 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:358
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 TER checkPermission(ReadView const &view, STTx const &tx)
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 checkSingleSign(PreclaimContext const &ctx, AccountID const &idSigner, AccountID const &idAccount, std::shared_ptr< SLE const > sleAccount)
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:421
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
ApplyView const & view() const
Definition Transactor.h:169
static NotTEC invokePreflight(PreflightContext const &ctx)
Definition Transactor.h:394
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:244
@ tapBATCH
Definition ApplyView.h:45
TERSubset< CanCvtToTER > TER
Definition TER.h:645
NotTEC preflight0(PreflightContext const &ctx, std::uint32_t flagMask)
Performs early sanity checks on the txid.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:605
@ 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