1#include <xrpl/tx/transactors/system/Batch.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/core/ServiceRegistry.h>
8#include <xrpl/ledger/ApplyView.h>
9#include <xrpl/ledger/ReadView.h>
10#include <xrpl/ledger/helpers/SponsorHelpers.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/Protocol.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/STAccount.h>
15#include <xrpl/protocol/STLedgerEntry.h>
16#include <xrpl/protocol/STObject.h>
17#include <xrpl/protocol/STTx.h>
18#include <xrpl/protocol/TER.h>
19#include <xrpl/protocol/TxFlags.h>
20#include <xrpl/protocol/TxFormats.h>
21#include <xrpl/protocol/XRPAmount.h>
22#include <xrpl/tx/Transactor.h>
23#include <xrpl/tx/applySteps.h>
52std::optional<XRPAmount>
61 if (baseFee > maxAmount -
view.fees().base)
63 JLOG(
debugLog().error()) <<
"BatchTrace: Base fee overflow detected.";
77 if (txnFees > maxAmount - fee)
79 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in txnFees calculation.";
95 JLOG(
debugLog().error()) <<
"BatchTrace: Batch Signers array exceeds max entries.";
100 for (
STObject const& signer : signers)
102 if (signer.isFieldPresent(sfTxnSignature))
106 else if (signer.isFieldPresent(sfSigners))
108 auto const& nestedSigners = signer.getFieldArray(sfSigners);
113 <<
"BatchTrace: Nested Signers array exceeds max entries.";
117 signerCount += nestedSigners.size();
123 if (signerCount > 0 &&
view.fees().base > maxAmount / signerCount)
125 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in signerCount calculation.";
133 if (signerFees > maxAmount - txnFees)
135 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in signerFees calculation.";
138 XRPAmount const innerFees = txnFees + signerFees;
139 if (innerFees > maxAmount - batchBase)
141 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in total fee calculation.";
147 return innerFees + batchBase;
157 return view.fees().base;
213 if (
std::popcount(flags & (tfAllOrNothing | tfOnlyOne | tfUntilFailure | tfIndependent)) != 1)
215 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
216 <<
"too many flags.";
224 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
225 <<
"spfSponsorReserve is not allowed on outer Batch.";
231 if (rawTxns.size() <= 1)
233 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
234 <<
"txns array must have at least 2 entries.";
241 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
249 auto checkSignatureFields =
250 [&parentBatchId, &j = ctx.
j](
252 if (sig.isFieldPresent(sfTxnSignature))
254 JLOG(j.debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
255 <<
"inner txn " << label <<
"cannot include TxnSignature. "
260 if (sig.isFieldPresent(sfSigners))
262 JLOG(j.debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
263 <<
"inner txn " << label <<
" cannot include Signers. "
268 if (!sig.getFieldVL(sfSigningPubKey).empty())
270 JLOG(j.debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
271 <<
"inner txn " << label <<
" SigningPubKey must be empty. "
280 STTx const& stx = *stxPtr;
282 if (!uniqueHashes.
emplace(hash).second)
284 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
285 <<
"duplicate Txn found. "
290 auto const txType = stx.
getFieldU16(sfTransactionType);
292 kDisabledTxTypes, [txType](
auto const& disabled) {
return txType == disabled; }))
299 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
300 <<
"inner txn must have the tfInnerBatchTxn flag. "
305 if (
auto const ret = checkSignatureFields(stx, hash))
312 auto const counterpartySignature = stx.
getFieldObject(sfCounterpartySignature);
314 checkSignatureFields(counterpartySignature, hash,
"counterparty signature "))
321 auto const sponsorSignature = stx.
getFieldObject(sfSponsorSignature);
322 if (
auto const ret = checkSignatureFields(sponsorSignature, hash,
"sponsor signature "))
331 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
332 <<
"inner txn must have a fee of 0. "
342 if (
auto const preflightResult =
346 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
347 <<
"inner txn preflight failed: " <<
transHuman(preflightResult.ter)
356 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
357 <<
"inner txn must have exactly one of Sequence and "
366 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
367 <<
"inner txn must have either Sequence or "
374 if ((flags & (tfAllOrNothing | tfUntilFailure)) != 0u)
376 if (
auto const seq = stx.
getFieldU32(sfSequence); seq != 0)
378 if (!accountSeqTicket[innerAccount].insert(seq).second)
380 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
381 <<
"duplicate sequence found: "
389 if (
auto const ticket = stx.
getFieldU32(sfTicketSequence);
390 !accountSeqTicket[innerAccount].
insert(ticket).second)
392 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
393 <<
"duplicate ticket found: "
408 ctx.
tx.
getTxnType() == ttBATCH,
"xrpl::Batch::preflightSigValidated : batch transaction");
418 STTx const& rb = *stxPtr;
425 if (authorizer != outerAccount)
429 if (
auto const counterparty = rb[~sfCounterparty];
430 counterparty && counterparty != outerAccount)
431 requiredSigners.
push_back(*counterparty);
433 if (
auto const sponsor = rb.
at(~sfSponsor);
434 sponsor && rb.
isFieldPresent(sfSponsorSignature) && sponsor != outerAccount)
439 requiredSigners.
erase(dupes.begin(), dupes.end());
452 for (
auto const& signer : signers)
454 AccountID const signerAccount = signer.getAccountID(sfAccount);
455 if (signerAccount == outerAccount)
457 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
458 <<
"signer cannot be the outer account: " << signerAccount;
462 if (lastBatchSigner == signerAccount)
464 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
465 <<
"duplicate signer found: " << signerAccount;
469 if (lastBatchSigner > signerAccount)
471 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
472 <<
"unsorted signers array: " << signerAccount;
475 lastBatchSigner = signerAccount;
477 if (numReqSignersMatched >= requiredSigners.
size() ||
478 requiredSigners[numReqSignersMatched] != signerAccount)
480 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
481 <<
"missing signer or extra signer provided: " << signerAccount;
484 ++numReqSignersMatched;
490 if (numReqSignersMatched != requiredSigners.
size())
492 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
493 <<
"invalid batch signers.";
504 for (
auto const& signer : signers)
510 auto const idAccount = signer.getAccountID(sfAccount);
A generic endpoint for log messages.
UInt size() const
Number of values in array or object.
static TER preclaim(PreclaimContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static std::optional< XRPAmount > calculateBaseFeeImpl(ReadView const &view, STTx const &tx)
Calculates the total base fee for a batch transaction.
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
static NotTEC preflight(PreflightContext const &ctx)
Performs preflight validation checks for a Batch transaction.
static NotTEC checkBatchSign(PreclaimContext const &ctx)
static NotTEC checkSign(PreclaimContext const &ctx)
Checks the validity of signatures for a batch transaction.
TER doApply() override
Applies the outer batch transaction.
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
static constexpr auto kDisabledTxTypes
static NotTEC preflightSigValidated(PreflightContext const &ctx)
bool native() const noexcept
std::shared_ptr< STLedgerEntry const > const & const_ref
T::value_type at(TypedField< T > const &f) const
Get the value of a field.
std::uint32_t getFieldU32(SField const &field) const
STArray const & getFieldArray(SField const &field) const
bool isFlag(std::uint32_t) 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
static constexpr std::size_t kMaxMultiSigners
TxType getTxnType() const
AccountID getInitiator() const
The account responsible for the authorization: the delegate when sfDelegate is present,...
uint256 getTransactionID() const
std::vector< std::shared_ptr< STTx const > > const & getBatchTransactions() const
The inner transactions of a Batch, built and validated at construction.
static NotTEC checkSign(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
constexpr std::size_t kMaxBatchSigners
The maximum number of batch signers.
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)
bool isReserveSponsored(STTx const &tx)
Whether the transaction's reserve is sponsored (sfSponsor present + spfSponsorReserve set).
TERSubset< CanCvtToNotTEC > NotTEC
bool isFeeSponsored(STTx const &tx)
Whether the transaction's fee is sponsored (sfSponsor present + spfSponsorFee set).
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Compute only the expected base fee for a transaction.
State information when determining if a tx is likely to claim a fee.
std::optional< uint256 const > const parentBatchId
State information when preflighting a tx.
std::reference_wrapper< ServiceRegistry > registry