xrpld
Loading...
Searching...
No Matches
Batch_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/SignerUtils.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/acctdelete.h>
6#include <test/jtx/amount.h>
7#include <test/jtx/balance.h> // IWYU pragma: keep
8#include <test/jtx/batch.h>
9#include <test/jtx/check.h>
10#include <test/jtx/delegate.h>
11#include <test/jtx/envconfig.h>
12#include <test/jtx/fee.h>
13#include <test/jtx/flags.h>
14#include <test/jtx/mpt.h>
15#include <test/jtx/multisign.h>
16#include <test/jtx/noop.h>
17#include <test/jtx/offer.h>
18#include <test/jtx/pay.h>
19#include <test/jtx/regkey.h>
20#include <test/jtx/seq.h>
21#include <test/jtx/sig.h>
22#include <test/jtx/tags.h>
23#include <test/jtx/ter.h>
24#include <test/jtx/ticket.h>
25#include <test/jtx/trust.h>
26#include <test/jtx/txflags.h>
27#include <test/jtx/utility.h>
28#include <test/jtx/vault.h>
29
30#include <xrpld/app/ledger/LedgerMaster.h>
31#include <xrpld/app/ledger/OpenLedger.h>
32#include <xrpld/app/misc/Transaction.h>
33#include <xrpld/app/misc/TxQ.h>
34
35#include <xrpl/basics/base_uint.h>
36#include <xrpl/basics/strHex.h>
37#include <xrpl/beast/unit_test/suite.h>
38#include <xrpl/beast/utility/Journal.h>
39#include <xrpl/config/BasicConfig.h>
40#include <xrpl/config/Constants.h>
41#include <xrpl/core/HashRouter.h>
42#include <xrpl/json/json_value.h>
43#include <xrpl/json/to_string.h>
44#include <xrpl/ledger/ApplyView.h>
45#include <xrpl/ledger/OpenView.h>
46#include <xrpl/protocol/AccountID.h>
47#include <xrpl/protocol/Batch.h>
48#include <xrpl/protocol/ErrorCodes.h>
49#include <xrpl/protocol/Feature.h>
50#include <xrpl/protocol/Indexes.h>
51#include <xrpl/protocol/Issue.h>
52#include <xrpl/protocol/KeyType.h>
53#include <xrpl/protocol/MPTIssue.h>
54#include <xrpl/protocol/Protocol.h>
55#include <xrpl/protocol/SField.h>
56#include <xrpl/protocol/STParsedJSON.h>
57#include <xrpl/protocol/STTx.h>
58#include <xrpl/protocol/SecretKey.h>
59#include <xrpl/protocol/SeqProxy.h>
60#include <xrpl/protocol/Serializer.h>
61#include <xrpl/protocol/Sign.h>
62#include <xrpl/protocol/TER.h>
63#include <xrpl/protocol/TxFlags.h>
64#include <xrpl/protocol/TxFormats.h>
65#include <xrpl/protocol/Units.h>
66#include <xrpl/protocol/XRPAmount.h>
67#include <xrpl/protocol/jss.h>
68#include <xrpl/server/NetworkOPs.h>
69#include <xrpl/tx/Transactor.h>
70#include <xrpl/tx/apply.h>
71#include <xrpl/tx/transactors/payment/Payment.h>
72#include <xrpl/tx/transactors/system/Batch.h>
73
74#include <cstddef>
75#include <cstdint>
76#include <exception>
77#include <iterator>
78#include <map>
79#include <memory>
80#include <optional>
81#include <string>
82#include <utility>
83#include <vector>
84
85namespace xrpl::test {
86
88{
97
103
104 static json::Value
105 getTxByIndex(json::Value const& jrr, int const index)
106 {
107 for (auto const& txn : jrr[jss::result][jss::ledger][jss::transactions])
108 {
109 if (txn[jss::metaData][sfTransactionIndex.jsonName] == index)
110 return txn;
111 }
112 return {};
113 }
114
115 static json::Value
117 {
118 json::Value params;
119 params[jss::ledger_index] = env.closed()->seq();
120 params[jss::transactions] = true;
121 params[jss::expand] = true;
122 return env.rpc("json", "ledger", to_string(params));
123 }
124
125 void
127 {
128 auto const jrr = getLastLedger(env);
129 auto const transactions = jrr[jss::result][jss::ledger][jss::transactions];
130 BEAST_EXPECT(transactions.size() == ledgerResults.size());
131 for (TestLedgerData const& ledgerResult : ledgerResults)
132 {
133 auto const txn = getTxByIndex(jrr, ledgerResult.index);
134 BEAST_EXPECT(txn[jss::hash].asString() == ledgerResult.txHash);
135 BEAST_EXPECT(txn.isMember(jss::metaData));
136 json::Value const meta = txn[jss::metaData];
137 BEAST_EXPECT(txn[sfTransactionType.jsonName] == ledgerResult.txType);
138 BEAST_EXPECT(meta[sfTransactionResult.jsonName] == ledgerResult.result);
139 if (ledgerResult.batchID)
140 {
142 env,
143 *ledgerResult.batchID,
144 ledgerResult.txHash,
145 ledgerResult.txType,
146 ledgerResult.result);
147 }
148 }
149 }
150
151 template <typename... Args>
153 submitBatch(jtx::Env& env, TER const& result, Args&&... args)
154 {
155 auto const batchTxn = env.jt(std::forward<Args>(args)...);
156 env(batchTxn, jtx::Ter(result));
157
158 auto const ids = batchTxn.stx->getBatchTransactionIDs();
160 txIDs.reserve(ids.size());
161 for (auto const& id : ids)
162 txIDs.push_back(strHex(id));
163 TxID const batchID = batchTxn.stx->getTransactionID();
164 return std::make_pair(txIDs, strHex(batchID));
165 }
166
167 static UInt256
168 getCheckIndex(AccountID const& account, std::uint32_t uSequence)
169 {
170 return keylet::check(account, SeqProxy::rawSequence(uSequence)).key;
171 }
172
176 std::map<std::string, std::string> extraVoting = {})
177 {
178 auto p = test::jtx::envconfig();
179 auto& section = p->section(Sections::kTransactionQueue);
180 section.set(Keys::kLedgersInQueue, "2");
181 section.set(Keys::kMinimumQueueSize, "2");
182 section.set(Keys::kMinLedgersToComputeSizeLimit, "3");
183 section.set(Keys::kMaxLedgerCountsToStore, "100");
184 section.set(Keys::kRetrySequencePercent, "25");
186
187 for (auto const& [k, v] : extraTxQ)
188 section.set(k, v);
189
190 return p;
191 }
192
193 static auto
194 openLedgerFee(jtx::Env& env, XRPAmount const& batchFee)
195 {
196 using namespace jtx;
197
198 auto const& view = *env.current();
199 auto const metrics = env.app().getTxQ().getMetrics(view);
200 return toDrops(metrics.openLedgerFeeLevel, batchFee) + 1;
201 }
202
203 void
205 {
206 using namespace test::jtx;
207 using namespace std::literals;
208
209 for (bool const withBatch : {true, false})
210 {
211 testcase << "enabled: Batch " << (withBatch ? "enabled" : "disabled");
212
213 auto const amend = withBatch ? features : features - featureBatchV1_1;
214
215 Env env{*this, amend};
216
217 auto const alice = Account("alice");
218 auto const bob = Account("bob");
219 auto const carol = Account("carol");
220 env.fund(XRP(10000), alice, bob, carol);
221 env.close();
222
223 // ttBatch
224 {
225 auto const seq = env.seq(alice);
226 auto const batchFee = batch::calcBatchFee(env, 0, 2);
227 auto const txResult = withBatch ? Ter(tesSUCCESS) : Ter(temDISABLED);
228 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
229 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
230 batch::Inner(pay(alice, bob, XRP(1)), seq + 2),
231 txResult);
232 env.close();
233 }
234
235 // tfInnerBatchTxn
236 // A standalone transaction carrying this flag is never valid, so it
237 // is rejected early in checkValidity() regardless of the amendment.
238 {
239 env(pay(alice, bob, XRP(1)), Txflags(tfInnerBatchTxn), Ter(telENV_RPC_FAILED));
240 env.close();
241 }
242
243 env.close();
244 }
245 }
246
247 void
249 {
250 testcase("preflight");
251
252 using namespace test::jtx;
253 using namespace std::literals;
254
255 //----------------------------------------------------------------------
256 // preflight
257
258 Env env{*this, features};
259
260 auto const alice = Account("alice");
261 auto const bob = Account("bob");
262 auto const carol = Account("carol");
263 env.fund(XRP(10000), alice, bob, carol);
264 env.close();
265
266 // temBAD_FEE: preflight1
267 {
268 env(batch::outer(alice, env.seq(alice), XRP(-1), tfAllOrNothing), Ter(temBAD_FEE));
269 env.close();
270 }
271
272 // DEFENSIVE: temINVALID_FLAG: Batch: inner batch flag.
273 // ACTUAL: telENV_RPC_FAILED: checkValidity()
274 {
275 auto const seq = env.seq(alice);
276 auto const batchFee = batch::calcBatchFee(env, 0, 0);
277 env(batch::outer(alice, seq, batchFee, tfInnerBatchTxn), Ter(telENV_RPC_FAILED));
278 env.close();
279 }
280
281 // temINVALID_FLAG: Batch: invalid flags.
282 {
283 auto const seq = env.seq(alice);
284 auto const batchFee = batch::calcBatchFee(env, 0, 0);
285 env(batch::outer(alice, seq, batchFee, tfDisallowXRP), Ter(temINVALID_FLAG));
286 env.close();
287 }
288
289 // temINVALID_FLAG: Batch: too many flags.
290 {
291 auto const seq = env.seq(alice);
292 auto const batchFee = batch::calcBatchFee(env, 0, 0);
293 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
294 Txflags(tfAllOrNothing | tfOnlyOne),
296 env.close();
297 }
298
299 // temARRAY_EMPTY: Batch: txns array must have at least 2 entries.
300 {
301 auto const seq = env.seq(alice);
302 auto const batchFee = batch::calcBatchFee(env, 0, 0);
303 env(batch::outer(alice, seq, batchFee, tfAllOrNothing), Ter(temARRAY_EMPTY));
304 env.close();
305 }
306
307 // temARRAY_EMPTY: Batch: txns array must have at least 2 entries.
308 {
309 auto const seq = env.seq(alice);
310 auto const batchFee = batch::calcBatchFee(env, 0, 0);
311 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
312 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
314 env.close();
315 }
316
317 // An oversized batch (more than kMaxBatchTxCount inners) fails STTx
318 // construction, so the transaction cannot be built.
319 {
320 auto const seq = env.seq(alice);
321 auto const batchFee = batch::calcBatchFee(env, 0, 9);
322 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
323 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
324 batch::Inner(pay(alice, bob, XRP(1)), seq + 2),
325 batch::Inner(pay(alice, bob, XRP(1)), seq + 3),
326 batch::Inner(pay(alice, bob, XRP(1)), seq + 4),
327 batch::Inner(pay(alice, bob, XRP(1)), seq + 5),
328 batch::Inner(pay(alice, bob, XRP(1)), seq + 6),
329 batch::Inner(pay(alice, bob, XRP(1)), seq + 7),
330 batch::Inner(pay(alice, bob, XRP(1)), seq + 8),
331 batch::Inner(pay(alice, bob, XRP(1)), seq + 9),
333 env.close();
334 }
335
336 // temREDUNDANT: Batch: duplicate Txn found.
337 {
338 auto const batchFee = batch::calcBatchFee(env, 1, 2);
339 auto const seq = env.seq(alice);
340 auto const jt = env.jtnofill(
341 batch::outer(alice, env.seq(alice), batchFee, tfAllOrNothing),
342 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
343 batch::Inner(pay(alice, bob, XRP(10)), seq + 1));
344
345 env(jt.jv, batch::Sig(bob), Ter(temREDUNDANT));
346 env.close();
347 }
348
349 // A batch may not contain a batch: the nested inner fails STTx
350 // construction, so the transaction cannot be built.
351 {
352 auto const seq = env.seq(alice);
353 auto const batchFee = batch::calcBatchFee(env, 0, 2);
354 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
355 batch::Inner(batch::outer(alice, seq, batchFee, tfAllOrNothing), seq),
356 batch::Inner(pay(alice, bob, XRP(1)), seq + 2),
358 env.close();
359 }
360
361 // temINVALID_FLAG: Batch: inner txn must have the
362 // tfInnerBatchTxn flag.
363 {
364 auto const batchFee = batch::calcBatchFee(env, 1, 2);
365 auto const seq = env.seq(alice);
366 auto tx1 = batch::Inner(pay(alice, bob, XRP(10)), seq + 1);
367 tx1[jss::Flags] = 0;
368 auto const jt = env.jtnofill(
369 batch::outer(alice, seq, batchFee, tfAllOrNothing),
370 tx1,
371 batch::Inner(pay(alice, bob, XRP(10)), seq + 2));
372
373 env(jt.jv, batch::Sig(bob), Ter(temINVALID_FLAG));
374 env.close();
375 }
376
377 // temBAD_SIGNATURE: Batch: inner txn cannot include TxnSignature.
378 {
379 auto const seq = env.seq(alice);
380 auto const batchFee = batch::calcBatchFee(env, 0, 2);
381 auto const jt = env.jt(pay(alice, bob, XRP(1)));
382 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
383 batch::Inner(jt.jv, seq + 1),
384 batch::Inner(pay(alice, bob, XRP(1)), seq + 2),
386 env.close();
387 }
388
389 // temBAD_SIGNER: Batch: inner txn cannot include Signers.
390 {
391 auto const seq = env.seq(alice);
392 auto const batchFee = batch::calcBatchFee(env, 0, 2);
393 auto tx1 = pay(alice, bob, XRP(1));
394 tx1[sfSigners.jsonName] = json::ValueType::Array;
395 tx1[sfSigners.jsonName][0U][sfSigner.jsonName] = json::ValueType::Object;
396 tx1[sfSigners.jsonName][0U][sfSigner.jsonName][sfAccount.jsonName] = alice.human();
397 tx1[sfSigners.jsonName][0U][sfSigner.jsonName][sfSigningPubKey.jsonName] =
398 strHex(alice.pk());
399 tx1[sfSigners.jsonName][0U][sfSigner.jsonName][sfTxnSignature.jsonName] = "DEADBEEF";
400 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
401 batch::Inner(tx1, seq + 1),
402 batch::Inner(pay(alice, bob, XRP(1)), seq + 2),
404 env.close();
405 }
406
407 // temBAD_REGKEY: Batch: inner txn must include empty
408 // SigningPubKey.
409 {
410 auto const seq = env.seq(alice);
411 auto const batchFee = batch::calcBatchFee(env, 0, 2);
412 auto tx1 = batch::Inner(pay(alice, bob, XRP(1)), seq + 1);
413 tx1[jss::SigningPubKey] = strHex(alice.pk());
414 auto const jt = env.jtnofill(
415 batch::outer(alice, seq, batchFee, tfAllOrNothing),
416 tx1,
417 batch::Inner(pay(alice, bob, XRP(1)), seq + 2));
418
419 env(jt.jv, Ter(temBAD_REGKEY));
420 env.close();
421 }
422
423 // temINVALID_INNER_BATCH: Batch: inner txn preflight failed.
424 {
425 auto const seq = env.seq(alice);
426 auto const batchFee = batch::calcBatchFee(env, 0, 2);
427 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
428 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
429 // amount can't be negative
430 batch::Inner(pay(alice, bob, XRP(-1)), seq + 2),
432 env.close();
433 }
434
435 // temINVALID_INNER_BATCH: tfInnerBatchTxn set but no parentBatchId.
436 {
437 auto const jtx = env.jt(pay(alice, bob, XRP(1)), Txflags(tfInnerBatchTxn));
438 PreflightContext const pfCtx(
439 env.app(), *jtx.stx, env.current()->rules(), TapNone, env.journal);
440 auto const pf = Transactor::invokePreflight<Payment>(pfCtx);
441 BEAST_EXPECT(pf == temINVALID_INNER_BATCH);
442 }
443
444 // temINVALID_FLAG: tfInnerBatchTxn set but featureBatchV1_1 disabled.
445 {
446 Env disabledEnv{*this, features - featureBatchV1_1};
447 disabledEnv.fund(XRP(10000), alice, bob);
448 disabledEnv.close();
449 auto const jtx = disabledEnv.jt(pay(alice, bob, XRP(1)), Txflags(tfInnerBatchTxn));
450 PreflightContext const pfCtx(
451 disabledEnv.app(),
452 *jtx.stx,
453 disabledEnv.current()->rules(),
454 TapNone,
455 disabledEnv.journal);
456 auto const pf = Transactor::invokePreflight<Payment>(pfCtx);
457 BEAST_EXPECT(pf == temINVALID_FLAG);
458 }
459
460 // temINVALID_INNER_BATCH: parentBatchId set but tfInnerBatchTxn not
461 // set.
462 {
463 auto const jtx = env.jt(pay(alice, bob, XRP(1)));
464 PreflightContext const pfCtx(
465 env.app(), *jtx.stx, UInt256{1}, env.current()->rules(), TapBatch, env.journal);
466 auto const pf = Transactor::invokePreflight<Payment>(pfCtx);
467 BEAST_EXPECT(pf == temINVALID_INNER_BATCH);
468 }
469
470 // temBAD_FEE: Batch: inner txn must have a fee of 0.
471 {
472 auto const seq = env.seq(alice);
473 auto const batchFee = batch::calcBatchFee(env, 0, 2);
474 auto tx1 = batch::Inner(pay(alice, bob, XRP(1)), seq + 1);
475 tx1[jss::Fee] = to_string(env.current()->fees().base);
476 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
477 tx1,
478 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
479 Ter(temBAD_FEE));
480 env.close();
481 }
482
483 // temBAD_FEE: Inner txn with negative fee
484 {
485 auto const seq = env.seq(alice);
486 auto const batchFee = batch::calcBatchFee(env, 0, 2);
487 auto tx1 = batch::Inner(pay(alice, bob, XRP(1)), seq + 1);
488 tx1[jss::Fee] = "-1";
489 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
490 tx1,
491 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
492 Ter(temBAD_FEE));
493 env.close();
494 }
495
496 // temBAD_FEE: Inner txn with non-integer fee
497 {
498 auto const seq = env.seq(alice);
499 auto const batchFee = batch::calcBatchFee(env, 0, 2);
500 auto tx1 = batch::Inner(pay(alice, bob, XRP(1)), seq + 1);
501 tx1[jss::Fee] = "1.5";
502 auto const g = env.getParseFailureGuard(true);
503 try
504 {
505 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
506 tx1,
507 batch::Inner(pay(alice, bob, XRP(2)), seq + 2));
508 fail("Expected parse_error for fractional fee");
509 }
510 catch (jtx::ParseError const&)
511 {
512 BEAST_EXPECT(true);
513 }
514 }
515
516 // temSEQ_AND_TICKET: Batch: inner txn cannot have both Sequence
517 // and TicketSequence.
518 {
519 auto const seq = env.seq(alice);
520 auto const batchFee = batch::calcBatchFee(env, 0, 2);
521 auto tx1 = batch::Inner(pay(alice, bob, XRP(1)), 0, 1);
522 tx1[jss::Sequence] = seq + 1;
523 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
524 tx1,
525 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
527 env.close();
528 }
529
530 // temSEQ_AND_TICKET: Batch: inner txn must have either Sequence or
531 // TicketSequence.
532 {
533 auto const seq = env.seq(alice);
534 auto const batchFee = batch::calcBatchFee(env, 0, 2);
535 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
536 batch::Inner(pay(alice, bob, XRP(1)), 0),
537 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
539 env.close();
540 }
541
542 // temREDUNDANT: Batch: duplicate sequence found:
543 {
544 auto const seq = env.seq(alice);
545 auto const batchFee = batch::calcBatchFee(env, 0, 2);
546 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
547 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
548 batch::Inner(pay(alice, bob, XRP(2)), seq + 1),
550 env.close();
551 }
552
553 // temREDUNDANT: Batch: duplicate ticket found:
554 {
555 auto const seq = env.seq(alice);
556 auto const batchFee = batch::calcBatchFee(env, 0, 2);
557 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
558 batch::Inner(pay(alice, bob, XRP(1)), 0, seq + 1),
559 batch::Inner(pay(alice, bob, XRP(2)), 0, seq + 1),
561 env.close();
562 }
563
564 // temREDUNDANT: Batch: duplicate ticket & sequence found:
565 {
566 auto const seq = env.seq(alice);
567 auto const batchFee = batch::calcBatchFee(env, 0, 2);
568 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
569 batch::Inner(pay(alice, bob, XRP(1)), 0, seq + 1),
570 batch::Inner(pay(alice, bob, XRP(2)), seq + 1),
572 env.close();
573 }
574
575 // DEFENSIVE: temARRAY_TOO_LARGE: Batch: signers array exceeds
576 // kMaxBatchSigners entries.
577 // ACTUAL: telENV_RPC_FAILED: isRawTransactionOkay()
578 {
579 auto const seq = env.seq(alice);
580 auto const batchFee = batch::calcBatchFee(env, kMaxBatchSigners + 1, 2);
581 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
582 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
583 batch::Inner(pay(alice, bob, XRP(5)), seq + 2),
586 env.close();
587 }
588
589 // temBAD_SIGNER: Batch: signer cannot be the outer account
590 {
591 auto const seq = env.seq(alice);
592 auto const batchFee = batch::calcBatchFee(env, 2, 2);
593 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
594 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
595 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
596 batch::Sig(alice, bob),
598 env.close();
599 }
600
601 // temBAD_SIGNER: Batch: duplicate signer (caught by ascending order check)
602 {
603 auto const seq = env.seq(alice);
604 auto const batchFee = batch::calcBatchFee(env, 2, 2);
605 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
606 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
607 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
608 batch::Sig(bob, bob),
610 env.close();
611 }
612
613 // temBAD_SIGNER: Batch: no account signature for inner txn.
614 // Note: Extra signature by bob
615 {
616 auto const seq = env.seq(alice);
617 auto const batchFee = batch::calcBatchFee(env, 1, 2);
618 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
619 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
620 batch::Inner(pay(alice, bob, XRP(5)), seq + 2),
621 batch::Sig(bob),
623 env.close();
624 }
625
626 // temBAD_SIGNER: Batch: no account signature for inner txn.
627 {
628 auto const seq = env.seq(alice);
629 auto const batchFee = batch::calcBatchFee(env, 1, 2);
630 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
631 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
632 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
633 batch::Sig(carol),
635 env.close();
636 }
637
638 // temBAD_SIGNATURE: Batch: invalid batch txn signature.
639 {
640 auto const seq = env.seq(alice);
641 auto const bobSeq = env.seq(bob);
642 auto const batchFee = batch::calcBatchFee(env, 1, 2);
643 auto jt = env.jtnofill(
644 batch::outer(alice, env.seq(alice), batchFee, tfAllOrNothing),
645 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
646 batch::Inner(pay(bob, alice, XRP(5)), bobSeq));
647
648 Serializer msg;
650 msg,
651 jt.stx->getAccountID(sfAccount),
652 jt.stx->getSeqProxy().value(),
653 tfAllOrNothing,
654 jt.stx->getBatchTransactionIDs());
655 finishMultiSigningData(bob.id(), msg);
656 auto const sig = xrpl::sign(bob.pk(), bob.sk(), msg.slice());
657 jt.jv[sfBatchSigners.jsonName][0u][sfBatchSigner.jsonName][sfAccount.jsonName] =
658 bob.human();
659 jt.jv[sfBatchSigners.jsonName][0u][sfBatchSigner.jsonName][sfSigningPubKey.jsonName] =
660 strHex(alice.pk());
661 jt.jv[sfBatchSigners.jsonName][0u][sfBatchSigner.jsonName][sfTxnSignature.jsonName] =
662 strHex(Slice{sig.data(), sig.size()});
663
664 env(jt.jv, Ter(telENV_RPC_FAILED));
665 env.close();
666 }
667
668 // temBAD_SIGNER: Batch: invalid batch signers.
669 {
670 auto const seq = env.seq(alice);
671 auto const batchFee = batch::calcBatchFee(env, 2, 2);
672 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
673 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
674 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
675 batch::Inner(pay(carol, alice, XRP(5)), env.seq(carol)),
676 batch::Sig(bob),
678 env.close();
679 }
680 }
681
682 void
684 {
685 testcase("preclaim");
686
687 using namespace test::jtx;
688 using namespace std::literals;
689
690 //----------------------------------------------------------------------
691 // preclaim
692
693 Env env{*this, features};
694
695 auto const alice = Account("alice");
696 auto const bob = Account("bob");
697 auto const carol = Account("carol");
698 auto const dave = Account("dave");
699 auto const elsa = Account("elsa");
700 auto const frank = Account("frank");
701 auto const phantom = Account("phantom");
702 env.memoize(phantom);
703
704 env.fund(XRP(10000), alice, bob, carol, dave, elsa, frank);
705 env.close();
706
707 //----------------------------------------------------------------------
708 // checkSign.checkSingleSign
709
710 // tefBAD_AUTH: Bob is not authorized to sign for Alice
711 {
712 auto const seq = env.seq(alice);
713 auto const batchFee = batch::calcBatchFee(env, 3, 2);
714 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
715 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
716 batch::Inner(pay(alice, bob, XRP(20)), seq + 2),
717 Sig(bob),
719 env.close();
720 }
721
722 //----------------------------------------------------------------------
723 // checkBatchSign.checkMultiSign
724
725 // tefNOT_MULTI_SIGNING: SignersList not enabled
726 {
727 auto const seq = env.seq(alice);
728 auto const batchFee = batch::calcBatchFee(env, 3, 2);
729 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
730 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
731 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
732 batch::Msig(bob, {dave, carol}),
734 env.close();
735 }
736
737 env(signers(alice, 2, {{bob, 1}, {carol, 1}}));
738 env.close();
739
740 env(signers(bob, 2, {{carol, 1}, {dave, 1}, {elsa, 1}}));
741 env.close();
742
743 // tefBAD_SIGNATURE: Account not in SignersList
744 {
745 auto const seq = env.seq(alice);
746 auto const batchFee = batch::calcBatchFee(env, 3, 2);
747 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
748 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
749 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
750 batch::Msig(bob, {carol, frank}),
752 env.close();
753 }
754
755 // tefBAD_SIGNATURE: Wrong publicKey type
756 {
757 auto const seq = env.seq(alice);
758 auto const batchFee = batch::calcBatchFee(env, 3, 2);
759 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
760 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
761 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
762 batch::Msig(bob, {carol, Account("dave", KeyType::Ed25519)}),
764 env.close();
765 }
766
767 // tefMASTER_DISABLED: Master key disabled
768 {
769 env(regkey(elsa, frank));
770 env(fset(elsa, asfDisableMaster), Sig(elsa));
771 auto const seq = env.seq(alice);
772 auto const batchFee = batch::calcBatchFee(env, 3, 2);
773 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
774 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
775 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
776 batch::Msig(bob, {carol, elsa}),
778 env.close();
779 }
780
781 // tefBAD_SIGNATURE: Signer does not exist
782 {
783 auto const seq = env.seq(alice);
784 auto const batchFee = batch::calcBatchFee(env, 3, 2);
785 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
786 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
787 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
788 batch::Msig(bob, {carol, phantom}),
790 env.close();
791 }
792
793 // tefBAD_SIGNATURE: Signer has not enabled RegularKey
794 {
795 auto const seq = env.seq(alice);
796 auto const batchFee = batch::calcBatchFee(env, 3, 2);
797 Account const davo{"davo", KeyType::Ed25519};
798 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
799 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
800 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
801 batch::Msig(bob, {carol, Reg{dave, davo}}),
803 env.close();
804 }
805
806 // tefBAD_SIGNATURE: Wrong RegularKey Set
807 {
808 env(regkey(dave, frank));
809 auto const seq = env.seq(alice);
810 auto const batchFee = batch::calcBatchFee(env, 3, 2);
811 Account const davo{"davo", KeyType::Ed25519};
812 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
813 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
814 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
815 batch::Msig(bob, {carol, Reg{dave, davo}}),
816 Ter(tefBAD_SIGNATURE));
817 env.close();
818 }
819
820 // tefBAD_QUORUM
821 {
822 auto const seq = env.seq(alice);
823 auto const batchFee = batch::calcBatchFee(env, 2, 2);
824 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
825 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
826 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
827 batch::Msig(bob, {carol}),
829 env.close();
830 }
831
832 // tesSUCCESS: BatchSigners.Signers
833 {
834 auto const seq = env.seq(alice);
835 auto const batchFee = batch::calcBatchFee(env, 3, 2);
836 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
837 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
838 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
839 batch::Msig(bob, {carol, dave}),
840 Ter(tesSUCCESS));
841 env.close();
842 }
843
844 // tesSUCCESS: Multisign + BatchSigners.Signers
845 {
846 auto const seq = env.seq(alice);
847 auto const batchFee = batch::calcBatchFee(env, 4, 2);
848 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
849 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
850 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
851 batch::Msig(bob, {carol, dave}),
852 Msig(bob, carol),
853 Ter(tesSUCCESS));
854 env.close();
855 }
856
857 //----------------------------------------------------------------------
858 // checkBatchSign.checkSingleSign
859
860 // tefBAD_AUTH: Inner Account is not signer
861 {
862 auto const ledSeq = env.current()->seq();
863 auto const seq = env.seq(alice);
864 auto const batchFee = batch::calcBatchFee(env, 1, 2);
865 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
866 batch::Inner(pay(alice, phantom, XRP(1000)), seq + 1),
867 batch::Inner(noop(phantom), ledSeq),
868 batch::Sig(Reg{phantom, carol}),
870 env.close();
871 }
872
873 // tefBAD_AUTH: Account is not signer
874 {
875 auto const ledSeq = env.current()->seq();
876 auto const seq = env.seq(alice);
877 auto const batchFee = batch::calcBatchFee(env, 1, 2);
878 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
879 batch::Inner(pay(alice, bob, XRP(1000)), seq + 1),
880 batch::Inner(noop(bob), ledSeq),
881 batch::Sig(Reg{bob, carol}),
883 env.close();
884 }
885
886 // tesSUCCESS: Signed With Regular Key
887 {
888 env(regkey(bob, carol));
889 auto const seq = env.seq(alice);
890 auto const batchFee = batch::calcBatchFee(env, 1, 2);
891 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
892 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
893 batch::Inner(pay(bob, alice, XRP(2)), env.seq(bob)),
894 batch::Sig(Reg{bob, carol}),
895 Ter(tesSUCCESS));
896 env.close();
897 }
898
899 // tesSUCCESS: Signed With Master Key
900 {
901 auto const seq = env.seq(alice);
902 auto const batchFee = batch::calcBatchFee(env, 1, 2);
903 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
904 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
905 batch::Inner(pay(bob, alice, XRP(2)), env.seq(bob)),
906 batch::Sig(bob),
907 Ter(tesSUCCESS));
908 env.close();
909 }
910
911 // tefMASTER_DISABLED: Signed With Master Key Disabled
912 {
913 env(regkey(bob, carol));
914 env(fset(bob, asfDisableMaster), Sig(bob));
915 auto const seq = env.seq(alice);
916 auto const batchFee = batch::calcBatchFee(env, 1, 2);
917 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
918 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
919 batch::Inner(pay(bob, alice, XRP(2)), env.seq(bob)),
920 batch::Sig(bob),
922 env.close();
923 }
924 }
925
926 void
928 {
929 testcase("bad raw txn");
930
931 using namespace test::jtx;
932 using namespace std::literals;
933
934 Env env{*this, features};
935
936 auto const alice = Account("alice");
937 auto const bob = Account("bob");
938
939 env.fund(XRP(10000), alice, bob);
940
941 // An inner missing a required field can no longer be submitted: the
942 // outer STTx builds and validates each inner at construction, so
943 // building the batch (as signing does) throws. Returns true if the
944 // build fails.
945 auto const batchCtorFails = [&](json::StaticString const& field) -> bool {
946 auto const batchFee = batch::calcBatchFee(env, 1, 2);
947 auto const seq = env.seq(alice);
948 auto tx1 = batch::Inner(pay(alice, bob, XRP(10)), seq + 1);
949 tx1.removeMember(field);
950 try
951 {
952 // Env::st swallows the construction failure and yields a null
953 // stx, so a malformed inner shows up as no transaction built.
954 auto const jt = env.jtnofill(
955 batch::outer(alice, seq, batchFee, tfAllOrNothing),
956 tx1,
957 batch::Inner(pay(alice, bob, XRP(10)), seq + 2));
958 return jt.stx == nullptr;
959 }
960 catch (std::exception const&)
961 {
962 return true;
963 }
964 };
965
966 // Invalid: sfTransactionType
967 BEAST_EXPECT(batchCtorFails(jss::TransactionType));
968 // Invalid: sfAccount
969 BEAST_EXPECT(batchCtorFails(jss::Account));
970 // Invalid: sfSequence
971 BEAST_EXPECT(batchCtorFails(jss::Sequence));
972 // Invalid: sfFee
973 BEAST_EXPECT(batchCtorFails(jss::Fee));
974 // Invalid: sfSigningPubKey
975 BEAST_EXPECT(batchCtorFails(jss::SigningPubKey));
976
977 // Inner OfferCreate with MPT TakerPays. Valid under featureMPTokensV2.
978 {
979 MPTIssue const issue(makeMptID(1, alice));
980 STAmount const mptAmt{issue, UINT64_C(100)};
981
982 auto const batchFee = batch::calcBatchFee(env, 0, 2);
983 auto const seq = env.seq(alice);
984
985 json::Value tx1;
986 tx1[jss::TransactionType] = jss::OfferCreate;
987 tx1[jss::Account] = alice.human();
988 tx1[jss::TakerPays] = mptAmt.getJson(JsonOptions::Values::None);
989 tx1[jss::TakerGets] = XRP(10).value().getJson(JsonOptions::Values::None);
990
991 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
992 batch::Inner(tx1, seq + 1),
993 batch::Inner(pay(alice, bob, XRP(1)), seq + 2),
994 Ter(tesSUCCESS));
995 env.close();
996 }
997
998 // Invalid: inner txn with invalid memo (non-URL-safe MemoType)
999 {
1000 auto const batchFee = batch::calcBatchFee(env, 0, 2);
1001 auto const seq = env.seq(alice);
1002
1003 auto tx1 = pay(alice, bob, XRP(10));
1004 auto& ma = tx1["Memos"];
1005 auto& mi = ma[ma.size()];
1006 auto& m = mi["Memo"];
1007 m["MemoType"] = strHex(std::string("\x01\x02\x03", 3));
1008 m["MemoData"] = strHex(std::string("test"));
1009
1010 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
1011 batch::Inner(tx1, seq + 1),
1012 batch::Inner(pay(alice, bob, XRP(1)), seq + 2),
1014 env.close();
1015 }
1016 }
1017
1018 void
1020 {
1021 testcase("bad sequence");
1022
1023 using namespace test::jtx;
1024 using namespace std::literals;
1025
1026 Env env{*this, features};
1027
1028 auto const alice = Account("alice");
1029 auto const bob = Account("bob");
1030 auto const gw = Account("gw");
1031 auto const usd = gw["USD"];
1032
1033 env.fund(XRP(10000), alice, bob, gw);
1034 env.close();
1035 env.trust(usd(1000), alice, bob);
1036 env(pay(gw, alice, usd(100)));
1037 env(pay(gw, bob, usd(100)));
1038 env.close();
1039
1040 env(noop(bob), Ter(tesSUCCESS));
1041 env.close();
1042
1043 // Invalid: Alice Sequence is a past sequence
1044 {
1045 auto const preAliceSeq = env.seq(alice);
1046 auto const preAlice = env.balance(alice);
1047 auto const preAliceUSD = env.balance(alice, usd.issue());
1048 auto const preBobSeq = env.seq(bob);
1049 auto const preBob = env.balance(bob);
1050 auto const preBobUSD = env.balance(bob, usd.issue());
1051
1052 auto const batchFee = batch::calcBatchFee(env, 1, 2);
1053 auto const [txIDs, batchID] = submitBatch(
1054 env,
1055 tesSUCCESS,
1056 batch::outer(alice, preAliceSeq, batchFee, tfAllOrNothing),
1057 batch::Inner(pay(alice, bob, XRP(10)), preAliceSeq - 10),
1058 batch::Inner(pay(bob, alice, XRP(5)), preBobSeq),
1059 batch::Sig(bob));
1060
1061 env.close();
1062 {
1063 std::vector<TestLedgerData> const testCases = {
1064 {
1065 .index = 0,
1066 .txType = "Batch",
1067 .result = "tesSUCCESS",
1068 .txHash = batchID,
1069 .batchID = std::nullopt,
1070 },
1071 };
1072 validateClosedLedger(env, testCases);
1073 }
1074
1075 env.close();
1076 {
1077 // next ledger is empty
1078 std::vector<TestLedgerData> const testCases = {};
1079 validateClosedLedger(env, testCases);
1080 }
1081
1082 // Alice pays fee & Bob should not be affected.
1083 BEAST_EXPECT(env.seq(alice) == preAliceSeq + 1);
1084 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
1085 BEAST_EXPECT(env.balance(alice, usd.issue()) == preAliceUSD);
1086 BEAST_EXPECT(env.seq(bob) == preBobSeq);
1087 BEAST_EXPECT(env.balance(bob) == preBob);
1088 BEAST_EXPECT(env.balance(bob, usd.issue()) == preBobUSD);
1089 }
1090
1091 // Invalid: Alice Sequence is a future sequence
1092 {
1093 auto const preAliceSeq = env.seq(alice);
1094 auto const preAlice = env.balance(alice);
1095 auto const preAliceUSD = env.balance(alice, usd.issue());
1096 auto const preBobSeq = env.seq(bob);
1097 auto const preBob = env.balance(bob);
1098 auto const preBobUSD = env.balance(bob, usd.issue());
1099
1100 auto const batchFee = batch::calcBatchFee(env, 1, 2);
1101 auto const [txIDs, batchID] = submitBatch(
1102 env,
1103 tesSUCCESS,
1104 batch::outer(alice, preAliceSeq, batchFee, tfAllOrNothing),
1105 batch::Inner(pay(alice, bob, XRP(10)), preAliceSeq + 10),
1106 batch::Inner(pay(bob, alice, XRP(5)), preBobSeq),
1107 batch::Sig(bob));
1108
1109 env.close();
1110 {
1111 std::vector<TestLedgerData> const testCases = {
1112 {
1113 .index = 0,
1114 .txType = "Batch",
1115 .result = "tesSUCCESS",
1116 .txHash = batchID,
1117 .batchID = std::nullopt,
1118 },
1119 };
1120 validateClosedLedger(env, testCases);
1121 }
1122
1123 env.close();
1124 {
1125 // next ledger is empty
1126 std::vector<TestLedgerData> const testCases = {};
1127 validateClosedLedger(env, testCases);
1128 }
1129
1130 // Alice pays fee & Bob should not be affected.
1131 BEAST_EXPECT(env.seq(alice) == preAliceSeq + 1);
1132 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
1133 BEAST_EXPECT(env.balance(alice, usd.issue()) == preAliceUSD);
1134 BEAST_EXPECT(env.seq(bob) == preBobSeq);
1135 BEAST_EXPECT(env.balance(bob) == preBob);
1136 BEAST_EXPECT(env.balance(bob, usd.issue()) == preBobUSD);
1137 }
1138
1139 // Invalid: Bob Sequence is a past sequence
1140 {
1141 auto const preAliceSeq = env.seq(alice);
1142 auto const preAlice = env.balance(alice);
1143 auto const preAliceUSD = env.balance(alice, usd.issue());
1144 auto const preBobSeq = env.seq(bob);
1145 auto const preBob = env.balance(bob);
1146 auto const preBobUSD = env.balance(bob, usd.issue());
1147
1148 auto const batchFee = batch::calcBatchFee(env, 1, 2);
1149 auto const [txIDs, batchID] = submitBatch(
1150 env,
1151 tesSUCCESS,
1152 batch::outer(alice, preAliceSeq, batchFee, tfAllOrNothing),
1153 batch::Inner(pay(alice, bob, XRP(10)), preAliceSeq + 1),
1154 batch::Inner(pay(bob, alice, XRP(5)), preBobSeq - 10),
1155 batch::Sig(bob));
1156
1157 env.close();
1158 {
1159 std::vector<TestLedgerData> const testCases = {
1160 {
1161 .index = 0,
1162 .txType = "Batch",
1163 .result = "tesSUCCESS",
1164 .txHash = batchID,
1165 .batchID = std::nullopt,
1166 },
1167 };
1168 validateClosedLedger(env, testCases);
1169 }
1170
1171 env.close();
1172 {
1173 // next ledger is empty
1174 std::vector<TestLedgerData> const testCases = {};
1175 validateClosedLedger(env, testCases);
1176 }
1177
1178 // Alice pays fee & Bob should not be affected.
1179 BEAST_EXPECT(env.seq(alice) == preAliceSeq + 1);
1180 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
1181 BEAST_EXPECT(env.balance(alice, usd.issue()) == preAliceUSD);
1182 BEAST_EXPECT(env.seq(bob) == preBobSeq);
1183 BEAST_EXPECT(env.balance(bob) == preBob);
1184 BEAST_EXPECT(env.balance(bob, usd.issue()) == preBobUSD);
1185 }
1186
1187 // Invalid: Bob Sequence is a future sequence
1188 {
1189 auto const preAliceSeq = env.seq(alice);
1190 auto const preAlice = env.balance(alice);
1191 auto const preAliceUSD = env.balance(alice, usd.issue());
1192 auto const preBobSeq = env.seq(bob);
1193 auto const preBob = env.balance(bob);
1194 auto const preBobUSD = env.balance(bob, usd.issue());
1195
1196 auto const batchFee = batch::calcBatchFee(env, 1, 2);
1197 auto const [txIDs, batchID] = submitBatch(
1198 env,
1199 tesSUCCESS,
1200 batch::outer(alice, preAliceSeq, batchFee, tfAllOrNothing),
1201 batch::Inner(pay(alice, bob, XRP(10)), preAliceSeq + 1),
1202 batch::Inner(pay(bob, alice, XRP(5)), preBobSeq + 10),
1203 batch::Sig(bob));
1204
1205 env.close();
1206 {
1207 std::vector<TestLedgerData> const testCases = {
1208 {
1209 .index = 0,
1210 .txType = "Batch",
1211 .result = "tesSUCCESS",
1212 .txHash = batchID,
1213 .batchID = std::nullopt,
1214 },
1215 };
1216 validateClosedLedger(env, testCases);
1217 }
1218
1219 env.close();
1220 {
1221 // next ledger is empty
1222 std::vector<TestLedgerData> const testCases = {};
1223 validateClosedLedger(env, testCases);
1224 }
1225
1226 // Alice pays fee & Bob should not be affected.
1227 BEAST_EXPECT(env.seq(alice) == preAliceSeq + 1);
1228 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
1229 BEAST_EXPECT(env.balance(alice, usd.issue()) == preAliceUSD);
1230 BEAST_EXPECT(env.seq(bob) == preBobSeq);
1231 BEAST_EXPECT(env.balance(bob) == preBob);
1232 BEAST_EXPECT(env.balance(bob, usd.issue()) == preBobUSD);
1233 }
1234
1235 // Invalid: Outer and Inner Sequence are the same
1236 {
1237 auto const preAliceSeq = env.seq(alice);
1238 auto const preAlice = env.balance(alice);
1239 auto const preAliceUSD = env.balance(alice, usd.issue());
1240 auto const preBobSeq = env.seq(bob);
1241 auto const preBob = env.balance(bob);
1242 auto const preBobUSD = env.balance(bob, usd.issue());
1243
1244 auto const batchFee = batch::calcBatchFee(env, 1, 2);
1245 auto const [txIDs, batchID] = submitBatch(
1246 env,
1247 tesSUCCESS,
1248 batch::outer(alice, preAliceSeq, batchFee, tfAllOrNothing),
1249 batch::Inner(pay(alice, bob, XRP(10)), preAliceSeq),
1250 batch::Inner(pay(bob, alice, XRP(5)), preBobSeq),
1251 batch::Sig(bob));
1252
1253 env.close();
1254 {
1255 std::vector<TestLedgerData> const testCases = {
1256 {
1257 .index = 0,
1258 .txType = "Batch",
1259 .result = "tesSUCCESS",
1260 .txHash = batchID,
1261 .batchID = std::nullopt,
1262 },
1263 };
1264 validateClosedLedger(env, testCases);
1265 }
1266
1267 env.close();
1268 {
1269 // next ledger is empty
1270 std::vector<TestLedgerData> const testCases = {};
1271 validateClosedLedger(env, testCases);
1272 }
1273
1274 // Alice pays fee & Bob should not be affected.
1275 BEAST_EXPECT(env.seq(alice) == preAliceSeq + 1);
1276 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
1277 BEAST_EXPECT(env.balance(alice, usd.issue()) == preAliceUSD);
1278 BEAST_EXPECT(env.seq(bob) == preBobSeq);
1279 BEAST_EXPECT(env.balance(bob) == preBob);
1280 BEAST_EXPECT(env.balance(bob, usd.issue()) == preBobUSD);
1281 }
1282 }
1283
1284 void
1286 {
1287 testcase("bad outer fee");
1288
1289 using namespace test::jtx;
1290 using namespace std::literals;
1291
1292 // Bad Fee Without Signer
1293 {
1294 Env env{*this, features};
1295
1296 auto const alice = Account("alice");
1297 auto const bob = Account("bob");
1298 env.fund(XRP(10000), alice, bob);
1299 env.close();
1300
1301 env(noop(bob), Ter(tesSUCCESS));
1302 env.close();
1303
1304 // Bad Fee: Should be batch::calcBatchFee(env, 0, 2)
1305 auto const batchFee = batch::calcBatchFee(env, 0, 1);
1306 auto const aliceSeq = env.seq(alice);
1307 env(batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
1308 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 1),
1309 batch::Inner(pay(alice, bob, XRP(15)), aliceSeq + 2),
1311 env.close();
1312 }
1313
1314 // Bad Fee With MultiSign
1315 {
1316 Env env{*this, features};
1317
1318 auto const alice = Account("alice");
1319 auto const bob = Account("bob");
1320 auto const carol = Account("carol");
1321 env.fund(XRP(10000), alice, bob, carol);
1322 env.close();
1323
1324 env(noop(bob), Ter(tesSUCCESS));
1325 env.close();
1326
1327 env(signers(alice, 2, {{bob, 1}, {carol, 1}}));
1328 env.close();
1329
1330 // Bad Fee: Should be batch::calcBatchFee(env, 2, 2)
1331 auto const batchFee = batch::calcBatchFee(env, 1, 2);
1332 auto const aliceSeq = env.seq(alice);
1333 env(batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
1334 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 1),
1335 batch::Inner(pay(alice, bob, XRP(15)), aliceSeq + 2),
1336 Msig(bob, carol),
1338 env.close();
1339 }
1340
1341 // Bad Fee With MultiSign + BatchSigners
1342 {
1343 Env env{*this, features};
1344
1345 auto const alice = Account("alice");
1346 auto const bob = Account("bob");
1347 auto const carol = Account("carol");
1348 env.fund(XRP(10000), alice, bob, carol);
1349 env.close();
1350
1351 env(noop(bob), Ter(tesSUCCESS));
1352 env.close();
1353
1354 env(signers(alice, 2, {{bob, 1}, {carol, 1}}));
1355 env.close();
1356
1357 // Bad Fee: Should be batch::calcBatchFee(env, 3, 2)
1358 auto const batchFee = batch::calcBatchFee(env, 2, 2);
1359 auto const aliceSeq = env.seq(alice);
1360 auto const bobSeq = env.seq(bob);
1361 env(batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
1362 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 1),
1363 batch::Inner(pay(bob, alice, XRP(5)), bobSeq),
1364 batch::Sig(bob),
1365 Msig(bob, carol),
1367 env.close();
1368 }
1369
1370 // Bad Fee With MultiSign + BatchSigners.Signers
1371 {
1372 Env env{*this, features};
1373
1374 auto const alice = Account("alice");
1375 auto const bob = Account("bob");
1376 auto const carol = Account("carol");
1377 env.fund(XRP(10000), alice, bob, carol);
1378 env.close();
1379
1380 env(noop(bob), Ter(tesSUCCESS));
1381 env.close();
1382
1383 env(signers(alice, 2, {{bob, 1}, {carol, 1}}));
1384 env.close();
1385
1386 env(signers(bob, 2, {{alice, 1}, {carol, 1}}));
1387 env.close();
1388
1389 // Bad Fee: Should be batch::calcBatchFee(env, 4, 2)
1390 auto const batchFee = batch::calcBatchFee(env, 3, 2);
1391 auto const aliceSeq = env.seq(alice);
1392 auto const bobSeq = env.seq(bob);
1393 env(batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
1394 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 1),
1395 batch::Inner(pay(bob, alice, XRP(5)), bobSeq),
1396 batch::Msig(bob, {alice, carol}),
1397 Msig(bob, carol),
1399 env.close();
1400 }
1401
1402 // Bad Fee With BatchSigners
1403 {
1404 Env env{*this, features};
1405
1406 auto const alice = Account("alice");
1407 auto const bob = Account("bob");
1408 env.fund(XRP(10000), alice, bob);
1409 env.close();
1410
1411 env(noop(bob), Ter(tesSUCCESS));
1412 env.close();
1413
1414 // Bad Fee: Should be batch::calcBatchFee(env, 1, 2)
1415 auto const batchFee = batch::calcBatchFee(env, 0, 2);
1416 auto const aliceSeq = env.seq(alice);
1417 auto const bobSeq = env.seq(bob);
1418 env(batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
1419 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 1),
1420 batch::Inner(pay(bob, alice, XRP(5)), bobSeq),
1421 batch::Sig(bob),
1423 env.close();
1424 }
1425
1426 // Bad Fee Dynamic Fee Calculation
1427 {
1428 Env env{*this, features};
1429
1430 auto const alice = Account("alice");
1431 auto const bob = Account("bob");
1432 auto const gw = Account("gw");
1433 auto const usd = gw["USD"];
1434
1435 env.fund(XRP(10000), alice, bob, gw);
1436 env.close();
1437 auto const ammCreate = [&alice](STAmount const& amount, STAmount const& amount2) {
1438 json::Value jv;
1439 jv[jss::Account] = alice.human();
1440 jv[jss::Amount] = amount.getJson(JsonOptions::Values::None);
1441 jv[jss::Amount2] = amount2.getJson(JsonOptions::Values::None);
1442 jv[jss::TradingFee] = 0;
1443 jv[jss::TransactionType] = jss::AMMCreate;
1444 return jv;
1445 };
1446
1447 auto const batchFee = batch::calcBatchFee(env, 0, 2);
1448 auto const seq = env.seq(alice);
1449 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
1450 batch::Inner(ammCreate(XRP(10), usd(10)), seq + 1),
1451 batch::Inner(pay(alice, bob, XRP(10)), seq + 2),
1453 env.close();
1454 }
1455 }
1456
1457 void
1459 {
1460 testcase("calculate base fee");
1461
1462 using namespace test::jtx;
1463 using namespace std::literals;
1464
1465 // telENV_RPC_FAILED: Batch: txns array exceeds 8 entries.
1466 {
1467 Env env{*this, features};
1468
1469 auto const alice = Account("alice");
1470 auto const bob = Account("bob");
1471 env.fund(XRP(10000), alice, bob);
1472 env.close();
1473
1474 auto const batchFee = batch::calcBatchFee(env, 0, 9);
1475 auto const aliceSeq = env.seq(alice);
1476 env(batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
1477 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1478 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1479 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1480 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1481 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1482 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1483 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1484 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1485 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1486 Ter(temMALFORMED));
1487 env.close();
1488 }
1489
1490 // An oversized batch (more than kMaxBatchTxCount inners) fails STTx
1491 // construction, so it never reaches apply or checkValidity - Env::st
1492 // swallows the failure and yields a null stx.
1493 {
1494 Env env{*this, features};
1495
1496 auto const alice = Account("alice");
1497 auto const bob = Account("bob");
1498 env.fund(XRP(10000), alice, bob);
1499 env.close();
1500
1501 auto const batchFee = batch::calcBatchFee(env, 0, 9);
1502 auto const aliceSeq = env.seq(alice);
1503 auto const jt = env.jtnofill(
1504 batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
1505 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1506 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1507 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1508 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1509 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1510 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1511 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1512 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1513 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq));
1514
1515 BEAST_EXPECT(jt.stx == nullptr);
1516 }
1517
1518 // An oversized batch cannot slip through as validly signed even when it
1519 // carries a BatchSigners array: the oversized inner array fails STTx
1520 // construction before any signature is checked, so the batch can't be
1521 // built at all (building it - as signing does - throws).
1522 {
1523 Env env{*this, features};
1524
1525 auto const alice = Account("alice");
1526 auto const bob = Account("bob");
1527 env.fund(XRP(10000), alice, bob);
1528 env.close();
1529
1530 auto const aliceSeq = env.seq(alice);
1531 auto const batchFee = batch::calcBatchFee(env, kMaxBatchSigners + 1, 9);
1532 bool threw = false;
1533 try
1534 {
1535 env.jtnofill(
1536 batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
1537 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1538 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1539 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1540 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1541 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1542 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1543 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1544 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1545 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
1547 }
1548 catch (std::exception const&)
1549 {
1550 threw = true;
1551 }
1552 BEAST_EXPECT(threw);
1553 }
1554
1555 // Regression: the relay-boundary local check (isBatchRawTransactionOkay)
1556 // caps the signers array at kMaxBatchSigners, matching Batch::preflight -
1557 // not kMaxBatchTxCount. A batch with more than kMaxBatchTxCount but at
1558 // most kMaxBatchSigners signers must get past local checks and reach
1559 // signer validation. Before the cap was aligned it was wrongly rejected
1560 // at the boundary (telENV_RPC_FAILED) before preflight ran; now it
1561 // reaches preflightSigValidated and fails there as extra signers
1562 // (temBAD_SIGNER) rather than being dropped pre-engine.
1563 {
1564 Env env{*this, features};
1565
1566 auto const alice = Account("alice");
1567 auto const bob = Account("bob");
1568 env.fund(XRP(10000), alice, bob);
1569 env.close();
1570
1571 // Over kMaxBatchTxCount (8) but within kMaxBatchSigners (24).
1572 std::size_t const signerCount = kMaxBatchTxCount + 1;
1573
1574 auto const aliceSeq = env.seq(alice);
1575 auto const batchFee = batch::calcBatchFee(env, signerCount, 2);
1576 env(batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
1577 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 1),
1578 batch::Inner(pay(alice, bob, XRP(5)), aliceSeq + 2),
1579 batch::Sig(std::vector<Reg>(signerCount, bob)),
1581 env.close();
1582 }
1583
1584 // temARRAY_TOO_LARGE: Batch preflight: signers array exceeds
1585 // kMaxBatchSigners entries.
1586 {
1587 Env env{*this, features};
1588
1589 auto const alice = Account("alice");
1590 auto const bob = Account("bob");
1591 env.fund(XRP(10000), alice, bob);
1592 env.close();
1593
1594 auto const batchFee = batch::calcBatchFee(env, kMaxBatchSigners + 1, 2);
1595 auto const aliceSeq = env.seq(alice);
1596 auto jt = env.jtnofill(
1597 batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
1598 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 1),
1599 batch::Inner(pay(alice, bob, XRP(5)), aliceSeq + 2),
1601
1602 env.app().getOpenLedger().modify([&](OpenView& view, beast::Journal j) {
1603 auto const result = xrpl::apply(env.app(), view, *jt.stx, TapNone, j);
1604 BEAST_EXPECT(!result.applied && result.ter == temARRAY_TOO_LARGE);
1605 return result.applied;
1606 });
1607
1608 // Regression (uncapped batch-signer verification): the relay
1609 // boundary (checkValidity) rejects the oversized signers array via
1610 // the checkBatchSign guard, BEFORE verifying a single signature.
1611 auto const [valid, reason] =
1612 xrpl::checkValidity(env.app().getHashRouter(), *jt.stx, env.current()->rules());
1613 BEAST_EXPECT(valid == xrpl::Validity::SigBad);
1614 BEAST_EXPECT(reason == "BatchSigners array exceeds max entries.");
1615 }
1616 }
1617
1618 void
1620 {
1621 testcase("all or nothing");
1622
1623 using namespace test::jtx;
1624 using namespace std::literals;
1625
1626 Env env{*this, features};
1627
1628 auto const alice = Account("alice");
1629 auto const bob = Account("bob");
1630 auto const gw = Account("gw");
1631 auto const usd = gw["USD"];
1632 env.fund(XRP(10000), alice, bob, gw);
1633 env.close();
1634
1635 // all
1636 {
1637 auto const preAlice = env.balance(alice);
1638 auto const preBob = env.balance(bob);
1639
1640 auto const batchFee = batch::calcBatchFee(env, 0, 2);
1641 auto const seq = env.seq(alice);
1642 auto const [txIDs, batchID] = submitBatch(
1643 env,
1644 tesSUCCESS,
1645 batch::outer(alice, seq, batchFee, tfAllOrNothing),
1646 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
1647 batch::Inner(pay(alice, bob, XRP(2)), seq + 2));
1648 env.close();
1649
1650 std::vector<TestLedgerData> const testCases = {
1651 {
1652 .index = 0,
1653 .txType = "Batch",
1654 .result = "tesSUCCESS",
1655 .txHash = batchID,
1656 .batchID = std::nullopt,
1657 },
1658 {
1659 .index = 1,
1660 .txType = "Payment",
1661 .result = "tesSUCCESS",
1662 .txHash = txIDs[0],
1663 .batchID = batchID,
1664 },
1665 {
1666 .index = 2,
1667 .txType = "Payment",
1668 .result = "tesSUCCESS",
1669 .txHash = txIDs[1],
1670 .batchID = batchID,
1671 },
1672 };
1673 validateClosedLedger(env, testCases);
1674
1675 // Alice consumes sequences (# of txns)
1676 BEAST_EXPECT(env.seq(alice) == seq + 3);
1677
1678 // Alice pays XRP & Fee; Bob receives XRP
1679 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(3) - batchFee);
1680 BEAST_EXPECT(env.balance(bob) == preBob + XRP(3));
1681 }
1682
1683 // tec failure
1684 {
1685 auto const preAlice = env.balance(alice);
1686 auto const preBob = env.balance(bob);
1687
1688 auto const batchFee = batch::calcBatchFee(env, 0, 2);
1689 auto const seq = env.seq(alice);
1690
1691 auto const [txIDs, batchID] = submitBatch(
1692 env,
1693 tesSUCCESS,
1694 batch::outer(alice, seq, batchFee, tfAllOrNothing),
1695 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
1696 // tecUNFUNDED_PAYMENT: alice does not have enough XRP
1697 batch::Inner(pay(alice, bob, XRP(9999)), seq + 2));
1698 env.close();
1699
1700 std::vector<TestLedgerData> const testCases = {
1701 {
1702 .index = 0,
1703 .txType = "Batch",
1704 .result = "tesSUCCESS",
1705 .txHash = batchID,
1706 .batchID = std::nullopt,
1707 },
1708 };
1709 validateClosedLedger(env, testCases);
1710
1711 // Alice consumes sequence
1712 BEAST_EXPECT(env.seq(alice) == seq + 1);
1713
1714 // Alice pays Fee; Bob should not be affected
1715 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
1716 BEAST_EXPECT(env.balance(bob) == preBob);
1717 }
1718
1719 // tef failure
1720 {
1721 auto const preAlice = env.balance(alice);
1722 auto const preBob = env.balance(bob);
1723
1724 auto const batchFee = batch::calcBatchFee(env, 0, 2);
1725 auto const seq = env.seq(alice);
1726 auto const [txIDs, batchID] = submitBatch(
1727 env,
1728 tesSUCCESS,
1729 batch::outer(alice, seq, batchFee, tfAllOrNothing),
1730 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
1731 // tefNO_AUTH_REQUIRED: trustline auth is not required
1732 batch::Inner(trust(alice, usd(1000), tfSetfAuth), seq + 2));
1733 env.close();
1734
1735 std::vector<TestLedgerData> const testCases = {
1736 {
1737 .index = 0,
1738 .txType = "Batch",
1739 .result = "tesSUCCESS",
1740 .txHash = batchID,
1741 .batchID = std::nullopt,
1742 },
1743 };
1744 validateClosedLedger(env, testCases);
1745
1746 // Alice consumes sequence
1747 BEAST_EXPECT(env.seq(alice) == seq + 1);
1748
1749 // Alice pays Fee; Bob should not be affected
1750 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
1751 BEAST_EXPECT(env.balance(bob) == preBob);
1752 }
1753
1754 // ter failure
1755 {
1756 auto const preAlice = env.balance(alice);
1757 auto const preBob = env.balance(bob);
1758
1759 auto const batchFee = batch::calcBatchFee(env, 0, 2);
1760 auto const seq = env.seq(alice);
1761 auto const [txIDs, batchID] = submitBatch(
1762 env,
1763 tesSUCCESS,
1764 batch::outer(alice, seq, batchFee, tfAllOrNothing),
1765 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
1766 // terPRE_TICKET: ticket does not exist
1767 batch::Inner(trust(alice, usd(1000), tfSetfAuth), 0, seq + 2));
1768 env.close();
1769
1770 std::vector<TestLedgerData> const testCases = {
1771 {
1772 .index = 0,
1773 .txType = "Batch",
1774 .result = "tesSUCCESS",
1775 .txHash = batchID,
1776 .batchID = std::nullopt,
1777 },
1778 };
1779 validateClosedLedger(env, testCases);
1780
1781 // Alice consumes sequence
1782 BEAST_EXPECT(env.seq(alice) == seq + 1);
1783
1784 // Alice pays Fee; Bob should not be affected
1785 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
1786 BEAST_EXPECT(env.balance(bob) == preBob);
1787 }
1788 }
1789
1790 void
1792 {
1793 testcase("only one");
1794
1795 using namespace test::jtx;
1796 using namespace std::literals;
1797
1798 Env env{*this, features};
1799
1800 auto const alice = Account("alice");
1801 auto const bob = Account("bob");
1802 auto const carol = Account("carol");
1803 auto const dave = Account("dave");
1804 auto const gw = Account("gw");
1805 auto const usd = gw["USD"];
1806 env.fund(XRP(10000), alice, bob, carol, dave, gw);
1807 env.close();
1808
1809 // all transactions fail
1810 {
1811 auto const preAlice = env.balance(alice);
1812 auto const preBob = env.balance(bob);
1813
1814 auto const batchFee = batch::calcBatchFee(env, 0, 3);
1815 auto const seq = env.seq(alice);
1816 auto const [txIDs, batchID] = submitBatch(
1817 env,
1818 tesSUCCESS,
1819 batch::outer(alice, seq, batchFee, tfOnlyOne),
1820 // tecUNFUNDED_PAYMENT: alice does not have enough XRP
1821 batch::Inner(pay(alice, bob, XRP(9999)), seq + 1),
1822 // tecUNFUNDED_PAYMENT: alice does not have enough XRP
1823 batch::Inner(pay(alice, bob, XRP(9999)), seq + 2),
1824 // tecUNFUNDED_PAYMENT: alice does not have enough XRP
1825 batch::Inner(pay(alice, bob, XRP(9999)), seq + 3));
1826 env.close();
1827
1828 std::vector<TestLedgerData> const testCases = {
1829 {
1830 .index = 0,
1831 .txType = "Batch",
1832 .result = "tesSUCCESS",
1833 .txHash = batchID,
1834 .batchID = std::nullopt,
1835 },
1836 {
1837 .index = 1,
1838 .txType = "Payment",
1839 .result = "tecUNFUNDED_PAYMENT",
1840 .txHash = txIDs[0],
1841 .batchID = batchID,
1842 },
1843 {
1844 .index = 2,
1845 .txType = "Payment",
1846 .result = "tecUNFUNDED_PAYMENT",
1847 .txHash = txIDs[1],
1848 .batchID = batchID,
1849 },
1850 {
1851 .index = 3,
1852 .txType = "Payment",
1853 .result = "tecUNFUNDED_PAYMENT",
1854 .txHash = txIDs[2],
1855 .batchID = batchID,
1856 },
1857 };
1858 validateClosedLedger(env, testCases);
1859
1860 // Alice consumes sequences (# of txns)
1861 BEAST_EXPECT(env.seq(alice) == seq + 4);
1862
1863 // Alice pays XRP & Fee; Bob receives XRP
1864 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
1865 BEAST_EXPECT(env.balance(bob) == preBob);
1866 }
1867
1868 // first transaction fails
1869 {
1870 auto const preAlice = env.balance(alice);
1871 auto const preBob = env.balance(bob);
1872
1873 auto const batchFee = batch::calcBatchFee(env, 0, 3);
1874 auto const seq = env.seq(alice);
1875 auto const [txIDs, batchID] = submitBatch(
1876 env,
1877 tesSUCCESS,
1878 batch::outer(alice, seq, batchFee, tfOnlyOne),
1879 // tecUNFUNDED_PAYMENT: alice does not have enough XRP
1880 batch::Inner(pay(alice, bob, XRP(9999)), seq + 1),
1881 batch::Inner(pay(alice, bob, XRP(1)), seq + 2),
1882 batch::Inner(pay(alice, bob, XRP(2)), seq + 3));
1883 env.close();
1884
1885 std::vector<TestLedgerData> const testCases = {
1886 {
1887 .index = 0,
1888 .txType = "Batch",
1889 .result = "tesSUCCESS",
1890 .txHash = batchID,
1891 .batchID = std::nullopt,
1892 },
1893 {
1894 .index = 1,
1895 .txType = "Payment",
1896 .result = "tecUNFUNDED_PAYMENT",
1897 .txHash = txIDs[0],
1898 .batchID = batchID,
1899 },
1900 {
1901 .index = 2,
1902 .txType = "Payment",
1903 .result = "tesSUCCESS",
1904 .txHash = txIDs[1],
1905 .batchID = batchID,
1906 },
1907 };
1908 validateClosedLedger(env, testCases);
1909
1910 // Alice consumes sequences (# of txns)
1911 BEAST_EXPECT(env.seq(alice) == seq + 3);
1912
1913 // Alice pays XRP & Fee; Bob receives XRP
1914 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(1) - batchFee);
1915 BEAST_EXPECT(env.balance(bob) == preBob + XRP(1));
1916 }
1917
1918 // tec failure
1919 {
1920 auto const preAlice = env.balance(alice);
1921 auto const preBob = env.balance(bob);
1922
1923 auto const batchFee = batch::calcBatchFee(env, 0, 3);
1924 auto const seq = env.seq(alice);
1925 auto const [txIDs, batchID] = submitBatch(
1926 env,
1927 tesSUCCESS,
1928 batch::outer(alice, seq, batchFee, tfOnlyOne),
1929 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
1930 // tecUNFUNDED_PAYMENT: alice does not have enough XRP
1931 batch::Inner(pay(alice, bob, XRP(9999)), seq + 2),
1932 batch::Inner(pay(alice, bob, XRP(2)), seq + 3));
1933 env.close();
1934
1935 std::vector<TestLedgerData> const testCases = {
1936 {
1937 .index = 0,
1938 .txType = "Batch",
1939 .result = "tesSUCCESS",
1940 .txHash = batchID,
1941 .batchID = std::nullopt,
1942 },
1943 {
1944 .index = 1,
1945 .txType = "Payment",
1946 .result = "tesSUCCESS",
1947 .txHash = txIDs[0],
1948 .batchID = batchID,
1949 },
1950 };
1951 validateClosedLedger(env, testCases);
1952
1953 // Alice consumes sequences (# of txns)
1954 BEAST_EXPECT(env.seq(alice) == seq + 2);
1955
1956 // Alice pays XRP & Fee; Bob receives XRP
1957 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(1) - batchFee);
1958 BEAST_EXPECT(env.balance(bob) == preBob + XRP(1));
1959 }
1960
1961 // tef failure
1962 {
1963 auto const preAlice = env.balance(alice);
1964 auto const preBob = env.balance(bob);
1965
1966 auto const batchFee = batch::calcBatchFee(env, 0, 3);
1967 auto const seq = env.seq(alice);
1968 auto const [txIDs, batchID] = submitBatch(
1969 env,
1970 tesSUCCESS,
1971 batch::outer(alice, seq, batchFee, tfOnlyOne),
1972 // tefNO_AUTH_REQUIRED: trustline auth is not required
1973 batch::Inner(trust(alice, usd(1000), tfSetfAuth), seq + 1),
1974 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
1975 batch::Inner(pay(alice, bob, XRP(2)), seq + 3));
1976 env.close();
1977
1978 std::vector<TestLedgerData> const testCases = {
1979 {
1980 .index = 0,
1981 .txType = "Batch",
1982 .result = "tesSUCCESS",
1983 .txHash = batchID,
1984 .batchID = std::nullopt,
1985 },
1986 {
1987 .index = 1,
1988 .txType = "Payment",
1989 .result = "tesSUCCESS",
1990 .txHash = txIDs[1],
1991 .batchID = batchID,
1992 },
1993 };
1994 validateClosedLedger(env, testCases);
1995
1996 // Alice consumes sequences (# of txns)
1997 BEAST_EXPECT(env.seq(alice) == seq + 2);
1998
1999 // Alice pays XRP & Fee; Bob receives XRP
2000 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee - XRP(1));
2001 BEAST_EXPECT(env.balance(bob) == preBob + XRP(1));
2002 }
2003
2004 // ter failure
2005 {
2006 auto const preAlice = env.balance(alice);
2007 auto const preBob = env.balance(bob);
2008
2009 auto const batchFee = batch::calcBatchFee(env, 0, 3);
2010 auto const seq = env.seq(alice);
2011 auto const [txIDs, batchID] = submitBatch(
2012 env,
2013 tesSUCCESS,
2014 batch::outer(alice, seq, batchFee, tfOnlyOne),
2015 // terPRE_TICKET: ticket does not exist
2016 batch::Inner(trust(alice, usd(1000), tfSetfAuth), 0, seq + 1),
2017 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
2018 batch::Inner(pay(alice, bob, XRP(2)), seq + 3));
2019 env.close();
2020
2021 std::vector<TestLedgerData> const testCases = {
2022 {
2023 .index = 0,
2024 .txType = "Batch",
2025 .result = "tesSUCCESS",
2026 .txHash = batchID,
2027 .batchID = std::nullopt,
2028 },
2029 {
2030 .index = 1,
2031 .txType = "Payment",
2032 .result = "tesSUCCESS",
2033 .txHash = txIDs[1],
2034 .batchID = batchID,
2035 },
2036 };
2037 validateClosedLedger(env, testCases);
2038
2039 // Alice consumes sequences (# of txns)
2040 BEAST_EXPECT(env.seq(alice) == seq + 2);
2041
2042 // Alice pays XRP & Fee; Bob receives XRP
2043 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee - XRP(1));
2044 BEAST_EXPECT(env.balance(bob) == preBob + XRP(1));
2045 }
2046
2047 // tec (tecKILLED) error
2048 {
2049 auto const preAlice = env.balance(alice);
2050 auto const preBob = env.balance(bob);
2051 auto const preCarol = env.balance(carol);
2052 auto const seq = env.seq(alice);
2053 auto const batchFee = batch::calcBatchFee(env, 0, 6);
2054
2055 auto const [txIDs, batchID] = submitBatch(
2056 env,
2057 tesSUCCESS,
2058 batch::outer(alice, seq, batchFee, tfOnlyOne),
2060 offer(alice, alice["USD"](100), XRP(100), tfImmediateOrCancel), seq + 1),
2062 offer(alice, alice["USD"](100), XRP(100), tfImmediateOrCancel), seq + 2),
2064 offer(alice, alice["USD"](100), XRP(100), tfImmediateOrCancel), seq + 3),
2065 batch::Inner(pay(alice, bob, XRP(100)), seq + 4),
2066 batch::Inner(pay(alice, carol, XRP(100)), seq + 5),
2067 batch::Inner(pay(alice, dave, XRP(100)), seq + 6));
2068 env.close();
2069
2070 std::vector<TestLedgerData> const testCases = {
2071 {
2072 .index = 0,
2073 .txType = "Batch",
2074 .result = "tesSUCCESS",
2075 .txHash = batchID,
2076 .batchID = std::nullopt,
2077 },
2078 {
2079 .index = 1,
2080 .txType = "OfferCreate",
2081 .result = "tecKILLED",
2082 .txHash = txIDs[0],
2083 .batchID = batchID,
2084 },
2085 {
2086 .index = 2,
2087 .txType = "OfferCreate",
2088 .result = "tecKILLED",
2089 .txHash = txIDs[1],
2090 .batchID = batchID,
2091 },
2092 {
2093 .index = 3,
2094 .txType = "OfferCreate",
2095 .result = "tecKILLED",
2096 .txHash = txIDs[2],
2097 .batchID = batchID,
2098 },
2099 {
2100 .index = 4,
2101 .txType = "Payment",
2102 .result = "tesSUCCESS",
2103 .txHash = txIDs[3],
2104 .batchID = batchID,
2105 },
2106 };
2107 validateClosedLedger(env, testCases);
2108
2109 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(100) - batchFee);
2110 BEAST_EXPECT(env.balance(bob) == preBob + XRP(100));
2111 BEAST_EXPECT(env.balance(carol) == preCarol);
2112 }
2113 }
2114
2115 void
2117 {
2118 testcase("until failure");
2119
2120 using namespace test::jtx;
2121 using namespace std::literals;
2122
2123 Env env{*this, features};
2124
2125 auto const alice = Account("alice");
2126 auto const bob = Account("bob");
2127 auto const carol = Account("carol");
2128 auto const dave = Account("dave");
2129 auto const gw = Account("gw");
2130 auto const usd = gw["USD"];
2131 env.fund(XRP(10000), alice, bob, carol, dave, gw);
2132 env.close();
2133
2134 // first transaction fails
2135 {
2136 auto const preAlice = env.balance(alice);
2137 auto const preBob = env.balance(bob);
2138
2139 auto const batchFee = batch::calcBatchFee(env, 0, 4);
2140 auto const seq = env.seq(alice);
2141 auto const [txIDs, batchID] = submitBatch(
2142 env,
2143 tesSUCCESS,
2144 batch::outer(alice, seq, batchFee, tfUntilFailure),
2145 // tecUNFUNDED_PAYMENT: alice does not have enough XRP
2146 batch::Inner(pay(alice, bob, XRP(9999)), seq + 1),
2147 batch::Inner(pay(alice, bob, XRP(1)), seq + 2),
2148 batch::Inner(pay(alice, bob, XRP(2)), seq + 3),
2149 batch::Inner(pay(alice, bob, XRP(3)), seq + 4));
2150 env.close();
2151
2152 std::vector<TestLedgerData> const testCases = {
2153 {
2154 .index = 0,
2155 .txType = "Batch",
2156 .result = "tesSUCCESS",
2157 .txHash = batchID,
2158 .batchID = std::nullopt,
2159 },
2160 {
2161 .index = 1,
2162 .txType = "Payment",
2163 .result = "tecUNFUNDED_PAYMENT",
2164 .txHash = txIDs[0],
2165 .batchID = batchID,
2166 },
2167 };
2168 validateClosedLedger(env, testCases);
2169
2170 // Alice consumes sequences (# of txns)
2171 BEAST_EXPECT(env.seq(alice) == seq + 2);
2172
2173 // Alice pays XRP & Fee; Bob receives XRP
2174 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
2175 BEAST_EXPECT(env.balance(bob) == preBob);
2176 }
2177
2178 // all transactions succeed
2179 {
2180 auto const preAlice = env.balance(alice);
2181 auto const preBob = env.balance(bob);
2182
2183 auto const batchFee = batch::calcBatchFee(env, 0, 4);
2184 auto const seq = env.seq(alice);
2185 auto const [txIDs, batchID] = submitBatch(
2186 env,
2187 tesSUCCESS,
2188 batch::outer(alice, seq, batchFee, tfUntilFailure),
2189 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
2190 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
2191 batch::Inner(pay(alice, bob, XRP(3)), seq + 3),
2192 batch::Inner(pay(alice, bob, XRP(4)), seq + 4));
2193 env.close();
2194
2195 std::vector<TestLedgerData> const testCases = {
2196 {
2197 .index = 0,
2198 .txType = "Batch",
2199 .result = "tesSUCCESS",
2200 .txHash = batchID,
2201 .batchID = std::nullopt,
2202 },
2203 {
2204 .index = 1,
2205 .txType = "Payment",
2206 .result = "tesSUCCESS",
2207 .txHash = txIDs[0],
2208 .batchID = batchID,
2209 },
2210 {
2211 .index = 2,
2212 .txType = "Payment",
2213 .result = "tesSUCCESS",
2214 .txHash = txIDs[1],
2215 .batchID = batchID,
2216 },
2217 {
2218 .index = 3,
2219 .txType = "Payment",
2220 .result = "tesSUCCESS",
2221 .txHash = txIDs[2],
2222 .batchID = batchID,
2223 },
2224 {
2225 .index = 4,
2226 .txType = "Payment",
2227 .result = "tesSUCCESS",
2228 .txHash = txIDs[3],
2229 .batchID = batchID,
2230 },
2231 };
2232 validateClosedLedger(env, testCases);
2233
2234 // Alice consumes sequences (# of txns)
2235 BEAST_EXPECT(env.seq(alice) == seq + 5);
2236
2237 // Alice pays XRP & Fee; Bob receives XRP
2238 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(10) - batchFee);
2239 BEAST_EXPECT(env.balance(bob) == preBob + XRP(10));
2240 }
2241
2242 // tec error
2243 {
2244 auto const preAlice = env.balance(alice);
2245 auto const preBob = env.balance(bob);
2246
2247 auto const batchFee = batch::calcBatchFee(env, 0, 4);
2248 auto const seq = env.seq(alice);
2249 auto const [txIDs, batchID] = submitBatch(
2250 env,
2251 tesSUCCESS,
2252 batch::outer(alice, seq, batchFee, tfUntilFailure),
2253 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
2254 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
2255 // tecUNFUNDED_PAYMENT: alice does not have enough XRP
2256 batch::Inner(pay(alice, bob, XRP(9999)), seq + 3),
2257 batch::Inner(pay(alice, bob, XRP(3)), seq + 4));
2258 env.close();
2259
2260 std::vector<TestLedgerData> const testCases = {
2261 {
2262 .index = 0,
2263 .txType = "Batch",
2264 .result = "tesSUCCESS",
2265 .txHash = batchID,
2266 .batchID = std::nullopt,
2267 },
2268 {
2269 .index = 1,
2270 .txType = "Payment",
2271 .result = "tesSUCCESS",
2272 .txHash = txIDs[0],
2273 .batchID = batchID,
2274 },
2275 {
2276 .index = 2,
2277 .txType = "Payment",
2278 .result = "tesSUCCESS",
2279 .txHash = txIDs[1],
2280 .batchID = batchID,
2281 },
2282 {
2283 .index = 3,
2284 .txType = "Payment",
2285 .result = "tecUNFUNDED_PAYMENT",
2286 .txHash = txIDs[2],
2287 .batchID = batchID,
2288 },
2289 };
2290 validateClosedLedger(env, testCases);
2291
2292 // Alice consumes sequences (# of txns)
2293 BEAST_EXPECT(env.seq(alice) == seq + 4);
2294
2295 // Alice pays XRP & Fee; Bob receives XRP
2296 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(3) - batchFee);
2297 BEAST_EXPECT(env.balance(bob) == preBob + XRP(3));
2298 }
2299
2300 // tef error
2301 {
2302 auto const preAlice = env.balance(alice);
2303 auto const preBob = env.balance(bob);
2304
2305 auto const batchFee = batch::calcBatchFee(env, 0, 4);
2306 auto const seq = env.seq(alice);
2307 auto const [txIDs, batchID] = submitBatch(
2308 env,
2309 tesSUCCESS,
2310 batch::outer(alice, seq, batchFee, tfUntilFailure),
2311 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
2312 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
2313 // tefNO_AUTH_REQUIRED: trustline auth is not required
2314 batch::Inner(trust(alice, usd(1000), tfSetfAuth), seq + 3),
2315 batch::Inner(pay(alice, bob, XRP(3)), seq + 4));
2316 env.close();
2317
2318 std::vector<TestLedgerData> const testCases = {
2319 {
2320 .index = 0,
2321 .txType = "Batch",
2322 .result = "tesSUCCESS",
2323 .txHash = batchID,
2324 .batchID = std::nullopt,
2325 },
2326 {
2327 .index = 1,
2328 .txType = "Payment",
2329 .result = "tesSUCCESS",
2330 .txHash = txIDs[0],
2331 .batchID = batchID,
2332 },
2333 {
2334 .index = 2,
2335 .txType = "Payment",
2336 .result = "tesSUCCESS",
2337 .txHash = txIDs[1],
2338 .batchID = batchID,
2339 },
2340 };
2341 validateClosedLedger(env, testCases);
2342
2343 // Alice consumes sequences (# of txns)
2344 BEAST_EXPECT(env.seq(alice) == seq + 3);
2345
2346 // Alice pays XRP & Fee; Bob receives XRP
2347 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(3) - batchFee);
2348 BEAST_EXPECT(env.balance(bob) == preBob + XRP(3));
2349 }
2350
2351 // ter error
2352 {
2353 auto const preAlice = env.balance(alice);
2354 auto const preBob = env.balance(bob);
2355
2356 auto const batchFee = batch::calcBatchFee(env, 0, 4);
2357 auto const seq = env.seq(alice);
2358 auto const [txIDs, batchID] = submitBatch(
2359 env,
2360 tesSUCCESS,
2361 batch::outer(alice, seq, batchFee, tfUntilFailure),
2362 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
2363 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
2364 // terPRE_TICKET: ticket does not exist
2365 batch::Inner(trust(alice, usd(1000), tfSetfAuth), 0, seq + 3),
2366 batch::Inner(pay(alice, bob, XRP(3)), seq + 4));
2367 env.close();
2368
2369 std::vector<TestLedgerData> const testCases = {
2370 {
2371 .index = 0,
2372 .txType = "Batch",
2373 .result = "tesSUCCESS",
2374 .txHash = batchID,
2375 .batchID = std::nullopt,
2376 },
2377 {
2378 .index = 1,
2379 .txType = "Payment",
2380 .result = "tesSUCCESS",
2381 .txHash = txIDs[0],
2382 .batchID = batchID,
2383 },
2384 {
2385 .index = 2,
2386 .txType = "Payment",
2387 .result = "tesSUCCESS",
2388 .txHash = txIDs[1],
2389 .batchID = batchID,
2390 },
2391 };
2392 validateClosedLedger(env, testCases);
2393
2394 // Alice consumes sequences (# of txns)
2395 BEAST_EXPECT(env.seq(alice) == seq + 3);
2396
2397 // Alice pays XRP & Fee; Bob receives XRP
2398 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(3) - batchFee);
2399 BEAST_EXPECT(env.balance(bob) == preBob + XRP(3));
2400 }
2401
2402 // tec (tecKILLED) error
2403 {
2404 auto const preAlice = env.balance(alice);
2405 auto const preBob = env.balance(bob);
2406 auto const preCarol = env.balance(carol);
2407 auto const seq = env.seq(alice);
2408 auto const batchFee = batch::calcBatchFee(env, 0, 4);
2409 auto const [txIDs, batchID] = submitBatch(
2410 env,
2411 tesSUCCESS,
2412 batch::outer(alice, seq, batchFee, tfUntilFailure),
2413 batch::Inner(pay(alice, bob, XRP(100)), seq + 1),
2414 batch::Inner(pay(alice, carol, XRP(100)), seq + 2),
2416 offer(alice, alice["USD"](100), XRP(100), tfImmediateOrCancel), seq + 3),
2417 batch::Inner(pay(alice, dave, XRP(100)), seq + 4));
2418 env.close();
2419
2420 std::vector<TestLedgerData> const testCases = {
2421 {
2422 .index = 0,
2423 .txType = "Batch",
2424 .result = "tesSUCCESS",
2425 .txHash = batchID,
2426 .batchID = std::nullopt,
2427 },
2428 {
2429 .index = 1,
2430 .txType = "Payment",
2431 .result = "tesSUCCESS",
2432 .txHash = txIDs[0],
2433 .batchID = batchID,
2434 },
2435 {
2436 .index = 2,
2437 .txType = "Payment",
2438 .result = "tesSUCCESS",
2439 .txHash = txIDs[1],
2440 .batchID = batchID,
2441 },
2442 {
2443 .index = 3,
2444 .txType = "OfferCreate",
2445 .result = "tecKILLED",
2446 .txHash = txIDs[2],
2447 .batchID = batchID,
2448 },
2449 };
2450 validateClosedLedger(env, testCases);
2451
2452 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(200) - batchFee);
2453 BEAST_EXPECT(env.balance(bob) == preBob + XRP(100));
2454 BEAST_EXPECT(env.balance(carol) == preCarol + XRP(100));
2455 }
2456 }
2457
2458 void
2460 {
2461 testcase("independent");
2462
2463 using namespace test::jtx;
2464 using namespace std::literals;
2465
2466 Env env{*this, features};
2467
2468 auto const alice = Account("alice");
2469 auto const bob = Account("bob");
2470 auto const carol = Account("carol");
2471 auto const gw = Account("gw");
2472 auto const usd = gw["USD"];
2473 env.fund(XRP(10000), alice, bob, carol, gw);
2474 env.close();
2475
2476 // multiple transactions fail
2477 {
2478 auto const preAlice = env.balance(alice);
2479 auto const preBob = env.balance(bob);
2480
2481 auto const batchFee = batch::calcBatchFee(env, 0, 4);
2482 auto const seq = env.seq(alice);
2483 auto const [txIDs, batchID] = submitBatch(
2484 env,
2485 tesSUCCESS,
2486 batch::outer(alice, seq, batchFee, tfIndependent),
2487 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
2488 // tecUNFUNDED_PAYMENT: alice does not have enough XRP
2489 batch::Inner(pay(alice, bob, XRP(9999)), seq + 2),
2490 // tecUNFUNDED_PAYMENT: alice does not have enough XRP
2491 batch::Inner(pay(alice, bob, XRP(9999)), seq + 3),
2492 batch::Inner(pay(alice, bob, XRP(3)), seq + 4));
2493 env.close();
2494
2495 std::vector<TestLedgerData> const testCases = {
2496 {
2497 .index = 0,
2498 .txType = "Batch",
2499 .result = "tesSUCCESS",
2500 .txHash = batchID,
2501 .batchID = std::nullopt,
2502 },
2503 {
2504 .index = 1,
2505 .txType = "Payment",
2506 .result = "tesSUCCESS",
2507 .txHash = txIDs[0],
2508 .batchID = batchID,
2509 },
2510 {
2511 .index = 2,
2512 .txType = "Payment",
2513 .result = "tecUNFUNDED_PAYMENT",
2514 .txHash = txIDs[1],
2515 .batchID = batchID,
2516 },
2517 {
2518 .index = 3,
2519 .txType = "Payment",
2520 .result = "tecUNFUNDED_PAYMENT",
2521 .txHash = txIDs[2],
2522 .batchID = batchID,
2523 },
2524 {
2525 .index = 4,
2526 .txType = "Payment",
2527 .result = "tesSUCCESS",
2528 .txHash = txIDs[3],
2529 .batchID = batchID,
2530 },
2531 };
2532 validateClosedLedger(env, testCases);
2533
2534 // Alice consumes sequences (# of txns)
2535 BEAST_EXPECT(env.seq(alice) == seq + 5);
2536
2537 // Alice pays XRP & Fee; Bob receives XRP
2538 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(4) - batchFee);
2539 BEAST_EXPECT(env.balance(bob) == preBob + XRP(4));
2540 }
2541
2542 // tec error
2543 {
2544 auto const preAlice = env.balance(alice);
2545 auto const preBob = env.balance(bob);
2546
2547 auto const batchFee = batch::calcBatchFee(env, 0, 4);
2548 auto const seq = env.seq(alice);
2549 auto const [txIDs, batchID] = submitBatch(
2550 env,
2551 tesSUCCESS,
2552 batch::outer(alice, seq, batchFee, tfIndependent),
2553 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
2554 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
2555 // tecUNFUNDED_PAYMENT: alice does not have enough XRP
2556 batch::Inner(pay(alice, bob, XRP(9999)), seq + 3),
2557 batch::Inner(pay(alice, bob, XRP(3)), seq + 4));
2558 env.close();
2559
2560 std::vector<TestLedgerData> const testCases = {
2561 {
2562 .index = 0,
2563 .txType = "Batch",
2564 .result = "tesSUCCESS",
2565 .txHash = batchID,
2566 .batchID = std::nullopt,
2567 },
2568 {
2569 .index = 1,
2570 .txType = "Payment",
2571 .result = "tesSUCCESS",
2572 .txHash = txIDs[0],
2573 .batchID = batchID,
2574 },
2575 {
2576 .index = 2,
2577 .txType = "Payment",
2578 .result = "tesSUCCESS",
2579 .txHash = txIDs[1],
2580 .batchID = batchID,
2581 },
2582 {
2583 .index = 3,
2584 .txType = "Payment",
2585 .result = "tecUNFUNDED_PAYMENT",
2586 .txHash = txIDs[2],
2587 .batchID = batchID,
2588 },
2589 {
2590 .index = 4,
2591 .txType = "Payment",
2592 .result = "tesSUCCESS",
2593 .txHash = txIDs[3],
2594 .batchID = batchID,
2595 },
2596 };
2597 validateClosedLedger(env, testCases);
2598
2599 // Alice consumes sequences (# of txns)
2600 BEAST_EXPECT(env.seq(alice) == seq + 5);
2601
2602 // Alice pays XRP & Fee; Bob receives XRP
2603 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(6) - batchFee);
2604 BEAST_EXPECT(env.balance(bob) == preBob + XRP(6));
2605 }
2606
2607 // tef error
2608 {
2609 auto const preAlice = env.balance(alice);
2610 auto const preBob = env.balance(bob);
2611
2612 auto const batchFee = batch::calcBatchFee(env, 0, 4);
2613 auto const seq = env.seq(alice);
2614 auto const [txIDs, batchID] = submitBatch(
2615 env,
2616 tesSUCCESS,
2617 batch::outer(alice, seq, batchFee, tfIndependent),
2618 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
2619 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
2620 // tefNO_AUTH_REQUIRED: trustline auth is not required
2621 batch::Inner(trust(alice, usd(1000), tfSetfAuth), seq + 3),
2622 batch::Inner(pay(alice, bob, XRP(3)), seq + 3));
2623 env.close();
2624
2625 std::vector<TestLedgerData> const testCases = {
2626 {
2627 .index = 0,
2628 .txType = "Batch",
2629 .result = "tesSUCCESS",
2630 .txHash = batchID,
2631 .batchID = std::nullopt,
2632 },
2633 {
2634 .index = 1,
2635 .txType = "Payment",
2636 .result = "tesSUCCESS",
2637 .txHash = txIDs[0],
2638 .batchID = batchID,
2639 },
2640 {
2641 .index = 2,
2642 .txType = "Payment",
2643 .result = "tesSUCCESS",
2644 .txHash = txIDs[1],
2645 .batchID = batchID,
2646 },
2647 {
2648 .index = 3,
2649 .txType = "Payment",
2650 .result = "tesSUCCESS",
2651 .txHash = txIDs[3],
2652 .batchID = batchID,
2653 },
2654 };
2655 validateClosedLedger(env, testCases);
2656
2657 // Alice consumes sequences (# of txns)
2658 BEAST_EXPECT(env.seq(alice) == seq + 4);
2659
2660 // Alice pays XRP & Fee; Bob receives XRP
2661 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee - XRP(6));
2662 BEAST_EXPECT(env.balance(bob) == preBob + XRP(6));
2663 }
2664
2665 // ter error
2666 {
2667 auto const preAlice = env.balance(alice);
2668 auto const preBob = env.balance(bob);
2669
2670 auto const batchFee = batch::calcBatchFee(env, 0, 4);
2671 auto const seq = env.seq(alice);
2672 auto const [txIDs, batchID] = submitBatch(
2673 env,
2674 tesSUCCESS,
2675 batch::outer(alice, seq, batchFee, tfIndependent),
2676 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
2677 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
2678 // terPRE_TICKET: ticket does not exist
2679 batch::Inner(trust(alice, usd(1000), tfSetfAuth), 0, seq + 3),
2680 batch::Inner(pay(alice, bob, XRP(3)), seq + 3));
2681 env.close();
2682
2683 std::vector<TestLedgerData> const testCases = {
2684 {
2685 .index = 0,
2686 .txType = "Batch",
2687 .result = "tesSUCCESS",
2688 .txHash = batchID,
2689 .batchID = std::nullopt,
2690 },
2691 {
2692 .index = 1,
2693 .txType = "Payment",
2694 .result = "tesSUCCESS",
2695 .txHash = txIDs[0],
2696 .batchID = batchID,
2697 },
2698 {
2699 .index = 2,
2700 .txType = "Payment",
2701 .result = "tesSUCCESS",
2702 .txHash = txIDs[1],
2703 .batchID = batchID,
2704 },
2705 {
2706 .index = 3,
2707 .txType = "Payment",
2708 .result = "tesSUCCESS",
2709 .txHash = txIDs[3],
2710 .batchID = batchID,
2711 },
2712 };
2713 validateClosedLedger(env, testCases);
2714
2715 // Alice consumes sequences (# of txns)
2716 BEAST_EXPECT(env.seq(alice) == seq + 4);
2717
2718 // Alice pays XRP & Fee; Bob receives XRP
2719 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee - XRP(6));
2720 BEAST_EXPECT(env.balance(bob) == preBob + XRP(6));
2721 }
2722
2723 // tec (tecKILLED) error
2724 {
2725 auto const preAlice = env.balance(alice);
2726 auto const preBob = env.balance(bob);
2727 auto const preCarol = env.balance(carol);
2728 auto const seq = env.seq(alice);
2729 auto const batchFee = batch::calcBatchFee(env, 0, 3);
2730 auto const [txIDs, batchID] = submitBatch(
2731 env,
2732 tesSUCCESS,
2733 batch::outer(alice, seq, batchFee, tfIndependent),
2734 batch::Inner(pay(alice, bob, XRP(100)), seq + 1),
2735 batch::Inner(pay(alice, carol, XRP(100)), seq + 2),
2737 offer(alice, alice["USD"](100), XRP(100), tfImmediateOrCancel), seq + 3));
2738 env.close();
2739
2740 std::vector<TestLedgerData> const testCases = {
2741 {
2742 .index = 0,
2743 .txType = "Batch",
2744 .result = "tesSUCCESS",
2745 .txHash = batchID,
2746 .batchID = std::nullopt,
2747 },
2748 {
2749 .index = 1,
2750 .txType = "Payment",
2751 .result = "tesSUCCESS",
2752 .txHash = txIDs[0],
2753 .batchID = batchID,
2754 },
2755 {
2756 .index = 2,
2757 .txType = "Payment",
2758 .result = "tesSUCCESS",
2759 .txHash = txIDs[1],
2760 .batchID = batchID,
2761 },
2762 {
2763 .index = 3,
2764 .txType = "OfferCreate",
2765 .result = "tecKILLED",
2766 .txHash = txIDs[2],
2767 .batchID = batchID,
2768 },
2769 };
2770 validateClosedLedger(env, testCases);
2771
2772 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(200) - batchFee);
2773 BEAST_EXPECT(env.balance(bob) == preBob + XRP(100));
2774 BEAST_EXPECT(env.balance(carol) == preCarol + XRP(100));
2775 }
2776 }
2777
2778 void
2779 doTestInnerSubmitRPC(FeatureBitset features, bool withBatch)
2780 {
2781 std::string const testName =
2782 std::string("inner submit rpc: batch ") + (withBatch ? "enabled" : "disabled") + ": ";
2783
2784 auto const amend = withBatch ? features : features - featureBatchV1_1;
2785
2786 using namespace test::jtx;
2787 using namespace std::literals;
2788
2789 Env env{*this, amend};
2790 if (!BEAST_EXPECT(amend[featureBatchV1_1] == withBatch))
2791 return;
2792
2793 auto const alice = Account("alice");
2794 auto const bob = Account("bob");
2795
2796 env.fund(XRP(10000), alice, bob);
2797 env.close();
2798
2799 // Any transaction carrying tfInnerBatchTxn is rejected in checkValidity()
2800 // before it reaches the tx engine, regardless of its signing fields or
2801 // whether the amendment is enabled.
2802 auto const submitAndValidate = [&](std::string caseName, Slice const& slice, int line) {
2803 testcase << testName << caseName;
2804 auto const jrr = env.rpc("submit", strHex(slice))[jss::result];
2805 expect(
2806 jrr[jss::status] == "error" && jrr[jss::error] == "invalidTransaction" &&
2807 !jrr.isMember(jss::error_code) && jrr[jss::error_message].isNull() &&
2808 jrr[jss::error_exception] ==
2809 "fails local checks: Batch inner transactions are never "
2810 "considered validly signed.",
2811 pretty(jrr),
2812 __FILE__,
2813 line);
2814 env.close();
2815 };
2816
2817 // Invalid RPC Submission: TxnSignature
2818 // + has `TxnSignature` field
2819 // - has no `SigningPubKey` field
2820 // - has no `Signers` field
2821 // + has `tfInnerBatchTxn` flag
2822 {
2823 auto txn = batch::Inner(pay(alice, bob, XRP(1)), env.seq(alice));
2824 txn[sfTxnSignature] = "DEADBEEF";
2825 STParsedJSONObject parsed("test", txn.getTxn());
2826 Serializer s;
2827 parsed.object->add(s); // NOLINT(bugprone-unchecked-optional-access)
2828 submitAndValidate("TxnSignature set", s.slice(), __LINE__);
2829 }
2830
2831 // Invalid RPC Submission: SigningPubKey
2832 // - has no `TxnSignature` field
2833 // + has `SigningPubKey` field
2834 // - has no `Signers` field
2835 // + has `tfInnerBatchTxn` flag
2836 {
2837 auto txn = batch::Inner(pay(alice, bob, XRP(1)), env.seq(alice));
2838 txn[sfSigningPubKey] = strHex(alice.pk());
2839 STParsedJSONObject parsed("test", txn.getTxn());
2840 Serializer s;
2841 parsed.object->add(s); // NOLINT(bugprone-unchecked-optional-access)
2842 submitAndValidate("SigningPubKey set", s.slice(), __LINE__);
2843 }
2844
2845 // Invalid RPC Submission: Signers
2846 // - has no `TxnSignature` field
2847 // + has empty `SigningPubKey` field
2848 // + has `Signers` field
2849 // + has `tfInnerBatchTxn` flag
2850 {
2851 auto txn = batch::Inner(pay(alice, bob, XRP(1)), env.seq(alice));
2852 txn[sfSigners] = json::ValueType::Array;
2853 STParsedJSONObject parsed("test", txn.getTxn());
2854 Serializer s;
2855 parsed.object->add(s); // NOLINT(bugprone-unchecked-optional-access)
2856 submitAndValidate("Signers set", s.slice(), __LINE__);
2857 }
2858
2859 {
2860 // Fully signed inner batch transaction
2861 auto const txn = batch::Inner(pay(alice, bob, XRP(1)), env.seq(alice));
2862 auto const jt = env.jt(txn.getTxn());
2863
2864 STParsedJSONObject parsed("test", jt.jv);
2865 Serializer s;
2866 parsed.object->add(s); // NOLINT(bugprone-unchecked-optional-access)
2867 submitAndValidate("Fully signed", s.slice(), __LINE__);
2868 }
2869
2870 // Invalid RPC Submission: tfInnerBatchTxn
2871 // - has no `TxnSignature` field
2872 // + has empty `SigningPubKey` field
2873 // - has no `Signers` field
2874 // + has `tfInnerBatchTxn` flag
2875 {
2876 auto const txn = batch::Inner(pay(alice, bob, XRP(1)), env.seq(alice));
2877 STParsedJSONObject parsed("test", txn.getTxn());
2878 Serializer s;
2879 parsed.object->add(s); // NOLINT(bugprone-unchecked-optional-access)
2880 submitAndValidate("No signing fields set", s.slice(), __LINE__);
2881 }
2882
2883 // Invalid RPC Submission: tfInnerBatchTxn pseudo-transaction
2884 // - has no `TxnSignature` field
2885 // + has empty `SigningPubKey` field
2886 // - has no `Signers` field
2887 // + has `tfInnerBatchTxn` flag
2888 {
2889 STTx const amendTx(ttAMENDMENT, [seq = env.closed()->header().seq + 1](auto& obj) {
2890 obj.setAccountID(sfAccount, AccountID());
2891 obj.setFieldH256(sfAmendment, featureBatchV1_1);
2892 obj.setFieldU32(sfLedgerSequence, seq);
2893 obj.setFieldU32(sfFlags, tfInnerBatchTxn);
2894 });
2895 auto const txn =
2896 batch::Inner(amendTx.getJson(JsonOptions::Values::None), env.seq(alice));
2897 STParsedJSONObject parsed("test", txn.getTxn());
2898 Serializer s;
2899 parsed.object->add(s); // NOLINT(bugprone-unchecked-optional-access)
2900 submitAndValidate("Pseudo-transaction", s.slice(), __LINE__);
2901 }
2902 }
2903
2904 void
2906 {
2907 for (bool const withBatch : {true, false})
2908 {
2909 doTestInnerSubmitRPC(features, withBatch);
2910 }
2911 }
2912
2913 void
2915 {
2916 testcase("account activation");
2917
2918 using namespace test::jtx;
2919 using namespace std::literals;
2920
2921 Env env{*this, features};
2922
2923 auto const alice = Account("alice");
2924 auto const bob = Account("bob");
2925 env.fund(XRP(10000), alice);
2926 env.close();
2927 env.memoize(bob);
2928
2929 auto const preAlice = env.balance(alice);
2930 auto const ledSeq = env.current()->seq();
2931 auto const seq = env.seq(alice);
2932 auto const batchFee = batch::calcBatchFee(env, 1, 2);
2933 auto const [txIDs, batchID] = submitBatch(
2934 env,
2935 tesSUCCESS,
2936 batch::outer(alice, seq, batchFee, tfAllOrNothing),
2937 batch::Inner(pay(alice, bob, XRP(1000)), seq + 1),
2938 batch::Inner(fset(bob, asfAllowTrustLineClawback), ledSeq),
2939 batch::Sig(bob));
2940 env.close();
2941
2942 std::vector<TestLedgerData> const testCases = {
2943 {
2944 .index = 0,
2945 .txType = "Batch",
2946 .result = "tesSUCCESS",
2947 .txHash = batchID,
2948 .batchID = std::nullopt,
2949 },
2950 {
2951 .index = 1,
2952 .txType = "Payment",
2953 .result = "tesSUCCESS",
2954 .txHash = txIDs[0],
2955 .batchID = batchID,
2956 },
2957 {
2958 .index = 2,
2959 .txType = "AccountSet",
2960 .result = "tesSUCCESS",
2961 .txHash = txIDs[1],
2962 .batchID = batchID,
2963 },
2964 };
2965 validateClosedLedger(env, testCases);
2966
2967 // Alice consumes sequences (# of txns)
2968 BEAST_EXPECT(env.seq(alice) == seq + 2);
2969
2970 // Bob consumes sequences (# of txns)
2971 BEAST_EXPECT(env.seq(bob) == ledSeq + 1);
2972
2973 // Alice pays XRP & Fee; Bob receives XRP
2974 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(1000) - batchFee);
2975 BEAST_EXPECT(env.balance(bob) == XRP(1000));
2976
2977 // An inner partial payment must never fund a new account. With
2978 // featureBatchV1_1 the partial-payment-to-create check runs on the
2979 // batch's closed view and rejects it (tefNO_DST_PARTIAL). A USD->XRP path is
2980 // set up so that, absent the check, the partial payment would deliver
2981 // XRP and create carol; the check must block exactly that. A
2982 // cross-currency shape (IOU SendMax, XRP Amount) also lets the inner
2983 // pass preflight and reach the preclaim check.
2984 {
2985 Env env{*this, features};
2986
2987 auto const alice = Account("alice");
2988 auto const gw = Account("gw");
2989 auto const carol = Account("carol"); // never funded
2990 auto const usd = gw["USD"];
2991 env.fund(XRP(10000), alice, gw);
2992 env.close();
2993 env.memoize(carol);
2994
2995 env(trust(alice, usd(100000)));
2996 env(pay(gw, alice, usd(10000)));
2997 env(offer(gw, usd(2000), XRP(2000))); // USD -> XRP liquidity
2998 env.close();
2999
3000 auto const seq = env.seq(alice);
3001 auto const batchFee = batch::calcBatchFee(env, 0, 2);
3002
3003 auto pp = pay(alice, carol, XRP(1000));
3004 pp[jss::SendMax] = usd(2000).value().getJson(JsonOptions::Values::None);
3005 pp[jss::Flags] = tfPartialPayment;
3006
3007 env(batch::outer(alice, seq, batchFee, tfIndependent),
3008 batch::Inner(pp, seq + 1),
3009 batch::Inner(noop(alice), seq + 2),
3010 Ter(tesSUCCESS));
3011 env.close();
3012
3013 // carol was not created despite the available path.
3014 BEAST_EXPECT(!env.le(carol));
3015 }
3016 }
3017
3018 void
3020 {
3021 testcase("check all signatures");
3022
3023 using namespace test::jtx;
3024 using namespace std::literals;
3025
3026 // Verifies that checkBatchSign validates all signers even when an
3027 // unfunded account (signed with its master key) appears first in the
3028 // sorted signer list. A funded account with an invalid signature must
3029 // still be rejected with tefBAD_AUTH.
3030
3031 Env env{*this, features};
3032
3033 auto const alice = Account("alice");
3034 // "aaa" sorts before other accounts alphabetically, ensuring the
3035 // unfunded account is checked first in the sorted signer list
3036 auto const unfunded = Account("aaa");
3037 auto const carol = Account("carol");
3038 env.fund(XRP(10000), alice, carol);
3039 env.close();
3040
3041 // Verify sort order: unfunded.id() < carol.id()
3042 BEAST_EXPECT(unfunded.id() < carol.id());
3043
3044 auto const seq = env.seq(alice);
3045 auto const ledSeq = env.current()->seq();
3046 auto const batchFee = batch::calcBatchFee(env, 2, 3);
3047
3048 // The batch includes:
3049 // 1. alice pays unfunded (to create unfunded's account)
3050 // 2. unfunded does a noop (signed by unfunded's master key - valid)
3051 // 3. carol pays alice (signed by alice's key - INVALID since alice is
3052 // not carol's regular key)
3053 //
3054 // checkBatchSign must validate all signers regardless of order.
3055 // This must fail with tefBAD_AUTH.
3056 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
3057 batch::Inner(pay(alice, unfunded, XRP(100)), seq + 1),
3058 batch::Inner(noop(unfunded), ledSeq),
3059 batch::Inner(pay(carol, alice, XRP(1000)), env.seq(carol)),
3060 batch::Sig(unfunded, Reg{carol, alice}),
3061 Ter(tefBAD_AUTH));
3062 env.close();
3063 }
3064
3065 void
3067 {
3068 testcase("account set");
3069
3070 using namespace test::jtx;
3071 using namespace std::literals;
3072
3073 Env env{*this, features};
3074
3075 auto const alice = Account("alice");
3076 auto const bob = Account("bob");
3077 env.fund(XRP(10000), alice, bob);
3078 env.close();
3079
3080 auto const preAlice = env.balance(alice);
3081 auto const preBob = env.balance(bob);
3082
3083 auto const seq = env.seq(alice);
3084 auto const batchFee = batch::calcBatchFee(env, 0, 2);
3085 auto tx1 = batch::Inner(noop(alice), seq + 1);
3086 std::string domain = "example.com";
3087 tx1[sfDomain] = strHex(domain);
3088 auto const [txIDs, batchID] = submitBatch(
3089 env,
3090 tesSUCCESS,
3091 batch::outer(alice, seq, batchFee, tfAllOrNothing),
3092 tx1,
3093 batch::Inner(pay(alice, bob, XRP(1)), seq + 2));
3094 env.close();
3095
3096 std::vector<TestLedgerData> const testCases = {
3097 {
3098 .index = 0,
3099 .txType = "Batch",
3100 .result = "tesSUCCESS",
3101 .txHash = batchID,
3102 .batchID = std::nullopt,
3103 },
3104 {
3105 .index = 1,
3106 .txType = "AccountSet",
3107 .result = "tesSUCCESS",
3108 .txHash = txIDs[0],
3109 .batchID = batchID,
3110 },
3111 {
3112 .index = 2,
3113 .txType = "Payment",
3114 .result = "tesSUCCESS",
3115 .txHash = txIDs[1],
3116 .batchID = batchID,
3117 },
3118 };
3119 validateClosedLedger(env, testCases);
3120
3121 auto const sle = env.le(keylet::account(alice));
3122 BEAST_EXPECT(sle);
3123 BEAST_EXPECT(sle->getFieldVL(sfDomain) == Blob(domain.begin(), domain.end()));
3124
3125 // Alice consumes sequences (# of txns)
3126 BEAST_EXPECT(env.seq(alice) == seq + 3);
3127
3128 // Alice pays XRP & Fee; Bob receives XRP
3129 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(1) - batchFee);
3130 BEAST_EXPECT(env.balance(bob) == preBob + XRP(1));
3131 }
3132
3133 void
3135 {
3136 testcase("account delete");
3137
3138 using namespace test::jtx;
3139 using namespace std::literals;
3140
3141 // tfIndependent: account delete success
3142 {
3143 Env env{*this, features};
3144
3145 auto const alice = Account("alice");
3146 auto const bob = Account("bob");
3147 env.fund(XRP(10000), alice, bob);
3148 env.close();
3149
3150 incLgrSeqForAccDel(env, alice);
3151 for (int i = 0; i < 5; ++i)
3152 env.close();
3153
3154 auto const preAlice = env.balance(alice);
3155 auto const preBob = env.balance(bob);
3156
3157 auto const seq = env.seq(alice);
3158 auto const batchFee = batch::calcBatchFee(env, 0, 2) + env.current()->fees().increment;
3159 auto const [txIDs, batchID] = submitBatch(
3160 env,
3161 tesSUCCESS,
3162 batch::outer(alice, seq, batchFee, tfIndependent),
3163 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
3164 batch::Inner(acctdelete(alice, bob), seq + 2),
3165 // terNO_ACCOUNT: alice does not exist
3166 batch::Inner(pay(alice, bob, XRP(2)), seq + 3));
3167 env.close();
3168
3169 std::vector<TestLedgerData> const testCases = {
3170 {
3171 .index = 0,
3172 .txType = "Batch",
3173 .result = "tesSUCCESS",
3174 .txHash = batchID,
3175 .batchID = std::nullopt,
3176 },
3177 {
3178 .index = 1,
3179 .txType = "Payment",
3180 .result = "tesSUCCESS",
3181 .txHash = txIDs[0],
3182 .batchID = batchID,
3183 },
3184 {
3185 .index = 2,
3186 .txType = "AccountDelete",
3187 .result = "tesSUCCESS",
3188 .txHash = txIDs[1],
3189 .batchID = batchID,
3190 },
3191 };
3192 validateClosedLedger(env, testCases);
3193
3194 // Alice does not exist; Bob receives Alice's XRP
3195 BEAST_EXPECT(!env.le(keylet::account(alice)));
3196 BEAST_EXPECT(env.balance(bob) == preBob + (preAlice - batchFee));
3197 }
3198
3199 // tfIndependent: account delete fails
3200 {
3201 Env env{*this, features};
3202
3203 auto const alice = Account("alice");
3204 auto const bob = Account("bob");
3205 env.fund(XRP(10000), alice, bob);
3206 env.close();
3207
3208 incLgrSeqForAccDel(env, alice);
3209 for (int i = 0; i < 5; ++i)
3210 env.close();
3211
3212 auto const preAlice = env.balance(alice);
3213 auto const preBob = env.balance(bob);
3214
3215 env.trust(bob["USD"](1000), alice);
3216 env.close();
3217
3218 auto const seq = env.seq(alice);
3219 auto const batchFee = batch::calcBatchFee(env, 0, 2) + env.current()->fees().increment;
3220 auto const [txIDs, batchID] = submitBatch(
3221 env,
3222 tesSUCCESS,
3223 batch::outer(alice, seq, batchFee, tfIndependent),
3224 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
3225 // tecHAS_OBLIGATIONS: alice has obligations
3226 batch::Inner(acctdelete(alice, bob), seq + 2),
3227 batch::Inner(pay(alice, bob, XRP(2)), seq + 3));
3228 env.close();
3229
3230 std::vector<TestLedgerData> const testCases = {
3231 {
3232 .index = 0,
3233 .txType = "Batch",
3234 .result = "tesSUCCESS",
3235 .txHash = batchID,
3236 .batchID = std::nullopt,
3237 },
3238 {
3239 .index = 1,
3240 .txType = "Payment",
3241 .result = "tesSUCCESS",
3242 .txHash = txIDs[0],
3243 .batchID = batchID,
3244 },
3245 {
3246 .index = 2,
3247 .txType = "AccountDelete",
3248 .result = "tecHAS_OBLIGATIONS",
3249 .txHash = txIDs[1],
3250 .batchID = batchID,
3251 },
3252 {
3253 .index = 3,
3254 .txType = "Payment",
3255 .result = "tesSUCCESS",
3256 .txHash = txIDs[2],
3257 .batchID = batchID,
3258 },
3259 };
3260 validateClosedLedger(env, testCases);
3261
3262 // Alice does not exist; Bob receives XRP
3263 BEAST_EXPECT(env.le(keylet::account(alice)));
3264 BEAST_EXPECT(env.balance(bob) == preBob + XRP(3));
3265 }
3266
3267 // tfAllOrNothing: account delete fails
3268 {
3269 Env env{*this, features};
3270
3271 auto const alice = Account("alice");
3272 auto const bob = Account("bob");
3273 env.fund(XRP(10000), alice, bob);
3274 env.close();
3275
3276 incLgrSeqForAccDel(env, alice);
3277 for (int i = 0; i < 5; ++i)
3278 env.close();
3279
3280 auto const preAlice = env.balance(alice);
3281 auto const preBob = env.balance(bob);
3282
3283 auto const seq = env.seq(alice);
3284 auto const batchFee = batch::calcBatchFee(env, 0, 2) + env.current()->fees().increment;
3285 auto const [txIDs, batchID] = submitBatch(
3286 env,
3287 tesSUCCESS,
3288 batch::outer(alice, seq, batchFee, tfAllOrNothing),
3289 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
3290 batch::Inner(acctdelete(alice, bob), seq + 2),
3291 // terNO_ACCOUNT: alice does not exist
3292 batch::Inner(pay(alice, bob, XRP(2)), seq + 3));
3293 env.close();
3294
3295 std::vector<TestLedgerData> const testCases = {
3296 {
3297 .index = 0,
3298 .txType = "Batch",
3299 .result = "tesSUCCESS",
3300 .txHash = batchID,
3301 .batchID = std::nullopt,
3302 },
3303 };
3304 validateClosedLedger(env, testCases);
3305
3306 // Alice still exists; Bob is unchanged
3307 BEAST_EXPECT(env.le(keylet::account(alice)));
3308 BEAST_EXPECT(env.balance(bob) == preBob);
3309 }
3310 }
3311
3312 void
3314 {
3315 testcase("loan");
3316
3317 bool const lendingBatchEnabled = features[featureLendingProtocolV1_2];
3318
3319 using namespace test::jtx;
3320
3321 Env env{*this, features};
3322
3323 Account const issuer{"issuer"};
3324 // For simplicity, lender will be the sole actor for the vault &
3325 // brokers.
3326 Account const lender{"lender"};
3327 // Borrower only wants to borrow
3328 Account const borrower{"borrower"};
3329
3330 // Fund the accounts and trust lines with the same amount so that tests
3331 // can use the same values regardless of the asset.
3332 env.fund(XRP(100'000), issuer, noripple(lender, borrower));
3333 env.close();
3334
3335 // Just use an XRP asset
3336 PrettyAsset const asset{xrpIssue(), 1'000'000};
3337
3338 Vault const vault{env};
3339
3340 auto const deposit = asset(50'000);
3341 auto const debtMaximumValue = asset(25'000).value();
3342 auto const coverDepositValue = asset(1000).value();
3343
3344 // Under featureLendingProtocolV1_1 LoanBrokerSet::preclaim only
3345 // accepts closed-ended vaults, so build one with a subscription
3346 // window that lets the lender deposit now, then advance the clock
3347 // past SubscriptionDate before creating loans.
3348 auto [tx, vaultKeylet, subscriptionDate] =
3349 vault.createClosedEnded({.owner = lender, .asset = asset});
3350 env(tx);
3351 env.close();
3352 BEAST_EXPECT(env.le(vaultKeylet));
3353
3354 env(vault.deposit({.depositor = lender, .id = vaultKeylet.key, .amount = deposit}));
3355 env.close();
3356
3357 // Move into the Investment phase before creating loans.
3358 vault.closePastSubscription(subscriptionDate);
3359
3360 auto const brokerKeylet =
3361 keylet::loanBroker(lender.id(), SeqProxy::rawSequence(env.seq(lender)));
3362
3363 {
3364 using namespace loan_broker;
3365 env(set(lender, vaultKeylet.key),
3366 kManagementFeeRate(TenthBips16(100)),
3367 kDebtMaximum(debtMaximumValue),
3368 kCoverRateMinimum(TenthBips32(percentageToTenthBips(10))),
3369 kCoverRateLiquidation(TenthBips32(percentageToTenthBips(25))));
3370
3371 env(coverDeposit(lender, brokerKeylet.key, coverDepositValue));
3372
3373 env.close();
3374 }
3375
3376 {
3377 using namespace loan;
3378 using namespace std::chrono_literals;
3379
3380 auto const lenderSeq = env.seq(lender);
3381 auto const batchFee = batch::calcBatchFee(env, 0, 2);
3382
3383 auto const loanKeylet = keylet::loan(brokerKeylet.key, SeqProxy::rawSequence(1));
3384 {
3385 auto const [txIDs, batchID] = submitBatch(
3386 env,
3387 lendingBatchEnabled ? temBAD_SIGNATURE : temINVALID_INNER_BATCH,
3388 batch::outer(lender, lenderSeq, batchFee, tfAllOrNothing),
3390 env.json(
3391 set(lender, brokerKeylet.key, asset(1000).value()),
3392 // Not allowed to include the counterparty signature
3393 Sig(sfCounterpartySignature, borrower),
3394 Sig(kNone),
3395 Fee(kNone),
3396 Seq(kNone)),
3397 lenderSeq + 1),
3399 pay(lender, loanKeylet.key, STAmount{asset, asset(500).value()}),
3400 lenderSeq + 2));
3401 }
3402 {
3403 auto const [txIDs, batchID] = submitBatch(
3404 env,
3406 batch::outer(lender, lenderSeq, batchFee, tfAllOrNothing),
3408 env.json(
3409 set(lender, brokerKeylet.key, asset(1000).value()),
3410 // Counterparty must be set
3411 Sig(kNone),
3412 Fee(kNone),
3413 Seq(kNone)),
3414 lenderSeq + 1),
3416 pay(lender, loanKeylet.key, STAmount{asset, asset(500).value()}),
3417 lenderSeq + 2));
3418 }
3419 {
3420 auto const [txIDs, batchID] = submitBatch(
3421 env,
3422 lendingBatchEnabled ? temBAD_SIGNER : temINVALID_INNER_BATCH,
3423 batch::outer(lender, lenderSeq, batchFee, tfAllOrNothing),
3425 env.json(
3426 set(lender, brokerKeylet.key, asset(1000).value()),
3427 // Counterparty must sign the outer transaction
3428 kCounterparty(borrower.id()),
3429 Sig(kNone),
3430 Fee(kNone),
3431 Seq(kNone)),
3432 lenderSeq + 1),
3434 pay(lender, loanKeylet.key, STAmount{asset, asset(500).value()}),
3435 lenderSeq + 2));
3436 }
3437 {
3438 // LoanSet normally charges at least 2x base fee, but since the
3439 // signature check is done by the batch, it only charges the
3440 // base fee.
3441 auto const batchFee = batch::calcBatchFee(env, 1, 2);
3442 auto const [txIDs, batchID] = submitBatch(
3443 env,
3444 lendingBatchEnabled ? TER(tesSUCCESS) : TER(temINVALID_INNER_BATCH),
3445 batch::outer(lender, lenderSeq, batchFee, tfAllOrNothing),
3447 env.json(
3448 set(lender, brokerKeylet.key, asset(1000).value()),
3449 kCounterparty(borrower.id()),
3450 Sig(kNone),
3451 Fee(kNone),
3452 Seq(kNone)),
3453 lenderSeq + 1),
3455 pay(
3456 // However, this inner transaction will fail,
3457 // because the lender is not allowed to draw the
3458 // transaction
3459 lender,
3460 loanKeylet.key,
3461 STAmount{asset, asset(500).value()}),
3462 lenderSeq + 2),
3463 batch::Sig(borrower));
3464 }
3465 env.close();
3466 BEAST_EXPECT(!env.le(loanKeylet));
3467 auto const brokerSleBefore = env.le(brokerKeylet);
3468 if (!BEAST_EXPECT(brokerSleBefore))
3469 return;
3470 auto const coverAvailableBefore = brokerSleBefore->at(sfCoverAvailable);
3471 auto const coverDepositAmount = asset(100).value();
3472 {
3473 // LoanSet normally charges at least 2x base fee, but since the
3474 // signature check is done by the batch, it only charges the
3475 // base fee.
3476 auto const lenderSeq = env.seq(lender);
3477 auto const batchFee = batch::calcBatchFee(env, 1, 2);
3478 auto const [txIDs, batchID] = submitBatch(
3479 env,
3480 lendingBatchEnabled ? TER(tesSUCCESS) : TER(temINVALID_INNER_BATCH),
3481 batch::outer(lender, lenderSeq, batchFee, tfAllOrNothing),
3483 env.json(
3484 set(lender, brokerKeylet.key, asset(1000).value()),
3485 kCounterparty(borrower.id()),
3486 Sig(kNone),
3487 Fee(kNone),
3488 Seq(kNone)),
3489 lenderSeq + 1),
3491 loan_broker::coverDeposit(lender, brokerKeylet.key, coverDepositAmount),
3492 lenderSeq + 2),
3493 batch::Sig(borrower));
3494 }
3495 env.close();
3496 BEAST_EXPECT(static_cast<bool>(env.le(loanKeylet)) == lendingBatchEnabled);
3497 if (auto const brokerSle = env.le(brokerKeylet); BEAST_EXPECT(brokerSle))
3498 {
3499 auto const expectedCover = lendingBatchEnabled
3500 ? coverAvailableBefore + coverDepositAmount
3501 : coverAvailableBefore;
3502 BEAST_EXPECT(brokerSle->at(sfCoverAvailable) == expectedCover);
3503 }
3504 }
3505 }
3506
3507 void
3509 {
3510 testcase("lending amendment");
3511
3512 using namespace test::jtx;
3513
3514 // Before LendingProtocolV1_2 a Vault inner transaction rejects the whole batch.
3515 auto const checkVaultBatch = [this](FeatureBitset amendments) {
3516 bool const lendingBatchEnabled = amendments[featureLendingProtocolV1_2];
3517 Env env{*this, amendments};
3518
3519 Account const payer{"payer"};
3520 Account const lender{"lender"};
3521 env.fund(XRP(100'000), payer, lender);
3522 env.close();
3523
3524 Vault const vault{env};
3525 auto [create, vaultKeylet] = vault.create({.owner = lender, .asset = xrpIssue()});
3526
3527 auto const payerSeq = env.seq(payer);
3528 auto const lenderSeq = env.seq(lender);
3529 auto const batchFee = batch::calcBatchFee(env, 1, 2);
3531 env,
3532 lendingBatchEnabled ? TER{tesSUCCESS} : TER{temINVALID_INNER_BATCH},
3533 batch::outer(payer, payerSeq, batchFee, tfAllOrNothing),
3534 batch::Inner(create, lenderSeq),
3536 vault.deposit(
3537 {.depositor = lender, .id = vaultKeylet.key, .amount = XRP(1'000)}),
3538 lenderSeq + 1),
3539 batch::Sig(lender));
3540 env.close();
3541
3542 BEAST_EXPECT(static_cast<bool>(env.le(vaultKeylet)) == lendingBatchEnabled);
3543 };
3544
3545 checkVaultBatch(features - featureLendingProtocolV1_2);
3546 checkVaultBatch(features);
3547 }
3548
3549 void
3551 {
3552 testcase("object create w/ sequence");
3553
3554 using namespace test::jtx;
3555 using namespace std::literals;
3556
3557 Env env{*this, features};
3558
3559 auto const alice = Account("alice");
3560 auto const bob = Account("bob");
3561 auto const gw = Account("gw");
3562 auto const usd = gw["USD"];
3563
3564 env.fund(XRP(10000), alice, bob, gw);
3565 env.close();
3566
3567 env.trust(usd(1000), alice, bob);
3568 env(pay(gw, alice, usd(100)));
3569 env(pay(gw, bob, usd(100)));
3570 env.close();
3571
3572 // success
3573 {
3574 auto const aliceSeq = env.seq(alice);
3575 auto const bobSeq = env.seq(bob);
3576 auto const preAlice = env.balance(alice);
3577 auto const preBob = env.balance(bob);
3578 auto const preAliceUSD = env.balance(alice, usd.issue());
3579 auto const preBobUSD = env.balance(bob, usd.issue());
3580
3581 auto const batchFee = batch::calcBatchFee(env, 1, 2);
3582 UInt256 const chkID{getCheckIndex(bob, env.seq(bob))};
3583 auto const [txIDs, batchID] = submitBatch(
3584 env,
3585 tesSUCCESS,
3586 batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
3587 batch::Inner(check::create(bob, alice, usd(10)), bobSeq),
3588 batch::Inner(check::cash(alice, chkID, usd(10)), aliceSeq + 1),
3589 batch::Sig(bob));
3590 env.close();
3591
3592 std::vector<TestLedgerData> const testCases = {
3593 {
3594 .index = 0,
3595 .txType = "Batch",
3596 .result = "tesSUCCESS",
3597 .txHash = batchID,
3598 .batchID = std::nullopt,
3599 },
3600 {
3601 .index = 1,
3602 .txType = "CheckCreate",
3603 .result = "tesSUCCESS",
3604 .txHash = txIDs[0],
3605 .batchID = batchID,
3606 },
3607 {
3608 .index = 2,
3609 .txType = "CheckCash",
3610 .result = "tesSUCCESS",
3611 .txHash = txIDs[1],
3612 .batchID = batchID,
3613 },
3614 };
3615 validateClosedLedger(env, testCases);
3616
3617 // Alice consumes sequences (# of txns)
3618 BEAST_EXPECT(env.seq(alice) == aliceSeq + 2);
3619
3620 // Alice consumes sequences (# of txns)
3621 BEAST_EXPECT(env.seq(bob) == bobSeq + 1);
3622
3623 // Alice pays Fee; Bob XRP Unchanged
3624 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
3625 BEAST_EXPECT(env.balance(bob) == preBob);
3626
3627 // Alice pays USD & Bob receives USD
3628 BEAST_EXPECT(env.balance(alice, usd.issue()) == preAliceUSD + usd(10));
3629 BEAST_EXPECT(env.balance(bob, usd.issue()) == preBobUSD - usd(10));
3630 }
3631
3632 // failure
3633 {
3634 env(fset(alice, asfRequireDest));
3635 env.close();
3636
3637 auto const aliceSeq = env.seq(alice);
3638 auto const bobSeq = env.seq(bob);
3639 auto const preAlice = env.balance(alice);
3640 auto const preBob = env.balance(bob);
3641 auto const preAliceUSD = env.balance(alice, usd.issue());
3642 auto const preBobUSD = env.balance(bob, usd.issue());
3643
3644 auto const batchFee = batch::calcBatchFee(env, 1, 2);
3645 UInt256 const chkID{getCheckIndex(bob, env.seq(bob))};
3646 auto const [txIDs, batchID] = submitBatch(
3647 env,
3648 tesSUCCESS,
3649 batch::outer(alice, aliceSeq, batchFee, tfIndependent),
3650 // tecDST_TAG_NEEDED - alice has enabled asfRequireDest
3651 batch::Inner(check::create(bob, alice, usd(10)), bobSeq),
3652 batch::Inner(check::cash(alice, chkID, usd(10)), aliceSeq + 1),
3653 batch::Sig(bob));
3654 env.close();
3655
3656 std::vector<TestLedgerData> const testCases = {
3657 {
3658 .index = 0,
3659 .txType = "Batch",
3660 .result = "tesSUCCESS",
3661 .txHash = batchID,
3662 .batchID = std::nullopt,
3663 },
3664 {
3665 .index = 1,
3666 .txType = "CheckCreate",
3667 .result = "tecDST_TAG_NEEDED",
3668 .txHash = txIDs[0],
3669 .batchID = batchID,
3670 },
3671 {
3672 .index = 2,
3673 .txType = "CheckCash",
3674 .result = "tecNO_ENTRY",
3675 .txHash = txIDs[1],
3676 .batchID = batchID,
3677 },
3678 };
3679 validateClosedLedger(env, testCases);
3680
3681 // Alice consumes sequences (# of txns)
3682 BEAST_EXPECT(env.seq(alice) == aliceSeq + 2);
3683
3684 // Bob consumes sequences (# of txns)
3685 BEAST_EXPECT(env.seq(bob) == bobSeq + 1);
3686
3687 // Alice pays Fee; Bob XRP Unchanged
3688 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
3689 BEAST_EXPECT(env.balance(bob) == preBob);
3690
3691 // Alice pays USD & Bob receives USD
3692 BEAST_EXPECT(env.balance(alice, usd.issue()) == preAliceUSD);
3693 BEAST_EXPECT(env.balance(bob, usd.issue()) == preBobUSD);
3694 }
3695 }
3696
3697 void
3699 {
3700 testcase("object create w/ ticket");
3701
3702 using namespace test::jtx;
3703 using namespace std::literals;
3704
3705 Env env{*this, features};
3706
3707 auto const alice = Account("alice");
3708 auto const bob = Account("bob");
3709 auto const gw = Account("gw");
3710 auto const usd = gw["USD"];
3711
3712 env.fund(XRP(10000), alice, bob, gw);
3713 env.close();
3714
3715 env.trust(usd(1000), alice, bob);
3716 env(pay(gw, alice, usd(100)));
3717 env(pay(gw, bob, usd(100)));
3718 env.close();
3719
3720 auto const aliceSeq = env.seq(alice);
3721 auto const bobSeq = env.seq(bob);
3722 auto const preAlice = env.balance(alice);
3723 auto const preBob = env.balance(bob);
3724 auto const preAliceUSD = env.balance(alice, usd.issue());
3725 auto const preBobUSD = env.balance(bob, usd.issue());
3726
3727 auto const batchFee = batch::calcBatchFee(env, 1, 3);
3728 UInt256 const chkID{getCheckIndex(bob, bobSeq + 1)};
3729 auto const [txIDs, batchID] = submitBatch(
3730 env,
3731 tesSUCCESS,
3732 batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
3733 batch::Inner(ticket::create(bob, 10), bobSeq),
3734 batch::Inner(check::create(bob, alice, usd(10)), 0, bobSeq + 1),
3735 batch::Inner(check::cash(alice, chkID, usd(10)), aliceSeq + 1),
3736 batch::Sig(bob));
3737 env.close();
3738
3739 std::vector<TestLedgerData> const testCases = {
3740 {
3741 .index = 0,
3742 .txType = "Batch",
3743 .result = "tesSUCCESS",
3744 .txHash = batchID,
3745 .batchID = std::nullopt,
3746 },
3747 {
3748 .index = 1,
3749 .txType = "TicketCreate",
3750 .result = "tesSUCCESS",
3751 .txHash = txIDs[0],
3752 .batchID = batchID,
3753 },
3754 {
3755 .index = 2,
3756 .txType = "CheckCreate",
3757 .result = "tesSUCCESS",
3758 .txHash = txIDs[1],
3759 .batchID = batchID,
3760 },
3761 {
3762 .index = 3,
3763 .txType = "CheckCash",
3764 .result = "tesSUCCESS",
3765 .txHash = txIDs[2],
3766 .batchID = batchID,
3767 },
3768 };
3769 validateClosedLedger(env, testCases);
3770
3771 BEAST_EXPECT(env.seq(alice) == aliceSeq + 2);
3772 BEAST_EXPECT(env.seq(bob) == bobSeq + 10 + 1);
3773 BEAST_EXPECT(env.balance(alice) == preAlice - batchFee);
3774 BEAST_EXPECT(env.balance(bob) == preBob);
3775 BEAST_EXPECT(env.balance(alice, usd.issue()) == preAliceUSD + usd(10));
3776 BEAST_EXPECT(env.balance(bob, usd.issue()) == preBobUSD - usd(10));
3777 }
3778
3779 void
3781 {
3782 testcase("object create w/ 3rd party");
3783
3784 using namespace test::jtx;
3785 using namespace std::literals;
3786
3787 Env env{*this, features};
3788
3789 auto const alice = Account("alice");
3790 auto const bob = Account("bob");
3791 auto const carol = Account("carol");
3792 auto const gw = Account("gw");
3793 auto const usd = gw["USD"];
3794
3795 env.fund(XRP(10000), alice, bob, carol, gw);
3796 env.close();
3797
3798 env.trust(usd(1000), alice, bob);
3799 env(pay(gw, alice, usd(100)));
3800 env(pay(gw, bob, usd(100)));
3801 env.close();
3802
3803 auto const aliceSeq = env.seq(alice);
3804 auto const bobSeq = env.seq(bob);
3805 auto const carolSeq = env.seq(carol);
3806 auto const preAlice = env.balance(alice);
3807 auto const preBob = env.balance(bob);
3808 auto const preCarol = env.balance(carol);
3809 auto const preAliceUSD = env.balance(alice, usd.issue());
3810 auto const preBobUSD = env.balance(bob, usd.issue());
3811
3812 auto const batchFee = batch::calcBatchFee(env, 2, 2);
3813 UInt256 const chkID{getCheckIndex(bob, env.seq(bob))};
3814 auto const [txIDs, batchID] = submitBatch(
3815 env,
3816 tesSUCCESS,
3817 batch::outer(carol, carolSeq, batchFee, tfAllOrNothing),
3818 batch::Inner(check::create(bob, alice, usd(10)), bobSeq),
3819 batch::Inner(check::cash(alice, chkID, usd(10)), aliceSeq),
3820 batch::Sig(alice, bob));
3821 env.close();
3822
3823 std::vector<TestLedgerData> const testCases = {
3824 {
3825 .index = 0,
3826 .txType = "Batch",
3827 .result = "tesSUCCESS",
3828 .txHash = batchID,
3829 .batchID = std::nullopt,
3830 },
3831 {
3832 .index = 1,
3833 .txType = "CheckCreate",
3834 .result = "tesSUCCESS",
3835 .txHash = txIDs[0],
3836 .batchID = batchID,
3837 },
3838 {
3839 .index = 2,
3840 .txType = "CheckCash",
3841 .result = "tesSUCCESS",
3842 .txHash = txIDs[1],
3843 .batchID = batchID,
3844 },
3845 };
3846 validateClosedLedger(env, testCases);
3847
3848 BEAST_EXPECT(env.seq(alice) == aliceSeq + 1);
3849 BEAST_EXPECT(env.seq(bob) == bobSeq + 1);
3850 BEAST_EXPECT(env.seq(carol) == carolSeq + 1);
3851 BEAST_EXPECT(env.balance(alice) == preAlice);
3852 BEAST_EXPECT(env.balance(bob) == preBob);
3853 BEAST_EXPECT(env.balance(carol) == preCarol - batchFee);
3854 BEAST_EXPECT(env.balance(alice, usd.issue()) == preAliceUSD + usd(10));
3855 BEAST_EXPECT(env.balance(bob, usd.issue()) == preBobUSD - usd(10));
3856 }
3857
3858 void
3860 {
3861 {
3862 testcase("tickets outer");
3863
3864 using namespace test::jtx;
3865 using namespace std::literals;
3866
3867 Env env{*this, features};
3868
3869 auto const alice = Account("alice");
3870 auto const bob = Account("bob");
3871
3872 env.fund(XRP(10000), alice, bob);
3873 env.close();
3874
3875 std::uint32_t const aliceTicketSeq{env.seq(alice) + 1};
3876 env(ticket::create(alice, 10));
3877 env.close();
3878
3879 auto const aliceSeq = env.seq(alice);
3880 auto const preAlice = env.balance(alice);
3881 auto const preBob = env.balance(bob);
3882
3883 auto const batchFee = batch::calcBatchFee(env, 0, 2);
3884 auto const [txIDs, batchID] = submitBatch(
3885 env,
3886 tesSUCCESS,
3887 batch::outer(alice, 0, batchFee, tfAllOrNothing),
3888 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq + 0),
3889 batch::Inner(pay(alice, bob, XRP(2)), aliceSeq + 1),
3890 ticket::Use(aliceTicketSeq));
3891 env.close();
3892
3893 std::vector<TestLedgerData> const testCases = {
3894 {
3895 .index = 0,
3896 .txType = "Batch",
3897 .result = "tesSUCCESS",
3898 .txHash = batchID,
3899 .batchID = std::nullopt,
3900 },
3901 {
3902 .index = 1,
3903 .txType = "Payment",
3904 .result = "tesSUCCESS",
3905 .txHash = txIDs[0],
3906 .batchID = batchID,
3907 },
3908 {
3909 .index = 2,
3910 .txType = "Payment",
3911 .result = "tesSUCCESS",
3912 .txHash = txIDs[1],
3913 .batchID = batchID,
3914 },
3915 };
3916 validateClosedLedger(env, testCases);
3917
3918 auto const sle = env.le(keylet::account(alice));
3919 BEAST_EXPECT(sle);
3920 BEAST_EXPECT(sle->getFieldU32(sfOwnerCount) == 9);
3921 BEAST_EXPECT(sle->getFieldU32(sfTicketCount) == 9);
3922
3923 BEAST_EXPECT(env.seq(alice) == aliceSeq + 2);
3924 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(3) - batchFee);
3925 BEAST_EXPECT(env.balance(bob) == preBob + XRP(3));
3926 }
3927
3928 {
3929 testcase("tickets inner");
3930
3931 using namespace test::jtx;
3932 using namespace std::literals;
3933
3934 Env env{*this, features};
3935
3936 auto const alice = Account("alice");
3937 auto const bob = Account("bob");
3938
3939 env.fund(XRP(10000), alice, bob);
3940 env.close();
3941
3942 std::uint32_t aliceTicketSeq{env.seq(alice) + 1};
3943 env(ticket::create(alice, 10));
3944 env.close();
3945
3946 auto const aliceSeq = env.seq(alice);
3947 auto const preAlice = env.balance(alice);
3948 auto const preBob = env.balance(bob);
3949
3950 auto const batchFee = batch::calcBatchFee(env, 0, 2);
3951 auto const [txIDs, batchID] = submitBatch(
3952 env,
3953 tesSUCCESS,
3954 batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
3955 batch::Inner(pay(alice, bob, XRP(1)), 0, aliceTicketSeq),
3956 batch::Inner(pay(alice, bob, XRP(2)), 0, aliceTicketSeq + 1));
3957 env.close();
3958
3959 std::vector<TestLedgerData> const testCases = {
3960 {
3961 .index = 0,
3962 .txType = "Batch",
3963 .result = "tesSUCCESS",
3964 .txHash = batchID,
3965 .batchID = std::nullopt,
3966 },
3967 {
3968 .index = 1,
3969 .txType = "Payment",
3970 .result = "tesSUCCESS",
3971 .txHash = txIDs[0],
3972 .batchID = batchID,
3973 },
3974 {
3975 .index = 2,
3976 .txType = "Payment",
3977 .result = "tesSUCCESS",
3978 .txHash = txIDs[1],
3979 .batchID = batchID,
3980 },
3981 };
3982 validateClosedLedger(env, testCases);
3983
3984 auto const sle = env.le(keylet::account(alice));
3985 BEAST_EXPECT(sle);
3986 BEAST_EXPECT(sle->getFieldU32(sfOwnerCount) == 8);
3987 BEAST_EXPECT(sle->getFieldU32(sfTicketCount) == 8);
3988
3989 BEAST_EXPECT(env.seq(alice) == aliceSeq + 1);
3990 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(3) - batchFee);
3991 BEAST_EXPECT(env.balance(bob) == preBob + XRP(3));
3992 }
3993
3994 {
3995 testcase("tickets outer inner");
3996
3997 using namespace test::jtx;
3998 using namespace std::literals;
3999
4000 Env env{*this, features};
4001
4002 auto const alice = Account("alice");
4003 auto const bob = Account("bob");
4004
4005 env.fund(XRP(10000), alice, bob);
4006 env.close();
4007
4008 std::uint32_t const aliceTicketSeq{env.seq(alice) + 1};
4009 env(ticket::create(alice, 10));
4010 env.close();
4011
4012 auto const aliceSeq = env.seq(alice);
4013 auto const preAlice = env.balance(alice);
4014 auto const preBob = env.balance(bob);
4015
4016 auto const batchFee = batch::calcBatchFee(env, 0, 2);
4017 auto const [txIDs, batchID] = submitBatch(
4018 env,
4019 tesSUCCESS,
4020 batch::outer(alice, 0, batchFee, tfAllOrNothing),
4021 batch::Inner(pay(alice, bob, XRP(1)), 0, aliceTicketSeq + 1),
4022 batch::Inner(pay(alice, bob, XRP(2)), aliceSeq),
4023 ticket::Use(aliceTicketSeq));
4024 env.close();
4025
4026 std::vector<TestLedgerData> const testCases = {
4027 {
4028 .index = 0,
4029 .txType = "Batch",
4030 .result = "tesSUCCESS",
4031 .txHash = batchID,
4032 .batchID = std::nullopt,
4033 },
4034 {
4035 .index = 1,
4036 .txType = "Payment",
4037 .result = "tesSUCCESS",
4038 .txHash = txIDs[0],
4039 .batchID = batchID,
4040 },
4041 {
4042 .index = 2,
4043 .txType = "Payment",
4044 .result = "tesSUCCESS",
4045 .txHash = txIDs[1],
4046 .batchID = batchID,
4047 },
4048 };
4049 validateClosedLedger(env, testCases);
4050
4051 auto const sle = env.le(keylet::account(alice));
4052 BEAST_EXPECT(sle);
4053 BEAST_EXPECT(sle->getFieldU32(sfOwnerCount) == 8);
4054 BEAST_EXPECT(sle->getFieldU32(sfTicketCount) == 8);
4055
4056 BEAST_EXPECT(env.seq(alice) == aliceSeq + 1);
4057 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(3) - batchFee);
4058 BEAST_EXPECT(env.balance(bob) == preBob + XRP(3));
4059 }
4060 }
4061
4062 void
4064 {
4065 testcase("sequence open ledger");
4066
4067 using namespace test::jtx;
4068 using namespace std::literals;
4069
4070 auto const alice = Account("alice");
4071 auto const bob = Account("bob");
4072 auto const carol = Account("carol");
4073
4074 // Before Batch Txn w/ retry following ledger
4075 {
4076 // IMPORTANT: The batch txn is applied first, then the noop txn.
4077 // Because of this ordering, the noop txn is not applied and is
4078 // overwritten by the payment in the batch transaction. Because the
4079 // terPRE_SEQ is outside of the batch this noop transaction will ge
4080 // reapplied in the following ledger
4081 Env env{*this, features};
4082 env.fund(XRP(10000), alice, bob, carol);
4083 env.close();
4084
4085 auto const aliceSeq = env.seq(alice);
4086 auto const carolSeq = env.seq(carol);
4087
4088 // AccountSet Txn
4089 auto const noopTxn = env.jt(noop(alice), Seq(aliceSeq + 2));
4090 auto const noopTxnID = to_string(noopTxn.stx->getTransactionID());
4091 env(noopTxn, Ter(terPRE_SEQ));
4092
4093 // Batch Txn
4094 auto const batchFee = batch::calcBatchFee(env, 1, 2);
4095 auto const [txIDs, batchID] = submitBatch(
4096 env,
4097 tesSUCCESS,
4098 batch::outer(carol, carolSeq, batchFee, tfAllOrNothing),
4099 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
4100 batch::Inner(pay(alice, bob, XRP(2)), aliceSeq + 1),
4101 batch::Sig(alice));
4102 env.close();
4103
4104 {
4105 std::vector<TestLedgerData> const testCases = {
4106 {
4107 .index = 0,
4108 .txType = "Batch",
4109 .result = "tesSUCCESS",
4110 .txHash = batchID,
4111 .batchID = std::nullopt,
4112 },
4113 {
4114 .index = 1,
4115 .txType = "Payment",
4116 .result = "tesSUCCESS",
4117 .txHash = txIDs[0],
4118 .batchID = batchID,
4119 },
4120 {
4121 .index = 2,
4122 .txType = "Payment",
4123 .result = "tesSUCCESS",
4124 .txHash = txIDs[1],
4125 .batchID = batchID,
4126 },
4127 };
4128 validateClosedLedger(env, testCases);
4129 }
4130
4131 env.close();
4132 {
4133 // next ledger contains noop txn
4134 std::vector<TestLedgerData> const testCases = {
4135 {
4136 .index = 0,
4137 .txType = "AccountSet",
4138 .result = "tesSUCCESS",
4139 .txHash = noopTxnID,
4140 .batchID = std::nullopt,
4141 },
4142 };
4143 validateClosedLedger(env, testCases);
4144 }
4145 }
4146
4147 // Before Batch Txn w/ same sequence
4148 {
4149 // IMPORTANT: The batch txn is applied first, then the noop txn.
4150 // Because of this ordering, the noop txn is not applied and is
4151 // overwritten by the payment in the batch transaction.
4152 Env env{*this, features};
4153 env.fund(XRP(10000), alice, bob);
4154 env.close();
4155
4156 auto const aliceSeq = env.seq(alice);
4157
4158 // AccountSet Txn
4159 auto const noopTxn = env.jt(noop(alice), Seq(aliceSeq + 1));
4160 env(noopTxn, Ter(terPRE_SEQ));
4161
4162 // Batch Txn
4163 auto const batchFee = batch::calcBatchFee(env, 0, 2);
4164 auto const [txIDs, batchID] = submitBatch(
4165 env,
4166 tesSUCCESS,
4167 batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
4168 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq + 1),
4169 batch::Inner(pay(alice, bob, XRP(2)), aliceSeq + 2));
4170 env.close();
4171
4172 {
4173 std::vector<TestLedgerData> const testCases = {
4174 {
4175 .index = 0,
4176 .txType = "Batch",
4177 .result = "tesSUCCESS",
4178 .txHash = batchID,
4179 .batchID = std::nullopt,
4180 },
4181 {
4182 .index = 1,
4183 .txType = "Payment",
4184 .result = "tesSUCCESS",
4185 .txHash = txIDs[0],
4186 .batchID = batchID,
4187 },
4188 {
4189 .index = 2,
4190 .txType = "Payment",
4191 .result = "tesSUCCESS",
4192 .txHash = txIDs[1],
4193 .batchID = batchID,
4194 },
4195 };
4196 validateClosedLedger(env, testCases);
4197 }
4198
4199 env.close();
4200 {
4201 // next ledger is empty
4202 std::vector<TestLedgerData> const testCases = {};
4203 validateClosedLedger(env, testCases);
4204 }
4205 }
4206
4207 // After Batch Txn w/ same sequence
4208 {
4209 // IMPORTANT: The batch txn is applied first, then the noop txn.
4210 // Because of this ordering, the noop txn is not applied and is
4211 // overwritten by the payment in the batch transaction.
4212 Env env{*this, features};
4213 env.fund(XRP(10000), alice, bob);
4214 env.close();
4215
4216 auto const aliceSeq = env.seq(alice);
4217 auto const batchFee = batch::calcBatchFee(env, 0, 2);
4218 auto const [txIDs, batchID] = submitBatch(
4219 env,
4220 tesSUCCESS,
4221 batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
4222 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq + 1),
4223 batch::Inner(pay(alice, bob, XRP(2)), aliceSeq + 2));
4224
4225 auto const noopTxn = env.jt(noop(alice), Seq(aliceSeq + 1));
4226 env(noopTxn, Ter(tesSUCCESS));
4227 env.close();
4228
4229 {
4230 std::vector<TestLedgerData> const testCases = {
4231 {
4232 .index = 0,
4233 .txType = "Batch",
4234 .result = "tesSUCCESS",
4235 .txHash = batchID,
4236 .batchID = std::nullopt,
4237 },
4238 {
4239 .index = 1,
4240 .txType = "Payment",
4241 .result = "tesSUCCESS",
4242 .txHash = txIDs[0],
4243 .batchID = batchID,
4244 },
4245 {
4246 .index = 2,
4247 .txType = "Payment",
4248 .result = "tesSUCCESS",
4249 .txHash = txIDs[1],
4250 .batchID = batchID,
4251 },
4252 };
4253 validateClosedLedger(env, testCases);
4254 }
4255
4256 env.close();
4257 {
4258 // next ledger is empty
4259 std::vector<TestLedgerData> const testCases = {};
4260 validateClosedLedger(env, testCases);
4261 }
4262 }
4263
4264 // Outer Batch terPRE_SEQ
4265 {
4266 Env env{*this, features};
4267 env.fund(XRP(10000), alice, bob, carol);
4268 env.close();
4269
4270 auto const aliceSeq = env.seq(alice);
4271 auto const carolSeq = env.seq(carol);
4272
4273 // Batch Txn
4274 auto const batchFee = batch::calcBatchFee(env, 1, 2);
4275 auto const [txIDs, batchID] = submitBatch(
4276 env,
4277 terPRE_SEQ,
4278 batch::outer(carol, carolSeq + 1, batchFee, tfAllOrNothing),
4279 batch::Inner(pay(alice, bob, XRP(1)), aliceSeq),
4280 batch::Inner(pay(alice, bob, XRP(2)), aliceSeq + 1),
4281 batch::Sig(alice));
4282
4283 // AccountSet Txn
4284 auto const noopTxn = env.jt(noop(carol), Seq(carolSeq));
4285 auto const noopTxnID = to_string(noopTxn.stx->getTransactionID());
4286 env(noopTxn, Ter(tesSUCCESS));
4287 env.close();
4288
4289 {
4290 std::vector<TestLedgerData> const testCases = {
4291 {
4292 .index = 0,
4293 .txType = "AccountSet",
4294 .result = "tesSUCCESS",
4295 .txHash = noopTxnID,
4296 .batchID = std::nullopt,
4297 },
4298 {
4299 .index = 1,
4300 .txType = "Batch",
4301 .result = "tesSUCCESS",
4302 .txHash = batchID,
4303 .batchID = std::nullopt,
4304 },
4305 {
4306 .index = 2,
4307 .txType = "Payment",
4308 .result = "tesSUCCESS",
4309 .txHash = txIDs[0],
4310 .batchID = batchID,
4311 },
4312 {
4313 .index = 3,
4314 .txType = "Payment",
4315 .result = "tesSUCCESS",
4316 .txHash = txIDs[1],
4317 .batchID = batchID,
4318 },
4319 };
4320 validateClosedLedger(env, testCases);
4321 }
4322
4323 env.close();
4324 {
4325 // next ledger contains no transactions
4326 std::vector<TestLedgerData> const testCases = {};
4327 validateClosedLedger(env, testCases);
4328 }
4329 }
4330 }
4331
4332 void
4334 {
4335 testcase("tickets open ledger");
4336
4337 using namespace test::jtx;
4338 using namespace std::literals;
4339
4340 auto const alice = Account("alice");
4341 auto const bob = Account("bob");
4342
4343 // Before Batch Txn w/ same ticket
4344 {
4345 // IMPORTANT: The batch txn is applied first, then the noop txn.
4346 // Because of this ordering, the noop txn is not applied and is
4347 // overwritten by the payment in the batch transaction.
4348 Env env{*this, features};
4349 env.fund(XRP(10000), alice, bob);
4350 env.close();
4351
4352 std::uint32_t const aliceTicketSeq{env.seq(alice) + 1};
4353 env(ticket::create(alice, 10));
4354 env.close();
4355
4356 auto const aliceSeq = env.seq(alice);
4357
4358 // AccountSet Txn
4359 auto const noopTxn = env.jt(noop(alice), ticket::Use(aliceTicketSeq + 1));
4360 env(noopTxn, Ter(tesSUCCESS));
4361
4362 // Batch Txn
4363 auto const batchFee = batch::calcBatchFee(env, 0, 2);
4364 auto const [txIDs, batchID] = submitBatch(
4365 env,
4366 tesSUCCESS,
4367 batch::outer(alice, 0, batchFee, tfAllOrNothing),
4368 batch::Inner(pay(alice, bob, XRP(1)), 0, aliceTicketSeq + 1),
4369 batch::Inner(pay(alice, bob, XRP(2)), aliceSeq),
4370 ticket::Use(aliceTicketSeq));
4371 env.close();
4372
4373 {
4374 std::vector<TestLedgerData> const testCases = {
4375 {
4376 .index = 0,
4377 .txType = "Batch",
4378 .result = "tesSUCCESS",
4379 .txHash = batchID,
4380 .batchID = std::nullopt,
4381 },
4382 {
4383 .index = 1,
4384 .txType = "Payment",
4385 .result = "tesSUCCESS",
4386 .txHash = txIDs[0],
4387 .batchID = batchID,
4388 },
4389 {
4390 .index = 2,
4391 .txType = "Payment",
4392 .result = "tesSUCCESS",
4393 .txHash = txIDs[1],
4394 .batchID = batchID,
4395 },
4396 };
4397 validateClosedLedger(env, testCases);
4398 }
4399
4400 env.close();
4401 {
4402 // next ledger is empty
4403 std::vector<TestLedgerData> const testCases = {};
4404 validateClosedLedger(env, testCases);
4405 }
4406 }
4407
4408 // After Batch Txn w/ same ticket
4409 {
4410 // IMPORTANT: The batch txn is applied first, then the noop txn.
4411 // Because of this ordering, the noop txn is not applied and is
4412 // overwritten by the payment in the batch transaction.
4413 Env env{*this, features};
4414 env.fund(XRP(10000), alice, bob);
4415 env.close();
4416
4417 std::uint32_t const aliceTicketSeq{env.seq(alice) + 1};
4418 env(ticket::create(alice, 10));
4419 env.close();
4420
4421 auto const aliceSeq = env.seq(alice);
4422
4423 // Batch Txn
4424 auto const batchFee = batch::calcBatchFee(env, 0, 2);
4425 auto const [txIDs, batchID] = submitBatch(
4426 env,
4427 tesSUCCESS,
4428 batch::outer(alice, 0, batchFee, tfAllOrNothing),
4429 batch::Inner(pay(alice, bob, XRP(1)), 0, aliceTicketSeq + 1),
4430 batch::Inner(pay(alice, bob, XRP(2)), aliceSeq),
4431 ticket::Use(aliceTicketSeq));
4432
4433 // AccountSet Txn
4434 auto const noopTxn = env.jt(noop(alice), ticket::Use(aliceTicketSeq + 1));
4435 env(noopTxn);
4436
4437 env.close();
4438 {
4439 std::vector<TestLedgerData> const testCases = {
4440 {
4441 .index = 0,
4442 .txType = "Batch",
4443 .result = "tesSUCCESS",
4444 .txHash = batchID,
4445 .batchID = std::nullopt,
4446 },
4447 {
4448 .index = 1,
4449 .txType = "Payment",
4450 .result = "tesSUCCESS",
4451 .txHash = txIDs[0],
4452 .batchID = batchID,
4453 },
4454 {
4455 .index = 2,
4456 .txType = "Payment",
4457 .result = "tesSUCCESS",
4458 .txHash = txIDs[1],
4459 .batchID = batchID,
4460 },
4461 };
4462 validateClosedLedger(env, testCases);
4463 }
4464
4465 env.close();
4466 {
4467 // next ledger is empty
4468 std::vector<TestLedgerData> const testCases = {};
4469 validateClosedLedger(env, testCases);
4470 }
4471 }
4472 }
4473
4474 void
4476 {
4477 testcase("objects open ledger");
4478
4479 using namespace test::jtx;
4480 using namespace std::literals;
4481
4482 auto const alice = Account("alice");
4483 auto const bob = Account("bob");
4484
4485 // Consume Object Before Batch Txn
4486 {
4487 // IMPORTANT: The initial result of `CheckCash` is tecNO_ENTRY
4488 // because the create transaction has not been applied because the
4489 // batch will run in the close ledger process. The batch will be
4490 // allied and then retry this transaction in the current ledger.
4491
4492 Env env{*this, features};
4493 env.fund(XRP(10000), alice, bob);
4494 env.close();
4495
4496 std::uint32_t const aliceTicketSeq{env.seq(alice) + 1};
4497 env(ticket::create(alice, 10));
4498 env.close();
4499
4500 auto const aliceSeq = env.seq(alice);
4501
4502 // CheckCash Txn
4503 UInt256 const chkID{getCheckIndex(alice, aliceSeq)};
4504 auto const objTxn = env.jt(check::cash(bob, chkID, XRP(10)));
4505 auto const objTxnID = to_string(objTxn.stx->getTransactionID());
4506 env(objTxn, Ter(tecNO_ENTRY));
4507
4508 // Batch Txn
4509 auto const batchFee = batch::calcBatchFee(env, 0, 2);
4510 auto const [txIDs, batchID] = submitBatch(
4511 env,
4512 tesSUCCESS,
4513 batch::outer(alice, 0, batchFee, tfAllOrNothing),
4514 batch::Inner(check::create(alice, bob, XRP(10)), aliceSeq),
4515 batch::Inner(pay(alice, bob, XRP(1)), 0, aliceTicketSeq + 1),
4516 ticket::Use(aliceTicketSeq));
4517
4518 env.close();
4519 {
4520 std::vector<TestLedgerData> const testCases = {
4521 {
4522 .index = 0,
4523 .txType = "Batch",
4524 .result = "tesSUCCESS",
4525 .txHash = batchID,
4526 .batchID = std::nullopt,
4527 },
4528 {
4529 .index = 1,
4530 .txType = "CheckCreate",
4531 .result = "tesSUCCESS",
4532 .txHash = txIDs[0],
4533 .batchID = batchID,
4534 },
4535 {
4536 .index = 2,
4537 .txType = "Payment",
4538 .result = "tesSUCCESS",
4539 .txHash = txIDs[1],
4540 .batchID = batchID,
4541 },
4542 {
4543 .index = 3,
4544 .txType = "CheckCash",
4545 .result = "tesSUCCESS",
4546 .txHash = objTxnID,
4547 .batchID = std::nullopt,
4548 },
4549 };
4550 validateClosedLedger(env, testCases);
4551 }
4552
4553 env.close();
4554 {
4555 // next ledger is empty
4556 std::vector<TestLedgerData> const testCases = {};
4557 validateClosedLedger(env, testCases);
4558 }
4559 }
4560
4561 // Create Object Before Batch Txn
4562 {
4563 Env env{*this, features};
4564 env.fund(XRP(10000), alice, bob);
4565 env.close();
4566
4567 std::uint32_t const aliceTicketSeq{env.seq(alice) + 1};
4568 env(ticket::create(alice, 10));
4569 env.close();
4570
4571 auto const aliceSeq = env.seq(alice);
4572 auto const bobSeq = env.seq(bob);
4573
4574 // CheckCreate Txn
4575 UInt256 const chkID{getCheckIndex(alice, aliceSeq)};
4576 auto const objTxn = env.jt(check::create(alice, bob, XRP(10)));
4577 auto const objTxnID = to_string(objTxn.stx->getTransactionID());
4578 env(objTxn, Ter(tesSUCCESS));
4579
4580 // Batch Txn
4581 auto const batchFee = batch::calcBatchFee(env, 1, 2);
4582 auto const [txIDs, batchID] = submitBatch(
4583 env,
4584 tesSUCCESS,
4585 batch::outer(alice, 0, batchFee, tfAllOrNothing),
4586 batch::Inner(check::cash(bob, chkID, XRP(10)), bobSeq),
4587 batch::Inner(pay(alice, bob, XRP(1)), 0, aliceTicketSeq + 1),
4588 ticket::Use(aliceTicketSeq),
4589 batch::Sig(bob));
4590
4591 env.close();
4592 {
4593 std::vector<TestLedgerData> const testCases = {
4594 {
4595 .index = 0,
4596 .txType = "CheckCreate",
4597 .result = "tesSUCCESS",
4598 .txHash = objTxnID,
4599 .batchID = std::nullopt,
4600 },
4601 {
4602 .index = 1,
4603 .txType = "Batch",
4604 .result = "tesSUCCESS",
4605 .txHash = batchID,
4606 .batchID = std::nullopt,
4607 },
4608 {
4609 .index = 2,
4610 .txType = "CheckCash",
4611 .result = "tesSUCCESS",
4612 .txHash = txIDs[0],
4613 .batchID = batchID,
4614 },
4615 {
4616 .index = 3,
4617 .txType = "Payment",
4618 .result = "tesSUCCESS",
4619 .txHash = txIDs[1],
4620 .batchID = batchID,
4621 },
4622 };
4623 validateClosedLedger(env, testCases);
4624 }
4625 }
4626
4627 // After Batch Txn
4628 {
4629 // IMPORTANT: The initial result of `CheckCash` is tecNO_ENTRY
4630 // because the create transaction has not been applied because the
4631 // batch will run in the close ledger process. The batch will be
4632 // applied and then retry this transaction in the current ledger.
4633
4634 Env env{*this, features};
4635 env.fund(XRP(10000), alice, bob);
4636 env.close();
4637
4638 std::uint32_t const aliceTicketSeq{env.seq(alice) + 1};
4639 env(ticket::create(alice, 10));
4640 env.close();
4641
4642 auto const aliceSeq = env.seq(alice);
4643
4644 // Batch Txn
4645 auto const batchFee = batch::calcBatchFee(env, 0, 2);
4646 UInt256 const chkID{getCheckIndex(alice, aliceSeq)};
4647 auto const [txIDs, batchID] = submitBatch(
4648 env,
4649 tesSUCCESS,
4650 batch::outer(alice, 0, batchFee, tfAllOrNothing),
4651 batch::Inner(check::create(alice, bob, XRP(10)), aliceSeq),
4652 batch::Inner(pay(alice, bob, XRP(1)), 0, aliceTicketSeq + 1),
4653 ticket::Use(aliceTicketSeq));
4654
4655 // CheckCash Txn
4656 auto const objTxn = env.jt(check::cash(bob, chkID, XRP(10)));
4657 auto const objTxnID = to_string(objTxn.stx->getTransactionID());
4658 env(objTxn, Ter(tecNO_ENTRY));
4659
4660 env.close();
4661 {
4662 std::vector<TestLedgerData> const testCases = {
4663 {
4664 .index = 0,
4665 .txType = "Batch",
4666 .result = "tesSUCCESS",
4667 .txHash = batchID,
4668 .batchID = std::nullopt,
4669 },
4670 {
4671 .index = 1,
4672 .txType = "CheckCreate",
4673 .result = "tesSUCCESS",
4674 .txHash = txIDs[0],
4675 .batchID = batchID,
4676 },
4677 {
4678 .index = 2,
4679 .txType = "Payment",
4680 .result = "tesSUCCESS",
4681 .txHash = txIDs[1],
4682 .batchID = batchID,
4683 },
4684 {
4685 .index = 3,
4686 .txType = "CheckCash",
4687 .result = "tesSUCCESS",
4688 .txHash = objTxnID,
4689 .batchID = std::nullopt,
4690 },
4691 };
4692 validateClosedLedger(env, testCases);
4693 }
4694 }
4695 }
4696
4697 void
4699 {
4700 testcase("pseudo txn with tfInnerBatchTxn");
4701
4702 using namespace test::jtx;
4703 using namespace std::literals;
4704
4705 Env env{*this, features};
4706
4707 auto const alice = Account("alice");
4708 auto const bob = Account("bob");
4709 env.fund(XRP(10000), alice, bob);
4710 env.close();
4711
4712 STTx const stx = STTx(ttAMENDMENT, [&](auto& obj) {
4713 obj.setAccountID(sfAccount, AccountID());
4714 obj.setFieldH256(sfAmendment, UInt256(2));
4715 obj.setFieldU32(sfLedgerSequence, env.seq(alice));
4716 obj.setFieldU32(sfFlags, tfInnerBatchTxn);
4717 });
4718
4719 std::string reason;
4720 BEAST_EXPECT(isPseudoTx(stx));
4721 BEAST_EXPECT(!passesLocalChecks(stx, reason));
4722 BEAST_EXPECT(reason == "Cannot submit pseudo transactions.");
4723 env.app().getOpenLedger().modify([&](OpenView& view, beast::Journal j) {
4724 auto const result = xrpl::apply(env.app(), view, stx, TapNone, j);
4725 BEAST_EXPECT(!result.applied && result.ter == temINVALID_FLAG);
4726 return result.applied;
4727 });
4728 }
4729
4730 void
4732 {
4733 testcase("batch open ledger");
4734 // IMPORTANT: When a transaction is submitted outside of a batch and
4735 // another transaction is part of the batch, the batch might fail
4736 // because the sequence is out of order. This is because the canonical
4737 // order of transactions is determined by the account first. So in this
4738 // case, alice's batch comes after bob's self submitted transaction even
4739 // though the payment was submitted after the batch.
4740
4741 using namespace test::jtx;
4742 using namespace std::literals;
4743
4744 Env env{*this, features};
4745 XRPAmount const baseFee = env.current()->fees().base;
4746
4747 auto const alice = Account("alice");
4748 auto const bob = Account("bob");
4749
4750 env.fund(XRP(10000), alice, bob);
4751 env.close();
4752
4753 env(noop(bob), Ter(tesSUCCESS));
4754 env.close();
4755
4756 auto const aliceSeq = env.seq(alice);
4757 auto const preAlice = env.balance(alice);
4758 auto const preBob = env.balance(bob);
4759 auto const bobSeq = env.seq(bob);
4760
4761 // Alice Pays Bob (Open Ledger)
4762 auto const payTxn1 = env.jt(pay(alice, bob, XRP(10)), Seq(aliceSeq));
4763 auto const payTxn1ID = to_string(payTxn1.stx->getTransactionID());
4764 env(payTxn1, Ter(tesSUCCESS));
4765
4766 // Alice & Bob Atomic Batch
4767 auto const batchFee = batch::calcBatchFee(env, 1, 2);
4768 auto const [txIDs, batchID] = submitBatch(
4769 env,
4770 tesSUCCESS,
4771 batch::outer(alice, aliceSeq + 1, batchFee, tfAllOrNothing),
4772 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 2),
4773 batch::Inner(pay(bob, alice, XRP(5)), bobSeq),
4774 batch::Sig(bob));
4775
4776 // Bob pays Alice (Open Ledger)
4777 auto const payTxn2 = env.jt(pay(bob, alice, XRP(5)), Seq(bobSeq + 1));
4778 auto const payTxn2ID = to_string(payTxn2.stx->getTransactionID());
4779 env(payTxn2, Ter(terPRE_SEQ));
4780 env.close();
4781
4782 std::vector<TestLedgerData> const testCases = {
4783 {
4784 .index = 0,
4785 .txType = "Payment",
4786 .result = "tesSUCCESS",
4787 .txHash = payTxn1ID,
4788 .batchID = std::nullopt,
4789 },
4790 {
4791 .index = 1,
4792 .txType = "Batch",
4793 .result = "tesSUCCESS",
4794 .txHash = batchID,
4795 .batchID = std::nullopt,
4796 },
4797 {
4798 .index = 2,
4799 .txType = "Payment",
4800 .result = "tesSUCCESS",
4801 .txHash = txIDs[0],
4802 .batchID = batchID,
4803 },
4804 {
4805 .index = 3,
4806 .txType = "Payment",
4807 .result = "tesSUCCESS",
4808 .txHash = txIDs[1],
4809 .batchID = batchID,
4810 },
4811 };
4812 validateClosedLedger(env, testCases);
4813
4814 env.close();
4815 {
4816 // next ledger includes the payment txn
4817 std::vector<TestLedgerData> const testCases = {
4818 {
4819 .index = 0,
4820 .txType = "Payment",
4821 .result = "tesSUCCESS",
4822 .txHash = payTxn2ID,
4823 .batchID = std::nullopt,
4824 },
4825 };
4826 validateClosedLedger(env, testCases);
4827 }
4828
4829 // Alice consumes sequences (# of txns)
4830 BEAST_EXPECT(env.seq(alice) == aliceSeq + 3);
4831
4832 // Alice consumes sequences (# of txns)
4833 BEAST_EXPECT(env.seq(bob) == bobSeq + 2);
4834
4835 // Alice pays XRP & Fee; Bob receives XRP & pays Fee
4836 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(10) - batchFee - baseFee);
4837 BEAST_EXPECT(env.balance(bob) == preBob + XRP(10) - baseFee);
4838 }
4839
4840 void
4842 {
4843 testcase("inner txns not in open ledger");
4844
4845 using namespace test::jtx;
4846
4847 Env env{*this, features};
4848
4849 auto const alice = Account("alice");
4850 auto const bob = Account("bob");
4851 env.fund(XRP(10000), alice, bob);
4852 env.close();
4853
4854 auto const aliceSeq = env.seq(alice);
4855 auto const bobSeq = env.seq(bob);
4856 auto const batchFee = batch::calcBatchFee(env, 1, 2);
4857 auto const batchTxn = env.jt(
4858 batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
4859 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 1),
4860 batch::Inner(pay(bob, alice, XRP(5)), bobSeq),
4861 batch::Sig(bob));
4862 env(batchTxn, Ter(tesSUCCESS));
4863
4864 auto const batchID = batchTxn.stx->getTransactionID();
4865 auto const innerIDs = batchTxn.stx->getBatchTransactionIDs();
4866
4867 // RCLConsensus::Adaptor::onClose builds the initial proposal set from this tx map.
4868 auto const expectOnlyOuterInOpenLedger = [&] {
4869 auto const view = env.app().getOpenLedger().current();
4870 BEAST_EXPECT(std::distance(view->txs.begin(), view->txs.end()) == 1);
4871 BEAST_EXPECT(view->txExists(batchID));
4872 for (auto const& id : innerIDs)
4873 BEAST_EXPECT(!view->txExists(id));
4874 for (auto const& [tx, meta] : view->txs)
4875 BEAST_EXPECT(!tx->isFlag(tfInnerBatchTxn));
4876 BEAST_EXPECT(env.seq(bob) == bobSeq);
4877 };
4878 expectOnlyOuterInOpenLedger();
4879
4880 // Rebuild the open ledger on the same parent, as when a consensus round leaves the Batch
4881 // out and OpenLedger::accept carries it over.
4882 {
4883 OrderedTxs retries{uint256{}};
4884 env.app().getOpenLedger().accept(
4885 env.app(),
4886 env.current()->rules(),
4888 OrderedTxs{uint256{}},
4889 false,
4890 retries,
4891 TapNone,
4892 "test");
4893 BEAST_EXPECT(retries.empty());
4894 expectOnlyOuterInOpenLedger();
4895 }
4896
4897 // Inner txns enter only the ledger built from the agreed set.
4898 env.close();
4899 auto const closed = env.closed();
4900 BEAST_EXPECT(closed->txExists(batchID));
4901 for (auto const& id : innerIDs)
4902 BEAST_EXPECT(closed->txExists(id));
4903 BEAST_EXPECT(env.seq(bob) == bobSeq + 1);
4904 }
4905
4906 void
4908 {
4909 testcase("batch tx queue");
4910
4911 using namespace test::jtx;
4912 using namespace std::literals;
4913
4914 // A Batch is never queued. Under open-ledger congestion a Batch that
4915 // pays only its base fee cannot apply directly and, unlike an ordinary
4916 // transaction, is rejected outright rather than held in the queue. A
4917 // Batch that pays the escalated open-ledger fee applies directly.
4918 {
4919 Env env{
4920 *this,
4922 features,
4923 nullptr,
4925
4926 auto const alice = Account("alice");
4927 auto const bob = Account("bob");
4928 auto const carol = Account("carol");
4929
4930 // Fund across several ledgers so the TxQ metrics stay restricted.
4931 env.fund(XRP(10000), noripple(alice, bob));
4932 env.close(env.now() + 5s, 10000ms);
4933 env.fund(XRP(10000), noripple(carol));
4934 env.close(env.now() + 5s, 10000ms);
4935
4936 // Fill the open ledger so escalation is active.
4937 env(noop(alice));
4938 env(noop(alice));
4939 env(noop(alice));
4940 checkMetrics(*this, env, 0, std::nullopt, 3, 2);
4941
4942 env(noop(carol), Ter(terQUEUED));
4943 checkMetrics(*this, env, 1, std::nullopt, 3, 2);
4944
4945 auto const aliceSeq = env.seq(alice);
4946 auto const bobSeq = env.seq(bob);
4947 auto const batchFee = batch::calcBatchFee(env, 1, 2);
4948
4949 // Paying only the base fee, the Batch cannot enter the congested
4950 // open ledger and is rejected rather than queued.
4951 env(batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
4952 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 1),
4953 batch::Inner(pay(bob, alice, XRP(5)), bobSeq),
4954 batch::Sig(bob),
4956
4957 // The Batch was not queued; only carol's transaction is queued.
4958 checkMetrics(*this, env, 1, std::nullopt, 3, 2);
4959
4960 // Paying the escalated open-ledger fee, the Batch applies directly.
4961 env(batch::outer(alice, aliceSeq, openLedgerFee(env, batchFee), tfAllOrNothing),
4962 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 1),
4963 batch::Inner(pay(bob, alice, XRP(5)), bobSeq),
4964 batch::Sig(bob),
4965 Ter(tesSUCCESS));
4966 }
4967
4968 // inner batch transactions are counter towards the ledger tx count
4969 {
4970 Env env{
4971 *this,
4973 features,
4974 nullptr,
4976
4977 auto const alice = Account("alice");
4978 auto const bob = Account("bob");
4979 auto const carol = Account("carol");
4980
4981 // Fund across several ledgers so the TxQ metrics stay restricted.
4982 env.fund(XRP(10000), noripple(alice, bob));
4983 env.close(env.now() + 5s, 10000ms);
4984 env.fund(XRP(10000), noripple(carol));
4985 env.close(env.now() + 5s, 10000ms);
4986
4987 // Fill the ledger leaving room for 1 queued transaction
4988 env(noop(alice));
4989 env(noop(alice));
4990 checkMetrics(*this, env, 0, std::nullopt, 2, 2);
4991
4992 auto const aliceSeq = env.seq(alice);
4993 auto const bobSeq = env.seq(bob);
4994 auto const batchFee = batch::calcBatchFee(env, 1, 2);
4995
4996 // Batch Successful
4997 {
4998 env(batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
4999 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 1),
5000 batch::Inner(pay(bob, alice, XRP(5)), bobSeq),
5001 batch::Sig(bob),
5002 Ter(tesSUCCESS));
5003 }
5004
5005 checkMetrics(*this, env, 0, std::nullopt, 3, 2);
5006
5007 env(noop(carol), Ter(terQUEUED));
5008 checkMetrics(*this, env, 1, std::nullopt, 3, 2);
5009 }
5010
5011 // A Batch is never queued, so it also cannot sit behind the account's
5012 // already-queued transactions: with a sequence gap it cannot apply
5013 // directly and is rejected rather than queued.
5014 {
5015 Env env{
5016 *this,
5018 features,
5019 nullptr,
5021
5022 auto const alice = Account("alice");
5023 auto const bob = Account("bob");
5024
5025 env.fund(XRP(10000), noripple(alice, bob));
5026 env.close(env.now() + 5s, 10000ms);
5027
5028 // Fill the open ledger so subsequent transactions queue.
5029 env(noop(alice));
5030 env(noop(alice));
5031 env(noop(alice));
5032 checkMetrics(*this, env, 0, std::nullopt, 3, 2);
5033
5034 auto const aliceSeq = env.seq(alice);
5035
5036 // Queue two normal transactions for alice.
5037 env(noop(alice), Seq(aliceSeq + 0), Ter(terQUEUED));
5038 env(noop(alice), Seq(aliceSeq + 1), Ter(terQUEUED));
5039 checkMetrics(*this, env, 2, std::nullopt, 3, 2);
5040
5041 // The Batch's sequence sits behind the queued transactions, so it
5042 // cannot apply directly and is rejected rather than queued.
5043 auto const bobSeq = env.seq(bob);
5044 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5045 env(batch::outer(alice, aliceSeq + 2, batchFee, tfAllOrNothing),
5046 batch::Inner(pay(alice, bob, XRP(10)), aliceSeq + 3),
5047 batch::Inner(pay(bob, alice, XRP(5)), bobSeq),
5048 batch::Sig(bob),
5050
5051 // The two queued transactions are untouched.
5052 checkMetrics(*this, env, 2, std::nullopt, 3, 2);
5053 }
5054 }
5055
5056 void
5058 {
5059 testcase("batch network ops");
5060
5061 using namespace test::jtx;
5062 using namespace std::literals;
5063
5064 Env env(*this, envconfig(), features, nullptr, beast::Severity::Disabled);
5065
5066 auto alice = Account("alice");
5067 auto bob = Account("bob");
5068 env.fund(XRP(10000), alice, bob);
5069 env.close();
5070
5071 auto const submitTx = [&](std::uint32_t flags) -> UInt256 {
5072 auto const jt = env.jt(pay(alice, bob, XRP(1)), Txflags(flags));
5073 Serializer s;
5074 jt.stx->add(s);
5075 env.app().getOPs().submitTransaction(jt.stx);
5076 return jt.stx->getTransactionID();
5077 };
5078
5079 auto const processTxn = [&](std::uint32_t flags) -> UInt256 {
5080 auto const jt = env.jt(pay(alice, bob, XRP(1)), Txflags(flags));
5081 Serializer s;
5082 jt.stx->add(s);
5083 std::string reason;
5084 auto transaction = std::make_shared<Transaction>(jt.stx, reason, env.app());
5086 transaction, false, true, NetworkOPs::FailHard::Yes);
5087 return transaction->getID();
5088 };
5089
5090 // Validate: NetworkOPs::submitTransaction()
5091 {
5092 // Submit a tx with tfInnerBatchTxn
5093 UInt256 const txBad = submitTx(tfInnerBatchTxn);
5094 BEAST_EXPECT(env.app().getHashRouter().getFlags(txBad) == HashRouterFlags::UNDEFINED);
5095 }
5096
5097 // Validate: NetworkOPs::processTransaction()
5098 {
5099 UInt256 const txid = processTxn(tfInnerBatchTxn);
5100 // HashRouter::getFlags() should return LedgerFlags::BAD
5101 BEAST_EXPECT(env.app().getHashRouter().getFlags(txid) == HashRouterFlags::BAD);
5102 }
5103 }
5104
5105 void
5107 {
5108 testcase("batch delegate consent");
5109
5110 using namespace test::jtx;
5111 using namespace std::literals;
5112
5113 // Delegate consent bypass.
5114 //
5115 // Alice delegates Payment to Bob. A Batch carries an inner Payment with
5116 // Delegate=Bob, but Bob never signs the batch (and, because the inner
5117 // Account == the outer Account, no BatchSigners are required at all).
5118 // Batch waives the per-inner signature check, and preflightSigValidated
5119 // derives required signers from sfAccount only -- it ignores sfDelegate
5120 // -- so Bob's delegated authority is exercised without Bob's consent.
5121 //
5122 // SECURE EXPECTATION: the delegate (Bob) must authorize the inner txn,
5123 // so the batch must be rejected (temBAD_SIGNER) when Bob does not sign.
5124 {
5125 Env env{*this, features};
5126
5127 auto const alice = Account("alice");
5128 auto const bob = Account("bob");
5129 auto const mallory = Account("mallory");
5130 env.fund(XRP(10000), alice, bob, mallory);
5131 env.close();
5132
5133 env(delegate::set(alice, bob, {"Payment"}));
5134 env.close();
5135
5136 auto const preMallory = env.balance(mallory);
5137
5138 auto const batchFee = batch::calcBatchFee(env, 0, 2);
5139 auto const seq = env.seq(alice);
5140
5141 auto inner = batch::Inner(pay(alice, mallory, XRP(1000)), seq + 1);
5142 inner[jss::Delegate] = bob.human();
5143
5144 // Bob is NOT among the batch signers; he never consents.
5145 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
5146 inner,
5147 batch::Inner(pay(alice, mallory, XRP(1)), seq + 2),
5149 env.close();
5150
5151 // No funds should have moved to Mallory.
5152 BEAST_EXPECT(env.balance(mallory) == preMallory);
5153 }
5154
5155 // Self-grant attribution forgery.
5156 //
5157 // With no pre-existing delegation, Alice places DelegateSet(authorize
5158 // Bob) and a Delegate=Bob action in the SAME batch. The second inner
5159 // reads the delegation created by the first from the batch's running
5160 // view, and Bob never signs -- manufacturing on-chain attribution of
5161 // the action to Bob without his consent.
5162 //
5163 // SECURE EXPECTATION: rejected (temBAD_SIGNER) -- Bob must consent, and
5164 // a grant cannot be created and exercised within the same atomic batch.
5165 {
5166 Env env{*this, features};
5167
5168 auto const alice = Account("alice");
5169 auto const bob = Account("bob");
5170 auto const mallory = Account("mallory");
5171 env.fund(XRP(10000), alice, bob, mallory);
5172 env.close();
5173
5174 auto const preMallory = env.balance(mallory);
5175
5176 auto const batchFee = batch::calcBatchFee(env, 0, 2);
5177 auto const seq = env.seq(alice);
5178
5179 auto inner = batch::Inner(pay(alice, mallory, XRP(1000)), seq + 2);
5180 inner[jss::Delegate] = bob.human();
5181
5182 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
5183 batch::Inner(delegate::set(alice, bob, {"Payment"}), seq + 1),
5184 inner,
5186 env.close();
5187
5188 BEAST_EXPECT(env.balance(mallory) == preMallory);
5189 }
5190
5191 // Legitimate counterpart: the same atomic grant-and-use is allowed when
5192 // the delegate co-signs the batch -- consent is present, so it succeeds.
5193 {
5194 Env env{*this, features};
5195
5196 auto const alice = Account("alice");
5197 auto const bob = Account("bob");
5198 auto const mallory = Account("mallory");
5199 env.fund(XRP(10000), alice, bob, mallory);
5200 env.close();
5201
5202 auto const preMallory = env.balance(mallory);
5203
5204 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5205 auto const seq = env.seq(alice);
5206
5207 auto inner = batch::Inner(pay(alice, mallory, XRP(1000)), seq + 2);
5208 inner[jss::Delegate] = bob.human();
5209
5210 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
5211 batch::Inner(delegate::set(alice, bob, {"Payment"}), seq + 1),
5212 inner,
5213 batch::Sig(bob),
5214 Ter(tesSUCCESS));
5215 env.close();
5216
5217 BEAST_EXPECT(env.balance(mallory) == preMallory + XRP(1000));
5218 }
5219
5220 // Multi-account: a delegated inner from a non-outer account also
5221 // requires the delegate's signature (not the account holder's).
5222 {
5223 Env env{*this, features};
5224
5225 auto const alice = Account("alice");
5226 auto const bob = Account("bob");
5227 auto const carol = Account("carol");
5228 env.fund(XRP(10000), alice, bob, carol);
5229 env.close();
5230
5231 env(delegate::set(bob, carol, {"Payment"}));
5232 env.close();
5233
5234 auto const preAlice = env.balance(alice);
5235
5236 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5237 auto const aliceSeq = env.seq(alice);
5238 auto const bobSeq = env.seq(bob);
5239
5240 auto inner = batch::Inner(pay(bob, alice, XRP(1)), bobSeq);
5241 inner[jss::Delegate] = carol.human();
5242
5243 // Carol (the delegate) does not sign.
5244 env(batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
5245 inner,
5246 batch::Inner(pay(alice, bob, XRP(2)), aliceSeq + 1),
5248 env.close();
5249
5250 BEAST_EXPECT(env.balance(alice) == preAlice);
5251 }
5252
5253 // Wrong signer: a batch signature from someone other than the named
5254 // delegate does not satisfy the requirement.
5255 {
5256 Env env{*this, features};
5257
5258 auto const alice = Account("alice");
5259 auto const bob = Account("bob");
5260 auto const carol = Account("carol");
5261 auto const mallory = Account("mallory");
5262 env.fund(XRP(10000), alice, bob, carol, mallory);
5263 env.close();
5264
5265 env(delegate::set(alice, bob, {"Payment"}));
5266 env.close();
5267
5268 auto const preMallory = env.balance(mallory);
5269
5270 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5271 auto const seq = env.seq(alice);
5272
5273 auto inner = batch::Inner(pay(alice, mallory, XRP(1000)), seq + 1);
5274 inner[jss::Delegate] = bob.human();
5275
5276 // Carol signs instead of the named delegate Bob.
5277 env(batch::outer(alice, seq, batchFee, tfAllOrNothing),
5278 inner,
5279 batch::Inner(pay(alice, mallory, XRP(1)), seq + 2),
5280 batch::Sig(carol),
5282 env.close();
5283
5284 BEAST_EXPECT(env.balance(mallory) == preMallory);
5285 }
5286
5287 // Delegate is the outer account: the outer signature already provides
5288 // the delegate's consent, so no BatchSigners are required.
5289 {
5290 Env env{*this, features};
5291
5292 auto const alice = Account("alice");
5293 auto const bob = Account("bob");
5294 env.fund(XRP(10000), alice, bob);
5295 env.close();
5296
5297 // Bob delegates Payment to Alice, who is also the batch submitter.
5298 env(delegate::set(bob, alice, {"Payment"}));
5299 env.close();
5300
5301 auto const preBob = env.balance(bob);
5302
5303 auto const batchFee = batch::calcBatchFee(env, 0, 2);
5304 auto const aliceSeq = env.seq(alice);
5305 auto const bobSeq = env.seq(bob);
5306
5307 auto inner = batch::Inner(pay(bob, alice, XRP(1)), bobSeq);
5308 inner[jss::Delegate] = alice.human();
5309
5310 env(batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
5311 inner,
5312 batch::Inner(pay(alice, bob, XRP(2)), aliceSeq + 1),
5313 Ter(tesSUCCESS));
5314 env.close();
5315
5316 // Net: Bob sends 1 to Alice, receives 2 from Alice.
5317 BEAST_EXPECT(env.balance(bob) == preBob + XRP(1));
5318 }
5319
5320 // Principal signs instead of the delegate. Bob delegates Payment to
5321 // Carol and an inner pays from Bob with Delegate=Carol, so Carol -- not
5322 // Bob -- is the required batch signer. Bob's own signature (the account
5323 // holder) does not satisfy the delegate-consent requirement.
5324 {
5325 Env env{*this, features};
5326
5327 auto const alice = Account("alice");
5328 auto const bob = Account("bob");
5329 auto const carol = Account("carol");
5330 env.fund(XRP(10000), alice, bob, carol);
5331 env.close();
5332
5333 env(delegate::set(bob, carol, {"Payment"}));
5334 env.close();
5335
5336 auto const preAlice = env.balance(alice);
5337
5338 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5339 auto const aliceSeq = env.seq(alice);
5340 auto const bobSeq = env.seq(bob);
5341
5342 auto inner = batch::Inner(pay(bob, alice, XRP(1)), bobSeq);
5343 inner[jss::Delegate] = carol.human();
5344
5345 // Bob (the principal) signs in place of Carol (the delegate).
5346 env(batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
5347 inner,
5348 batch::Inner(pay(alice, bob, XRP(2)), aliceSeq + 1),
5349 batch::Sig(bob),
5351 env.close();
5352
5353 BEAST_EXPECT(env.balance(alice) == preAlice);
5354 }
5355
5356 // Control for the revocation case below: identical setup, but inner 1
5357 // is a benign payment instead of a revocation. With Alice's delegation
5358 // intact, the delegated inner 2 succeeds -- proving the failure in the
5359 // revocation case is caused by the revocation, not the setup.
5360 {
5361 Env env{*this, features};
5362
5363 auto const alice = Account("alice");
5364 auto const bob = Account("bob");
5365 auto const mallory = Account("mallory");
5366 env.fund(XRP(10000), alice, bob, mallory);
5367 env.close();
5368
5369 env(delegate::set(bob, alice, {"Payment"}));
5370 env.close();
5371
5372 auto const preMallory = env.balance(mallory);
5373
5374 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5375 auto const seq = env.seq(bob);
5376
5377 auto inner2 = batch::Inner(pay(bob, mallory, XRP(1000)), seq + 2);
5378 inner2[jss::Delegate] = alice.human();
5379
5380 // Inner 1 is Bob's own payment (no revocation); Alice co-signs for
5381 // the delegated inner 2.
5382 auto const [txIDs, batchID] = submitBatch(
5383 env,
5384 tesSUCCESS,
5385 batch::outer(bob, seq, batchFee, tfIndependent),
5386 batch::Inner(pay(bob, alice, XRP(1)), seq + 1),
5387 inner2,
5388 batch::Sig(alice));
5389 env.close();
5390
5391 std::vector<TestLedgerData> const testCases = {
5392 {
5393 .index = 0,
5394 .txType = "Batch",
5395 .result = "tesSUCCESS",
5396 .txHash = batchID,
5397 .batchID = std::nullopt,
5398 },
5399 {
5400 .index = 1,
5401 .txType = "Payment",
5402 .result = "tesSUCCESS",
5403 .txHash = txIDs[0],
5404 .batchID = batchID,
5405 },
5406 {
5407 .index = 2,
5408 .txType = "Payment",
5409 .result = "tesSUCCESS",
5410 .txHash = txIDs[1],
5411 .batchID = batchID,
5412 },
5413 };
5414 validateClosedLedger(env, testCases);
5415
5416 // The delegated payment applied: Mallory received the funds.
5417 BEAST_EXPECT(env.balance(mallory) == preMallory + XRP(1000));
5418 }
5419
5420 // Revocation within the same batch. Bob grants Alice Payment permission
5421 // beforehand; an earlier inner revokes it before a later delegated
5422 // inner tries to use it. Alice must still co-sign (delegate consent is
5423 // derived from the static tx), but the revoked permission makes the
5424 // delegated inner fail at apply time -- a grant cannot be used after it
5425 // is removed earlier in the same batch.
5426 {
5427 Env env{*this, features};
5428
5429 auto const alice = Account("alice");
5430 auto const bob = Account("bob");
5431 auto const mallory = Account("mallory");
5432 env.fund(XRP(10000), alice, bob, mallory);
5433 env.close();
5434
5435 env(delegate::set(bob, alice, {"Payment"}));
5436 env.close();
5437
5438 auto const preMallory = env.balance(mallory);
5439
5440 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5441 auto const seq = env.seq(bob);
5442
5443 // Inner 2: Alice acts as Bob's delegate, so Alice is the required
5444 // batch signer.
5445 auto inner2 = batch::Inner(pay(bob, mallory, XRP(1000)), seq + 2);
5446 inner2[jss::Delegate] = alice.human();
5447
5448 // tfIndependent: inner 1 (the revocation) applies; inner 2 then
5449 // fails for lack of permission without reverting inner 1. An empty
5450 // permission list deletes the Delegate object.
5451 auto const [txIDs, batchID] = submitBatch(
5452 env,
5453 tesSUCCESS,
5454 batch::outer(bob, seq, batchFee, tfIndependent),
5455 batch::Inner(delegate::set(bob, alice, {}), seq + 1),
5456 inner2,
5457 batch::Sig(alice));
5458 env.close();
5459
5460 std::vector<TestLedgerData> const testCases = {
5461 {
5462 .index = 0,
5463 .txType = "Batch",
5464 .result = "tesSUCCESS",
5465 .txHash = batchID,
5466 .batchID = std::nullopt,
5467 },
5468 {
5469 .index = 1,
5470 .txType = "DelegateSet",
5471 .result = "tesSUCCESS",
5472 .txHash = txIDs[0],
5473 .batchID = batchID,
5474 },
5475 // inner 2 fails: Alice's permission was revoked in inner 1.
5476 };
5477 validateClosedLedger(env, testCases);
5478
5479 // The delegated payment never applied.
5480 BEAST_EXPECT(env.balance(mallory) == preMallory);
5481 auto const txResult = env.rpc("tx", txIDs[1])[jss::result];
5482 BEAST_EXPECT(txResult[jss::error] == "txnNotFound");
5483 BEAST_EXPECT(txResult[jss::error_code] == RpcTxnNotFound);
5484 BEAST_EXPECT(txResult[jss::error_message] == "Transaction not found.");
5485 }
5486 }
5487
5488 void
5490 {
5491 testcase("batch delegate");
5492
5493 using namespace test::jtx;
5494 using namespace std::literals;
5495
5496 // delegated non atomic inner
5497 {
5498 Env env{*this, features};
5499
5500 auto const alice = Account("alice");
5501 auto const bob = Account("bob");
5502 auto const gw = Account("gw");
5503 auto const usd = gw["USD"];
5504 env.fund(XRP(10000), alice, bob, gw);
5505 env.close();
5506
5507 env(delegate::set(alice, bob, {"Payment"}));
5508 env.close();
5509
5510 auto const preAlice = env.balance(alice);
5511 auto const preBob = env.balance(bob);
5512
5513 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5514 auto const seq = env.seq(alice);
5515
5516 auto tx = batch::Inner(pay(alice, bob, XRP(1)), seq + 1);
5517 tx[jss::Delegate] = bob.human();
5518 // The delegate (Bob) authorizes the delegated inner, so Bob must
5519 // provide the batch signature.
5520 auto const [txIDs, batchID] = submitBatch(
5521 env,
5522 tesSUCCESS,
5523 batch::outer(alice, seq, batchFee, tfAllOrNothing),
5524 tx,
5525 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
5526 batch::Sig(bob));
5527 env.close();
5528
5529 std::vector<TestLedgerData> const testCases = {
5530 {
5531 .index = 0,
5532 .txType = "Batch",
5533 .result = "tesSUCCESS",
5534 .txHash = batchID,
5535 .batchID = std::nullopt,
5536 },
5537 {
5538 .index = 1,
5539 .txType = "Payment",
5540 .result = "tesSUCCESS",
5541 .txHash = txIDs[0],
5542 .batchID = batchID,
5543 },
5544 {
5545 .index = 2,
5546 .txType = "Payment",
5547 .result = "tesSUCCESS",
5548 .txHash = txIDs[1],
5549 .batchID = batchID,
5550 },
5551 };
5552 validateClosedLedger(env, testCases);
5553
5554 // Alice consumes sequences (# of txns)
5555 BEAST_EXPECT(env.seq(alice) == seq + 3);
5556
5557 // Alice pays XRP & Fee; Bob receives XRP
5558 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(3) - batchFee);
5559 BEAST_EXPECT(env.balance(bob) == preBob + XRP(3));
5560 }
5561
5562 // delegated atomic inner
5563 {
5564 Env env{*this, features};
5565
5566 auto const alice = Account("alice");
5567 auto const bob = Account("bob");
5568 auto const carol = Account("carol");
5569 auto const gw = Account("gw");
5570 auto const usd = gw["USD"];
5571 env.fund(XRP(10000), alice, bob, carol, gw);
5572 env.close();
5573
5574 env(delegate::set(bob, carol, {"Payment"}));
5575 env.close();
5576
5577 auto const preAlice = env.balance(alice);
5578 auto const preBob = env.balance(bob);
5579 auto const preCarol = env.balance(carol);
5580
5581 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5582 auto const aliceSeq = env.seq(alice);
5583 auto const bobSeq = env.seq(bob);
5584
5585 auto tx = batch::Inner(pay(bob, alice, XRP(1)), bobSeq);
5586 tx[jss::Delegate] = carol.human();
5587 // Carol is the delegate authorizing the inner txn on Bob's behalf,
5588 // so Carol -- not Bob -- must provide the batch signature.
5589 auto const [txIDs, batchID] = submitBatch(
5590 env,
5591 tesSUCCESS,
5592 batch::outer(alice, aliceSeq, batchFee, tfAllOrNothing),
5593 tx,
5594 batch::Inner(pay(alice, bob, XRP(2)), aliceSeq + 1),
5595 batch::Sig(carol));
5596 env.close();
5597
5598 std::vector<TestLedgerData> const testCases = {
5599 {
5600 .index = 0,
5601 .txType = "Batch",
5602 .result = "tesSUCCESS",
5603 .txHash = batchID,
5604 .batchID = std::nullopt,
5605 },
5606 {
5607 .index = 1,
5608 .txType = "Payment",
5609 .result = "tesSUCCESS",
5610 .txHash = txIDs[0],
5611 .batchID = batchID,
5612 },
5613 {
5614 .index = 2,
5615 .txType = "Payment",
5616 .result = "tesSUCCESS",
5617 .txHash = txIDs[1],
5618 .batchID = batchID,
5619 },
5620 };
5621 validateClosedLedger(env, testCases);
5622
5623 BEAST_EXPECT(env.seq(alice) == aliceSeq + 2);
5624 BEAST_EXPECT(env.seq(bob) == bobSeq + 1);
5625 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(1) - batchFee);
5626 BEAST_EXPECT(env.balance(bob) == preBob + XRP(1));
5627 // NOTE: Carol would normally pay the fee for delegated txns, but
5628 // because the batch is atomic, the fee is paid by the batch
5629 BEAST_EXPECT(env.balance(carol) == preCarol);
5630 }
5631
5632 // delegated non atomic inner (AccountSet)
5633 // this also makes sure tfInnerBatchTxn won't block delegated AccountSet
5634 // with granular permission
5635 {
5636 Env env{*this, features};
5637
5638 auto const alice = Account("alice");
5639 auto const bob = Account("bob");
5640 auto const gw = Account("gw");
5641 auto const usd = gw["USD"];
5642 env.fund(XRP(10000), alice, bob, gw);
5643 env.close();
5644
5645 env(delegate::set(alice, bob, {"AccountDomainSet"}));
5646 env.close();
5647
5648 auto const preAlice = env.balance(alice);
5649 auto const preBob = env.balance(bob);
5650
5651 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5652 auto const seq = env.seq(alice);
5653
5654 auto tx = batch::Inner(noop(alice), seq + 1);
5655 std::string const domain = "example.com";
5656 tx[sfDomain.jsonName] = strHex(domain);
5657 tx[jss::Delegate] = bob.human();
5658 // Bob is the delegate authorizing the inner AccountSet, so Bob must
5659 // provide the batch signature.
5660 auto const [txIDs, batchID] = submitBatch(
5661 env,
5662 tesSUCCESS,
5663 batch::outer(alice, seq, batchFee, tfAllOrNothing),
5664 tx,
5665 batch::Inner(pay(alice, bob, XRP(2)), seq + 2),
5666 batch::Sig(bob));
5667 env.close();
5668
5669 std::vector<TestLedgerData> const testCases = {
5670 {
5671 .index = 0,
5672 .txType = "Batch",
5673 .result = "tesSUCCESS",
5674 .txHash = batchID,
5675 .batchID = std::nullopt,
5676 },
5677 {
5678 .index = 1,
5679 .txType = "AccountSet",
5680 .result = "tesSUCCESS",
5681 .txHash = txIDs[0],
5682 .batchID = batchID,
5683 },
5684 {
5685 .index = 2,
5686 .txType = "Payment",
5687 .result = "tesSUCCESS",
5688 .txHash = txIDs[1],
5689 .batchID = batchID,
5690 },
5691 };
5692 validateClosedLedger(env, testCases);
5693
5694 // Alice consumes sequences (# of txns)
5695 BEAST_EXPECT(env.seq(alice) == seq + 3);
5696
5697 // Alice pays XRP & Fee; Bob receives XRP
5698 BEAST_EXPECT(env.balance(alice) == preAlice - XRP(2) - batchFee);
5699 BEAST_EXPECT(env.balance(bob) == preBob + XRP(2));
5700 }
5701
5702 // delegated non atomic inner (MPTokenIssuanceSet)
5703 // this also makes sure tfInnerBatchTxn won't block delegated
5704 // MPTokenIssuanceSet with granular permission
5705 {
5706 Env env{*this, features};
5707 Account const alice{"alice"};
5708 Account const bob{"bob"};
5709 env.fund(XRP(100000), alice, bob);
5710 env.close();
5711
5712 auto const mptID = makeMptID(env.seq(alice), alice);
5713 MPTTester mpt(env, alice, {.fund = false});
5714 env.close();
5715 mpt.create({.flags = tfMPTCanLock});
5716 env.close();
5717
5718 // alice gives granular permission to bob of MPTokenIssuanceLock
5719 env(delegate::set(alice, bob, {"MPTokenIssuanceLock", "MPTokenIssuanceUnlock"}));
5720 env.close();
5721
5722 auto const seq = env.seq(alice);
5723 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5724
5725 json::Value jv1;
5726 jv1[sfTransactionType] = jss::MPTokenIssuanceSet;
5727 jv1[sfAccount] = alice.human();
5728 jv1[sfDelegate] = bob.human();
5729 jv1[sfSequence] = seq + 1;
5730 jv1[sfMPTokenIssuanceID] = to_string(mptID);
5731 jv1[sfFlags] = tfMPTLock;
5732
5733 json::Value jv2;
5734 jv2[sfTransactionType] = jss::MPTokenIssuanceSet;
5735 jv2[sfAccount] = alice.human();
5736 jv2[sfDelegate] = bob.human();
5737 jv2[sfSequence] = seq + 2;
5738 jv2[sfMPTokenIssuanceID] = to_string(mptID);
5739 jv2[sfFlags] = tfMPTUnlock;
5740
5741 // Both inners are delegated to Bob, so Bob must provide the batch
5742 // signature (one signer covers both).
5743 auto const [txIDs, batchID] = submitBatch(
5744 env,
5745 tesSUCCESS,
5746 batch::outer(alice, seq, batchFee, tfAllOrNothing),
5747 batch::Inner(jv1, seq + 1),
5748 batch::Inner(jv2, seq + 2),
5749 batch::Sig(bob));
5750 env.close();
5751
5752 std::vector<TestLedgerData> const testCases = {
5753 {
5754 .index = 0,
5755 .txType = "Batch",
5756 .result = "tesSUCCESS",
5757 .txHash = batchID,
5758 .batchID = std::nullopt,
5759 },
5760 {
5761 .index = 1,
5762 .txType = "MPTokenIssuanceSet",
5763 .result = "tesSUCCESS",
5764 .txHash = txIDs[0],
5765 .batchID = batchID,
5766 },
5767 {
5768 .index = 2,
5769 .txType = "MPTokenIssuanceSet",
5770 .result = "tesSUCCESS",
5771 .txHash = txIDs[1],
5772 .batchID = batchID,
5773 },
5774 };
5775 validateClosedLedger(env, testCases);
5776 }
5777
5778 // delegated non atomic inner (TrustSet)
5779 // this also makes sure tfInnerBatchTxn won't block delegated TrustSet
5780 // with granular permission
5781 {
5782 Env env{*this, features};
5783 Account const gw{"gw"};
5784 Account const alice{"alice"};
5785 Account const bob{"bob"};
5786 env.fund(XRP(10000), gw, alice, bob);
5787 env(fset(gw, asfRequireAuth));
5788 env.close();
5789 env(trust(alice, gw["USD"](50)));
5790 env.close();
5791
5792 env(delegate::set(gw, bob, {"TrustlineAuthorize", "TrustlineFreeze"}));
5793 env.close();
5794
5795 auto const seq = env.seq(gw);
5796 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5797
5798 auto jv1 = trust(gw, gw["USD"](0), alice, tfSetfAuth);
5799 jv1[sfDelegate] = bob.human();
5800 auto jv2 = trust(gw, gw["USD"](0), alice, tfSetFreeze);
5801 jv2[sfDelegate] = bob.human();
5802
5803 // Both inners are delegated to Bob, so Bob must provide the batch
5804 // signature.
5805 auto const [txIDs, batchID] = submitBatch(
5806 env,
5807 tesSUCCESS,
5808 batch::outer(gw, seq, batchFee, tfAllOrNothing),
5809 batch::Inner(jv1, seq + 1),
5810 batch::Inner(jv2, seq + 2),
5811 batch::Sig(bob));
5812 env.close();
5813
5814 std::vector<TestLedgerData> const testCases = {
5815 {
5816 .index = 0,
5817 .txType = "Batch",
5818 .result = "tesSUCCESS",
5819 .txHash = batchID,
5820 .batchID = std::nullopt,
5821 },
5822 {
5823 .index = 1,
5824 .txType = "TrustSet",
5825 .result = "tesSUCCESS",
5826 .txHash = txIDs[0],
5827 .batchID = batchID,
5828 },
5829 {
5830 .index = 2,
5831 .txType = "TrustSet",
5832 .result = "tesSUCCESS",
5833 .txHash = txIDs[1],
5834 .batchID = batchID,
5835 },
5836 };
5837 validateClosedLedger(env, testCases);
5838 }
5839
5840 // inner transaction not authorized by the delegating account.
5841 {
5842 Env env{*this, features};
5843 Account const gw{"gw"};
5844 Account const alice{"alice"};
5845 Account const bob{"bob"};
5846 env.fund(XRP(10000), gw, alice, bob);
5847 env(fset(gw, asfRequireAuth));
5848 env.close();
5849 env(trust(alice, gw["USD"](50)));
5850 env.close();
5851
5852 env(delegate::set(gw, bob, {"TrustlineAuthorize", "TrustlineFreeze"}));
5853 env.close();
5854
5855 auto const seq = env.seq(gw);
5856 auto const batchFee = batch::calcBatchFee(env, 1, 2);
5857
5858 auto jv1 = trust(gw, gw["USD"](0), alice, tfSetFreeze);
5859 jv1[sfDelegate] = bob.human();
5860 auto jv2 = trust(gw, gw["USD"](0), alice, tfClearFreeze);
5861 jv2[sfDelegate] = bob.human();
5862
5863 // Both inners are delegated to Bob, so Bob must provide the batch
5864 // signature; jv2 still fails preclaim for lack of permission.
5865 auto const [txIDs, batchID] = submitBatch(
5866 env,
5867 tesSUCCESS,
5868 batch::outer(gw, seq, batchFee, tfIndependent),
5869 batch::Inner(jv1, seq + 1),
5870 // terNO_DELEGATE_PERMISSION: not authorized to clear freeze
5871 batch::Inner(jv2, seq + 2),
5872 batch::Sig(bob));
5873 env.close();
5874
5875 std::vector<TestLedgerData> const testCases = {
5876 {
5877 .index = 0,
5878 .txType = "Batch",
5879 .result = "tesSUCCESS",
5880 .txHash = batchID,
5881 .batchID = std::nullopt,
5882 },
5883 {
5884 .index = 1,
5885 .txType = "TrustSet",
5886 .result = "tesSUCCESS",
5887 .txHash = txIDs[0],
5888 .batchID = batchID,
5889 },
5890 // jv2 fails with terNO_DELEGATE_PERMISSION.
5891 };
5892 validateClosedLedger(env, testCases);
5893
5894 // verify jv2 is not present in the closed ledger.
5895 auto const txResult = env.rpc("tx", txIDs[1])[jss::result];
5896 BEAST_EXPECT(txResult[jss::error] == "txnNotFound");
5897 BEAST_EXPECT(txResult[jss::error_code] == RpcTxnNotFound);
5898 BEAST_EXPECT(txResult[jss::error_message] == "Transaction not found.");
5899 }
5900 }
5901
5902 void
5904 {
5905 // Verifying that the RPC response from submit includes
5906 // the account_sequence_available, account_sequence_next,
5907 // open_ledger_cost and validated_ledger_index fields.
5908 testcase("Validate RPC response");
5909
5910 using namespace jtx;
5911 Env env(*this, features);
5912 Account const alice("alice");
5913 Account const bob("bob");
5914 env.fund(XRP(10000), alice, bob);
5915 env.close();
5916
5917 // tes
5918 {
5919 auto const baseFee = env.current()->fees().base;
5920 auto const aliceSeq = env.seq(alice);
5921 auto const jtx = env.jt(pay(alice, bob, XRP(1)));
5922
5923 Serializer s;
5924 jtx.stx->add(s);
5925 auto const jr = env.rpc("submit", strHex(s.slice()))[jss::result];
5926 env.close();
5927
5928 BEAST_EXPECT(jr.isMember(jss::account_sequence_available));
5929 BEAST_EXPECT(jr[jss::account_sequence_available].asUInt() == aliceSeq + 1);
5930 BEAST_EXPECT(jr.isMember(jss::account_sequence_next));
5931 BEAST_EXPECT(jr[jss::account_sequence_next].asUInt() == aliceSeq + 1);
5932 BEAST_EXPECT(jr.isMember(jss::open_ledger_cost));
5933 BEAST_EXPECT(jr[jss::open_ledger_cost] == to_string(baseFee));
5934 BEAST_EXPECT(jr.isMember(jss::validated_ledger_index));
5935 }
5936
5937 // tec failure
5938 {
5939 auto const baseFee = env.current()->fees().base;
5940 auto const aliceSeq = env.seq(alice);
5941 env(fset(bob, asfRequireDest));
5942 auto const jtx = env.jt(pay(alice, bob, XRP(1)), Seq(aliceSeq));
5943
5944 Serializer s;
5945 jtx.stx->add(s);
5946 auto const jr = env.rpc("submit", strHex(s.slice()))[jss::result];
5947 env.close();
5948
5949 BEAST_EXPECT(jr.isMember(jss::account_sequence_available));
5950 BEAST_EXPECT(jr[jss::account_sequence_available].asUInt() == aliceSeq + 1);
5951 BEAST_EXPECT(jr.isMember(jss::account_sequence_next));
5952 BEAST_EXPECT(jr[jss::account_sequence_next].asUInt() == aliceSeq + 1);
5953 BEAST_EXPECT(jr.isMember(jss::open_ledger_cost));
5954 BEAST_EXPECT(jr[jss::open_ledger_cost] == to_string(baseFee));
5955 BEAST_EXPECT(jr.isMember(jss::validated_ledger_index));
5956 }
5957
5958 // tem failure
5959 {
5960 auto const baseFee = env.current()->fees().base;
5961 auto const aliceSeq = env.seq(alice);
5962 auto const jtx = env.jt(pay(alice, bob, XRP(1)), Seq(aliceSeq + 1));
5963
5964 Serializer s;
5965 jtx.stx->add(s);
5966 auto const jr = env.rpc("submit", strHex(s.slice()))[jss::result];
5967 env.close();
5968
5969 BEAST_EXPECT(jr.isMember(jss::account_sequence_available));
5970 BEAST_EXPECT(jr[jss::account_sequence_available].asUInt() == aliceSeq);
5971 BEAST_EXPECT(jr.isMember(jss::account_sequence_next));
5972 BEAST_EXPECT(jr[jss::account_sequence_next].asUInt() == aliceSeq);
5973 BEAST_EXPECT(jr.isMember(jss::open_ledger_cost));
5974 BEAST_EXPECT(jr[jss::open_ledger_cost] == to_string(baseFee));
5975 BEAST_EXPECT(jr.isMember(jss::validated_ledger_index));
5976 }
5977 }
5978
5979 void
5981 {
5982 using namespace jtx;
5983 Env env(*this, features);
5984 Account const alice("alice");
5985 Account const bob("bob");
5986 Account const carol("carol");
5987 env.fund(XRP(10000), alice, bob, carol);
5988 env.close();
5989
5990 auto const getBaseFee = [&](JTx const& jtx) -> XRPAmount {
5991 Serializer s;
5992 jtx.stx->add(s);
5993 return Batch::calculateBaseFee(*env.current(), *jtx.stx);
5994 };
5995
5996 // bad: a batch may not contain a batch - the nested inner fails STTx
5997 // construction, so the transaction cannot be built.
5998 {
5999 auto const seq = env.seq(alice);
6000 XRPAmount const batchFee = batch::calcBatchFee(env, 0, 2);
6001 auto const jtx = env.jt(
6002 batch::outer(alice, seq, batchFee, tfAllOrNothing),
6003 batch::Inner(batch::outer(alice, seq, batchFee, tfAllOrNothing), seq),
6004 batch::Inner(pay(alice, bob, XRP(1)), seq + 2));
6005 BEAST_EXPECT(jtx.stx == nullptr);
6006 }
6007
6008 // bad: an oversized batch (more than kMaxBatchTxCount inners) fails
6009 // STTx construction, so it cannot be built.
6010 {
6011 auto const seq = env.seq(alice);
6012 XRPAmount const batchFee = batch::calcBatchFee(env, 0, 2);
6013
6014 auto const jtx = env.jt(
6015 batch::outer(alice, seq, batchFee, tfAllOrNothing),
6016 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
6017 batch::Inner(pay(alice, bob, XRP(1)), seq + 2),
6018 batch::Inner(pay(alice, bob, XRP(1)), seq + 3),
6019 batch::Inner(pay(alice, bob, XRP(1)), seq + 4),
6020 batch::Inner(pay(alice, bob, XRP(1)), seq + 5),
6021 batch::Inner(pay(alice, bob, XRP(1)), seq + 6),
6022 batch::Inner(pay(alice, bob, XRP(1)), seq + 7),
6023 batch::Inner(pay(alice, bob, XRP(1)), seq + 8),
6024 batch::Inner(pay(alice, bob, XRP(1)), seq + 9));
6025
6026 BEAST_EXPECT(jtx.stx == nullptr);
6027 }
6028
6029 // bad: Signers array exceeds max entries.
6030 {
6031 auto const seq = env.seq(alice);
6032 XRPAmount const batchFee = batch::calcBatchFee(env, 0, 2);
6033
6034 auto const jtx = env.jt(
6035 batch::outer(alice, seq, batchFee, tfAllOrNothing),
6036 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
6037 batch::Inner(pay(alice, bob, XRP(5)), seq + 2),
6039 // Failure paths fall back to the ledger base fee.
6040 XRPAmount const txBaseFee = getBaseFee(jtx);
6041 BEAST_EXPECT(txBaseFee == env.current()->fees().base);
6042 }
6043
6044 // good:
6045 {
6046 auto const seq = env.seq(alice);
6047 XRPAmount const batchFee = batch::calcBatchFee(env, 0, 2);
6048 auto const jtx = env.jt(
6049 batch::outer(alice, seq, batchFee, tfAllOrNothing),
6050 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
6051 batch::Inner(pay(bob, alice, XRP(2)), seq + 2));
6052 XRPAmount const txBaseFee = getBaseFee(jtx);
6053 BEAST_EXPECT(txBaseFee == batchFee);
6054 }
6055 }
6056
6057 void
6059 {
6060 testcase("standalone tx with tfInnerBatchTxn rejected");
6061
6062 using namespace test::jtx;
6063 using namespace std::literals;
6064
6065 // A standalone Payment with tfInnerBatchTxn must be rejected.
6066 // Without proper guards this would bypass signature verification
6067 // in preflight2.
6068 {
6069 Env env{*this, features};
6070
6071 auto const alice = Account("alice");
6072 auto const bob = Account("bob");
6073 env.fund(XRP(10000), alice, bob);
6074 env.close();
6075
6076 // Submit a normal Payment with tfInnerBatchTxn flag.
6077 // preflight1 must reject with temINVALID_INNER_BATCH because
6078 // the flag is set but no parentBatchId exists.
6079 env(pay(alice, bob, XRP(1)), Txflags(tfInnerBatchTxn), Ter(telENV_RPC_FAILED));
6080 env.close();
6081
6082 // Verify via direct apply path (bypassing RPC layer)
6083 env.app().getOpenLedger().modify([&](OpenView& view, beast::Journal j) {
6084 // Construct a Payment STTx with tfInnerBatchTxn,
6085 // empty signing pub key, and no signature — mimicking
6086 // what an attacker would send to skip sig verification.
6087 STTx const stx = STTx(ttPAYMENT, [&](auto& obj) {
6088 obj.setAccountID(sfAccount, alice.id());
6089 obj.setAccountID(sfDestination, bob.id());
6090 obj.setFieldAmount(sfAmount, XRP(1));
6091 obj.setFieldAmount(sfFee, XRP(0));
6092 obj.setFieldU32(sfSequence, env.seq(alice));
6093 obj.setFieldU32(sfFlags, tfInnerBatchTxn);
6094 });
6095
6096 auto const result = xrpl::apply(env.app(), view, stx, TapNone, j);
6097 // Must NOT be applied — signature was never checked
6098 BEAST_EXPECT(!result.applied);
6099 return false;
6100 });
6101 }
6102 }
6103
6104 void
6106 {
6107 testcase("outer binding");
6108
6109 using namespace test::jtx;
6110
6111 // Signatures captured from one outer account cannot be replayed
6112 // under a different outer account.
6113 {
6114 Env env{*this, features};
6115
6116 auto const alice = Account("alice");
6117 auto const bob = Account("bob");
6118 auto const carol = Account("carol");
6119 auto const eve = Account("eve");
6120 env.fund(XRP(10000), alice, bob, carol, eve);
6121 env.close();
6122
6123 auto const preCarol = env.balance(carol);
6124 auto const bobSeq = env.seq(bob);
6125 auto const carolSeq = env.seq(carol);
6126
6127 auto const aliceSeq = env.seq(alice);
6128 auto const batchFee1 = batch::calcBatchFee(env, 2, 2);
6129 auto jt1 = env.jt(
6130 batch::outer(alice, aliceSeq, batchFee1, tfOnlyOne),
6131 batch::Inner(pay(bob, alice, XRP(100)), bobSeq),
6132 batch::Inner(pay(carol, alice, XRP(50)), carolSeq),
6133 batch::Sig(bob, carol));
6134
6135 auto const capturedSigners = jt1.jv[sfBatchSigners.jsonName];
6136
6137 env(jt1, Ter(tesSUCCESS));
6138 env.close();
6139 BEAST_EXPECT(env.seq(bob) == bobSeq + 1);
6140 BEAST_EXPECT(env.seq(carol) == carolSeq);
6141
6142 auto const batchFee2 = batch::calcBatchFee(env, 2, 2);
6143 auto jt2 = env.jtnofill(
6144 batch::outer(eve, env.seq(eve), batchFee2, tfOnlyOne),
6145 batch::Inner(pay(bob, alice, XRP(100)), bobSeq),
6146 batch::Inner(pay(carol, alice, XRP(50)), carolSeq));
6147
6148 jt2.jv[sfBatchSigners.jsonName] = capturedSigners;
6149 env(jt2.jv, Ter(telENV_RPC_FAILED));
6150 env.close();
6151
6152 BEAST_EXPECT(env.seq(carol) == carolSeq);
6153 BEAST_EXPECT(env.balance(carol) == preCarol);
6154 }
6155
6156 // Signatures are bound to the outer sequence; replaying them
6157 // at a higher sequence must fail.
6158 {
6159 Env env{*this, features};
6160
6161 auto const alice = Account("alice");
6162 auto const bob = Account("bob");
6163 auto const carol = Account("carol");
6164 env.fund(XRP(10000), alice, bob, carol);
6165 env.close();
6166
6167 auto const preCarol = env.balance(carol);
6168 auto const bobSeq = env.seq(bob);
6169 auto const carolSeq = env.seq(carol);
6170
6171 auto const aliceSeq1 = env.seq(alice);
6172 auto const batchFee1 = batch::calcBatchFee(env, 2, 2);
6173 auto jt1 = env.jt(
6174 batch::outer(alice, aliceSeq1, batchFee1, tfOnlyOne),
6175 batch::Inner(pay(bob, alice, XRP(500)), bobSeq),
6176 batch::Inner(pay(carol, alice, XRP(500)), carolSeq),
6177 batch::Sig(bob, carol));
6178
6179 auto const capturedSigners = jt1.jv[sfBatchSigners.jsonName];
6180
6181 env(jt1, Ter(tesSUCCESS));
6182 env.close();
6183 BEAST_EXPECT(env.seq(bob) == bobSeq + 1);
6184 BEAST_EXPECT(env.seq(carol) == carolSeq);
6185
6186 auto const batchFee2 = batch::calcBatchFee(env, 2, 2);
6187 auto jt2 = env.jtnofill(
6188 batch::outer(alice, env.seq(alice), batchFee2, tfOnlyOne),
6189 batch::Inner(pay(bob, alice, XRP(500)), bobSeq),
6190 batch::Inner(pay(carol, alice, XRP(500)), carolSeq));
6191
6192 jt2.jv[sfBatchSigners.jsonName] = capturedSigners;
6193 env(jt2.jv, Ter(telENV_RPC_FAILED));
6194 env.close();
6195
6196 BEAST_EXPECT(env.balance(carol) == preCarol);
6197 BEAST_EXPECT(env.seq(carol) == carolSeq);
6198 }
6199
6200 // Multi-signed batch signer entries are bound to their account;
6201 // reusing inner signatures under a different batch signer must fail.
6202 {
6203 Env env{*this, features};
6204
6205 auto const alice = Account("alice");
6206 auto const bob = Account("bob");
6207 auto const carol = Account("carol");
6208 auto const dave = Account("dave");
6209 auto const elsa = Account("elsa");
6210 env.fund(XRP(10000), alice, bob, carol, dave, elsa);
6211 env.close();
6212
6213 env(signers(bob, 2, {{dave, 1}, {elsa, 1}}));
6214 env.close();
6215 env(signers(carol, 2, {{dave, 1}, {elsa, 1}}));
6216 env.close();
6217
6218 auto const seq = env.seq(alice);
6219 auto const batchFee = batch::calcBatchFee(env, 3, 2);
6220 auto jt1 = env.jt(
6221 batch::outer(alice, seq, batchFee, tfAllOrNothing),
6222 batch::Inner(pay(alice, bob, XRP(10)), seq + 1),
6223 batch::Inner(pay(bob, alice, XRP(5)), env.seq(bob)),
6224 batch::Msig(bob, {dave, elsa}),
6225 Ter(tesSUCCESS));
6226
6227 auto const bobSignerEntry = jt1.jv[sfBatchSigners.jsonName][0u];
6228
6229 env(jt1, Ter(tesSUCCESS));
6230 env.close();
6231
6232 auto const seq2 = env.seq(alice);
6233 auto const batchFee2 = batch::calcBatchFee(env, 3, 2);
6234 auto jt2 = env.jtnofill(
6235 batch::outer(alice, seq2, batchFee2, tfAllOrNothing),
6236 batch::Inner(pay(alice, carol, XRP(10)), seq2 + 1),
6237 batch::Inner(pay(carol, alice, XRP(5)), env.seq(carol)));
6238
6239 json::Value carolSigner;
6240 carolSigner[sfBatchSigner.jsonName][jss::Account] = carol.human();
6241 carolSigner[sfBatchSigner.jsonName][jss::SigningPubKey] = "";
6242 carolSigner[sfBatchSigner.jsonName][sfSigners.jsonName] =
6243 bobSignerEntry[sfBatchSigner.jsonName][sfSigners.jsonName];
6244
6245 jt2.jv[sfBatchSigners.jsonName][0u] = carolSigner;
6246 env(jt2.jv, Ter(telENV_RPC_FAILED));
6247 env.close();
6248 }
6249 }
6250
6251 void
6253 {
6254 testcase("unsorted batch signers");
6255
6256 using namespace test::jtx;
6257
6258 Env env{*this, features};
6259
6260 auto const alice = Account("alice");
6261 auto const bob = Account("bob");
6262 auto const carol = Account("carol");
6263 env.fund(XRP(10000), alice, bob, carol);
6264 env.close();
6265
6266 auto const seq = env.seq(alice);
6267 auto const bobSeq = env.seq(bob);
6268 auto const carolSeq = env.seq(carol);
6269 auto const batchFee = batch::calcBatchFee(env, 2, 2);
6270
6271 auto jt = env.jt(
6272 batch::outer(alice, seq, batchFee, tfAllOrNothing),
6273 batch::Inner(pay(bob, alice, XRP(10)), bobSeq),
6274 batch::Inner(pay(carol, alice, XRP(5)), carolSeq),
6275 batch::Sig(bob, carol));
6276
6277 auto const s0 = jt.jv[sfBatchSigners.jsonName][0u];
6278 auto const s1 = jt.jv[sfBatchSigners.jsonName][1u];
6279 jt.jv[sfBatchSigners.jsonName][0u] = s1;
6280 jt.jv[sfBatchSigners.jsonName][1u] = s0;
6281
6282 env(jt.jv, Ter(temBAD_SIGNER));
6283 env.close();
6284 }
6285
6286 void
6288 {
6289 testcase("batch signature caching");
6290
6291 using namespace test::jtx;
6292
6293 // Mirrors apply.cpp's file-local kSfSiggood (the standard signature-good
6294 // cache); batch signer sigs are now verified and cached alongside the
6295 // outer signature in checkSign/checkValidity.
6297
6298 // Valid batch: alice (outer) + an inner from bob, who co-signs.
6299 auto const buildValidBatch = [](Env& env) {
6300 auto const alice = Account("alice");
6301 auto const bob = Account("bob");
6302 auto const seq = env.seq(alice);
6303 auto const batchFee = batch::calcBatchFee(env, 1, 2);
6304 return env.jt(
6305 batch::outer(alice, seq, batchFee, tfAllOrNothing),
6306 batch::Inner(pay(alice, bob, XRP(1)), seq + 1),
6307 batch::Inner(pay(bob, alice, XRP(2)), env.seq(bob)),
6308 batch::Sig(bob));
6309 };
6310
6311 // WRITE: a valid batch records "good" on its tx id.
6312 {
6313 Env env{*this, features};
6314 env.fund(XRP(10000), Account("alice"), Account("bob"));
6315 env.close();
6316
6317 auto const jt = buildValidBatch(env);
6318 auto const txid = jt.stx->getTransactionID();
6319
6320 BEAST_EXPECT(!any(env.app().getHashRouter().getFlags(txid) & kSfSiggood));
6321 env(jt, Ter(tesSUCCESS));
6322 BEAST_EXPECT(any(env.app().getHashRouter().getFlags(txid) & kSfSiggood));
6323 env.close();
6324 }
6325
6326 // READ: corrupt only a signer's signature (outer sig + signer key
6327 // untouched), so just the crypto would fail. Caught when uncached...
6328 {
6329 Env env{*this, features};
6330 env.fund(XRP(10000), Account("alice"), Account("bob"));
6331 env.close();
6332
6333 auto jt = buildValidBatch(env);
6334 jt.jv[sfBatchSigners.jsonName][0u][sfBatchSigner.jsonName][sfTxnSignature.jsonName] =
6335 "00";
6336 env(jt.jv, Ter(telENV_RPC_FAILED));
6337 env.close();
6338 }
6339 {
6340 // ...but a planted "good" skips the crypto, so it applies.
6341 Env env{*this, features};
6342 env.fund(XRP(10000), Account("alice"), Account("bob"));
6343 env.close();
6344
6345 auto jt = buildValidBatch(env);
6346 jt.jv[sfBatchSigners.jsonName][0u][sfBatchSigner.jsonName][sfTxnSignature.jsonName] =
6347 "00";
6348 auto const txid = STTx{parse(jt.jv)}.getTransactionID();
6349 env.app().getHashRouter().setFlags(txid, kSfSiggood);
6350 env(jt.jv, Ter(tesSUCCESS));
6351 env.close();
6352 }
6353 }
6354
6355 void
6357 {
6358 testcase("wrapper field submission");
6359
6360 using namespace test::jtx;
6361
6362 // Object fields with no InnerObjectFormats template, used in place of
6363 // sfRawTransaction as the wrapper of each inner transaction.
6364 for (SField const* wrapper : {
6365 &sfRawTransaction,
6366 &sfCreatedNode,
6367 &sfModifiedNode,
6368 &sfDeletedNode,
6369 &sfTemplateEntry,
6370 &sfEmitDetails,
6371 &sfMemo,
6372 &sfFinalFields,
6373 &sfNewFields,
6374 &sfPreviousFields,
6375 &sfTransactionMetaData,
6376 })
6377 {
6378 bool const poisoned = (wrapper != &sfRawTransaction);
6379
6380 auto const alice = Account("alice");
6381 auto const bob = Account("bob");
6382
6383 auto const wrap = [&](std::uint32_t const s) {
6384 json::Value inner = pay(alice, bob, XRP(1));
6385 inner[jss::SigningPubKey] = "";
6386 inner[jss::Sequence] = s;
6387 inner[jss::Fee] = "0";
6388 inner[jss::Flags] = tfInnerBatchTxn;
6389
6390 json::Value wrapped;
6391 wrapped[wrapper->jsonName] = inner;
6392 return wrapped;
6393 };
6394
6395 auto const submit = [&](Env& env, TER const expected) {
6396 env.fund(XRP(10000), alice, bob);
6397 env.close();
6398
6399 auto const preBob = env.balance(bob);
6400 auto const batchFee = batch::calcBatchFee(env, 0, 2);
6401 auto const seq = env.seq(alice);
6402
6403 auto jv = batch::outer(alice, seq, batchFee, tfAllOrNothing);
6404 jv[jss::RawTransactions][0u] = wrap(seq + 1);
6405 jv[jss::RawTransactions][1u] = wrap(seq + 2);
6406
6407 env(jv, Ter(expected));
6408 env.close();
6409
6410 return env.balance(bob) == preBob + XRP(2);
6411 };
6412
6413 // Without fixBatchV1_2 every wrapper executes.
6414 {
6415 Env env{*this, features - fixBatchV1_2};
6416 BEAST_EXPECTS(
6417 submit(env, tesSUCCESS),
6418 wrapper->getName() + " did not execute without fixBatchV1_2");
6419 }
6420
6421 // With fixBatchV1_2 only sfRawTransaction is accepted.
6422 {
6423 Env env{*this, features | fixBatchV1_2};
6424 bool const delivered = submit(env, poisoned ? TER{temMALFORMED} : TER{tesSUCCESS});
6425 BEAST_EXPECTS(
6426 delivered == !poisoned, wrapper->getName() + " wrong result with fixBatchV1_2");
6427 }
6428 }
6429 }
6430
6431 void
6433 {
6434 testEnable(features);
6435 testPreflight(features);
6436 testPreclaim(features);
6437 testBadRawTxn(features);
6438 testBadSequence(features);
6439 testBadOuterFee(features);
6440 testCalculateBaseFee(features);
6441 testAllOrNothing(features);
6442 testOnlyOne(features);
6443 testUntilFailure(features);
6444 testIndependent(features);
6445 testInnerSubmitRPC(features);
6446 testAccountActivation(features);
6447 testCheckAllSignatures(features);
6448 testAccountSet(features);
6449 testAccountDelete(features);
6450 testLendingAmendment(features);
6451 testLoan(features);
6452 testObjectCreateSequence(features);
6453 testObjectCreateTicket(features);
6454 testObjectCreate3rdParty(features);
6455 testTickets(features);
6456 testSequenceOpenLedger(features);
6457 testTicketsOpenLedger(features);
6458 testObjectsOpenLedger(features);
6459 testPseudoTxn(features);
6460 testOpenLedger(features);
6462 testBatchTxQueue(features);
6463 testBatchNetworkOps(features);
6464 testBatchDelegate(features);
6465 testBatchDelegateConsent(features);
6466 testValidateRPCResponse(features);
6467 testBatchCalculateBaseFee(features);
6469 testOuterBinding(features);
6470 testUnsortedBatchSigners(features);
6471 testBatchSigCache(features);
6473 }
6474
6475public:
6476 void
6477 run() override
6478 {
6479 using namespace test::jtx;
6480
6481 auto const sa = testableAmendments();
6482 testWithFeats(sa);
6483 }
6484};
6485
6487
6488} // namespace xrpl::test
T begin(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
A testsuite class.
Definition suite.h:52
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition suite.h:235
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Lightweight wrapper to tag static string.
Definition json_value.h:52
Represents a JSON value.
Definition json_value.h:121
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Definition Batch.cpp:174
bool setFlags(UInt256 const &key, HashRouterFlags flags)
Set the flags on a hash.
HashRouterFlags getFlags(UInt256 const &key)
std::shared_ptr< Ledger const > getClosedLedger()
The finalized ledger is the last closed/accepted ledger.
virtual void submitTransaction(std::shared_ptr< STTx const > const &)=0
virtual void processTransaction(std::shared_ptr< Transaction > &transaction, bool bUnlimited, bool bLocal, FailHard failType)=0
Process transactions as they arrive from the network or which are submitted by clients.
void accept(Application &app, Rules const &rules, std::shared_ptr< Ledger const > const &ledger, OrderedTxs const &locals, bool retriesFirst, OrderedTxs &retries, ApplyFlags flags, std::string_view suffix="", ModifyType const &f={})
Accept a new ledger.
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
bool modify(ModifyType const &f)
Modify the open ledger.
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
Identifies fields.
Definition SField.h:131
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
Holds the serialized result of parsing an input JSON object.
std::optional< STObject > object
The STObject if the parse was successful.
UInt256 getTransactionID() const
Definition STTx.h:262
json::Value getJson(JsonOptions options) const override
Definition STTx.cpp:351
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
Slice slice() const noexcept
Definition Serializer.h:141
virtual TxQ & getTxQ()=0
virtual NetworkOPs & getOPs()=0
virtual OpenLedger & getOpenLedger()=0
virtual LedgerMaster & getLedgerMaster()=0
virtual HashRouter & getHashRouter()=0
An immutable linear range of bytes.
Definition Slice.h:28
static NotTEC invokePreflight(PreflightContext const &ctx)
Definition Transactor.h:664
Metrics getMetrics(OpenView const &view) const
Returns fee metrics in reference fee level units.
Definition TxQ.cpp:2070
void testOpenLedger(FeatureBitset features)
void testLendingAmendment(FeatureBitset features)
void testWrappedInnerSubmission(FeatureBitset features)
void testBatchDelegateConsent(FeatureBitset features)
void testTicketsOpenLedger(FeatureBitset features)
static json::Value getLastLedger(jtx::Env &env)
void run() override
Runs the suite.
void testBadRawTxn(FeatureBitset features)
void testObjectsOpenLedger(FeatureBitset features)
void testStandaloneInnerBatchFlag(FeatureBitset features)
void testSequenceOpenLedger(FeatureBitset features)
void testEnable(FeatureBitset features)
void testPreflight(FeatureBitset features)
void testInnerTxnsNotInOpenLedger(FeatureBitset features)
void testObjectCreate3rdParty(FeatureBitset features)
static UInt256 getCheckIndex(AccountID const &account, std::uint32_t uSequence)
void testIndependent(FeatureBitset features)
void doTestInnerSubmitRPC(FeatureBitset features, bool withBatch)
void testBatchDelegate(FeatureBitset features)
void testBadSequence(FeatureBitset features)
void testTickets(FeatureBitset features)
void testAccountSet(FeatureBitset features)
std::pair< std::vector< std::string >, std::string > submitBatch(jtx::Env &env, TER const &result, Args &&... args)
void testPreclaim(FeatureBitset features)
void testBatchTxQueue(FeatureBitset features)
static auto openLedgerFee(jtx::Env &env, XRPAmount const &batchFee)
void testAllOrNothing(FeatureBitset features)
void testCheckAllSignatures(FeatureBitset features)
void testValidateRPCResponse(FeatureBitset features)
void testWithFeats(FeatureBitset features)
void testBatchCalculateBaseFee(FeatureBitset features)
void testLoan(FeatureBitset features)
void testOuterBinding(FeatureBitset features)
void testBatchNetworkOps(FeatureBitset features)
static json::Value getTxByIndex(json::Value const &jrr, int const index)
void testObjectCreateTicket(FeatureBitset features)
void testInnerSubmitRPC(FeatureBitset features)
void validateClosedLedger(jtx::Env &env, std::vector< TestLedgerData > const &ledgerResults)
void testCalculateBaseFee(FeatureBitset features)
void testAccountActivation(FeatureBitset features)
void testUntilFailure(FeatureBitset features)
void testObjectCreateSequence(FeatureBitset features)
void testOnlyOne(FeatureBitset features)
static std::unique_ptr< Config > makeSmallQueueConfig(std::map< std::string, std::string > extraTxQ={}, std::map< std::string, std::string > extraVoting={})
void testAccountDelete(FeatureBitset features)
void testUnsortedBatchSigners(FeatureBitset features)
void testPseudoTxn(FeatureBitset features)
void testBadOuterFee(FeatureBitset features)
void testBatchSigCache(FeatureBitset features)
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
AccountID id() const
Returns the Account ID.
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
json::Value json(JsonValue &&jv, FN const &... fN)
Create JSON from parameters.
Definition Env.h:752
SLE::const_pointer le(Account const &account) const
Return an account root.
Definition Env.cpp:311
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
Definition Env.cpp:127
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
ParseFailureGuard getParseFailureGuard(bool b)
Gets an RAII guard to set and restore the parse failure flag.
Definition Env.h:596
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition Env.cpp:302
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:1058
JTx jt(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:723
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
Definition Env.cpp:201
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:354
JTx jtnofill(JsonValue &&jv, FN const &... fN)
Create a JTx from parameters.
Definition Env.h:737
void memoize(Account const &account)
Associate AccountID with account.
Definition Env.cpp:174
beast::Journal const journal
Definition Env.h:204
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
NetClock::time_point now()
Returns the current network time.
Definition Env.h:326
Set the fee on a JTx.
Definition fee.h:20
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
Definition mpt.h:512
Set a multisignature on a JTx.
Definition multisign.h:53
Set the regular signature on a JTx.
Definition sig.h:19
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
Set the flags on a JTx.
Definition txflags.h:14
Adds an inner Batch transaction to a JTx and autofills it.
Definition batch.h:91
Sets a nested multi-signature for a Batch transaction on a JTx.
Definition batch.h:167
Sets the Batch transaction signers on a JTx.
Definition batch.h:142
Set a ticket sequence on a JTx.
Definition ticket.h:36
T distance(T... args)
T end(T... args)
T forward(T... args)
T make_pair(T... args)
T make_shared(T... args)
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Keylet check(AccountID const &id, SeqProxy const &seq) noexcept
A Check.
Definition Indexes.cpp:372
Keylet loanBroker(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:571
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Keylet loan(UInt256 const &loanBrokerID, SeqProxy const &loanSeq) noexcept
Definition Indexes.cpp:577
void validateInnerTxn(Env &env, std::string const &batchID, std::string const &txHash, std::string const &txType, std::string const &result, std::source_location const &loc=std::source_location::current())
Expect an inner Batch transaction to be recorded with the given type and result, and with ParentBatch...
Definition batch.cpp:68
json::Value outer(jtx::Account const &account, uint32_t seq, STAmount const &fee, std::uint32_t flags)
Build an outer Batch transaction JSON object.
Definition batch.cpp:55
XRPAmount calcBatchFee(jtx::Env const &env, uint32_t const &numSigners, uint32_t const &txns=0)
Calculate the expected outer Batch transaction fee.
Definition batch.cpp:37
json::Value cash(jtx::Account const &dest, UInt256 const &checkId, STAmount const &amount)
Cash a check requiring that a specific amount be delivered.
Definition check.cpp:15
json::Value create(A const &account, A const &dest, STAmount const &sendMax)
Create a check.
json::Value set(jtx::Account const &account, jtx::Account const &authorize, std::vector< std::string > const &permissions)
Definition delegate.cpp:17
Deposit preauthorize operations.
Definition deposit.h:16
json::Value coverDeposit(AccountID const &account, UInt256 const &brokerID, STAmount const &amount, uint32_t flags)
json::Value create(AccountID const &account, AccountID const &to, STAmount const &amount, NetClock::duration const &settleDelay, PublicKey const &pk, std::optional< NetClock::time_point > const &cancelAfter, std::optional< std::uint32_t > const &dstTag)
Definition paychan.cpp:31
json::Value create(Account const &account, std::uint32_t count)
Create one of more tickets.
Definition ticket.cpp:16
static NoneT const kNone
Definition tags.h:9
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
json::Value regkey(Account const &account, DisabledT)
Disable the regular key.
Definition regkey.cpp:13
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
json::Value noop(Account const &account)
The null transaction.
Definition noop.h:14
void checkMetrics(Suite &test, jtx::Env &env, std::size_t expectedCount, std::optional< std::size_t > expectedMaxCount, std::size_t expectedInLedger, std::size_t expectedPerLedger, std::uint64_t expectedMinFeeLevel=kBaseFeeLevel.fee(), std::uint64_t expectedMedFeeLevel=kMinEscalationFeeLevel.fee(), std::source_location const location=std::source_location::current())
FeatureBitset testableAmendments()
Definition Env.h:92
json::Value acctdelete(Account const &account, Account const &dest)
Delete account.
void incLgrSeqForAccDel(jtx::Env &env, jtx::Account const &acc, std::uint32_t margin=0)
std::array< Account, 1+sizeof...(Args)> noripple(Account const &account, Args const &... args)
Designate accounts as no-ripple in Env::fund.
Definition Env.h:86
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:14
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:38
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Definition trust.cpp:18
json::Value signers(Account const &account, std::uint32_t quorum, std::vector< Signer > const &v)
Definition multisign.cpp:31
json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
Definition flags.cpp:15
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:53
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ telINSUF_FEE_P
Definition TER.h:43
@ telENV_RPC_FAILED
Definition TER.h:54
@ telCAN_NOT_QUEUE
Definition TER.h:45
@ terPRE_SEQ
Definition TER.h:223
@ terQUEUED
Definition TER.h:227
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
constexpr FlagValue tfInnerBatchTxn
Definition TxFlags.h:44
@ RpcTxnNotFound
Definition ErrorCodes.h:63
constexpr std::size_t kMaxBatchSigners
The maximum number of batch signers.
Definition Protocol.h:487
@ SigBad
Signature is bad.
Definition apply.h:28
ApplyResult apply(ServiceRegistry &registry, OpenView &view, STTx const &tx, ApplyFlags flags, beast::Journal journal)
Apply a transaction to an OpenView.
Definition apply.cpp:181
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
std::pair< Validity, std::string > checkValidity(HashRouter &router, STTx const &tx, Rules const &rules)
Checks transaction signature and local checks.
Definition apply.cpp:62
constexpr TenthBips32 percentageToTenthBips(std::uint32_t percentage)
Definition Protocol.h:127
constexpr std::size_t kMaxBatchTxCount
The maximum number of transactions that can be in a batch.
Definition Protocol.h:482
constexpr HashRouterFlags kSfSiggood
Definition apply.cpp:30
@ tefBAD_QUORUM
Definition TER.h:174
@ tefMASTER_DISABLED
Definition TER.h:171
@ tefBAD_SIGNATURE
Definition TER.h:173
@ tefBAD_AUTH
Definition TER.h:163
@ tefNOT_MULTI_SIGNING
Definition TER.h:175
UInt256 TxID
A transaction identifier.
Definition Protocol.h:430
XRPAmount toDrops(FeeLevel< T > const &level, XRPAmount baseFee)
Definition TxQ.h:1011
TenthBips< std::uint32_t > TenthBips32
Definition Units.h:454
TenthBips< std::uint16_t > TenthBips16
Definition Units.h:453
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
bool passesLocalChecks(STTx const &tx, std::string &)
Definition STTx.cpp:850
void finishMultiSigningData(AccountID const &signingID, Serializer &s)
Definition Sign.h:128
BaseUInt< 256 > UInt256
Definition base_uint.h:581
HashRouterFlags
Definition HashRouter.h:20
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Definition Indexes.cpp:219
@ TapNone
Definition ApplyView.h:28
@ TapBatch
Definition ApplyView.h:42
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
@ temBAD_REGKEY
Definition TER.h:86
@ temARRAY_TOO_LARGE
Definition TER.h:129
@ temBAD_FEE
Definition TER.h:80
@ temINVALID_FLAG
Definition TER.h:99
@ temMALFORMED
Definition TER.h:75
@ temARRAY_EMPTY
Definition TER.h:128
@ temSEQ_AND_TICKET
Definition TER.h:114
@ temDISABLED
Definition TER.h:102
@ temBAD_SIGNATURE
Definition TER.h:93
@ temINVALID_INNER_BATCH
Definition TER.h:131
@ temREDUNDANT
Definition TER.h:100
@ temBAD_SIGNER
Definition TER.h:103
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecNO_ENTRY
Definition TER.h:317
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
void serializeBatch(Serializer &msg, AccountID const &outerAccount, std::uint32_t outerSeqValue, std::uint32_t const &flags, std::vector< UInt256 > const &txids)
bool isPseudoTx(STObject const &tx)
Check whether a transaction is a pseudo-transaction.
Definition STTx.cpp:889
@ tesSUCCESS
Definition TER.h:253
UInt256 uint256
Definition base_uint.h:587
CanonicalTXSet OrderedTxs
Definition OpenLedger.h:35
constexpr bool any(HashRouterFlags flags)
Definition HashRouter.h:81
T parse(T... args)
T push_back(T... args)
T reserve(T... args)
T size(T... args)
UInt256 key
Definition Keylet.h:21
static constexpr auto kMinLedgersToComputeSizeLimit
Definition Constants.h:132
static constexpr auto kRetrySequencePercent
Definition Constants.h:155
static constexpr auto kMaxLedgerCountsToStore
Definition Constants.h:124
static constexpr auto kMinimumQueueSize
Definition Constants.h:135
static constexpr auto kNormalConsensusIncreasePercent
Definition Constants.h:138
static constexpr auto kLedgersInQueue
Definition Constants.h:120
static constexpr auto kMinimumTxnInLedgerStandalone
Definition Constants.h:137
State information when preflighting a tx.
Definition Transactor.h:39
static constexpr auto kTransactionQueue
Definition Constants.h:66
std::optional< std::string > batchID
Execution context for applying a JSON transaction.
Definition JTx.h:27
std::shared_ptr< STTx const > stx
Definition JTx.h:37
json::Value jv
Definition JTx.h:28
Thrown when parse fails.
Definition utility.h:23
Set the sequence number on a JTx.
Definition seq.h:16