rippled
Loading...
Searching...
No Matches
Batch.cpp
1#include <xrpld/app/tx/apply.h>
2#include <xrpld/app/tx/detail/Batch.h>
3
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>
11
12namespace ripple {
13
34XRPAmount
35Batch::calculateBaseFee(ReadView const& view, STTx const& tx)
36{
37 XRPAmount const maxAmount{
39
40 // batchBase: view.fees().base for batch processing + default base fee
41 XRPAmount const baseFee = Transactor::calculateBaseFee(view, tx);
42
43 // LCOV_EXCL_START
44 if (baseFee > maxAmount - view.fees().base)
45 {
46 JLOG(debugLog().error()) << "BatchTrace: Base fee overflow detected.";
47 return XRPAmount{INITIAL_XRP};
48 }
49 // LCOV_EXCL_STOP
50
51 XRPAmount const batchBase = view.fees().base + baseFee;
52
53 // Calculate the Inner Txn Fees
54 XRPAmount txnFees{0};
55 if (tx.isFieldPresent(sfRawTransactions))
56 {
57 auto const& txns = tx.getFieldArray(sfRawTransactions);
58
59 // LCOV_EXCL_START
60 if (txns.size() > maxBatchTxCount)
61 {
62 JLOG(debugLog().error())
63 << "BatchTrace: Raw Transactions array exceeds max entries.";
64 return XRPAmount{INITIAL_XRP};
65 }
66 // LCOV_EXCL_STOP
67
68 for (STObject txn : txns)
69 {
70 STTx const stx = STTx{std::move(txn)};
71
72 // LCOV_EXCL_START
73 if (stx.getTxnType() == ttBATCH)
74 {
75 JLOG(debugLog().error())
76 << "BatchTrace: Inner Batch transaction found.";
77 return XRPAmount{INITIAL_XRP};
78 }
79 // LCOV_EXCL_STOP
80
81 auto const fee = ripple::calculateBaseFee(view, stx);
82 // LCOV_EXCL_START
83 if (txnFees > maxAmount - fee)
84 {
85 JLOG(debugLog().error())
86 << "BatchTrace: XRPAmount overflow in txnFees calculation.";
87 return XRPAmount{INITIAL_XRP};
88 }
89 // LCOV_EXCL_STOP
90 txnFees += fee;
91 }
92 }
93
94 // Calculate the Signers/BatchSigners Fees
95 std::int32_t signerCount = 0;
96 if (tx.isFieldPresent(sfBatchSigners))
97 {
98 auto const& signers = tx.getFieldArray(sfBatchSigners);
99
100 // LCOV_EXCL_START
101 if (signers.size() > maxBatchTxCount)
102 {
103 JLOG(debugLog().error())
104 << "BatchTrace: Batch Signers array exceeds max entries.";
105 return XRPAmount{INITIAL_XRP};
106 }
107 // LCOV_EXCL_STOP
108
109 for (STObject const& signer : signers)
110 {
111 if (signer.isFieldPresent(sfTxnSignature))
112 signerCount += 1;
113 else if (signer.isFieldPresent(sfSigners))
114 signerCount += signer.getFieldArray(sfSigners).size();
115 }
116 }
117
118 // LCOV_EXCL_START
119 if (signerCount > 0 && view.fees().base > maxAmount / signerCount)
120 {
121 JLOG(debugLog().error())
122 << "BatchTrace: XRPAmount overflow in signerCount calculation.";
123 return XRPAmount{INITIAL_XRP};
124 }
125 // LCOV_EXCL_STOP
126
127 XRPAmount signerFees = signerCount * view.fees().base;
128
129 // LCOV_EXCL_START
130 if (signerFees > maxAmount - txnFees)
131 {
132 JLOG(debugLog().error())
133 << "BatchTrace: XRPAmount overflow in signerFees calculation.";
134 return XRPAmount{INITIAL_XRP};
135 }
136 if (txnFees + signerFees > maxAmount - batchBase)
137 {
138 JLOG(debugLog().error())
139 << "BatchTrace: XRPAmount overflow in total fee calculation.";
140 return XRPAmount{INITIAL_XRP};
141 }
142 // LCOV_EXCL_STOP
143
144 // 10 drops per batch signature + sum of inner tx fees + batchBase
145 return signerFees + txnFees + batchBase;
146}
147
150{
151 return tfBatchMask;
152}
153
187NotTEC
189{
190 auto const parentBatchId = ctx.tx.getTransactionID();
191 auto const flags = ctx.tx.getFlags();
192
193 if (std::popcount(
194 flags &
196 {
197 JLOG(ctx.j.debug()) << "BatchTrace[" << parentBatchId << "]:"
198 << "too many flags.";
199 return temINVALID_FLAG;
200 }
201
202 auto const& rawTxns = ctx.tx.getFieldArray(sfRawTransactions);
203 if (rawTxns.size() <= 1)
204 {
205 JLOG(ctx.j.debug()) << "BatchTrace[" << parentBatchId << "]:"
206 << "txns array must have at least 2 entries.";
207 return temARRAY_EMPTY;
208 }
209
210 if (rawTxns.size() > maxBatchTxCount)
211 {
212 JLOG(ctx.j.debug()) << "BatchTrace[" << parentBatchId << "]:"
213 << "txns array exceeds 8 entries.";
214 return temARRAY_TOO_LARGE;
215 }
216
217 // Validation Inner Batch Txns
218 std::unordered_set<uint256> uniqueHashes;
220 accountSeqTicket;
221 auto checkSignatureFields = [&parentBatchId, &j = ctx.j](
222 STObject const& sig,
223 uint256 const& hash,
224 char const* label = "") -> NotTEC {
225 if (sig.isFieldPresent(sfTxnSignature))
226 {
227 JLOG(j.debug())
228 << "BatchTrace[" << parentBatchId << "]: "
229 << "inner txn " << label << "cannot include TxnSignature. "
230 << "txID: " << hash;
231 return temBAD_SIGNATURE;
232 }
233
234 if (sig.isFieldPresent(sfSigners))
235 {
236 JLOG(j.debug())
237 << "BatchTrace[" << parentBatchId << "]: "
238 << "inner txn " << label << " cannot include Signers. "
239 << "txID: " << hash;
240 return temBAD_SIGNER;
241 }
242
243 if (!sig.getFieldVL(sfSigningPubKey).empty())
244 {
245 JLOG(j.debug())
246 << "BatchTrace[" << parentBatchId << "]: "
247 << "inner txn " << label << " SigningPubKey must be empty. "
248 << "txID: " << hash;
249 return temBAD_REGKEY;
250 }
251
252 return tesSUCCESS;
253 };
254 for (STObject rb : rawTxns)
255 {
256 STTx const stx = STTx{std::move(rb)};
257 auto const hash = stx.getTransactionID();
258 if (!uniqueHashes.emplace(hash).second)
259 {
260 JLOG(ctx.j.debug()) << "BatchTrace[" << parentBatchId << "]: "
261 << "duplicate Txn found. "
262 << "txID: " << hash;
263 return temREDUNDANT;
264 }
265
266 if (stx.getFieldU16(sfTransactionType) == ttBATCH)
267 {
268 JLOG(ctx.j.debug()) << "BatchTrace[" << parentBatchId << "]: "
269 << "batch cannot have an inner batch txn. "
270 << "txID: " << hash;
271 return temINVALID;
272 }
273
274 if (!(stx.getFlags() & tfInnerBatchTxn))
275 {
276 JLOG(ctx.j.debug())
277 << "BatchTrace[" << parentBatchId << "]: "
278 << "inner txn must have the tfInnerBatchTxn flag. "
279 << "txID: " << hash;
280 return temINVALID_FLAG;
281 }
282
283 if (auto const ret = checkSignatureFields(stx, hash))
284 return ret;
285
286 /* Placeholder for field that will be added by Lending Protocol
287 // Note that the CounterpartySignature is optional, and should not be
288 // included, but if it is, ensure it doesn't contain a signature.
289 if (stx.isFieldPresent(sfCounterpartySignature))
290 {
291 auto const counterpartySignature =
292 stx.getFieldObject(sfCounterpartySignature);
293 if (auto const ret = checkSignatureFields(
294 counterpartySignature, hash, "counterparty signature "))
295 {
296 return ret;
297 }
298 }
299 */
300
301 auto const innerAccount = stx.getAccountID(sfAccount);
302 if (auto const preflightResult = ripple::preflight(
303 ctx.app, ctx.rules, parentBatchId, stx, tapBATCH, ctx.j);
304 preflightResult.ter != tesSUCCESS)
305 {
306 JLOG(ctx.j.debug()) << "BatchTrace[" << parentBatchId << "]: "
307 << "inner txn preflight failed: "
308 << transHuman(preflightResult.ter) << " "
309 << "txID: " << hash;
311 }
312
313 // Check that the fee is zero
314 if (auto const fee = stx.getFieldAmount(sfFee);
315 !fee.native() || fee.xrp() != beast::zero)
316 {
317 JLOG(ctx.j.debug()) << "BatchTrace[" << parentBatchId << "]: "
318 << "inner txn must have a fee of 0. "
319 << "txID: " << hash;
320 return temBAD_FEE;
321 }
322
323 // Check that Sequence and TicketSequence are not both present
324 if (stx.isFieldPresent(sfTicketSequence) &&
325 stx.getFieldU32(sfSequence) != 0)
326 {
327 JLOG(ctx.j.debug())
328 << "BatchTrace[" << parentBatchId << "]: "
329 << "inner txn must have exactly one of Sequence and "
330 "TicketSequence. "
331 << "txID: " << hash;
332 return temSEQ_AND_TICKET;
333 }
334
335 // Verify that either Sequence or TicketSequence is present
336 if (!stx.isFieldPresent(sfTicketSequence) &&
337 stx.getFieldU32(sfSequence) == 0)
338 {
339 JLOG(ctx.j.debug()) << "BatchTrace[" << parentBatchId << "]: "
340 << "inner txn must have either Sequence or "
341 "TicketSequence. "
342 << "txID: " << hash;
343 return temSEQ_AND_TICKET;
344 }
345
346 // Duplicate sequence and ticket checks
347 if (flags & (tfAllOrNothing | tfUntilFailure))
348 {
349 if (auto const seq = stx.getFieldU32(sfSequence); seq != 0)
350 {
351 if (!accountSeqTicket[innerAccount].insert(seq).second)
352 {
353 JLOG(ctx.j.debug())
354 << "BatchTrace[" << parentBatchId << "]: "
355 << "duplicate sequence found: "
356 << "txID: " << hash;
357 return temREDUNDANT;
358 }
359 }
360
361 if (stx.isFieldPresent(sfTicketSequence))
362 {
363 if (auto const ticket = stx.getFieldU32(sfTicketSequence);
364 !accountSeqTicket[innerAccount].insert(ticket).second)
365 {
366 JLOG(ctx.j.debug())
367 << "BatchTrace[" << parentBatchId << "]: "
368 << "duplicate ticket found: "
369 << "txID: " << hash;
370 return temREDUNDANT;
371 }
372 }
373 }
374 }
375
376 return tesSUCCESS;
377}
378
379NotTEC
381{
382 auto const parentBatchId = ctx.tx.getTransactionID();
383 auto const outerAccount = ctx.tx.getAccountID(sfAccount);
384 auto const& rawTxns = ctx.tx.getFieldArray(sfRawTransactions);
385
386 // Build the signers list
387 std::unordered_set<AccountID> requiredSigners;
388 for (STObject const& rb : rawTxns)
389 {
390 auto const innerAccount = rb.getAccountID(sfAccount);
391
392 // If the inner account is the same as the outer account, do not add the
393 // inner account to the required signers set.
394 if (innerAccount != outerAccount)
395 requiredSigners.insert(innerAccount);
396 /* Placeholder for field that will be added by Lending Protocol
397 // Some transactions have a Counterparty, who must also sign the
398 // transaction if they are not the outer account
399 if (auto const counterparty = rb.at(~sfCounterparty);
400 counterparty && counterparty != outerAccount)
401 requiredSigners.insert(*counterparty);
402 */
403 }
404
405 // Validation Batch Signers
407 if (ctx.tx.isFieldPresent(sfBatchSigners))
408 {
409 STArray const& signers = ctx.tx.getFieldArray(sfBatchSigners);
410
411 // Check that the batch signers array is not too large.
412 if (signers.size() > maxBatchTxCount)
413 {
414 JLOG(ctx.j.debug()) << "BatchTrace[" << parentBatchId << "]: "
415 << "signers array exceeds 8 entries.";
416 return temARRAY_TOO_LARGE;
417 }
418
419 // Add batch signers to the set to ensure all signer accounts are
420 // unique. Meanwhile, remove signer accounts from the set of inner
421 // transaction accounts (`requiredSigners`). By the end of the loop,
422 // `requiredSigners` should be empty, indicating that all inner
423 // accounts are matched with signers.
424 for (auto const& signer : signers)
425 {
426 AccountID const signerAccount = signer.getAccountID(sfAccount);
427 if (signerAccount == outerAccount)
428 {
429 JLOG(ctx.j.debug())
430 << "BatchTrace[" << parentBatchId << "]: "
431 << "signer cannot be the outer account: " << signerAccount;
432 return temBAD_SIGNER;
433 }
434
435 if (!batchSigners.insert(signerAccount).second)
436 {
437 JLOG(ctx.j.debug())
438 << "BatchTrace[" << parentBatchId << "]: "
439 << "duplicate signer found: " << signerAccount;
440 return temREDUNDANT;
441 }
442
443 // Check that the batch signer is in the required signers set.
444 // Remove it if it does, as it can be crossed off the list.
445 if (requiredSigners.erase(signerAccount) == 0)
446 {
447 JLOG(ctx.j.debug()) << "BatchTrace[" << parentBatchId << "]: "
448 << "no account signature for inner txn.";
449 return temBAD_SIGNER;
450 }
451 }
452
453 // Check the batch signers signatures.
454 auto const sigResult = ctx.tx.checkBatchSign(
456
457 if (!sigResult)
458 {
459 JLOG(ctx.j.debug())
460 << "BatchTrace[" << parentBatchId << "]: "
461 << "invalid batch txn signature: " << sigResult.error();
462 return temBAD_SIGNATURE;
463 }
464 }
465
466 if (!requiredSigners.empty())
467 {
468 JLOG(ctx.j.debug()) << "BatchTrace[" << parentBatchId << "]: "
469 << "invalid batch signers.";
470 return temBAD_SIGNER;
471 }
472 return tesSUCCESS;
473}
474
492NotTEC
494{
495 if (auto ret = Transactor::checkSign(ctx); !isTesSuccess(ret))
496 return ret;
497
498 if (auto ret = Transactor::checkBatchSign(ctx); !isTesSuccess(ret))
499 return ret;
500
501 return tesSUCCESS;
502}
503
514TER
516{
517 return tesSUCCESS;
518}
519
520} // namespace ripple
Stream debug() const
Definition Journal.h:309
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
Definition Batch.cpp:149
TER doApply() override
Applies the outer batch transaction.
Definition Batch.cpp:515
static NotTEC preflight(PreflightContext const &ctx)
Performs preflight validation checks for a Batch transaction.
Definition Batch.cpp:188
static NotTEC preflightSigValidated(PreflightContext const &ctx)
Definition Batch.cpp:380
static NotTEC checkSign(PreclaimContext const &ctx)
Checks the validity of signatures for a batch transaction.
Definition Batch.cpp:493
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Calculates the total base fee for a batch transaction.
Definition Batch.cpp:35
A view into a ledger.
Definition ReadView.h:32
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:638
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:683
std::uint16_t getFieldU16(SField const &field) const
Definition STObject.cpp:590
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:596
STAmount const & getFieldAmount(SField const &field) const
Definition STObject.cpp:652
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:465
std::uint32_t getFlags() const
Definition STObject.cpp:518
TxType getTxnType() const
Definition STTx.h:218
uint256 getTransactionID() const
Definition STTx.h:230
Expected< void, std::string > checkBatchSign(RequireFullyCanonicalSig requireCanonicalSig, Rules const &rules) const
Definition STTx.cpp:283
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
static NotTEC checkSign(PreclaimContext const &ctx)
ApplyView & view()
Definition Transactor.h:144
static NotTEC checkBatchSign(PreclaimContext const &ctx)
T emplace(T... args)
T empty(T... args)
T erase(T... args)
T insert(T... args)
T max(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string transHuman(TER code)
Definition TER.cpp:254
constexpr std::uint32_t tfAllOrNothing
Definition TxFlags.h:257
constexpr std::uint32_t const tfBatchMask
Definition TxFlags.h:266
PreflightResult preflight(Application &app, Rules const &rules, STTx const &tx, ApplyFlags flags, beast::Journal j)
Gate a transaction based on static information.
constexpr std::uint32_t tfOnlyOne
Definition TxFlags.h:258
constexpr std::uint32_t tfIndependent
Definition TxFlags.h:260
XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Compute only the expected base fee for a transaction.
constexpr XRPAmount INITIAL_XRP
Configure the native currency.
constexpr std::uint32_t tfUntilFailure
Definition TxFlags.h:259
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:457
@ tesSUCCESS
Definition TER.h:226
bool isTesSuccess(TER x) noexcept
Definition TER.h:659
@ tapBATCH
Definition ApplyView.h:26
std::size_t constexpr maxBatchTxCount
The maximum number of transactions that can be in a batch.
Definition Protocol.h:163
constexpr std::uint32_t tfInnerBatchTxn
Definition TxFlags.h:42
@ temREDUNDANT
Definition TER.h:93
@ temBAD_FEE
Definition TER.h:73
@ temBAD_SIGNER
Definition TER.h:96
@ temSEQ_AND_TICKET
Definition TER.h:107
@ temINVALID_INNER_BATCH
Definition TER.h:124
@ temBAD_REGKEY
Definition TER.h:79
@ temINVALID
Definition TER.h:91
@ temINVALID_FLAG
Definition TER.h:92
@ temARRAY_EMPTY
Definition TER.h:121
@ temARRAY_TOO_LARGE
Definition TER.h:122
@ temBAD_SIGNATURE
Definition TER.h:86
T popcount(T... args)
XRPAmount base
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
State information when preflighting a tx.
Definition Transactor.h:16
beast::Journal const j
Definition Transactor.h:23