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