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