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/Feature.h>
13#include <xrpl/protocol/Protocol.h>
14#include <xrpl/protocol/SField.h>
15#include <xrpl/protocol/STAccount.h>
16#include <xrpl/protocol/STLedgerEntry.h>
17#include <xrpl/protocol/STObject.h>
18#include <xrpl/protocol/STTx.h>
19#include <xrpl/protocol/TER.h>
20#include <xrpl/protocol/TxFlags.h>
21#include <xrpl/protocol/TxFormats.h>
22#include <xrpl/protocol/XRPAmount.h>
23#include <xrpl/tx/Transactor.h>
24#include <xrpl/tx/applySteps.h>
26#include <boost/container/small_vector.hpp>
48 std::flat_set<Key, std::less<>, boost::container::small_vector<Key, kMaxBatchTxCount>>;
66std::optional<XRPAmount>
75 if (baseFee > maxAmount -
view.fees().base)
77 JLOG(
debugLog().error()) <<
"BatchTrace: Base fee overflow detected.";
93 <<
"BatchTrace: base fee of inner transaction " << stx->getTransactionID()
94 <<
" could not be computed: " <<
transToken(fee.error());
99 if (txnFees > maxAmount - *fee)
101 UNREACHABLE(
"XRPAmount overflow in txnFees calculation");
102 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in txnFees calculation.";
118 JLOG(
debugLog().error()) <<
"BatchTrace: Batch Signers array exceeds max entries.";
123 for (
STObject const& signer : signers)
125 if (signer.isFieldPresent(sfTxnSignature))
129 else if (signer.isFieldPresent(sfSigners))
131 auto const& nestedSigners = signer.getFieldArray(sfSigners);
136 <<
"BatchTrace: Nested Signers array exceeds max entries.";
140 signerCount += nestedSigners.size();
146 if (signerCount > 0 &&
view.fees().base > maxAmount / signerCount)
148 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in signerCount calculation.";
156 if (signerFees > maxAmount - txnFees)
158 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in signerFees calculation.";
161 XRPAmount const innerFees = txnFees + signerFees;
162 if (innerFees > maxAmount - batchBase)
164 JLOG(
debugLog().error()) <<
"BatchTrace: XRPAmount overflow in total fee calculation.";
170 return innerFees + batchBase;
180 return view.fees().base;
237 if (
std::popcount(flags & (tfAllOrNothing | tfOnlyOne | tfUntilFailure | tfIndependent)) != 1)
239 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
240 <<
"too many flags.";
248 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
249 <<
"spfSponsorReserve is not allowed on outer Batch.";
255 if (rawTxns.size() <= 1)
257 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
258 <<
"txns array must have at least 2 entries.";
265 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
271 auto const checkSignatureFields =
272 [&parentBatchId, &j = ctx.
j](
274 if (sig.isFieldPresent(sfTxnSignature))
276 JLOG(j.debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
277 <<
"inner txn " << label <<
"cannot include TxnSignature. "
282 if (sig.isFieldPresent(sfSigners))
284 JLOG(j.debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
285 <<
"inner txn " << label <<
" cannot include Signers. "
290 if (!sig.getFieldVL(sfSigningPubKey).empty())
292 JLOG(j.debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
293 <<
"inner txn " << label <<
" SigningPubKey must be empty. "
301 InlineSet<std::pair<AccountID, std::uint32_t>> accountSeqTicket;
302 InlineSet<UInt256> uniqueHashes;
306 STTx const& stx = *stxPtr;
310 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]:"
311 <<
"txns array may contain only RawTransaction objects.";
317 if (!uniqueHashes.emplace(hash).second)
319 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
320 <<
"duplicate Txn found. "
325 auto const txType = stx.
getFieldU16(sfTransactionType);
328 bool const rejectedPreV12 = !ctx.
rules.
enabled(featureLendingProtocolV1_2) &&
330 kDisabledTxTypes, [txType](
auto const& disabled) {
return txType == disabled; });
336 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
337 <<
"inner txn must have the tfInnerBatchTxn flag. "
342 if (
auto const ret = checkSignatureFields(stx, hash))
349 auto const counterpartySignature = stx.
getFieldObject(sfCounterpartySignature);
351 checkSignatureFields(counterpartySignature, hash,
"counterparty signature "))
358 auto const sponsorSignature = stx.
getFieldObject(sfSponsorSignature);
359 if (
auto const ret = checkSignatureFields(sponsorSignature, hash,
"sponsor signature "))
368 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
369 <<
"inner txn must have a fee of 0. "
391 if (
auto const preflightResult =
395 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
396 <<
"inner txn preflight failed: " <<
transHuman(preflightResult.ter)
405 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
406 <<
"inner txn must have exactly one of Sequence and "
415 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
416 <<
"inner txn must have either Sequence or "
424 if ((flags & (tfAllOrNothing | tfUntilFailure)) != 0u)
427 !accountSeqTicket.emplace(innerAccount, seq.value()).second)
430 <<
"BatchTrace[" << parentBatchId <<
"]: "
431 <<
"duplicate " << (seq.isTicket() ?
"ticket" :
"sequence") <<
" found: "
445 ctx.
tx.
getTxnType() == ttBATCH,
"xrpl::Batch::preflightSigValidated : batch transaction");
462 STTx const& rb = *stxPtr;
469 if (authorizer != outerAccount)
473 if (
auto const counterparty = rb[~sfCounterparty];
474 counterparty && counterparty != outerAccount)
475 requiredSigners.
push_back(*counterparty);
477 if (
auto const sponsor = rb.
at(~sfSponsor);
478 sponsor && rb.
isFieldPresent(sfSponsorSignature) && sponsor != outerAccount)
483 requiredSigners.
erase(dupes.begin(), dupes.end());
496 for (
auto const& signer : signers)
498 AccountID const signerAccount = signer.getAccountID(sfAccount);
499 if (signerAccount == outerAccount)
501 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
502 <<
"signer cannot be the outer account: " << signerAccount;
506 if (lastBatchSigner == signerAccount)
508 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
509 <<
"duplicate signer found: " << signerAccount;
513 if (lastBatchSigner > signerAccount)
515 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
516 <<
"unsorted signers array: " << signerAccount;
519 lastBatchSigner = signerAccount;
521 if (numReqSignersMatched >= requiredSigners.
size() ||
522 requiredSigners[numReqSignersMatched] != signerAccount)
524 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
525 <<
"missing signer or extra signer provided: " << signerAccount;
528 ++numReqSignersMatched;
534 if (numReqSignersMatched != requiredSigners.
size())
536 JLOG(ctx.
j.
debug()) <<
"BatchTrace[" << parentBatchId <<
"]: "
537 <<
"invalid batch signers.";
548 for (
auto const& signer : signers)
554 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)
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.
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this 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
Inner transaction types that preflight rejects before LendingProtocolV1_2.
static NotTEC preflightSigValidated(PreflightContext const &ctx)
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
bool native() const noexcept
SField const & getFName() const
std::shared_ptr< STLedgerEntry const > const & ConstRef
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
SeqProxy getSeqProxy() const
static constexpr std::size_t kMaxMultiSigners
TxType getTxnType() const
UInt256 getTransactionID() const
AccountID getInitiator() const
The account responsible for the authorization: the delegate when sfDelegate is present,...
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.
std::expected< XRPAmount, TER > calculateBaseFee(ReadView const &view, STTx const &tx)
Compute only the expected base fee for a transaction.
beast::Journal debugLog()
Returns a debug journal.
std::string transHuman(TER code)
std::string transToken(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
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