1#include <xrpld/app/tx/apply.h>
2#include <xrpld/app/tx/detail/Batch.h>
4#include <xrpl/basics/Log.h>
5#include <xrpl/ledger/Sandbox.h>
6#include <xrpl/ledger/View.h>
7#include <xrpl/protocol/Feature.h>
8#include <xrpl/protocol/Indexes.h>
9#include <xrpl/protocol/TER.h>
10#include <xrpl/protocol/TxFlags.h>
46 JLOG(
debugLog().error()) <<
"BatchTrace: Base fee overflow detected.";
63 <<
"BatchTrace: Raw Transactions array exceeds max entries.";
70 STTx const stx =
STTx{std::move(txn)};
76 <<
"BatchTrace: Inner Batch transaction found.";
83 if (txnFees > maxAmount - fee)
86 <<
"BatchTrace: XRPAmount overflow in txnFees calculation.";
104 <<
"BatchTrace: Batch Signers array exceeds max entries.";
109 for (
STObject const& signer : signers)
111 if (signer.isFieldPresent(sfTxnSignature))
113 else if (signer.isFieldPresent(sfSigners))
114 signerCount += signer.getFieldArray(sfSigners).size();
119 if (signerCount > 0 &&
view.
fees().
base > maxAmount / signerCount)
122 <<
"BatchTrace: XRPAmount overflow in signerCount calculation.";
130 if (signerFees > maxAmount - txnFees)
133 <<
"BatchTrace: XRPAmount overflow in signerFees calculation.";
136 if (txnFees + signerFees > maxAmount - batchBase)
139 <<
"BatchTrace: XRPAmount overflow in total fee calculation.";
145 return signerFees + txnFees + batchBase;
197 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
198 <<
"too many flags.";
203 if (rawTxns.size() <= 1)
205 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
206 <<
"txns array must have at least 2 entries.";
212 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
213 <<
"txns array exceeds 8 entries.";
221 auto checkSignatureFields = [&parentBatchId, &j = ctx.
j](
224 char const* label =
"") ->
NotTEC {
225 if (sig.isFieldPresent(sfTxnSignature))
228 <<
"BatchTrace[" << parentBatchId <<
"]: "
229 <<
"inner txn " << label <<
"cannot include TxnSignature. "
234 if (sig.isFieldPresent(sfSigners))
237 <<
"BatchTrace[" << parentBatchId <<
"]: "
238 <<
"inner txn " << label <<
" cannot include Signers. "
243 if (!sig.getFieldVL(sfSigningPubKey).empty())
246 <<
"BatchTrace[" << parentBatchId <<
"]: "
247 <<
"inner txn " << label <<
" SigningPubKey must be empty. "
256 STTx const stx =
STTx{std::move(rb)};
258 if (!uniqueHashes.
emplace(hash).second)
260 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
261 <<
"duplicate Txn found. "
266 auto const txType = stx.
getFieldU16(sfTransactionType);
267 if (txType == ttBATCH)
269 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
270 <<
"batch cannot have an inner batch txn. "
278 [txType](
auto const& disabled) { return txType == disabled; }))
286 <<
"BatchTrace[" << parentBatchId <<
"]: "
287 <<
"inner txn must have the tfInnerBatchTxn flag. "
292 if (
auto const ret = checkSignatureFields(stx, hash))
299 auto const counterpartySignature =
301 if (
auto const ret = checkSignatureFields(
302 counterpartySignature, hash,
"counterparty signature "))
310 !fee.native() || fee.xrp() != beast::zero)
312 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
313 <<
"inner txn must have a fee of 0. "
323 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
324 <<
"inner txn preflight failed: "
335 <<
"BatchTrace[" << parentBatchId <<
"]: "
336 <<
"inner txn must have exactly one of Sequence and "
346 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
347 <<
"inner txn must have either Sequence or "
356 if (
auto const seq = stx.
getFieldU32(sfSequence); seq != 0)
358 if (!accountSeqTicket[innerAccount].insert(seq).second)
361 <<
"BatchTrace[" << parentBatchId <<
"]: "
362 <<
"duplicate sequence found: "
370 if (
auto const ticket = stx.
getFieldU32(sfTicketSequence);
371 !accountSeqTicket[innerAccount].
insert(ticket).second)
374 <<
"BatchTrace[" << parentBatchId <<
"]: "
375 <<
"duplicate ticket found: "
397 auto const innerAccount = rb.getAccountID(sfAccount);
401 if (innerAccount != outerAccount)
402 requiredSigners.
insert(innerAccount);
405 if (
auto const counterparty = rb.at(~sfCounterparty);
406 counterparty && counterparty != outerAccount)
407 requiredSigners.
insert(*counterparty);
419 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
420 <<
"signers array exceeds 8 entries.";
429 for (
auto const& signer : signers)
431 AccountID const signerAccount = signer.getAccountID(sfAccount);
432 if (signerAccount == outerAccount)
435 <<
"BatchTrace[" << parentBatchId <<
"]: "
436 <<
"signer cannot be the outer account: " << signerAccount;
440 if (!batchSigners.
insert(signerAccount).second)
443 <<
"BatchTrace[" << parentBatchId <<
"]: "
444 <<
"duplicate signer found: " << signerAccount;
450 if (requiredSigners.
erase(signerAccount) == 0)
452 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
453 <<
"no account signature for inner txn.";
464 <<
"BatchTrace[" << parentBatchId <<
"]: "
465 <<
"invalid batch txn signature: " << sigResult.error();
470 if (!requiredSigners.
empty())
472 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
473 <<
"invalid batch signers.";
static constexpr auto disabledTxTypes
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Calculates the total base fee for a batch transaction.
static NotTEC preflight(PreflightContext const &ctx)
Performs preflight validation checks for a Batch transaction.
static NotTEC checkSign(PreclaimContext const &ctx)
Checks the validity of signatures for a batch transaction.
TER doApply() override
Applies the outer batch transaction.
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static NotTEC preflightSigValidated(PreflightContext const &ctx)
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
std::uint32_t getFieldU32(SField const &field) const
STArray const & getFieldArray(SField const &field) const
bool isFieldPresent(SField const &field) const
STObject getFieldObject(SField const &field) const
AccountID getAccountID(SField const &field) const
std::uint16_t getFieldU16(SField const &field) const
STAmount const & getFieldAmount(SField const &field) const
std::uint32_t getFlags() const
Expected< void, std::string > checkBatchSign(Rules const &rules) const
TxType getTxnType() const
uint256 getTransactionID() const
static NotTEC checkSign(PreclaimContext const &ctx)
static NotTEC checkBatchSign(PreclaimContext const &ctx)
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
beast::Journal debugLog()
Returns a debug journal.
constexpr std::uint32_t tfInnerBatchTxn
PreflightResult preflight(Application &app, Rules const &rules, STTx const &tx, ApplyFlags flags, beast::Journal j)
Gate a transaction based on static information.
std::string transHuman(TER code)
constexpr std::uint32_t tfAllOrNothing
std::size_t constexpr maxBatchTxCount
The maximum number of transactions that can be in a batch.
constexpr std::uint32_t tfOnlyOne
constexpr std::uint32_t const tfBatchMask
bool isTesSuccess(TER x) noexcept
constexpr XRPAmount INITIAL_XRP
Configure the native currency.
constexpr std::uint32_t tfUntilFailure
XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Compute only the expected base fee for a transaction.
constexpr std::uint32_t tfIndependent
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.