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>
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)
82 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in txnFees calculation.";
99 JLOG(
debugLog().error()) <<
"BatchTrace: Batch Signers array exceeds max entries.";
104 for (
STObject const& signer : signers)
106 if (signer.isFieldPresent(sfTxnSignature))
108 else if (signer.isFieldPresent(sfSigners))
109 signerCount += signer.getFieldArray(sfSigners).size();
114 if (signerCount > 0 &&
view.
fees().
base > maxAmount / signerCount)
116 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in signerCount calculation.";
124 if (signerFees > maxAmount - txnFees)
126 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in signerFees calculation.";
129 if (txnFees + signerFees > maxAmount - batchBase)
131 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in total fee calculation.";
137 return signerFees + txnFees + batchBase;
187 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
188 <<
"too many flags.";
193 if (rawTxns.size() <= 1)
195 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
196 <<
"txns array must have at least 2 entries.";
202 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
203 <<
"txns array exceeds 8 entries.";
210 auto checkSignatureFields = [&parentBatchId, &j = ctx.
j](
212 if (sig.isFieldPresent(sfTxnSignature))
214 JLOG(j.debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
215 <<
"inner txn " << label <<
"cannot include TxnSignature. "
220 if (sig.isFieldPresent(sfSigners))
222 JLOG(j.debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
223 <<
"inner txn " << label <<
" cannot include Signers. "
228 if (!sig.getFieldVL(sfSigningPubKey).empty())
230 JLOG(j.debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
231 <<
"inner txn " << label <<
" SigningPubKey must be empty. "
240 STTx const stx =
STTx{std::move(rb)};
242 if (!uniqueHashes.
emplace(hash).second)
244 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
245 <<
"duplicate Txn found. "
250 auto const txType = stx.
getFieldU16(sfTransactionType);
251 if (txType == ttBATCH)
253 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
254 <<
"batch cannot have an inner batch txn. "
260 return txType == disabled;
268 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
269 <<
"inner txn must have the tfInnerBatchTxn flag. "
274 if (
auto const ret = checkSignatureFields(stx, hash))
281 auto const counterpartySignature = stx.
getFieldObject(sfCounterpartySignature);
282 if (
auto const ret = checkSignatureFields(counterpartySignature, hash,
"counterparty signature "))
289 if (
auto const fee = stx.
getFieldAmount(sfFee); !fee.native() || fee.xrp() != beast::zero)
291 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
292 <<
"inner txn must have a fee of 0. "
301 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
302 <<
"inner txn preflight failed: " <<
transHuman(preflightResult.ter) <<
" "
310 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
311 <<
"inner txn must have exactly one of Sequence and "
320 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
321 <<
"inner txn must have either Sequence or "
330 if (
auto const seq = stx.
getFieldU32(sfSequence); seq != 0)
332 if (!accountSeqTicket[innerAccount].insert(seq).second)
334 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
335 <<
"duplicate sequence found: "
343 if (
auto const ticket = stx.
getFieldU32(sfTicketSequence);
344 !accountSeqTicket[innerAccount].
insert(ticket).second)
346 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
347 <<
"duplicate ticket found: "
369 auto const innerAccount = rb.getAccountID(sfAccount);
373 if (innerAccount != outerAccount)
374 requiredSigners.
insert(innerAccount);
377 if (
auto const counterparty = rb.at(~sfCounterparty); counterparty && counterparty != outerAccount)
378 requiredSigners.
insert(*counterparty);
390 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
391 <<
"signers array exceeds 8 entries.";
400 for (
auto const& signer : signers)
402 AccountID const signerAccount = signer.getAccountID(sfAccount);
403 if (signerAccount == outerAccount)
405 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
406 <<
"signer cannot be the outer account: " << signerAccount;
410 if (!batchSigners.
insert(signerAccount).second)
412 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
413 <<
"duplicate signer found: " << signerAccount;
419 if (requiredSigners.
erase(signerAccount) == 0)
421 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
422 <<
"no account signature for inner txn.";
432 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
433 <<
"invalid batch txn signature: " << sigResult.error();
438 if (!requiredSigners.
empty())
440 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
441 <<
"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.