xrpld
Loading...
Searching...
No Matches
LedgerRPC_test.cpp
1
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/envconfig.h>
6#include <test/jtx/fee.h>
7#include <test/jtx/last_ledger_sequence.h>
8#include <test/jtx/mpt.h>
9#include <test/jtx/noop.h>
10#include <test/jtx/offer.h>
11#include <test/jtx/pay.h>
12#include <test/jtx/seq.h>
13#include <test/jtx/ter.h>
14
15#include <xrpld/app/misc/TxQ.h>
16#include <xrpld/rpc/CTID.h>
17
18#include <xrpl/basics/base_uint.h>
19#include <xrpl/beast/unit_test/suite.h>
20#include <xrpl/config/Constants.h>
21#include <xrpl/core/NetworkIDService.h>
22#include <xrpl/json/json_value.h>
23#include <xrpl/json/to_string.h>
24#include <xrpl/protocol/ErrorCodes.h>
25#include <xrpl/protocol/SField.h>
26#include <xrpl/protocol/TER.h>
27#include <xrpl/protocol/TxFlags.h>
28#include <xrpl/protocol/jss.h>
29
30#include <cstdint>
31#include <memory>
32#include <string>
33#include <utility>
34
35namespace xrpl::test {
36
38{
39 void
41 json::Value const& jv,
42 std::string const& err,
43 ErrorCodeI code,
44 std::string const& msg)
45 {
46 if (BEAST_EXPECT(jv.isMember(jss::status)))
47 BEAST_EXPECT(jv[jss::status] == "error");
48 if (BEAST_EXPECT(jv.isMember(jss::error)))
49 BEAST_EXPECT(jv[jss::error] == err);
50 if (BEAST_EXPECT(jv.isMember(jss::error_code)))
51 BEAST_EXPECT(jv[jss::error_code] == code);
52 if (msg.empty())
53 {
54 BEAST_EXPECT(
55 jv[jss::error_message] == json::ValueType::Null || jv[jss::error_message] == "");
56 }
57 else if (BEAST_EXPECT(jv.isMember(jss::error_message)))
58 {
59 BEAST_EXPECTS(
60 jv[jss::error_message] == msg,
61 "Expected error message \"" + msg + "\", received \"" +
62 jv[jss::error_message].asString() + "\"");
63 }
64 }
65
66 // Corrupt a valid address by replacing the 10th character with '!'.
67 // '!' is not part of the ripple alphabet.
68 static std::string
70 {
71 std::string ret = std::move(good);
72 ret.replace(10, 1, 1, '!');
73 return ret;
74 }
75
76 void
78 {
79 testcase("Basic Request");
80 using namespace test::jtx;
81
82 Env env{*this};
83
84 env.close();
85 BEAST_EXPECT(env.current()->header().seq == 4);
86
87 {
88 json::Value jvParams;
89 // can be either numeric or quoted numeric
90 jvParams[jss::ledger_index] = 1;
91 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
92 BEAST_EXPECT(jrr[jss::ledger][jss::closed] == true);
93 BEAST_EXPECT(jrr[jss::ledger][jss::ledger_index] == "1");
94 }
95
96 {
97 json::Value jvParams;
98 jvParams[jss::ledger_index] = "1";
99 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
100 BEAST_EXPECT(jrr[jss::ledger][jss::closed] == true);
101 BEAST_EXPECT(jrr[jss::ledger][jss::ledger_index] == "1");
102 }
103
104 {
105 // using current identifier
106 auto const jrr = env.rpc("ledger", "current")[jss::result];
107 BEAST_EXPECT(jrr[jss::ledger][jss::closed] == false);
108 BEAST_EXPECT(
109 jrr[jss::ledger][jss::ledger_index] == std::to_string(env.current()->header().seq));
110 BEAST_EXPECT(jrr[jss::ledger_current_index] == env.current()->header().seq);
111 }
112
113 {
114 auto const jrr = env.rpc("json", "ledger", "{}")[jss::result];
115 BEAST_EXPECT(!jrr.isMember(jss::ledger));
116 BEAST_EXPECT(jrr[jss::closed][jss::ledger][jss::closed] == true);
117 BEAST_EXPECT(
118 jrr[jss::closed][jss::ledger][jss::ledger_index] ==
119 std::to_string(env.closed()->header().seq));
120 BEAST_EXPECT(jrr[jss::open][jss::ledger][jss::closed] == false);
121 BEAST_EXPECT(
122 jrr[jss::open][jss::ledger][jss::ledger_index] ==
123 std::to_string(env.current()->header().seq));
124 }
125 }
126
127 void
129 {
130 testcase("Bad Input");
131 using namespace test::jtx;
132 Env env{*this};
133 Account const gw{"gateway"};
134 auto const usd = gw["USD"];
135 Account const bob{"bob"};
136
137 env.fund(XRP(10000), gw, bob);
138 env.close();
139 env.trust(usd(1000), bob);
140 env.close();
141
142 {
143 // ask for an arbitrary string - index
144 json::Value jvParams;
145 jvParams[jss::ledger_index] = "potato";
146 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
148 jrr,
149 "invalidParams",
151 "Invalid field 'ledger_index', not string or number.");
152 }
153
154 {
155 // ask for a negative index
156 json::Value jvParams;
157 jvParams[jss::ledger_index] = -1;
158 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
160 jrr,
161 "invalidParams",
163 "Invalid field 'ledger_index', not string or number.");
164 }
165
166 {
167 // ask for a bad ledger index
168 json::Value jvParams;
169 jvParams[jss::ledger_index] = 10u;
170 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
171 checkErrorValue(jrr, "lgrNotFound", RpcLgrNotFound, "ledgerNotFound");
172 }
173
174 {
175 // unrecognized string arg -- error
176 auto const jrr = env.rpc("ledger", "arbitrary_text")[jss::result];
177 checkErrorValue(jrr, "lgrNotFound", RpcLgrNotFound, "ledgerNotFound");
178 }
179
180 {
181 // Request queue for closed ledger
182 json::Value jvParams;
183 jvParams[jss::ledger_index] = "validated";
184 jvParams[jss::queue] = true;
185 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
186 checkErrorValue(jrr, "invalidParams", RpcInvalidParams, "Invalid parameters.");
187 }
188
189 {
190 // Request a ledger with a very large (double) sequence.
191 auto const ret = env.rpc("json", "ledger", "{ \"ledger_index\" : 2e15 }");
192 BEAST_EXPECT(ret[jss::error] == "invalidParams");
193 BEAST_EXPECT(ret[jss::error_code] == RpcInvalidParams);
194 BEAST_EXPECT(ret[jss::error_message] == "Invalid parameters.");
195 }
196
197 {
198 // Request a ledger with very large (integer) sequence.
199 auto const ret = env.rpc("json", "ledger", "{ \"ledger_index\" : 1000000000000000 }");
200 checkErrorValue(ret, "invalidParams", RpcInvalidParams, "Invalid parameters.");
201 }
202
203 {
204 // test all boolean fields with non-boolean values
205 auto const testBooleanField = [&](json::StaticString const& field) {
206 json::Value jvParams;
207 jvParams[field] = "blah";
208 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
209 checkErrorValue(jrr, "invalidParams", RpcInvalidParams, "Invalid parameters.");
210 };
211 testBooleanField(jss::full);
212 testBooleanField(jss::accounts);
213 testBooleanField(jss::transactions);
214 testBooleanField(jss::expand);
215 testBooleanField(jss::binary);
216 testBooleanField(jss::owner_funds);
217 testBooleanField(jss::queue);
218 }
219 }
220
221 void
223 {
224 testcase("ledger_current Request");
225 using namespace test::jtx;
226
227 Env env{*this};
228
229 env.close();
230 BEAST_EXPECT(env.current()->header().seq == 4);
231
232 {
233 auto const jrr = env.rpc("ledger_current")[jss::result];
234 BEAST_EXPECT(jrr[jss::ledger_current_index] == env.current()->header().seq);
235 }
236 }
237
238 void
240 {
241 testcase("Ledger Request, Full Option");
242 using namespace test::jtx;
243
244 Env env{*this};
245
246 env.close();
247
248 json::Value jvParams;
249 jvParams[jss::ledger_index] = 3u;
250 jvParams[jss::full] = true;
251 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
252 BEAST_EXPECT(jrr[jss::ledger].isMember(jss::accountState));
253 BEAST_EXPECT(jrr[jss::ledger][jss::accountState].isArray());
254 BEAST_EXPECT(jrr[jss::ledger][jss::accountState].size() == 3u);
255 }
256
257 void
259 {
260 testcase("Ledger Request, Full Option Without Admin");
261 using namespace test::jtx;
262
263 Env env{*this, envconfig(noAdmin)};
264
265 // env.close();
266
267 json::Value jvParams;
268 jvParams[jss::ledger_index] = 1u;
269 jvParams[jss::full] = true;
270 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
272 jrr, "noPermission", RpcNoPermission, "You don't have permission for this command.");
273 }
274
275 void
277 {
278 testcase("Ledger Request, Accounts Option");
279 using namespace test::jtx;
280
281 Env env{*this};
282
283 env.close();
284
285 json::Value jvParams;
286 jvParams[jss::ledger_index] = 3u;
287 jvParams[jss::accounts] = true;
288 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
289 BEAST_EXPECT(jrr[jss::ledger].isMember(jss::accountState));
290 BEAST_EXPECT(jrr[jss::ledger][jss::accountState].isArray());
291 BEAST_EXPECT(jrr[jss::ledger][jss::accountState].size() == 3u);
292 }
293
294 void
296 {
297 testcase("Ledger owner_funds with MPT offer");
298 using namespace test::jtx;
299
300 Env env{*this};
301 Account const gw{"gateway"};
302 Account const alice{"alice"};
303 auto const usd = gw["USD"];
304
305 env.fund(XRP(10'000), gw, alice);
306 env.close();
307 env.trust(usd(1'000), alice);
308 env(pay(gw, alice, usd(100)));
309 MPTTester mpt({
310 .env = env,
311 .issuer = gw,
312 .holders = {alice},
313 .pay = 100,
314 .flags = tfMPTRequireAuth | kMptDexFlags,
315 .authHolder = true,
316 .close = false,
317 });
318 MPT const mptAsset = mpt;
319 env.close();
320
321 env(noop(alice));
322 // These offers differ only by TakerGets asset type. Omitting
323 // owner_funds serializes the tx JSON without computing offer balances;
324 // owner_funds=true asks LedgerToJson to compute accountFunds(TakerGets)
325 // for both offers, which is where IOU and MPT used to diverge.
326 env(offer(alice, XRP(10), usd(10)));
327 env(offer(alice, XRP(10), mptAsset(10)));
328 // The MPT offer was created while authorized. Unauthorizing in the
329 // same ledger makes owner_funds depend on AuthHandling::IgnoreAuth.
330 mpt.authorize({.account = gw, .holder = alice, .flags = tfMPTUnauthorize});
331 env(noop(alice));
332 env.close();
333
334 auto const ledgerHash = to_string(env.closed()->header().hash);
335
336 auto const getTransactions = [&](bool includeOwnerFunds) {
337 json::Value params;
338 params[jss::ledger_hash] = ledgerHash;
339 params[jss::transactions] = true;
340 params[jss::expand] = true;
341 // The baseline omits owner_funds, which the RPC treats as false.
342 // Setting it true requests the same ledger, but asks the ledger
343 // serializer to add owner_funds to offer transactions in that
344 // ledger's transaction array.
345 if (includeOwnerFunds)
346 params[jss::owner_funds] = true;
347
348 auto const result = env.rpc("json", "ledger", to_string(params))[jss::result];
349 BEAST_EXPECT(!result.isMember(jss::error));
350 BEAST_EXPECT(result[jss::ledger][jss::transactions].isArray());
351 return result[jss::ledger][jss::transactions];
352 };
353
354 auto const findOffer = [](json::Value const& txs, bool mpt) -> json::Value const* {
355 for (auto i = 0u; i < txs.size(); ++i)
356 {
357 auto const& tx = txs[i].isMember(jss::tx_json) ? txs[i][jss::tx_json] : txs[i];
358 if (tx[jss::TransactionType] == jss::OfferCreate &&
359 tx[jss::TakerGets].isMember(jss::mpt_issuance_id) == mpt)
360 {
361 return &txs[i];
362 }
363 }
364 return nullptr;
365 };
366
367 // Baseline: same ledger request without owner_funds fields.
368 auto const baseline = getTransactions(false);
369 BEAST_EXPECT(baseline.size() == 5u);
370 BEAST_EXPECT(findOffer(baseline, false) != nullptr);
371 BEAST_EXPECT(findOffer(baseline, true) != nullptr);
372
373 // Same ledger request with owner_funds added to eligible offer txs.
374 auto const withOwnerFunds = getTransactions(true);
375 // Requesting owner_funds must not change which ledger transactions are
376 // returned, even when one offer's TakerGets is MPT.
377 BEAST_EXPECT(withOwnerFunds.size() == baseline.size());
378
379 // The IOU offer is the control case for expected owner_funds output.
380 auto const* iouOfferTx = findOffer(withOwnerFunds, false);
381 if (BEAST_EXPECT(iouOfferTx != nullptr))
382 BEAST_EXPECT((*iouOfferTx)[jss::owner_funds] == "100");
383
384 // MPT owner_funds should match the IOU behavior, even though Alice is
385 // unauthorized by the ledger snapshot used for serialization.
386 auto const* mptOfferTx = findOffer(withOwnerFunds, true);
387 if (BEAST_EXPECT(mptOfferTx != nullptr))
388 BEAST_EXPECT((*mptOfferTx)[jss::owner_funds] == "100");
389 }
390
397 void
399 {
400 testcase("Lookup ledger");
401 using namespace test::jtx;
402
403 auto cfg = envconfig();
404 cfg->fees.referenceFee = 10;
405 Env env{*this, std::move(cfg), FeatureBitset{}}; // hashes requested below
406 // assume no amendments
407 env.fund(XRP(10000), "alice");
408 env.close();
409 env.fund(XRP(10000), "bob");
410 env.close();
411 env.fund(XRP(10000), "jim");
412 env.close();
413 env.fund(XRP(10000), "jill");
414
415 {
416 // access via the legacy ledger field, keyword index values
417 json::Value jvParams;
418 jvParams[jss::ledger] = "closed";
419 auto jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
420 BEAST_EXPECT(jrr.isMember(jss::ledger));
421 BEAST_EXPECT(jrr.isMember(jss::ledger_hash));
422 BEAST_EXPECT(jrr[jss::ledger][jss::ledger_index] == "5");
423
424 jvParams[jss::ledger] = "validated";
425 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
426 BEAST_EXPECT(jrr.isMember(jss::ledger));
427 BEAST_EXPECT(jrr.isMember(jss::ledger_hash));
428 BEAST_EXPECT(jrr[jss::ledger][jss::ledger_index] == "5");
429
430 jvParams[jss::ledger] = "current";
431 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
432 BEAST_EXPECT(jrr.isMember(jss::ledger));
433 BEAST_EXPECT(jrr[jss::ledger][jss::ledger_index] == "6");
434
435 // ask for a bad ledger keyword
436 jvParams[jss::ledger] = "invalid";
437 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
438 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
439 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
440 BEAST_EXPECT(
441 jrr[jss::error_message] == "Invalid field 'ledger', not string or number.");
442
443 // numeric index
444 jvParams[jss::ledger] = 4;
445 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
446 BEAST_EXPECT(jrr.isMember(jss::ledger));
447 BEAST_EXPECT(jrr.isMember(jss::ledger_hash));
448 BEAST_EXPECT(jrr[jss::ledger][jss::ledger_index] == "4");
449
450 // numeric index - out of range
451 jvParams[jss::ledger] = 20;
452 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
453 BEAST_EXPECT(jrr[jss::error] == "lgrNotFound");
454 BEAST_EXPECT(jrr[jss::error_code] == RpcLgrNotFound);
455 BEAST_EXPECT(jrr[jss::error_message] == "ledgerNotFound");
456 }
457
458 {
459 std::string const hash5{
460 "BFF5D915A6386545C8F1D8D9B165BDB6B29636653009509E0B43517E52E36CAE"};
461 // access via the ledger_hash field
462 json::Value jvParams;
463 jvParams[jss::ledger_hash] = hash5;
464 auto jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
465 BEAST_EXPECT(jrr.isMember(jss::ledger));
466 BEAST_EXPECT(jrr.isMember(jss::ledger_hash));
467 BEAST_EXPECT(jrr[jss::ledger][jss::ledger_index] == "5");
468
469 // extra leading hex chars in hash are not allowed
470 jvParams[jss::ledger_hash] = "DEADBEEF" + hash5;
471 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
472 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
473 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
474 BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'ledger_hash', not hex string.");
475
476 // request with non-string ledger_hash
477 jvParams[jss::ledger_hash] = 2;
478 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
479 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
480 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
481 BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'ledger_hash', not hex string.");
482
483 // malformed (non hex) hash
484 jvParams[jss::ledger_hash] =
485 "2E81FC6EC0DD943197EGC7E3FBE9AE30"
486 "7F2775F2F7485BB37307984C3C0F2340";
487 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
488 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
489 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
490 BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'ledger_hash', not hex string.");
491
492 // properly formed, but just doesn't exist
493 jvParams[jss::ledger_hash] =
494 "8C3EEDB3124D92E49E75D81A8826A2E6"
495 "5A75FD71FC3FD6F36FEB803C5F1D812D";
496 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
497 BEAST_EXPECT(jrr[jss::error] == "lgrNotFound");
498 BEAST_EXPECT(jrr[jss::error_code] == RpcLgrNotFound);
499 BEAST_EXPECT(jrr[jss::error_message] == "ledgerNotFound");
500 }
501
502 {
503 // access via the ledger_index field, keyword index values
504 json::Value jvParams;
505 jvParams[jss::ledger_index] = "closed";
506 auto jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
507 BEAST_EXPECT(jrr.isMember(jss::ledger));
508 BEAST_EXPECT(jrr.isMember(jss::ledger_hash));
509 BEAST_EXPECT(jrr[jss::ledger][jss::ledger_index] == "5");
510 BEAST_EXPECT(jrr.isMember(jss::ledger_index));
511
512 jvParams[jss::ledger_index] = "validated";
513 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
514 BEAST_EXPECT(jrr.isMember(jss::ledger));
515 BEAST_EXPECT(jrr.isMember(jss::ledger_hash));
516 BEAST_EXPECT(jrr[jss::ledger][jss::ledger_index] == "5");
517
518 jvParams[jss::ledger_index] = "current";
519 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
520 BEAST_EXPECT(jrr.isMember(jss::ledger));
521 BEAST_EXPECT(jrr[jss::ledger][jss::ledger_index] == "6");
522 BEAST_EXPECT(jrr.isMember(jss::ledger_current_index));
523
524 // ask for a bad ledger keyword
525 jvParams[jss::ledger_index] = "invalid";
526 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
527 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
528 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
529 BEAST_EXPECT(
530 jrr[jss::error_message] == "Invalid field 'ledger_index', not string or number.");
531
532 // numeric index
533 for (auto const i : {1, 2, 3, 4, 5, 6})
534 {
535 jvParams[jss::ledger_index] = i;
536 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
537 BEAST_EXPECT(jrr.isMember(jss::ledger));
538 if (i < 6)
539 BEAST_EXPECT(jrr.isMember(jss::ledger_hash));
540 BEAST_EXPECT(jrr[jss::ledger][jss::ledger_index] == std::to_string(i));
541 }
542
543 // numeric index - out of range
544 jvParams[jss::ledger_index] = 7;
545 jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
546 BEAST_EXPECT(jrr[jss::error] == "lgrNotFound");
547 BEAST_EXPECT(jrr[jss::error_code] == RpcLgrNotFound);
548 BEAST_EXPECT(jrr[jss::error_message] == "ledgerNotFound");
549 }
550 }
551
552 void
554 {
555 testcase("Ledger with queueing disabled");
556 using namespace test::jtx;
557 Env env{*this};
558
559 json::Value jv;
560 jv[jss::ledger_index] = "current";
561 jv[jss::queue] = true;
562 jv[jss::expand] = true;
563
564 auto const jrr = env.rpc("json", "ledger", to_string(jv))[jss::result];
565 BEAST_EXPECT(!jrr.isMember(jss::queue_data));
566 }
567
568 void
570 {
571 testcase("Ledger with Queued Transactions");
572 using namespace test::jtx;
573 auto cfg = envconfig([](std::unique_ptr<Config> cfg) {
574 auto& section = cfg->section(Sections::kTransactionQueue);
575 section.set(Keys::kMinimumTxnInLedgerStandalone, "3");
577 return cfg;
578 });
579
580 cfg->fees.referenceFee = 10;
581 Env env(*this, std::move(cfg));
582
583 json::Value jv;
584 jv[jss::ledger_index] = "current";
585 jv[jss::queue] = true;
586 jv[jss::expand] = true;
587
588 Account const alice{"alice"};
589 Account const bob{"bob"};
590 Account const charlie{"charlie"};
591 Account const daria{"daria"};
592 env.fund(XRP(10000), alice);
593 env.fund(XRP(10000), bob);
594 env.close();
595 env.fund(XRP(10000), charlie);
596 env.fund(XRP(10000), daria);
597 env.close();
598
599 auto jrr = env.rpc("json", "ledger", to_string(jv))[jss::result];
600 BEAST_EXPECT(!jrr.isMember(jss::queue_data));
601
602 // Fill the open ledger
603 for (;;)
604 {
605 auto const metrics = env.app().getTxQ().getMetrics(*env.current());
606 if (metrics.openLedgerFeeLevel > metrics.minProcessingFeeLevel)
607 break;
608 env(noop(alice));
609 }
610
611 BEAST_EXPECT(env.current()->header().seq == 5);
612 // Put some txs in the queue
613 // Alice
614 auto aliceSeq = env.seq(alice);
615 env(pay(alice, "george", XRP(1000)), LastLedgerSeq(7), Ter(terQUEUED));
616 env(offer(alice, XRP(50000), alice["USD"](5000)), Seq(aliceSeq + 1), Ter(terQUEUED));
617 env(noop(alice), Seq(aliceSeq + 2), Ter(terQUEUED));
618 // Bob
619 auto const batch = [&env](Account a) {
620 auto aSeq = env.seq(a);
621 // Enough fee to get in front of alice in the queue
622 for (int i = 0; i < 10; ++i)
623 {
624 env(noop(a), Fee(1000 + i), Seq(aSeq + i), Ter(terQUEUED));
625 }
626 };
627 batch(bob);
628 // Charlie
629 batch(charlie);
630 // Daria
631 batch(daria);
632
633 jrr = env.rpc("json", "ledger", to_string(jv))[jss::result];
634 BEAST_EXPECT(jrr[jss::queue_data].size() == 33);
635
636 // Close enough ledgers so that alice's first tx expires.
637 env.close();
638 env.close();
639 env.close();
640 BEAST_EXPECT(env.current()->header().seq == 8);
641
642 jrr = env.rpc("json", "ledger", to_string(jv))[jss::result];
643 BEAST_EXPECT(jrr[jss::queue_data].size() == 11);
644
645 env.close();
646
647 jrr = env.rpc("json", "ledger", to_string(jv))[jss::result];
648 std::string const txid0 = [&] {
649 auto const& parentHash = env.current()->header().parentHash;
650 if (BEAST_EXPECT(jrr[jss::queue_data].size() == 2))
651 {
652 std::string const txid1 = [&] {
653 auto const& txj = jrr[jss::queue_data][1u];
654 BEAST_EXPECT(txj[jss::account] == alice.human());
655 BEAST_EXPECT(txj[jss::fee_level] == "256");
656 BEAST_EXPECT(txj["preflight_result"] == "tesSUCCESS");
657 BEAST_EXPECT(txj["retries_remaining"] == 10);
658 BEAST_EXPECT(txj.isMember(jss::tx));
659 auto const& tx = txj[jss::tx];
660 BEAST_EXPECT(tx[jss::Account] == alice.human());
661 BEAST_EXPECT(tx[jss::TransactionType] == jss::AccountSet);
662 return tx[jss::hash].asString();
663 }();
664
665 auto const& txj = jrr[jss::queue_data][0u];
666 BEAST_EXPECT(txj[jss::account] == alice.human());
667 BEAST_EXPECT(txj[jss::fee_level] == "256");
668 BEAST_EXPECT(txj["preflight_result"] == "tesSUCCESS");
669 BEAST_EXPECT(txj["retries_remaining"] == 10);
670 BEAST_EXPECT(txj.isMember(jss::tx));
671 auto const& tx = txj[jss::tx];
672 BEAST_EXPECT(tx[jss::Account] == alice.human());
673 BEAST_EXPECT(tx[jss::TransactionType] == jss::OfferCreate);
674 auto const txid0 = tx[jss::hash].asString();
675 UInt256 tx0, tx1;
676 BEAST_EXPECT(tx0.parseHex(txid0));
677 BEAST_EXPECT(tx1.parseHex(txid1));
678 BEAST_EXPECT((tx0 ^ parentHash) < (tx1 ^ parentHash));
679 return txid0;
680 }
681 return std::string{};
682 }();
683
684 env.close();
685
686 jv[jss::expand] = false;
687
688 jrr = env.rpc("json", "ledger", to_string(jv))[jss::result];
689 if (BEAST_EXPECT(jrr[jss::queue_data].size() == 2))
690 {
691 auto const& parentHash = env.current()->header().parentHash;
692 auto const txid1 = [&] {
693 auto const& txj = jrr[jss::queue_data][0u];
694 BEAST_EXPECT(txj[jss::account] == alice.human());
695 BEAST_EXPECT(txj[jss::fee_level] == "256");
696 BEAST_EXPECT(txj["preflight_result"] == "tesSUCCESS");
697 BEAST_EXPECT(txj.isMember(jss::tx));
698 return txj[jss::tx].asString();
699 }();
700 auto const& txj = jrr[jss::queue_data][1u];
701 BEAST_EXPECT(txj[jss::account] == alice.human());
702 BEAST_EXPECT(txj[jss::fee_level] == "256");
703 BEAST_EXPECT(txj["preflight_result"] == "tesSUCCESS");
704 BEAST_EXPECT(txj["retries_remaining"] == 9);
705 BEAST_EXPECT(txj["last_result"] == "terPRE_SEQ");
706 BEAST_EXPECT(txj.isMember(jss::tx));
707 BEAST_EXPECT(txj[jss::tx] == txid0);
708 UInt256 tx0, tx1;
709 BEAST_EXPECT(tx0.parseHex(txid0));
710 BEAST_EXPECT(tx1.parseHex(txid1));
711 BEAST_EXPECT((tx1 ^ parentHash) < (tx0 ^ parentHash));
712 }
713
714 env.close();
715
716 jv[jss::expand] = true;
717 jv[jss::binary] = true;
718
719 jrr = env.rpc("json", "ledger", to_string(jv))[jss::result];
720 if (BEAST_EXPECT(jrr[jss::queue_data].size() == 2))
721 {
722 auto const& txj = jrr[jss::queue_data][0u];
723 BEAST_EXPECT(txj[jss::account] == alice.human());
724 BEAST_EXPECT(txj[jss::fee_level] == "256");
725 BEAST_EXPECT(txj["preflight_result"] == "tesSUCCESS");
726 BEAST_EXPECT(txj["retries_remaining"] == 8);
727 BEAST_EXPECT(txj["last_result"] == "terPRE_SEQ");
728 BEAST_EXPECT(txj.isMember(jss::tx));
729 BEAST_EXPECT(txj[jss::tx].isMember(jss::tx_blob));
730
731 auto const& txj2 = jrr[jss::queue_data][1u];
732 BEAST_EXPECT(txj2[jss::account] == alice.human());
733 BEAST_EXPECT(txj2[jss::fee_level] == "256");
734 BEAST_EXPECT(txj2["preflight_result"] == "tesSUCCESS");
735 BEAST_EXPECT(txj2["retries_remaining"] == 10);
736 BEAST_EXPECT(!txj2.isMember("last_result"));
737 BEAST_EXPECT(txj2.isMember(jss::tx));
738 BEAST_EXPECT(txj2[jss::tx].isMember(jss::tx_blob));
739 }
740
741 for (int i = 0; i != 9; ++i)
742 {
743 env.close();
744 }
745
746 jv[jss::expand] = false;
747 jv[jss::binary] = false;
748
749 jrr = env.rpc("json", "ledger", to_string(jv))[jss::result];
750 std::string const txid2 = [&] {
751 if (BEAST_EXPECT(jrr[jss::queue_data].size() == 1))
752 {
753 auto const& txj = jrr[jss::queue_data][0u];
754 BEAST_EXPECT(txj[jss::account] == alice.human());
755 BEAST_EXPECT(txj[jss::fee_level] == "256");
756 BEAST_EXPECT(txj["preflight_result"] == "tesSUCCESS");
757 BEAST_EXPECT(txj["retries_remaining"] == 1);
758 BEAST_EXPECT(txj["last_result"] == "terPRE_SEQ");
759 BEAST_EXPECT(txj.isMember(jss::tx));
760 BEAST_EXPECT(txj[jss::tx] != txid0);
761 return txj[jss::tx].asString();
762 }
763 return std::string{};
764 }();
765
766 jv[jss::full] = true;
767
768 jrr = env.rpc("json", "ledger", to_string(jv))[jss::result];
769 if (BEAST_EXPECT(jrr[jss::queue_data].size() == 1))
770 {
771 auto const& txj = jrr[jss::queue_data][0u];
772 BEAST_EXPECT(txj[jss::account] == alice.human());
773 BEAST_EXPECT(txj[jss::fee_level] == "256");
774 BEAST_EXPECT(txj["preflight_result"] == "tesSUCCESS");
775 BEAST_EXPECT(txj["retries_remaining"] == 1);
776 BEAST_EXPECT(txj["last_result"] == "terPRE_SEQ");
777 BEAST_EXPECT(txj.isMember(jss::tx));
778 auto const& tx = txj[jss::tx];
779 BEAST_EXPECT(tx[jss::Account] == alice.human());
780 BEAST_EXPECT(tx[jss::TransactionType] == jss::AccountSet);
781 BEAST_EXPECT(tx[jss::hash] == txid2);
782 }
783 }
784
785 void
787 {
788 testcase("Ledger Request, Accounts Hashes");
789 using namespace test::jtx;
790
791 Env env{*this};
792
793 env.close();
794
795 std::string index;
796 int hashesLedgerEntryIndex = -1;
797 {
798 json::Value jvParams;
799 jvParams[jss::ledger_index] = 3u;
800 jvParams[jss::accounts] = true;
801 jvParams[jss::expand] = true;
802 jvParams[jss::type] = "hashes";
803 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
804 BEAST_EXPECT(jrr[jss::ledger].isMember(jss::accountState));
805 BEAST_EXPECT(jrr[jss::ledger][jss::accountState].isArray());
806
807 for (auto i = 0; i < jrr[jss::ledger][jss::accountState].size(); i++)
808 {
809 if (jrr[jss::ledger][jss::accountState][i]["LedgerEntryType"] == jss::LedgerHashes)
810 {
811 index = jrr[jss::ledger][jss::accountState][i]["index"].asString();
812 hashesLedgerEntryIndex = i;
813 }
814 }
815
816 for (auto const& object : jrr[jss::ledger][jss::accountState])
817 {
818 if (object["LedgerEntryType"] == jss::LedgerHashes)
819 index = object["index"].asString();
820 }
821
822 // jss::type is a deprecated field
823 BEAST_EXPECT(
824 jrr.isMember(jss::warnings) && jrr[jss::warnings].isArray() &&
825 jrr[jss::warnings].size() == 1 &&
826 jrr[jss::warnings][0u][jss::id].asInt() == WarnRpcFieldsDeprecated);
827 }
828 {
829 json::Value jvParams;
830 jvParams[jss::ledger_index] = 3u;
831 jvParams[jss::accounts] = true;
832 jvParams[jss::expand] = false;
833 jvParams[jss::type] = "hashes";
834 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
835 BEAST_EXPECT(jrr[jss::ledger].isMember(jss::accountState));
836 BEAST_EXPECT(jrr[jss::ledger][jss::accountState].isArray());
837 BEAST_EXPECT(
838 hashesLedgerEntryIndex > 0 &&
839 jrr[jss::ledger][jss::accountState][hashesLedgerEntryIndex] == index);
840
841 // jss::type is a deprecated field
842 BEAST_EXPECT(
843 jrr.isMember(jss::warnings) && jrr[jss::warnings].isArray() &&
844 jrr[jss::warnings].size() == 1 &&
845 jrr[jss::warnings][0u][jss::id].asInt() == WarnRpcFieldsDeprecated);
846 }
847 }
848
849 void
851 {
852 testcase("Expanded Transactions CTID");
853 using namespace test::jtx;
854
855 Env env{*this};
856 Account const alice{"alice"};
857 env.fund(XRP(10000), alice);
858 env.close();
859
860 uint32_t const netID = env.app().getNetworkIDService().getNetworkID();
861
862 // API v2 non-binary: CTID present
863 {
864 json::Value jvParams;
865 jvParams[jss::ledger_index] = "validated";
866 jvParams[jss::transactions] = true;
867 jvParams[jss::expand] = true;
868 jvParams[jss::api_version] = 2;
869 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
870 BEAST_EXPECT(jrr[jss::status] == "success");
871 auto const& txns = jrr[jss::ledger][jss::transactions];
872 BEAST_EXPECT(txns.isArray() && txns.size() > 0);
873 for (auto const& txn : txns)
874 {
875 BEAST_EXPECT(txn.isMember(jss::ctid));
876 auto const expectedCtid = rpc::encodeCTID(
877 jrr[jss::ledger][jss::ledger_index].asUInt(),
878 txn[jss::meta][sfTransactionIndex.jsonName].asUInt(),
879 netID);
880 // NOLINTBEGIN(bugprone-unchecked-optional-access)
881 if (BEAST_EXPECT(expectedCtid.has_value()))
882 BEAST_EXPECT(txn[jss::ctid] == expectedCtid.value());
883 // NOLINTEND(bugprone-unchecked-optional-access)
884 }
885 }
886
887 // API v1 non-binary: CTID present
888 {
889 json::Value jvParams;
890 jvParams[jss::ledger_index] = "validated";
891 jvParams[jss::transactions] = true;
892 jvParams[jss::expand] = true;
893 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
894 BEAST_EXPECT(jrr[jss::status] == "success");
895 auto const& txns = jrr[jss::ledger][jss::transactions];
896 BEAST_EXPECT(txns.isArray() && txns.size() > 0);
897 for (auto const& txn : txns)
898 {
899 BEAST_EXPECT(txn.isMember(jss::ctid));
900 }
901 }
902
903 // Binary expanded: CTID present
904 {
905 json::Value jvParams;
906 jvParams[jss::ledger_index] = "validated";
907 jvParams[jss::transactions] = true;
908 jvParams[jss::expand] = true;
909 jvParams[jss::binary] = true;
910 jvParams[jss::api_version] = 2;
911 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
912 BEAST_EXPECT(jrr[jss::status] == "success");
913 auto const& txns = jrr[jss::ledger][jss::transactions];
914 BEAST_EXPECT(txns.isArray() && txns.size() > 0);
915 for (auto const& txn : txns)
916 {
917 BEAST_EXPECT(txn.isMember(jss::ctid));
918 }
919 }
920
921 // Non-expanded: transactions are plain hash strings, no CTID
922 {
923 json::Value jvParams;
924 jvParams[jss::ledger_index] = "validated";
925 jvParams[jss::transactions] = true;
926 jvParams[jss::api_version] = 2;
927 auto const jrr = env.rpc("json", "ledger", to_string(jvParams))[jss::result];
928 BEAST_EXPECT(jrr[jss::status] == "success");
929 auto const& txns = jrr[jss::ledger][jss::transactions];
930 BEAST_EXPECT(txns.isArray() && txns.size() > 0);
931 for (auto const& txn : txns)
932 {
933 BEAST_EXPECT(txn.isString());
934 }
935 }
936 }
937
938public:
939 void
955};
956
958
959} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
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
UInt size() const
Number of values in array or object.
bool isMember(char const *key) const
Return true if the object has a member named key.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:526
virtual std::uint32_t getNetworkID() const noexcept=0
Get the configured network ID.
virtual TxQ & getTxQ()=0
virtual NetworkIDService & getNetworkIDService()=0
Metrics getMetrics(OpenView const &view) const
Returns fee metrics in reference fee level units.
Definition TxQ.cpp:2070
void testLookupLedger()
ledger RPC requests as a way to drive input options to lookupLedger.
void run() override
Runs the suite.
static std::string makeBadAddress(std::string good)
void checkErrorValue(json::Value const &jv, std::string const &err, ErrorCodeI code, std::string const &msg)
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
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
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
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
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:354
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
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
Converts to MPT Issue or STAmount.
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
T empty(T... args)
@ Null
'null' value
Definition json_value.h:26
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
Helpers for constructing Batch test transactions.
Definition batch.h:26
auto const kMptDexFlags
Definition mpt.h:47
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
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
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
std::unique_ptr< Config > noAdmin(std::unique_ptr< Config >)
adjust config so no admin ports are enabled
Definition envconfig.cpp:76
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
static UInt256 ledgerHash(LedgerHeader const &info)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ WarnRpcFieldsDeprecated
Definition ErrorCodes.h:162
@ terQUEUED
Definition TER.h:227
ErrorCodeI
Definition ErrorCodes.h:23
@ RpcLgrNotFound
Definition ErrorCodes.h:55
@ RpcInvalidParams
Definition ErrorCodes.h:67
@ RpcNoPermission
Definition ErrorCodes.h:36
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
BaseUInt< 256 > UInt256
Definition base_uint.h:581
T replace(T... args)
static constexpr auto kNormalConsensusIncreasePercent
Definition Constants.h:138
static constexpr auto kMinimumTxnInLedgerStandalone
Definition Constants.h:137
static constexpr auto kTransactionQueue
Definition Constants.h:66
Set the sequence number on a JTx.
Definition seq.h:16
T to_string(T... args)