xrpld
Loading...
Searching...
No Matches
Transaction_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/amount.h>
4#include <test/jtx/envconfig.h>
5#include <test/jtx/multisign.h>
6#include <test/jtx/noop.h>
7#include <test/jtx/pay.h>
8#include <test/jtx/seq.h>
9#include <test/jtx/ter.h>
10
11#include <xrpld/app/rdb/backend/SQLiteDatabase.h>
12#include <xrpld/rpc/CTID.h>
13
14#include <xrpl/basics/base_uint.h>
15#include <xrpl/basics/strHex.h>
16#include <xrpl/beast/unit_test/suite.h>
17#include <xrpl/core/NetworkIDService.h>
18#include <xrpl/json/json_value.h>
19#include <xrpl/json/to_string.h>
20#include <xrpl/protocol/ApiVersion.h>
21#include <xrpl/protocol/ErrorCodes.h>
22#include <xrpl/protocol/Feature.h>
23#include <xrpl/protocol/SField.h>
24#include <xrpl/protocol/STBase.h>
25#include <xrpl/protocol/STObject.h>
26#include <xrpl/protocol/STTx.h>
27#include <xrpl/protocol/TER.h>
28#include <xrpl/protocol/jss.h>
29#include <xrpl/protocol/serialize.h>
30
31#include <algorithm>
32#include <cctype>
33#include <cstddef>
34#include <cstdint>
35#include <memory>
36#include <optional>
37#include <string>
38#include <tuple>
39#include <vector>
40
41namespace xrpl {
42
44{
46 makeNetworkConfig(uint32_t networkID)
47 {
48 using namespace test::jtx;
49 return envconfig([&](std::unique_ptr<Config> cfg) {
50 cfg->networkId = networkID;
51 return cfg;
52 });
53 }
54
55 void
57 {
58 testcase("Test Range Request");
59
60 using namespace test::jtx;
61 using std::to_string;
62
63 char const* command = jss::tx.cStr();
64 char const* binary = jss::binary.cStr();
65 char const* notFound = rpc::getErrorInfo(RpcTxnNotFound).token;
66 char const* invalid = rpc::getErrorInfo(RpcInvalidLgrRange).token;
67 char const* excessive = rpc::getErrorInfo(RpcExcessiveLgrRange).token;
68
69 Env env{*this, features};
70 auto const alice = Account("alice");
71 env.fund(XRP(1000), alice);
72 env.close();
73
76 auto const startLegSeq = env.current()->header().seq;
77 for (int i = 0; i < 750; ++i)
78 {
79 env(noop(alice));
80 txns.emplace_back(env.tx());
81 env.close();
82 metas.emplace_back(env.closed()->txRead(env.tx()->getTransactionID()).second);
83 }
84 auto const endLegSeq = env.closed()->header().seq;
85
86 // Find the existing transactions
87 for (size_t i = 0; i < txns.size(); ++i)
88 {
89 auto const& tx = txns[i];
90 auto const& meta = metas[i];
91 auto const result = env.rpc(
92 command,
93 to_string(tx->getTransactionID()),
94 binary,
95 to_string(startLegSeq),
96 to_string(endLegSeq));
97
98 BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
99 BEAST_EXPECT(result[jss::result][jss::tx] == strHex(tx->getSerializer().getData()));
100 BEAST_EXPECT(result[jss::result][jss::meta] == strHex(meta->getSerializer().getData()));
101 }
102
103 auto const tx = env.jt(noop(alice), Seq(env.seq(alice))).stx;
104 for (int deltaEndSeq = 0; deltaEndSeq < 2; ++deltaEndSeq)
105 {
106 auto const result = env.rpc(
107 command,
108 to_string(tx->getTransactionID()),
109 binary,
110 to_string(startLegSeq),
111 to_string(endLegSeq + deltaEndSeq));
112
113 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
114 BEAST_EXPECT(result[jss::result][jss::error] == notFound);
115 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcTxnNotFound);
116 BEAST_EXPECT(result[jss::result][jss::error_message] == "Transaction not found.");
117
118 if (deltaEndSeq != 0)
119 {
120 BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
121 }
122 else
123 {
124 BEAST_EXPECT(result[jss::result][jss::searched_all].asBool());
125 }
126 }
127
128 // Find transactions outside of provided range.
129 for (auto&& tx : txns)
130 {
131 auto const result = env.rpc(
132 command,
133 to_string(tx->getTransactionID()),
134 binary,
135 to_string(endLegSeq + 1),
136 to_string(endLegSeq + 100));
137
138 BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
139 BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
140 }
141
142 auto const deletedLedger = (startLegSeq + endLegSeq) / 2;
143 {
144 // Remove one of the ledgers from the database directly
145 env.app().getRelationalDatabase().deleteTransactionByLedgerSeq(deletedLedger);
146 }
147
148 for (int deltaEndSeq = 0; deltaEndSeq < 2; ++deltaEndSeq)
149 {
150 auto const result = env.rpc(
151 command,
152 to_string(tx->getTransactionID()),
153 binary,
154 to_string(startLegSeq),
155 to_string(endLegSeq + deltaEndSeq));
156
157 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
158 BEAST_EXPECT(result[jss::result][jss::error] == notFound);
159 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcTxnNotFound);
160 BEAST_EXPECT(result[jss::result][jss::error_message] == "Transaction not found.");
161 BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
162 }
163
164 // Provide range without providing the `binary`
165 // field. (Tests parameter parsing)
166 {
167 auto const result = env.rpc(
168 command,
169 to_string(tx->getTransactionID()),
170 to_string(startLegSeq),
171 to_string(endLegSeq));
172
173 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
174 BEAST_EXPECT(result[jss::result][jss::error] == notFound);
175 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcTxnNotFound);
176 BEAST_EXPECT(result[jss::result][jss::error_message] == "Transaction not found.");
177
178 BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
179 }
180
181 // Provide range without providing the `binary`
182 // field. (Tests parameter parsing)
183 {
184 auto const result = env.rpc(
185 command,
186 to_string(tx->getTransactionID()),
187 to_string(startLegSeq),
188 to_string(deletedLedger - 1));
189
190 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
191 BEAST_EXPECT(result[jss::result][jss::error] == notFound);
192 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcTxnNotFound);
193 BEAST_EXPECT(result[jss::result][jss::error_message] == "Transaction not found.");
194
195 BEAST_EXPECT(result[jss::result][jss::searched_all].asBool());
196 }
197
198 // Provide range without providing the `binary`
199 // field. (Tests parameter parsing)
200 {
201 auto const result = env.rpc(
202 command,
203 to_string(txns[0]->getTransactionID()),
204 to_string(startLegSeq),
205 to_string(deletedLedger - 1));
206
207 BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
208 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
209 }
210
211 // Provide an invalid range: (min > max)
212 {
213 auto const result = env.rpc(
214 command,
215 to_string(tx->getTransactionID()),
216 binary,
217 to_string(deletedLedger - 1),
218 to_string(startLegSeq));
219
220 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
221 BEAST_EXPECT(result[jss::result][jss::error] == invalid);
222 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcInvalidLgrRange);
223 BEAST_EXPECT(result[jss::result][jss::error_message] == "Ledger range is invalid.");
224
225 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
226 }
227
228 // Provide an invalid range: (min < 0)
229 {
230 auto const result = env.rpc(
231 command,
232 to_string(tx->getTransactionID()),
233 binary,
234 to_string(-1),
235 to_string(deletedLedger - 1));
236
237 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
238 BEAST_EXPECT(result[jss::result][jss::error] == invalid);
239 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcInvalidLgrRange);
240 BEAST_EXPECT(result[jss::result][jss::error_message] == "Ledger range is invalid.");
241
242 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
243 }
244
245 // Provide an invalid range: (min < 0, max < 0)
246 {
247 auto const result = env.rpc(
248 command, to_string(tx->getTransactionID()), binary, to_string(-20), to_string(-10));
249
250 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
251 BEAST_EXPECT(result[jss::result][jss::error] == invalid);
252 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcInvalidLgrRange);
253 BEAST_EXPECT(result[jss::result][jss::error_message] == "Ledger range is invalid.");
254
255 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
256 }
257
258 // Provide an invalid range: (only one value)
259 {
260 auto const result =
261 env.rpc(command, to_string(tx->getTransactionID()), binary, to_string(20));
262
263 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
264 BEAST_EXPECT(result[jss::result][jss::error] == invalid);
265 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcInvalidLgrRange);
266 BEAST_EXPECT(result[jss::result][jss::error_message] == "Ledger range is invalid.");
267
268 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
269 }
270
271 // Provide an invalid range: (only one value)
272 {
273 auto const result = env.rpc(command, to_string(tx->getTransactionID()), to_string(20));
274
275 // Since we only provided one value for the range,
276 // the interface parses it as a false binary flag,
277 // as single-value ranges are not accepted. Since
278 // the error this causes differs depending on the platform
279 // we don't call out a specific error here.
280 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
281
282 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
283 }
284
285 // Provide an invalid range: (max - min > 1000)
286 {
287 auto const result = env.rpc(
288 command,
289 to_string(tx->getTransactionID()),
290 binary,
291 to_string(startLegSeq),
292 to_string(startLegSeq + 1001));
293
294 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
295 BEAST_EXPECT(result[jss::result][jss::error] == excessive);
296 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcExcessiveLgrRange);
297 BEAST_EXPECT(result[jss::result][jss::error_message] == "Ledger range exceeds 1000.");
298
299 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
300 }
301 }
302
303 void
305 {
306 testcase("CTID Range Request");
307
308 using namespace test::jtx;
309 using std::to_string;
310
311 char const* command = jss::tx.cStr();
312 char const* binary = jss::binary.cStr();
313 char const* notFound = rpc::getErrorInfo(RpcTxnNotFound).token;
314 char const* invalid = rpc::getErrorInfo(RpcInvalidLgrRange).token;
315 char const* excessive = rpc::getErrorInfo(RpcExcessiveLgrRange).token;
316
317 Env env{*this, makeNetworkConfig(11111)};
318 uint32_t const netID = env.app().getNetworkIDService().getNetworkID();
319
320 auto const alice = Account("alice");
321 env.fund(XRP(1000), alice);
322 env.close();
323
326 auto const startLegSeq = env.current()->header().seq;
327 for (int i = 0; i < 750; ++i)
328 {
329 env(noop(alice));
330 txns.emplace_back(env.tx());
331 env.close();
332 metas.emplace_back(env.closed()->txRead(env.tx()->getTransactionID()).second);
333 }
334 auto const endLegSeq = env.closed()->header().seq;
335
336 // Find the existing transactions
337 for (size_t i = 0; i < txns.size(); ++i)
338 {
339 auto const& tx = txns[i];
340 auto const& meta = metas[i];
341 uint32_t const txnIdx = meta->getFieldU32(sfTransactionIndex);
342 auto const result = env.rpc(
343 command,
344 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
345 *rpc::encodeCTID(startLegSeq + i, txnIdx, netID),
346 binary,
347 to_string(startLegSeq),
348 to_string(endLegSeq));
349
350 BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
351 BEAST_EXPECT(result[jss::result][jss::tx] == strHex(tx->getSerializer().getData()));
352 BEAST_EXPECT(result[jss::result][jss::meta] == strHex(meta->getSerializer().getData()));
353 }
354
355 auto const tx = env.jt(noop(alice), Seq(env.seq(alice))).stx;
356 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
357 auto const ctid = *rpc::encodeCTID(endLegSeq, tx->getSeqProxy().value(), netID);
358 for (int deltaEndSeq = 0; deltaEndSeq < 2; ++deltaEndSeq)
359 {
360 auto const result = env.rpc(
361 command, ctid, binary, to_string(startLegSeq), to_string(endLegSeq + deltaEndSeq));
362
363 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
364 BEAST_EXPECT(result[jss::result][jss::error] == notFound);
365 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcTxnNotFound);
366 BEAST_EXPECT(result[jss::result][jss::error_message] == "Transaction not found.");
367
368 if (deltaEndSeq != 0)
369 {
370 BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
371 }
372 else
373 {
374 BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
375 }
376 }
377
378 // Find transactions outside of provided range.
379 for (size_t i = 0; i < txns.size(); ++i)
380 {
381 // auto const& tx = txns[i];
382 auto const& meta = metas[i];
383 uint32_t const txnIdx = meta->getFieldU32(sfTransactionIndex);
384 auto const result = env.rpc(
385 command,
386 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
387 *rpc::encodeCTID(startLegSeq + i, txnIdx, netID),
388 binary,
389 to_string(endLegSeq + 1),
390 to_string(endLegSeq + 100));
391
392 BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
393 BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
394 }
395
396 auto const deletedLedger = (startLegSeq + endLegSeq) / 2;
397 {
398 // Remove one of the ledgers from the database directly
399 env.app().getRelationalDatabase().deleteTransactionByLedgerSeq(deletedLedger);
400 }
401
402 for (int deltaEndSeq = 0; deltaEndSeq < 2; ++deltaEndSeq)
403 {
404 auto const result = env.rpc(
405 command, ctid, binary, to_string(startLegSeq), to_string(endLegSeq + deltaEndSeq));
406
407 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
408 BEAST_EXPECT(result[jss::result][jss::error] == notFound);
409 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcTxnNotFound);
410 BEAST_EXPECT(result[jss::result][jss::error_message] == "Transaction not found.");
411 BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
412 }
413
414 // Provide range without providing the `binary`
415 // field. (Tests parameter parsing)
416 {
417 auto const result =
418 env.rpc(command, ctid, to_string(startLegSeq), to_string(endLegSeq));
419
420 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
421 BEAST_EXPECT(result[jss::result][jss::error] == notFound);
422 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcTxnNotFound);
423 BEAST_EXPECT(result[jss::result][jss::error_message] == "Transaction not found.");
424
425 BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
426 }
427
428 // Provide range without providing the `binary`
429 // field. (Tests parameter parsing)
430 {
431 auto const result =
432 env.rpc(command, ctid, to_string(startLegSeq), to_string(deletedLedger - 1));
433
434 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
435 BEAST_EXPECT(result[jss::result][jss::error] == notFound);
436 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcTxnNotFound);
437 BEAST_EXPECT(result[jss::result][jss::error_message] == "Transaction not found.");
438
439 BEAST_EXPECT(!result[jss::result][jss::searched_all].asBool());
440 }
441
442 // Provide range without providing the `binary`
443 // field. (Tests parameter parsing)
444 {
445 auto const& meta = metas[0];
446 uint32_t const txnIdx = meta->getFieldU32(sfTransactionIndex);
447 auto const result = env.rpc(
448 command,
449 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
450 *rpc::encodeCTID(endLegSeq, txnIdx, netID),
451 to_string(startLegSeq),
452 to_string(deletedLedger - 1));
453
454 BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
455 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
456 }
457
458 // Provide an invalid range: (min > max)
459 {
460 auto const result = env.rpc(
461 command, ctid, binary, to_string(deletedLedger - 1), to_string(startLegSeq));
462
463 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
464 BEAST_EXPECT(result[jss::result][jss::error] == invalid);
465 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcInvalidLgrRange);
466 BEAST_EXPECT(result[jss::result][jss::error_message] == "Ledger range is invalid.");
467
468 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
469 }
470
471 // Provide an invalid range: (min < 0)
472 {
473 auto const result =
474 env.rpc(command, ctid, binary, to_string(-1), to_string(deletedLedger - 1));
475
476 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
477 BEAST_EXPECT(result[jss::result][jss::error] == invalid);
478 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcInvalidLgrRange);
479 BEAST_EXPECT(result[jss::result][jss::error_message] == "Ledger range is invalid.");
480
481 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
482 }
483
484 // Provide an invalid range: (min < 0, max < 0)
485 {
486 auto const result = env.rpc(command, ctid, binary, to_string(-20), to_string(-10));
487
488 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
489 BEAST_EXPECT(result[jss::result][jss::error] == invalid);
490 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcInvalidLgrRange);
491 BEAST_EXPECT(result[jss::result][jss::error_message] == "Ledger range is invalid.");
492
493 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
494 }
495
496 // Provide an invalid range: (only one value)
497 {
498 auto const result = env.rpc(command, ctid, binary, to_string(20));
499
500 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
501 BEAST_EXPECT(result[jss::result][jss::error] == invalid);
502 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcInvalidLgrRange);
503 BEAST_EXPECT(result[jss::result][jss::error_message] == "Ledger range is invalid.");
504
505 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
506 }
507
508 // Provide an invalid range: (only one value)
509 {
510 auto const result = env.rpc(command, ctid, to_string(20));
511
512 // Since we only provided one value for the range,
513 // the interface parses it as a false binary flag,
514 // as single-value ranges are not accepted. Since
515 // the error this causes differs depending on the platform
516 // we don't call out a specific error here.
517 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
518
519 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
520 }
521
522 // Provide an invalid range: (max - min > 1000)
523 {
524 auto const result = env.rpc(
525 command, ctid, binary, to_string(startLegSeq), to_string(startLegSeq + 1001));
526
527 BEAST_EXPECT(result[jss::result][jss::status] == jss::error);
528 BEAST_EXPECT(result[jss::result][jss::error] == excessive);
529 BEAST_EXPECT(result[jss::result][jss::error_code] == RpcExcessiveLgrRange);
530 BEAST_EXPECT(result[jss::result][jss::error_message] == "Ledger range exceeds 1000.");
531
532 BEAST_EXPECT(!result[jss::result].isMember(jss::searched_all));
533 }
534 }
535
536 void
538 {
539 testcase("CTID Validation");
540
541 using namespace test::jtx;
542 using std::to_string;
543
544 Env const env{*this, makeNetworkConfig(11111)};
545
546 // Test case 1: Valid input values
547 auto const expected11 = std::optional<std::string>("CFFFFFFFFFFFFFFF");
548 BEAST_EXPECT(rpc::encodeCTID(0x0FFF'FFFFUL, 0xFFFFU, 0xFFFFU) == expected11);
549 auto const expected12 = std::optional<std::string>("C000000000000000");
550 BEAST_EXPECT(rpc::encodeCTID(0, 0, 0) == expected12);
551 auto const expected13 = std::optional<std::string>("C000000100020003");
552 BEAST_EXPECT(rpc::encodeCTID(1U, 2U, 3U) == expected13);
553 auto const expected14 = std::optional<std::string>("C0CA2AA7326FFFFF");
554 BEAST_EXPECT(rpc::encodeCTID(13249191UL, 12911U, 65535U) == expected14);
555
556 // Test case 2: ledger_seq greater than 0xFFFFFFF
557 BEAST_EXPECT(!rpc::encodeCTID(0x1000'0000UL, 0xFFFFU, 0xFFFFU));
558
559 // Test case 3: txn_index greater than 0xFFFF
560 BEAST_EXPECT(!rpc::encodeCTID(0x0FFF'FFFF, 0x1'0000, 0xFFFF));
561
562 // Test case 4: network_id greater than 0xFFFF
563 BEAST_EXPECT(!rpc::encodeCTID(0x0FFF'FFFFUL, 0xFFFFU, 0x1'0000U));
564
565 // Test case 5: Valid input values
566 auto const expected51 =
568 BEAST_EXPECT(rpc::decodeCTID("C000000000000000") == expected51);
569 auto const expected52 =
571 BEAST_EXPECT(rpc::decodeCTID("C000000100020003") == expected52);
573 std::make_tuple(13249191UL, 12911U, 49221U));
574 BEAST_EXPECT(rpc::decodeCTID("C0CA2AA7326FC045") == expected53);
575
576 // Test case 6: ctid not a string or big int
577 BEAST_EXPECT(!rpc::decodeCTID(0xCFF));
578
579 // Test case 7: ctid not a hexadecimal string
580 BEAST_EXPECT(!rpc::decodeCTID("C003FFFFFFFFFFFG"));
581
582 // Test case 8: ctid not exactly 16 nibbles
583 BEAST_EXPECT(!rpc::decodeCTID("C003FFFFFFFFFFF"));
584
585 // Test case 9: ctid too large to be a valid CTID value
586 BEAST_EXPECT(!rpc::decodeCTID("CFFFFFFFFFFFFFFFF"));
587
588 // Test case 10: ctid doesn't start with a C nibble
589 BEAST_EXPECT(!rpc::decodeCTID("FFFFFFFFFFFFFFFF"));
590
591 // Test case 11: Valid input values
592 BEAST_EXPECT(
593 (rpc::decodeCTID(0xCFFF'FFFF'FFFF'FFFFULL) ==
595 std::make_tuple(0x0FFF'FFFFUL, 0xFFFFU, 0xFFFFU))));
596 BEAST_EXPECT(
597 (rpc::decodeCTID(0xC000'0000'0000'0000ULL) ==
599 BEAST_EXPECT(
600 (rpc::decodeCTID(0xC000'0001'0002'0003ULL) ==
602 BEAST_EXPECT(
603 (rpc::decodeCTID(0xC0CA'2AA7'326F'C045ULL) ==
605 std::make_tuple(1324'9191UL, 12911U, 49221U))));
606
607 // Test case 12: ctid not exactly 16 nibbles
608 BEAST_EXPECT(!rpc::decodeCTID(0xC003'FFFF'FFFF'FFF));
609
610 // Test case 13: ctid too large to be a valid CTID value
611 // this test case is not possible in c++ because it would overflow the
612 // type, left in for completeness
613 // BEAST_EXPECT(!rpc::decodeCTID(0xCFFFFFFFFFFFFFFFFULL));
614
615 // Test case 14: ctid doesn't start with a C nibble
616 BEAST_EXPECT(!rpc::decodeCTID(0xFFFF'FFFF'FFFF'FFFFULL));
617 }
618
619 void
621 {
622 testcase("CTID RPC");
623
624 using namespace test::jtx;
625
626 // Use a Concise Transaction Identifier to request a transaction.
627 for (uint32_t const netID : {11111, 65535, 65536})
628 {
629 Env env{*this, makeNetworkConfig(netID)};
630 BEAST_EXPECT(netID == env.app().getNetworkIDService().getNetworkID());
631
632 auto const alice = Account("alice");
633 auto const bob = Account("bob");
634
635 auto const startLegSeq = env.current()->header().seq;
636 env.fund(XRP(10000), alice, bob);
637 env(pay(alice, bob, XRP(10)));
638 env.close();
639
640 auto const ctid = rpc::encodeCTID(startLegSeq, 0, netID);
641 if (netID > 0xFFFF)
642 {
643 // Concise transaction IDs do not support a network ID > 0xFFFF.
644 BEAST_EXPECT(ctid == std::nullopt);
645 continue;
646 }
647
648 json::Value jsonTx;
649 jsonTx[jss::binary] = false;
650 jsonTx[jss::ctid] = *ctid; // NOLINT(bugprone-unchecked-optional-access)
651 jsonTx[jss::id] = 1;
652 auto const jrr = env.rpc("json", "tx", to_string(jsonTx))[jss::result];
653 BEAST_EXPECT(jrr[jss::ctid] == ctid);
654 BEAST_EXPECT(jrr.isMember(jss::hash));
655 }
656
657 // test querying with mixed case ctid
658 {
659 Env env{*this, makeNetworkConfig(11111)};
660 std::uint32_t const netID = env.app().getNetworkIDService().getNetworkID();
661
662 Account const alice = Account("alice");
663 Account const bob = Account("bob");
664
665 std::uint32_t const startLegSeq = env.current()->header().seq;
666 env.fund(XRP(10000), alice, bob);
667 env(pay(alice, bob, XRP(10)));
668 env.close();
669
670 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
671 std::string const ctid = *rpc::encodeCTID(startLegSeq, 0, netID);
672 auto const isUpper = [](char c) { return std::isupper(c) != 0; };
673
674 // Verify that there are at least two upper case letters in ctid and
675 // test a mixed case
676 if (BEAST_EXPECT(std::count_if(ctid.begin(), ctid.end(), isUpper) > 1))
677 {
678 // Change the first upper case letter to lower case.
679 std::string mixedCase = ctid;
680 {
681 auto const iter = std::ranges::find_if(mixedCase, isUpper);
682 *iter = std::tolower(*iter);
683 }
684 BEAST_EXPECT(ctid != mixedCase);
685
686 json::Value jsonTx;
687 jsonTx[jss::binary] = false;
688 jsonTx[jss::ctid] = mixedCase;
689 jsonTx[jss::id] = 1;
690 json::Value const jrr = env.rpc("json", "tx", to_string(jsonTx))[jss::result];
691 BEAST_EXPECT(jrr[jss::ctid] == ctid);
692 BEAST_EXPECT(jrr[jss::hash]);
693 }
694 }
695
696 // test that if the network is 65535 the ctid is not in the response
697 // Using a hash to request the transaction, test the network ID
698 // boundary where the CTID is (not) in the response.
699 for (uint32_t const netID : {2, 1024, 65535, 65536})
700 {
701 Env env{*this, makeNetworkConfig(netID)};
702 BEAST_EXPECT(netID == env.app().getNetworkIDService().getNetworkID());
703
704 auto const alice = Account("alice");
705 auto const bob = Account("bob");
706
707 env.fund(XRP(10000), alice, bob);
708 env(pay(alice, bob, XRP(10)));
709 env.close();
710
711 auto const ledgerSeq = env.current()->header().seq;
712
713 env(noop(alice), Ter(tesSUCCESS));
714 env.close();
715
716 json::Value params;
717 params[jss::id] = 1;
718 auto const hash = env.tx()->getJson(JsonOptions::Values::None)[jss::hash];
719 params[jss::transaction] = hash;
720 auto const jrr = env.rpc("json", "tx", to_string(params))[jss::result];
721 BEAST_EXPECT(jrr[jss::hash] == hash);
722
723 BEAST_EXPECT(jrr.isMember(jss::ctid) == (netID <= 0xFFFF));
724 if (jrr.isMember(jss::ctid))
725 {
726 auto const ctid = rpc::encodeCTID(ledgerSeq, 0, netID);
727 BEAST_EXPECT(
728 jrr[jss::ctid] == *ctid); // NOLINT(bugprone-unchecked-optional-access)
729 }
730 }
731
732 // test the wrong network ID was submitted
733 {
734 Env env{*this, makeNetworkConfig(21337)};
735 uint32_t const netID = env.app().getNetworkIDService().getNetworkID();
736
737 auto const alice = Account("alice");
738 auto const bob = Account("bob");
739
740 auto const startLegSeq = env.current()->header().seq;
741 env.fund(XRP(10000), alice, bob);
742 env(pay(alice, bob, XRP(10)));
743 env.close();
744
745 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
746 auto const ctid = *rpc::encodeCTID(startLegSeq, 0, netID + 1);
747 json::Value jsonTx;
748 jsonTx[jss::binary] = false;
749 jsonTx[jss::ctid] = ctid;
750 jsonTx[jss::id] = 1;
751 auto const jrr = env.rpc("json", "tx", to_string(jsonTx))[jss::result];
752 BEAST_EXPECT(jrr[jss::error] == "wrongNetwork");
753 BEAST_EXPECT(jrr[jss::error_code] == RpcWrongNetwork);
754 BEAST_EXPECT(
755 jrr[jss::error_message] ==
756 "Wrong network. You should submit this request to a node "
757 "running on NetworkID: 21338");
758 }
759 }
760
761 void
762 testRequest(FeatureBitset features, unsigned apiVersion)
763 {
764 testcase("Test Request API version " + std::to_string(apiVersion));
765
766 using namespace test::jtx;
767 using std::to_string;
768
769 Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
770 cfg->fees.referenceFee = 10;
771 return cfg;
772 })};
773 Account const alice{"alice"};
774 Account const alie{"alie"};
775 Account const gw{"gw"};
776 auto const usd{gw["USD"]};
777
778 env.fund(XRP(1000000), alice, gw);
779 env.close();
780
781 // AccountSet
782 env(noop(alice));
783
784 // Payment
785 env(pay(alice, gw, XRP(100)));
786
787 std::shared_ptr<STTx const> const txn = env.tx();
788 env.close();
790 env.closed()->txRead(env.tx()->getTransactionID()).second;
791
792 json::Value expected = txn->getJson(JsonOptions::Values::None);
793 expected[jss::DeliverMax] = expected[jss::Amount];
794 if (apiVersion > 1)
795 {
796 expected.removeMember(jss::hash);
797 expected.removeMember(jss::Amount);
798 }
799
800 json::Value const result = {
801 [&env, txn, apiVersion] {
803 params[jss::transaction] = to_string(txn->getTransactionID());
804 params[jss::binary] = false;
805 params[jss::api_version] = apiVersion;
806 return env.client().invoke("tx", params);
807 }(),
808 };
809
810 BEAST_EXPECT(result[jss::result][jss::status] == jss::success);
811 if (apiVersion > 1)
812 {
813 BEAST_EXPECT(result[jss::result][jss::close_time_iso] == "2000-01-01T00:00:20Z");
814 BEAST_EXPECT(result[jss::result][jss::hash] == to_string(txn->getTransactionID()));
815 BEAST_EXPECT(result[jss::result][jss::validated] == true);
816 BEAST_EXPECT(result[jss::result][jss::ledger_index] == 4);
817 BEAST_EXPECT(
818 result[jss::result][jss::ledger_hash] ==
819 "CBA44903F72735C7BC0803388C319299E7AF09DEE2CDD6E454512DC4382EB1ED");
820 }
821
822 for (auto memberIt = expected.begin(); memberIt != expected.end(); memberIt++)
823 {
824 std::string const name = memberIt.memberName();
825 auto const& resultTransaction =
826 (apiVersion > 1 ? result[jss::result][jss::tx_json] : result[jss::result]);
827 if (BEAST_EXPECT(resultTransaction.isMember(name)))
828 {
829 auto const received = resultTransaction[name];
830 BEAST_EXPECTS(
831 received == *memberIt,
832 "Transaction contains \n\"" + name + "\": " //
833 + to_string(received) //
834 + " but expected " //
835 + to_string(expected));
836 }
837 }
838 }
839
840 void
841 testBinaryRequest(unsigned apiVersion)
842 {
843 testcase("Test binary request API version " + std::to_string(apiVersion));
844
845 using namespace test::jtx;
846 using std::to_string;
847
848 Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
849 cfg->fees.referenceFee = 10;
850 return cfg;
851 })};
852 Account const alice{"alice"};
853 Account const gw{"gw"};
854 auto const usd{gw["USD"]};
855
856 env.fund(XRP(1000000), alice, gw);
857 std::shared_ptr<STTx const> const txn = env.tx();
858 BEAST_EXPECT(
859 to_string(txn->getTransactionID()) ==
860 "3F8BDE5A5F82C4F4708E5E9255B713E303E6E1A371FD5C7A704AFD1387C23981");
861 env.close();
863 env.closed()->txRead(txn->getTransactionID()).second;
864
865 std::string const expectedTxBlob = serializeHex(*txn);
866 std::string const expectedMetaBlob = serializeHex(*meta);
867
868 json::Value const result = [&env, txn, apiVersion] {
870 params[jss::transaction] = to_string(txn->getTransactionID());
871 params[jss::binary] = true;
872 params[jss::api_version] = apiVersion;
873 return env.client().invoke("tx", params);
874 }();
875
876 if (BEAST_EXPECT(result[jss::status] == "success"))
877 {
878 BEAST_EXPECT(result[jss::result][jss::status] == "success");
879 BEAST_EXPECT(result[jss::result][jss::validated] == true);
880 BEAST_EXPECT(result[jss::result][jss::hash] == to_string(txn->getTransactionID()));
881 BEAST_EXPECT(result[jss::result][jss::ledger_index] == 3);
882 BEAST_EXPECT(result[jss::result][jss::ctid] == "C000000300030000");
883
884 if (apiVersion > 1)
885 {
886 BEAST_EXPECT(result[jss::result][jss::tx_blob] == expectedTxBlob);
887 BEAST_EXPECT(result[jss::result][jss::meta_blob] == expectedMetaBlob);
888 BEAST_EXPECT(
889 result[jss::result][jss::ledger_hash] ==
890 "3DAA1AD2334EA6F69EEEB0454E52FC64CA9209E8EF86BE72CF1D54A2ABC6BA05");
891 BEAST_EXPECT(result[jss::result][jss::close_time_iso] == "2000-01-01T00:00:10Z");
892 }
893 else
894 {
895 BEAST_EXPECT(result[jss::result][jss::tx] == expectedTxBlob);
896 BEAST_EXPECT(result[jss::result][jss::meta] == expectedMetaBlob);
897 BEAST_EXPECT(result[jss::result][jss::date] == 10);
898 }
899 }
900 }
901
902 void
904 {
905 testcase("SignFor NetworkID validation");
906 using namespace test::jtx;
907
908 Account const owner{"owner"};
909 Account const signer{"signer"};
910
911 auto const makeConfig = [](std::uint32_t networkID) {
912 return envconfig([networkID](std::unique_ptr<Config> cfg) {
913 cfg->networkId = networkID;
914 return cfg;
915 });
916 };
917
918 auto const setupEnv = [&](Env& env) {
919 env.fund(XRP(10'000), owner, signer);
920 env.close();
921 env(signers(owner, 1, {{signer, 1}}));
922 env.close();
923 };
924
925 auto const makeTx = [&](Env& env) {
926 json::Value tx;
927 tx[jss::TransactionType] = jss::AccountSet;
928 tx[jss::Account] = owner.human();
929 tx[jss::Sequence] = env.seq(owner);
930 tx[jss::Fee] = "100";
931 tx[jss::SigningPubKey] = "";
932 return tx;
933 };
934
935 auto const signFor = [&](Env& env, json::Value const& tx) {
936 json::Value signReq;
937 signReq[jss::tx_json] = tx;
938 signReq[jss::account] = signer.human();
939 signReq[jss::secret] = signer.name();
940 return env.rpc("json", "sign_for", to_string(signReq))[jss::result];
941 };
942
943 // Test case: NetworkID < 1024 - field is not required
944 {
945 Env env{*this, makeConfig(500)};
946 setupEnv(env);
947
948 auto const tx = makeTx(env);
949 auto result = signFor(env, tx);
950
951 BEAST_EXPECT(result[jss::status] == "success");
952 BEAST_EXPECT(!result[jss::tx_json].isMember(jss::NetworkID));
953 }
954
955 // Test case: NetworkID > 1024 - missing NetworkID field
956 {
957 Env env{*this, makeConfig(2040)};
958 setupEnv(env);
959
960 auto const tx = makeTx(env);
961 auto result = signFor(env, tx);
962
963 BEAST_EXPECT(result[jss::error] == "invalidParams");
964 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
965 BEAST_EXPECT(result[jss::error_message] == "Missing field 'tx_json.NetworkID'.");
966 }
967
968 // Test case: NetworkID > 1024 - NetworkID field is not a number
969 {
970 Env env{*this, makeConfig(2040)};
971 setupEnv(env);
972
973 auto tx = makeTx(env);
974 tx[jss::NetworkID] = "not_a_number";
975 auto result = signFor(env, tx);
976
977 BEAST_EXPECT(result[jss::error] == "invalidParams");
978 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
979 BEAST_EXPECT(result[jss::error_message] == "Invalid field 'tx_json.NetworkID'.");
980 }
981
982 // Test case: NetworkID > 1024 - NetworkID field is not integral
983 {
984 Env env{*this, makeConfig(2040)};
985 setupEnv(env);
986
987 auto tx = makeTx(env);
988 tx[jss::NetworkID] = 2040.1;
989 auto result = signFor(env, tx);
990
991 BEAST_EXPECT(result[jss::error] == "invalidParams");
992 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
993 BEAST_EXPECT(result[jss::error_message] == "Invalid field 'tx_json.NetworkID'.");
994 }
995
996 // Test case: NetworkID > 1024 - NetworkID field is different from
997 // actual NetworkID
998 {
999 Env env{*this, makeConfig(2040)};
1000 setupEnv(env);
1001
1002 auto tx = makeTx(env);
1003 tx[jss::NetworkID] = 9999;
1004 auto result = signFor(env, tx);
1005
1006 BEAST_EXPECT(result[jss::error] == "invalidParams");
1007 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
1008 BEAST_EXPECT(result[jss::error_message] == "Invalid field 'tx_json.NetworkID'.");
1009 }
1010
1011 // Test case: NetworkID > 1024 - NetworkID field is correct
1012 {
1013 Env env{*this, makeConfig(2040)};
1014 setupEnv(env);
1015
1016 auto tx = makeTx(env);
1017 tx[jss::NetworkID] = 2040;
1018 auto result = signFor(env, tx);
1019
1020 BEAST_EXPECT(result[jss::status] == "success");
1021 BEAST_EXPECT(result[jss::tx_json][jss::NetworkID].asUInt() == 2040);
1022 }
1023 }
1024
1025public:
1026 void
1027 run() override
1028 {
1029 using namespace test::jtx;
1030 forAllApiVersions([this](unsigned apiVersion) { testBinaryRequest(apiVersion); });
1031
1032 FeatureBitset const all{testableAmendments()};
1033 testWithFeats(all);
1034
1036 }
1037
1038 void
1040 {
1041 testRangeRequest(features);
1042 testRangeCTIDRequest(features);
1043 testCTIDValidation(features);
1044 testRPCsForCTID(features);
1046 [this, features](unsigned apiVersion) { testRequest(features, apiVersion); });
1047 }
1048};
1049
1051
1052} // namespace xrpl
T begin(T... args)
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
const_iterator begin() const
Value removeMember(char const *key)
Remove and return the named member.
const_iterator end() const
void testCTIDValidation(FeatureBitset features)
void run() override
Runs the suite.
void testBinaryRequest(unsigned apiVersion)
void testRangeCTIDRequest(FeatureBitset features)
static std::unique_ptr< Config > makeNetworkConfig(uint32_t networkID)
void testRangeRequest(FeatureBitset features)
void testRPCsForCTID(FeatureBitset features)
void testWithFeats(FeatureBitset features)
void testRequest(FeatureBitset features, unsigned apiVersion)
T count_if(T... args)
T emplace_back(T... args)
T end(T... args)
T find_if(T... args)
T make_tuple(T... args)
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
The api_version numbers this server serves.
Definition ApiVersion.h:26
std::optional< std::string > encodeCTID(uint32_t ledgerSeq, uint32_t txnIndex, uint32_t networkID) noexcept
Encodes ledger sequence, transaction index, and network ID into a CTID string.
Definition CTID.h:36
std::optional< std::tuple< uint32_t, uint16_t, uint16_t > > decodeCTID(T const ctid) noexcept
Decodes a CTID string or integer into its component parts.
Definition CTID.h:63
ErrorInfo const & getErrorInfo(ErrorCodeI code)
Returns an ErrorInfo that reflects the error code.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcExcessiveLgrRange
Definition ErrorCodes.h:118
@ RpcTxnNotFound
Definition ErrorCodes.h:63
@ RpcInvalidParams
Definition ErrorCodes.h:67
@ RpcWrongNetwork
Definition ErrorCodes.h:33
@ RpcInvalidLgrRange
Definition ErrorCodes.h:119
void forAllApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:159
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
std::string serializeHex(STObject const &o)
Serialize an object to a hex string.
Definition serialize.h:28
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
@ tesSUCCESS
Definition TER.h:253
T size(T... args)
json::StaticString token
Definition ErrorCodes.h:194
T to_string(T... args)