1#include <xrpl/basics/Log.h>
2#include <xrpl/ledger/Sandbox.h>
3#include <xrpl/ledger/View.h>
4#include <xrpl/protocol/Feature.h>
5#include <xrpl/protocol/Indexes.h>
6#include <xrpl/protocol/SystemParameters.h>
7#include <xrpl/protocol/TER.h>
8#include <xrpl/protocol/TxFlags.h>
9#include <xrpl/tx/apply.h>
10#include <xrpl/tx/transactors/system/Batch.h>
45 JLOG(
debugLog().error()) <<
"BatchTrace: Base fee overflow detected.";
61 JLOG(
debugLog().error()) <<
"BatchTrace: Raw Transactions array exceeds max entries.";
68 STTx const stx =
STTx{std::move(txn)};
73 JLOG(
debugLog().error()) <<
"BatchTrace: Inner Batch transaction found.";
80 if (txnFees > maxAmount - fee)
83 <<
"BatchTrace: XRPAmount overflow in txnFees calculation.";
100 JLOG(
debugLog().error()) <<
"BatchTrace: Batch Signers array exceeds max entries.";
105 for (
STObject const& signer : signers)
107 if (signer.isFieldPresent(sfTxnSignature))
111 else if (signer.isFieldPresent(sfSigners))
113 signerCount += signer.getFieldArray(sfSigners).size();
119 if (signerCount > 0 &&
view.
fees().
base > maxAmount / signerCount)
121 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in signerCount calculation.";
129 if (signerFees > maxAmount - txnFees)
131 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in signerFees calculation.";
134 if (txnFees + signerFees > maxAmount - batchBase)
136 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in total fee calculation.";
142 return signerFees + txnFees + batchBase;
190 if (
std::popcount(flags & (tfAllOrNothing | tfOnlyOne | tfUntilFailure | tfIndependent)) != 1)
192 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
193 <<
"too many flags.";
198 if (rawTxns.size() <= 1)
200 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
201 <<
"txns array must have at least 2 entries.";
207 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
208 <<
"txns array exceeds 8 entries.";
215 auto checkSignatureFields =
216 [&parentBatchId, &j = ctx.
j](
218 if (sig.isFieldPresent(sfTxnSignature))
220 JLOG(j.debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
221 <<
"inner txn " << label <<
"cannot include TxnSignature. "
226 if (sig.isFieldPresent(sfSigners))
228 JLOG(j.debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
229 <<
"inner txn " << label <<
" cannot include Signers. "
234 if (!sig.getFieldVL(sfSigningPubKey).empty())
236 JLOG(j.debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
237 <<
"inner txn " << label <<
" SigningPubKey must be empty. "
246 STTx const stx =
STTx{std::move(rb)};
248 if (!uniqueHashes.
emplace(hash).second)
250 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
251 <<
"duplicate Txn found. "
256 auto const txType = stx.
getFieldU16(sfTransactionType);
257 if (txType == ttBATCH)
259 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
260 <<
"batch cannot have an inner batch txn. "
267 return txType == disabled;
275 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
276 <<
"inner txn must have the tfInnerBatchTxn flag. "
281 if (
auto const ret = checkSignatureFields(stx, hash))
288 auto const counterpartySignature = stx.
getFieldObject(sfCounterpartySignature);
290 checkSignatureFields(counterpartySignature, hash,
"counterparty signature "))
297 if (
auto const fee = stx.
getFieldAmount(sfFee); !fee.native() || fee.xrp() != beast::zero)
299 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
300 <<
"inner txn must have a fee of 0. "
306 if (
auto const preflightResult =
310 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
311 <<
"inner txn preflight failed: " <<
transHuman(preflightResult.ter)
320 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
321 <<
"inner txn must have exactly one of Sequence and "
330 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
331 <<
"inner txn must have either Sequence or "
338 if ((flags & (tfAllOrNothing | tfUntilFailure)) != 0u)
340 if (
auto const seq = stx.
getFieldU32(sfSequence); seq != 0)
342 if (!accountSeqTicket[innerAccount].insert(seq).second)
344 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
345 <<
"duplicate sequence found: "
353 if (
auto const ticket = stx.
getFieldU32(sfTicketSequence);
354 !accountSeqTicket[innerAccount].
insert(ticket).second)
356 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
357 <<
"duplicate ticket found: "
379 auto const innerAccount = rb.getAccountID(sfAccount);
383 if (innerAccount != outerAccount)
384 requiredSigners.
insert(innerAccount);
387 if (
auto const counterparty = rb.at(~sfCounterparty);
388 counterparty && counterparty != outerAccount)
389 requiredSigners.
insert(*counterparty);
401 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
402 <<
"signers array exceeds 8 entries.";
411 for (
auto const& signer : signers)
413 AccountID const signerAccount = signer.getAccountID(sfAccount);
414 if (signerAccount == outerAccount)
416 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
417 <<
"signer cannot be the outer account: " << signerAccount;
421 if (!batchSigners.
insert(signerAccount).second)
423 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
424 <<
"duplicate signer found: " << signerAccount;
430 if (requiredSigners.
erase(signerAccount) == 0)
432 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
433 <<
"no account signature for inner txn.";
443 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
444 <<
"invalid batch txn signature: " << sigResult.error();
449 if (!requiredSigners.
empty())
451 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
452 <<
"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.
constexpr FlagValue tfInnerBatchTxn
PreflightResult preflight(ServiceRegistry ®istry, Rules const &rules, STTx const &tx, ApplyFlags flags, beast::Journal j)
Gate a transaction based on static information.
beast::Journal debugLog()
Returns a debug journal.
std::string transHuman(TER code)
std::size_t constexpr maxBatchTxCount
The maximum number of transactions that can be in a batch.
bool isTesSuccess(TER x) noexcept
constexpr XRPAmount INITIAL_XRP
Configure the native currency.
XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Compute only the expected base fee for a transaction.
XRPAmount base
Cost of a reference transaction in drops.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.
std::reference_wrapper< ServiceRegistry > registry