xrpld
Loading...
Searching...
No Matches
LedgerRequest_test.cpp
1
2
3#include <test/jtx/Account.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/envconfig.h>
7
8#include <xrpl/beast/unit_test/suite.h>
9#include <xrpl/json/json_value.h>
10#include <xrpl/protocol/ApiVersion.h>
11#include <xrpl/protocol/ErrorCodes.h>
12#include <xrpl/protocol/jss.h>
13
14#include <memory>
15#include <string>
16#include <utility>
17
18namespace xrpl::rpc {
19
21{
22 static constexpr char const* kHash1 =
23 "8520BB17C478632B910814803743457CFDB94668B7B74CABB9A4511C03CC66AA";
24 static constexpr char const* kAccountHash1 =
25 "3BA579989E7A881E38F529962AA72740EEF951623012F4D4AADAB476FB8AA84B";
26
27 static constexpr char const* kZeroHASH =
28 "0000000000000000000000000000000000000000000000000000000000000000";
29
30public:
31 void
33 {
34 using namespace test::jtx;
35
36 Env env(*this);
37
38 env.close();
39 env.close();
40 BEAST_EXPECT(env.current()->header().seq == 5);
41
42 {
43 // arbitrary text is converted to 0.
44 auto const result = env.rpc("ledger_request", "arbitrary_text");
45 BEAST_EXPECT(
46 result[jss::result][jss::error] == "invalidParams" &&
47 result[jss::result][jss::error_code] == RpcInvalidParams &&
48 result[jss::result][jss::error_message] == "Ledger index too small");
49 }
50
51 {
52 auto const result = env.rpc("ledger_request", "-1");
53 BEAST_EXPECT(
54 result[jss::result][jss::error] == "invalidParams" &&
55 result[jss::result][jss::error_code] == RpcInvalidParams &&
56 result[jss::result][jss::error_message] == "Ledger index too small");
57 }
58
59 {
60 auto const result = env.rpc("ledger_request", "0");
61 BEAST_EXPECT(
62 result[jss::result][jss::error] == "invalidParams" &&
63 result[jss::result][jss::error_code] == RpcInvalidParams &&
64 result[jss::result][jss::error_message] == "Ledger index too small");
65 }
66
67 {
68 auto const result = env.rpc("ledger_request", "1");
69 BEAST_EXPECT(
70 !rpc::containsError(result[jss::result]) &&
71 result[jss::result][jss::ledger_index] == 1 &&
72 result[jss::result].isMember(jss::ledger));
73 BEAST_EXPECT(
74 result[jss::result][jss::ledger].isMember(jss::ledger_hash) &&
75 result[jss::result][jss::ledger][jss::ledger_hash].isString());
76 }
77
78 {
79 auto const result = env.rpc("ledger_request", "2");
80 BEAST_EXPECT(
81 !rpc::containsError(result[jss::result]) &&
82 result[jss::result][jss::ledger_index] == 2 &&
83 result[jss::result].isMember(jss::ledger));
84 BEAST_EXPECT(
85 result[jss::result][jss::ledger].isMember(jss::ledger_hash) &&
86 result[jss::result][jss::ledger][jss::ledger_hash].isString());
87 }
88
89 {
90 auto const result = env.rpc("ledger_request", "3");
91 BEAST_EXPECT(
92 !rpc::containsError(result[jss::result]) &&
93 result[jss::result][jss::ledger_index] == 3 &&
94 result[jss::result].isMember(jss::ledger));
95 BEAST_EXPECT(
96 result[jss::result][jss::ledger].isMember(jss::ledger_hash) &&
97 result[jss::result][jss::ledger][jss::ledger_hash].isString());
98
99 auto const ledgerHash = result[jss::result][jss::ledger][jss::ledger_hash].asString();
100
101 {
102 auto const r = env.rpc("ledger_request", ledgerHash);
103 BEAST_EXPECT(
104 !rpc::containsError(r[jss::result]) && r[jss::result][jss::ledger_index] == 3 &&
105 r[jss::result].isMember(jss::ledger));
106 BEAST_EXPECT(
107 r[jss::result][jss::ledger].isMember(jss::ledger_hash) &&
108 r[jss::result][jss::ledger][jss::ledger_hash] == ledgerHash);
109 }
110 }
111
112 {
113 std::string const ledgerHash(64, 'q');
114
115 auto const result = env.rpc("ledger_request", ledgerHash);
116
117 BEAST_EXPECT(
118 result[jss::result][jss::error] == "invalidParams" &&
119 result[jss::result][jss::error_code] == RpcInvalidParams &&
120 result[jss::result][jss::error_message] ==
121 "Invalid field 'ledger_hash', not hex string.");
122 }
123
124 {
125 std::string const ledgerHash(64, '1');
126
127 auto const result = env.rpc("ledger_request", ledgerHash);
128
129 BEAST_EXPECT(
130 !rpc::containsError(result[jss::result]) &&
131 result[jss::result][jss::have_header] == false);
132 }
133
134 {
135 auto const result = env.rpc("ledger_request", "4");
136 BEAST_EXPECT(
137 result[jss::result][jss::error] == "invalidParams" &&
138 result[jss::result][jss::error_code] == RpcInvalidParams &&
139 result[jss::result][jss::error_message] == "Ledger index too large");
140 }
141
142 {
143 auto const result = env.rpc("ledger_request", "5");
144 BEAST_EXPECT(
145 result[jss::result][jss::error] == "invalidParams" &&
146 result[jss::result][jss::error_code] == RpcInvalidParams &&
147 result[jss::result][jss::error_message] == "Ledger index too large");
148 }
149 }
150
151 void
153 {
154 using namespace test::jtx;
155
156 auto cfg = envconfig();
157 cfg->fees.referenceFee = 10;
158 Env env{*this, std::move(cfg), FeatureBitset{}}; // the hashes being checked below
159 // assume no amendments
160 Account const gw{"gateway"};
161 auto const usd = gw["USD"];
162 env.fund(XRP(100000), gw);
163 env.close();
164
165 env.memoize("bob");
166 env.fund(XRP(1000), "bob");
167 env.close();
168
169 env.memoize("alice");
170 env.fund(XRP(1000), "alice");
171 env.close();
172
173 env.memoize("carol");
174 env.fund(XRP(1000), "carol");
175 env.close();
176
177 auto result = env.rpc("ledger_request", "1")[jss::result];
178 BEAST_EXPECT(result[jss::ledger][jss::ledger_index] == "1");
179 BEAST_EXPECT(result[jss::ledger][jss::total_coins] == "100000000000000000");
180 BEAST_EXPECT(result[jss::ledger][jss::closed] == true);
181 BEAST_EXPECT(result[jss::ledger][jss::ledger_hash] == kHash1);
182 BEAST_EXPECT(result[jss::ledger][jss::parent_hash] == kZeroHASH);
183 BEAST_EXPECT(result[jss::ledger][jss::account_hash] == kAccountHash1);
184 BEAST_EXPECT(result[jss::ledger][jss::transaction_hash] == kZeroHASH);
185
186 result = env.rpc("ledger_request", "2")[jss::result];
187 static constexpr char const* kHash2 =
188 "81B485208CBB93989F2D675F61D385CE9E15A03C68B8CEF1B071C0DD5C5781E1";
189 BEAST_EXPECT(result[jss::ledger][jss::ledger_index] == "2");
190 BEAST_EXPECT(result[jss::ledger][jss::total_coins] == "100000000000000000");
191 BEAST_EXPECT(result[jss::ledger][jss::closed] == true);
192 BEAST_EXPECT(result[jss::ledger][jss::ledger_hash] == kHash2);
193 BEAST_EXPECT(result[jss::ledger][jss::parent_hash] == kHash1);
194 BEAST_EXPECT(
195 result[jss::ledger][jss::account_hash] ==
196 "A2C367B31313EACE2670747A0CC4947FB79055795FEEA1E730E31756E73E012C");
197 BEAST_EXPECT(result[jss::ledger][jss::transaction_hash] == kZeroHASH);
198
199 result = env.rpc("ledger_request", "3")[jss::result];
200 static constexpr char const* kHash3 =
201 "925EAD8F55A13B5138DB7E63122066305E3931431364A2F311A9F646D373D631";
202 BEAST_EXPECT(result[jss::ledger][jss::ledger_index] == "3");
203 BEAST_EXPECT(result[jss::ledger][jss::total_coins] == "99999999999999980");
204 BEAST_EXPECT(result[jss::ledger][jss::closed] == true);
205 BEAST_EXPECT(result[jss::ledger][jss::ledger_hash] == kHash3);
206 BEAST_EXPECT(result[jss::ledger][jss::parent_hash] == kHash2);
207 BEAST_EXPECT(
208 result[jss::ledger][jss::account_hash] ==
209 "400903DC93B7A8CF9ABC74216A900C187C8D3CAF1B40C1688D1965C73EF79C39");
210 BEAST_EXPECT(
211 result[jss::ledger][jss::transaction_hash] ==
212 "CBD7F0948EBFA2241DE4EA627939A0FFEE6B80A90FE09C42C825DA546E9B73FF");
213
214 result = env.rpc("ledger_request", "4")[jss::result];
215 static constexpr char const* kHash4 =
216 "93EDC738C6FC00AC5992BAE98274F8B214719730C4A938FE177B4DDEC497C670";
217 BEAST_EXPECT(result[jss::ledger][jss::ledger_index] == "4");
218 BEAST_EXPECT(result[jss::ledger][jss::total_coins] == "99999999999999960");
219 BEAST_EXPECT(result[jss::ledger][jss::closed] == true);
220 BEAST_EXPECT(result[jss::ledger][jss::ledger_hash] == kHash4);
221 BEAST_EXPECT(result[jss::ledger][jss::parent_hash] == kHash3);
222 BEAST_EXPECT(
223 result[jss::ledger][jss::account_hash] ==
224 "5FC723ED8E323EDB2E37B4CE63070CF7D722F7BD35F9337BD55B6271FE146511");
225 BEAST_EXPECT(
226 result[jss::ledger][jss::transaction_hash] ==
227 "9BBDDBF926100DFFF364E16268F544B19F5B9BC6ECCBBC104F98D13FA9F3BC35");
228
229 result = env.rpc("ledger_request", "5")[jss::result];
230 static constexpr char const* kHash5 =
231 "0A069F4B2CE5D5892EBF7C1BB1F7C3A023BF67B7309D10CA5C5607F959DF4C2F";
232 BEAST_EXPECT(result[jss::ledger][jss::ledger_index] == "5");
233 BEAST_EXPECT(result[jss::ledger][jss::total_coins] == "99999999999999940");
234 BEAST_EXPECT(result[jss::ledger][jss::closed] == true);
235 BEAST_EXPECT(result[jss::ledger][jss::ledger_hash] == kHash5);
236 BEAST_EXPECT(result[jss::ledger][jss::parent_hash] == kHash4);
237 BEAST_EXPECT(
238 result[jss::ledger][jss::account_hash] ==
239 "0941305F3FD175CD3F956BF9840D811C937A5DD8C443019621F58496796B6FD4");
240 BEAST_EXPECT(
241 result[jss::ledger][jss::transaction_hash] ==
242 "8FE8592EF22FBC2E8C774C7A1ED76AA3FCE64BED17D748CBA9AFDF7072FE36C7");
243
244 result = env.rpc("ledger_request", "6")[jss::result];
245 BEAST_EXPECT(result[jss::status] == "error");
246 BEAST_EXPECT(result[jss::error] == "invalidParams");
247 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
248 BEAST_EXPECT(result[jss::error_message] == "Ledger index too large");
249 }
250
251 void
252 testBadInput(unsigned apiVersion)
253 {
254 using namespace test::jtx;
255 Env env{*this};
256 Account const gw{"gateway"};
257 auto const usd = gw["USD"];
258 env.fund(XRP(100000), gw);
259 env.close();
260
261 {
262 json::Value jvParams;
263 jvParams[jss::ledger_hash] =
264 "AB868A6CFEEC779C2FF845C0AF00A642259986AF40C01976A7F842B6918936"
265 "C7";
266 jvParams[jss::ledger_index] = "1";
267 auto const result =
268 env.rpc("json", "ledger_request", jvParams.toStyledString())[jss::result];
269 BEAST_EXPECT(result[jss::status] == "error");
270 BEAST_EXPECT(result[jss::error] == "invalidParams");
271 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
272 BEAST_EXPECT(
273 result[jss::error_message] ==
274 "Exactly one of 'ledger_hash' or 'ledger_index' can be "
275 "specified.");
276 }
277
278 {
279 json::Value jvParams;
280 jvParams[jss::ledger_index] = "index";
281 auto const result =
282 env.rpc("json", "ledger_request", jvParams.toStyledString())[jss::result];
283 BEAST_EXPECT(result[jss::status] == "error");
284 BEAST_EXPECT(result[jss::error] == "invalidParams");
285 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
286 BEAST_EXPECT(result[jss::error_message] == "Invalid field 'ledger_index', not number.");
287 }
288
289 // the purpose in this test is to force the ledger expiration/out of
290 // date check to trigger
291 env.timeKeeper().adjustCloseTime(Weeks{3});
292 auto const result = env.rpc(apiVersion, "ledger_request", "1")[jss::result];
293 BEAST_EXPECT(result[jss::status] == "error");
294 if (apiVersion == 1)
295 {
296 BEAST_EXPECT(result[jss::error] == "noCurrent");
297 BEAST_EXPECT(result[jss::error_code] == RpcNoCurrent);
298 BEAST_EXPECT(result[jss::error_message] == "Current ledger is unavailable.");
299 }
300 else
301 {
302 BEAST_EXPECT(result[jss::error] == "notSynced");
303 BEAST_EXPECT(result[jss::error_code] == RpcNotSynced);
304 BEAST_EXPECT(result[jss::error_message] == "Not synced to the network.");
305 }
306 }
307
308 void
310 {
311 using namespace test::jtx;
312 using namespace std::chrono_literals;
313 Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
314 cfg->fees.referenceFee = 10;
315 cfg->nodeSize = 0;
316 return cfg;
317 })};
318 Account const gw{"gateway"};
319 auto const usd = gw["USD"];
320 env.fund(XRP(100000), gw);
321
322 int const maxLimit = 256;
323
324 for (auto i = 0; i < maxLimit + 10; i++)
325 {
326 Account const bob{std::string("bob") + std::to_string(i)};
327 env.fund(XRP(1000), bob);
328 env.close();
329 }
330
331 auto result = env.rpc("ledger_request", "1")[jss::result];
332 BEAST_EXPECT(result[jss::ledger][jss::ledger_index] == "1");
333 BEAST_EXPECT(result[jss::ledger][jss::total_coins] == "100000000000000000");
334 BEAST_EXPECT(result[jss::ledger][jss::closed] == true);
335 BEAST_EXPECT(result[jss::ledger][jss::ledger_hash] == kHash1);
336 BEAST_EXPECT(result[jss::ledger][jss::parent_hash] == kZeroHASH);
337 BEAST_EXPECT(result[jss::ledger][jss::account_hash] == kAccountHash1);
338 BEAST_EXPECT(result[jss::ledger][jss::transaction_hash] == kZeroHASH);
339 }
340
341 void
343 {
344 using namespace test::jtx;
345 Env env{*this, envconfig(noAdmin)};
346 Account const gw{"gateway"};
347 auto const usd = gw["USD"];
348 env.fund(XRP(100000), gw);
349
350 env.setRetries(0);
351 auto const result = env.rpc("ledger_request", "1")[jss::result];
352 // The current HTTP/S ServerHandler returns an HTTP 403 error code here
353 // rather than a noPermission JSON error. The JSONRPCClient just eats
354 // that error and returns an null result.
355 BEAST_EXPECT(result.type() == json::ValueType::Null);
356 }
357
358 void
359 run() override
360 {
363 forAllApiVersions([this](unsigned apiVersion) { testBadInput(apiVersion); });
365 testNonAdmin();
366 }
367};
368
370
371} // namespace xrpl::rpc
A testsuite class.
Definition suite.h:52
Represents a JSON value.
Definition json_value.h:121
std::string toStyledString() const
void run() override
Runs the suite.
static constexpr char const * kHash1
static constexpr char const * kAccountHash1
void testBadInput(unsigned apiVersion)
static constexpr char const * kZeroHASH
@ Null
'null' value
Definition json_value.h:26
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.
BEAST_DEFINE_TESTSUITE(AccountLines, rpc, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcNotSynced
Definition ErrorCodes.h:50
@ RpcInvalidParams
Definition ErrorCodes.h:67
@ RpcNoCurrent
Definition ErrorCodes.h:48
void forAllApiVersions(Fn const &fn, Args &&... args)
Definition ApiVersion.h:159
std::chrono::duration< int, std::ratio_multiply< Days::period, std::ratio< 7 > > > Weeks
Definition chrono.h:22
T to_string(T... args)