xrpld
Loading...
Searching...
No Matches
TransactionEntry_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/offer.h>
6#include <test/jtx/pay.h>
7
8#include <xrpld/core/Config.h>
9
10#include <xrpl/basics/base_uint.h>
11#include <xrpl/basics/contract.h>
12#include <xrpl/beast/unit_test/suite.h>
13#include <xrpl/json/json_reader.h>
14#include <xrpl/json/json_value.h>
15#include <xrpl/json/to_string.h>
16#include <xrpl/protocol/ApiVersion.h>
17#include <xrpl/protocol/ErrorCodes.h>
18#include <xrpl/protocol/jss.h>
19
20#include <memory>
21#include <stdexcept>
22#include <string>
23
24namespace xrpl {
25
27{
28 void
30 {
31 testcase("Invalid request params");
32 using namespace test::jtx;
33 Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
34 cfg->fees.referenceFee = 10;
35 return cfg;
36 })};
37
38 {
39 // no params
40 auto const result = env.client().invoke("transaction_entry", {})[jss::result];
41 BEAST_EXPECT(result[jss::status] == "error");
42 BEAST_EXPECT(result[jss::error] == "fieldNotFoundTransaction");
43 BEAST_EXPECT(!result.isMember(jss::error_code));
44 BEAST_EXPECT(result[jss::error_message].isNull());
45 }
46
47 {
49 params[jss::ledger] = 20;
50 auto const result = env.client().invoke("transaction_entry", params)[jss::result];
51 BEAST_EXPECT(result[jss::status] == "error");
52 BEAST_EXPECT(result[jss::error] == "lgrNotFound");
53 BEAST_EXPECT(result[jss::error_code] == RpcLgrNotFound);
54 BEAST_EXPECT(result[jss::error_message] == "ledgerNotFound");
55 }
56
57 {
59 params[jss::ledger] = "current";
60 params[jss::tx_hash] = "DEADBEEF";
61 auto const result = env.client().invoke("transaction_entry", params)[jss::result];
62 BEAST_EXPECT(result[jss::status] == "error");
63 BEAST_EXPECT(result[jss::error] == "notYetImplemented");
64 BEAST_EXPECT(!result.isMember(jss::error_code));
65 BEAST_EXPECT(result[jss::error_message].isNull());
66 }
67
68 {
69 // No ledger selects the current one, not any ledger.
71 params[jss::tx_hash] =
72 "E2FE8D4AF3FCC3944DDF6CD8CDDC5E3F0AD50863EF8919AFEF10CB6408CD4D05";
73 auto const result = env.client().invoke("transaction_entry", params)[jss::result];
74 BEAST_EXPECT(result[jss::status] == "error");
75 BEAST_EXPECT(result[jss::error] == "notYetImplemented");
76 BEAST_EXPECT(!result.isMember(jss::error_code));
77 BEAST_EXPECT(result[jss::error_message].isNull());
78 BEAST_EXPECT(result.isMember(jss::ledger_current_index));
79 BEAST_EXPECT(!result.isMember(jss::ledger_hash));
80 BEAST_EXPECT(result[jss::validated] == false);
81 }
82
83 {
85 params[jss::ledger] = "closed";
86 params[jss::tx_hash] = "DEADBEEF";
87 auto const result = env.client().invoke("transaction_entry", params)[jss::result];
88 BEAST_EXPECT(!result[jss::ledger_hash].asString().empty());
89 BEAST_EXPECT(result[jss::status] == "error");
90 BEAST_EXPECT(result[jss::error] == "malformedRequest");
91 BEAST_EXPECT(!result.isMember(jss::error_code));
92 BEAST_EXPECT(result[jss::error_message].isNull());
93 }
94
95 for (auto const type : {json::ValueType::Object, json::ValueType::Array})
96 {
98 params[jss::ledger] = "closed";
99 params[jss::tx_hash] = json::Value{type};
100 auto const result = env.client().invoke("transaction_entry", params)[jss::result];
101 BEAST_EXPECT(result[jss::status] == "error");
102 BEAST_EXPECT(result[jss::error] == "malformedRequest");
103 BEAST_EXPECT(!result.isMember(jss::error_code));
104 BEAST_EXPECT(result[jss::error_message].isNull());
105 }
106
107 std::string const txHash{
108 "E2FE8D4AF3FCC3944DDF6CD8CDDC5E3F0AD50863EF8919AFEF10CB6408CD4D05"};
109
110 // Command line format
111 {
112 // No arguments
113 json::Value const result{env.rpc("transaction_entry")};
114 BEAST_EXPECT(result[jss::ledger_hash].asString().empty());
115 BEAST_EXPECT(result[jss::status] == "error");
116 BEAST_EXPECT(result[jss::error] == "badSyntax");
117 BEAST_EXPECT(result[jss::error_code] == RpcBadSyntax);
118 BEAST_EXPECT(result[jss::error_message] == "Syntax error.");
119 }
120
121 {
122 // One argument
123 json::Value const result{env.rpc("transaction_entry", txHash)};
124 BEAST_EXPECT(result[jss::status] == "error");
125 BEAST_EXPECT(result[jss::error] == "badSyntax");
126 BEAST_EXPECT(result[jss::error_code] == RpcBadSyntax);
127 BEAST_EXPECT(result[jss::error_message] == "Syntax error.");
128 }
129
130 {
131 // First argument with too few characters
132 json::Value const result{env.rpc("transaction_entry", txHash.substr(1), "closed")};
133 BEAST_EXPECT(result[jss::status] == "error");
134 BEAST_EXPECT(result[jss::error] == "invalidParams");
135 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
136 BEAST_EXPECT(result[jss::error_message] == "Invalid parameters.");
137 }
138
139 {
140 // First argument with too many characters
141 json::Value const result{env.rpc("transaction_entry", txHash + "A", "closed")};
142 BEAST_EXPECT(result[jss::status] == "error");
143 BEAST_EXPECT(result[jss::error] == "invalidParams");
144 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
145 BEAST_EXPECT(result[jss::error_message] == "Invalid parameters.");
146 }
147
148 {
149 // Second argument not valid
150 json::Value const result{env.rpc("transaction_entry", txHash, "closer")};
151 BEAST_EXPECT(result[jss::status] == "error");
152 BEAST_EXPECT(result[jss::error] == "invalidParams");
153 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
154 BEAST_EXPECT(result[jss::error_message] == "Invalid parameters.");
155 }
156
157 {
158 // Ledger index of 0 is not valid
159 json::Value const result{env.rpc("transaction_entry", txHash, "0")};
160 BEAST_EXPECT(result[jss::status] == "error");
161 BEAST_EXPECT(result[jss::error] == "invalidParams");
162 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
163 BEAST_EXPECT(result[jss::error_message] == "Invalid parameters.");
164 }
165
166 {
167 // Three arguments
168 json::Value const result{env.rpc("transaction_entry", txHash, "closed", "extra")};
169 BEAST_EXPECT(result[jss::status] == "error");
170 BEAST_EXPECT(result[jss::error] == "badSyntax");
171 BEAST_EXPECT(result[jss::error_code] == RpcBadSyntax);
172 BEAST_EXPECT(result[jss::error_message] == "Syntax error.");
173 }
174
175 {
176 // Valid structure, but transaction not found.
177 json::Value const result{env.rpc("transaction_entry", txHash, "closed")};
178 BEAST_EXPECT(!result[jss::result][jss::ledger_hash].asString().empty());
179 BEAST_EXPECT(result[jss::result][jss::status] == "error");
180 BEAST_EXPECT(result[jss::result][jss::error] == "transactionNotFound");
181 BEAST_EXPECT(!result[jss::result].isMember(jss::error_code));
182 BEAST_EXPECT(result[jss::result][jss::error_message].isNull());
183 }
184 }
185
186 void
187 testRequest(unsigned apiVersion)
188 {
189 testcase("Basic request API version " + std::to_string(apiVersion));
190 using namespace test::jtx;
191 Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
192 cfg->fees.referenceFee = 10;
193 return cfg;
194 })};
195
196 auto const checkTx = [this, &env, apiVersion](
197 int index,
198 std::string const txhash,
199 std::string const expectedJson = "",
200 std::string const expectedLedgerHash = "",
201 std::string const closeTimeIso = "") {
202 // first request using ledger_index to lookup
203 json::Value const resIndex{
204 [&env, index, &txhash, apiVersion] {
206 params[jss::ledger_index] = index;
207 params[jss::tx_hash] = txhash;
208 params[jss::api_version] = apiVersion;
209 return env.client().invoke("transaction_entry", params)[jss::result];
210 }(),
211 };
212
213 if (!BEAST_EXPECT(resIndex.isMember(jss::tx_json)))
214 return;
215
216 BEAST_EXPECT(resIndex[jss::validated] == true);
217 BEAST_EXPECT(resIndex[jss::ledger_index] == index);
218 BEAST_EXPECT(resIndex[jss::ledger_hash] == expectedLedgerHash);
219 if (apiVersion > 1)
220 {
221 BEAST_EXPECT(resIndex[jss::hash] == txhash);
222 BEAST_EXPECT(!resIndex[jss::tx_json].isMember(jss::hash));
223 BEAST_EXPECT(!resIndex[jss::tx_json].isMember(jss::Amount));
224
225 if (BEAST_EXPECT(!closeTimeIso.empty()))
226 BEAST_EXPECT(resIndex[jss::close_time_iso] == closeTimeIso);
227 }
228 else
229 {
230 BEAST_EXPECT(resIndex[jss::tx_json][jss::hash] == txhash);
231 BEAST_EXPECT(!resIndex.isMember(jss::hash));
232 BEAST_EXPECT(!resIndex.isMember(jss::close_time_iso));
233 }
234
235 if (!expectedJson.empty())
236 {
237 json::Value expected;
238 json::Reader().parse(expectedJson, expected);
239 if (rpc::containsError(expected))
240 Throw<std::runtime_error>("Internal JSONRPC_test error. Bad test JSON.");
241
242 for (auto memberIt = expected.begin(); memberIt != expected.end(); memberIt++)
243 {
244 auto const name = memberIt.memberName();
245 if (BEAST_EXPECT(resIndex[jss::tx_json].isMember(name)))
246 {
247 auto const received = resIndex[jss::tx_json][name];
248 BEAST_EXPECTS(
249 received == *memberIt,
250 txhash + " contains \n\"" + name + "\": " //
251 + to_string(received) //
252 + " but expected " //
253 + to_string(expected));
254 }
255 }
256 }
257
258 // second request using ledger_hash to lookup and verify
259 // both responses match
260 {
262 params[jss::ledger_hash] = resIndex[jss::ledger_hash];
263 params[jss::tx_hash] = txhash;
264 params[jss::api_version] = apiVersion;
265 json::Value const resHash =
266 env.client().invoke("transaction_entry", params)[jss::result];
267 BEAST_EXPECT(resHash == resIndex);
268 }
269
270 // Use the command line form with the index.
271 json::Value const clIndex{
272 env.rpc(apiVersion, "transaction_entry", txhash, std::to_string(index)),
273 };
274 BEAST_EXPECT(clIndex["result"] == resIndex);
275
276 // Use the command line form with the ledger_hash.
277 json::Value const clHash{
278 env.rpc(
279 apiVersion, "transaction_entry", txhash, resIndex[jss::ledger_hash].asString()),
280 };
281 BEAST_EXPECT(clHash["result"] == resIndex);
282 };
283
284 Account const a1{"A1"};
285 Account const a2{"A2"};
286
287 env.fund(XRP(10000), a1);
288 auto const fund1Tx = to_string(env.tx()->getTransactionID());
289 BEAST_EXPECT(fund1Tx == "F4E9DF90D829A9E8B423FF68C34413E240D8D8BB0EFD080DF08114ED398E2506");
290
291 env.fund(XRP(10000), a2);
292 auto const fund2Tx = to_string(env.tx()->getTransactionID());
293 BEAST_EXPECT(fund2Tx == "6853CD8226A05068C951CB1F54889FF4E40C5B440DC1C5BA38F114C4E0B1E705");
294
295 env.close();
296
297 // these are actually AccountSet txs because fund does two txs and
298 // env.tx only reports the last one
299 checkTx(
300 env.closed()->seq(),
301 fund1Tx,
302 R"({
303 "Account" : "r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf",
304 "Fee" : "10",
305 "Sequence" : 3,
306 "SetFlag" : 8,
307 "SigningPubKey" : "0324CAAFA2212D2AEAB9D42D481535614AED486293E1FB1380FF070C3DD7FB4264",
308 "TransactionType" : "AccountSet",
309 "TxnSignature" : "3044022007B35E3B99460534FF6BC3A66FBBA03591C355CC38E38588968E87CCD01BE229022071A443026DE45041B55ABB1CC76812A87EA701E475BBB7E165513B4B242D3474",
310})",
311 "A1E8FDA5E13F61FBD4F965214654522C0423432565CEF9E38CE233AA776CA250",
312 "2000-01-01T00:00:10Z");
313 checkTx(
314 env.closed()->seq(),
315 fund2Tx,
316 R"({
317 "Account" : "rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD",
318 "Fee" : "10",
319 "Sequence" : 3,
320 "SetFlag" : 8,
321 "SigningPubKey" : "03CFF28E067A2CCE6CC5A598C0B845CBD3F30A7863BE9C0DD55F4960EFABCCF4D0",
322 "TransactionType" : "AccountSet",
323 "TxnSignature" : "3045022100C8857FC0759A2AC0D2F320684691A66EAD252EAED9EF88C79791BC58BFCC9D860220421722286487DD0ED6BBA626CE6FCBDD14289F7F4726870C3465A4054C2702D7",
324})",
325 "A1E8FDA5E13F61FBD4F965214654522C0423432565CEF9E38CE233AA776CA250",
326 "2000-01-01T00:00:10Z");
327
328 env.trust(a2["USD"](1000), a1);
329 // the trust tx is actually a payment since the trust method
330 // refunds fees with a payment after TrustSet..so just ignore the type
331 // in the check below
332 auto const trustTx = to_string(env.tx()->getTransactionID());
333 BEAST_EXPECT(trustTx == "C992D97D88FF444A1AB0C06B27557EC54B7F7DA28254778E60238BEA88E0C101");
334
335 env(pay(a2, a1, a2["USD"](5)));
336 auto const payTx = to_string(env.tx()->getTransactionID());
337 env.close();
338 BEAST_EXPECT(payTx == "988046D484ACE9F5F6A8C792D89C6EA2DB307B5DDA9864AEBA88E6782ABD0865");
339
340 checkTx(
341 env.closed()->seq(),
342 trustTx,
343 R"({
344 "Account" : "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
345 "DeliverMax" : "10",
346 "Destination" : "r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf",
347 "Fee" : "10",
348 "Flags" : 2147483648,
349 "Sequence" : 3,
350 "SigningPubKey" : "0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD020",
351 "TransactionType" : "Payment",
352 "TxnSignature" : "3044022033D9EBF7F02950AF2F6B13C07AEE641C8FEBDD540A338FCB9027A965A4AED35B02206E4E227DCC226A3456C0FEF953449D21645A24EB63CA0BB7C5B62470147FD1D1",
353})",
354 "1508BD19CCB23735A614431CA6E8FE9507A5B7FFF1274C651A0801EF02D0521B",
355 "2000-01-01T00:00:20Z");
356
357 checkTx(
358 env.closed()->seq(),
359 payTx,
360 R"({
361 "Account" : "rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD",
362 "DeliverMax" :
363 {
364 "currency" : "USD",
365 "issuer" : "rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD",
366 "value" : "5"
367 },
368 "Destination" : "r4nmQNH4Fhjfh6cHDbvVSsBv7KySbj4cBf",
369 "Fee" : "10",
370 "Flags" : 2147483648,
371 "Sequence" : 4,
372 "SigningPubKey" : "03CFF28E067A2CCE6CC5A598C0B845CBD3F30A7863BE9C0DD55F4960EFABCCF4D0",
373 "TransactionType" : "Payment",
374 "TxnSignature" : "30450221008A722B7F16EDB2348886E88ED4EC682AE9973CC1EE0FF37C93BB2CEC821D3EDF022059E464472031BA5E0D88A93E944B6A8B8DB3E1D5E5D1399A805F615789DB0BED",
375})",
376 "1508BD19CCB23735A614431CA6E8FE9507A5B7FFF1274C651A0801EF02D0521B",
377 "2000-01-01T00:00:20Z");
378
379 env(offer(a2, XRP(100), a2["USD"](1)));
380 auto const offerTx = to_string(env.tx()->getTransactionID());
381 BEAST_EXPECT(offerTx == "5FCC1A27A7664F82A0CC4BE5766FBBB7C560D52B93AA7B550CD33B27AEC7EFFB");
382
383 env.close();
384 checkTx(
385 env.closed()->seq(),
386 offerTx,
387 R"({
388 "Account" : "rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD",
389 "Fee" : "10",
390 "Sequence" : 5,
391 "SigningPubKey" : "03CFF28E067A2CCE6CC5A598C0B845CBD3F30A7863BE9C0DD55F4960EFABCCF4D0",
392 "TakerGets" :
393 {
394 "currency" : "USD",
395 "issuer" : "rGpeQzUWFu4fMhJHZ1Via5aqFC3A5twZUD",
396 "value" : "1"
397 },
398 "TakerPays" : "100000000",
399 "TransactionType" : "OfferCreate",
400 "TxnSignature" : "304502210093FC93ACB77B4E3DE3315441BD010096734859080C1797AB735EB47EBD541BD102205020BB1A7C3B4141279EE4C287C13671E2450EA78914EFD0C6DB2A18344CD4F2",
401})",
402 "38FB25254CAB30480E3AC819A456CBA1F3B9C0ACEF51B7320CDD3FFD3ECF61BA",
403 "2000-01-01T00:00:30Z");
404 }
405
406public:
407 void
408 run() override
409 {
410 testBadInput();
411 forAllApiVersions([this](unsigned apiVersion) { testRequest(apiVersion); });
412 }
413};
414
415BEAST_DEFINE_TESTSUITE(TransactionEntry, rpc, xrpl);
416
417} // namespace xrpl
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Unserialize a JSON document into a Value.
Definition json_reader.h:20
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Represents a JSON value.
Definition json_value.h:121
const_iterator begin() const
const_iterator end() const
bool isMember(char const *key) const
Return true if the object has a member named key.
void testRequest(unsigned apiVersion)
void run() override
Runs the suite.
T empty(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
The api_version numbers this server serves.
Definition ApiVersion.h:26
bool containsError(json::Value const &json)
Returns true if the json contains an rpc error specification.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcLgrNotFound
Definition ErrorCodes.h:55
@ RpcInvalidParams
Definition ErrorCodes.h:67
@ RpcBadSyntax
Definition ErrorCodes.h:29
void forAllApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:159
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T substr(T... args)
T to_string(T... args)