xrpld
Loading...
Searching...
No Matches
NoRippleCheck_test.cpp
1#include <test/jtx/Env.h>
2#include <test/jtx/amount.h>
3#include <test/jtx/balance.h> // IWYU pragma: keep
4#include <test/jtx/envconfig.h>
5#include <test/jtx/fee.h>
6#include <test/jtx/flags.h>
7#include <test/jtx/pay.h>
8#include <test/jtx/seq.h>
9#include <test/jtx/sig.h>
10#include <test/jtx/tags.h>
11#include <test/jtx/trust.h>
12
13#include <xrpld/app/misc/TxQ.h>
14#include <xrpld/rpc/detail/Tuning.h>
15
16#include <xrpl/basics/DecayingSample.h>
17#include <xrpl/beast/clock/abstract_clock.h>
18#include <xrpl/beast/net/IPEndpoint.h>
19#include <xrpl/beast/unit_test/suite.h>
20#include <xrpl/json/json_value.h>
21#include <xrpl/json/to_string.h>
22#include <xrpl/protocol/ErrorCodes.h>
23#include <xrpl/protocol/SecretKey.h>
24#include <xrpl/protocol/TxFlags.h>
25#include <xrpl/protocol/jss.h>
26#include <xrpl/protocol/tokens.h>
27#include <xrpl/resource/ResourceManager.h>
28#include <xrpl/resource/detail/Entry.h>
29#include <xrpl/resource/detail/Tuning.h>
30
31#include <chrono>
32#include <string>
33
34namespace xrpl {
35
37{
38 void
40 {
41 testcase("Bad input to noripple_check");
42
43 using namespace test::jtx;
44 Env env{*this};
45
46 auto const alice = Account{"alice"};
47 env.fund(XRP(10000), alice);
48 env.close();
49
50 { // missing account field
51 auto const result = env.rpc("json", "noripple_check", "{}")[jss::result];
52 BEAST_EXPECT(result[jss::error] == "invalidParams");
53 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
54 BEAST_EXPECT(result[jss::error_message] == "Missing field 'account'.");
55 }
56
57 { // missing role field
58 json::Value params;
59 params[jss::account] = alice.human();
60 auto const result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
61 BEAST_EXPECT(result[jss::error] == "invalidParams");
62 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
63 BEAST_EXPECT(result[jss::error_message] == "Missing field 'role'.");
64 }
65
66 // test account non-string
67 {
68 auto const testInvalidAccountParam = [&](auto const& param) {
69 json::Value params;
70 params[jss::account] = param;
71 params[jss::role] = "user";
72 auto jrr = env.rpc("json", "noripple_check", to_string(params))[jss::result];
73 BEAST_EXPECT(jrr[jss::error] == "invalidParams");
74 BEAST_EXPECT(jrr[jss::error_code] == RpcInvalidParams);
75 BEAST_EXPECT(jrr[jss::error_message] == "Invalid field 'account'.");
76 };
77
78 testInvalidAccountParam(1);
79 testInvalidAccountParam(1.1);
80 testInvalidAccountParam(true);
81 testInvalidAccountParam(json::Value(json::ValueType::Null));
82 testInvalidAccountParam(json::Value(json::ValueType::Object));
83 testInvalidAccountParam(json::Value(json::ValueType::Array));
84 }
85
86 { // invalid role field
87 json::Value params;
88 params[jss::account] = alice.human();
89 params[jss::role] = "not_a_role";
90 auto const result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
91 BEAST_EXPECT(result[jss::error] == "invalidParams");
92 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
93 BEAST_EXPECT(result[jss::error_message] == "Invalid field 'role'.");
94 }
95
96 { // invalid limit
97 json::Value params;
98 params[jss::account] = alice.human();
99 params[jss::role] = "user";
100 params[jss::limit] = -1;
101 auto const result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
102 BEAST_EXPECT(result[jss::error] == "invalidParams");
103 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
104 BEAST_EXPECT(
105 result[jss::error_message] == "Invalid field 'limit', not unsigned integer.");
106 }
107
108 { // invalid ledger (hash)
109 json::Value params;
110 params[jss::account] = alice.human();
111 params[jss::role] = "user";
112 params[jss::ledger_hash] = 1;
113 auto const result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
114 BEAST_EXPECT(result[jss::error] == "invalidParams");
115 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
116 BEAST_EXPECT(
117 result[jss::error_message] == "Invalid field 'ledger_hash', not hex string.");
118 }
119
120 { // account not found
121 json::Value params;
122 params[jss::account] = Account{"nobody"}.human();
123 params[jss::role] = "user";
124 params[jss::ledger] = "current";
125 auto const result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
126 BEAST_EXPECT(result[jss::error] == "actNotFound");
127 BEAST_EXPECT(result[jss::error_code] == RpcActNotFound);
128 BEAST_EXPECT(result[jss::error_message] == "Account not found.");
129 }
130
131 { // passing an account private key will cause
132 // parsing as a seed to fail
133 json::Value params;
134 params[jss::account] = toBase58(TokenType::NodePrivate, alice.sk());
135 params[jss::role] = "user";
136 params[jss::ledger] = "current";
137 params[jss::transactions] = true;
138 auto const result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
139 BEAST_EXPECT(result[jss::error] == "actMalformed");
140 BEAST_EXPECT(result[jss::error_code] == RpcActMalformed);
141 BEAST_EXPECT(result[jss::error_message] == "Account malformed.");
142 // The changelog promises malformed-account responses carry
143 // neither `transactions` nor any ledger metadata.
144 BEAST_EXPECT(!result.isMember(jss::transactions));
145 BEAST_EXPECT(!result.isMember(jss::ledger_hash));
146 BEAST_EXPECT(!result.isMember(jss::ledger_index));
147 BEAST_EXPECT(!result.isMember(jss::validated));
148 }
149
150 {
151 // ledger and ledger_hash are included
152 json::Value params;
153 params[jss::account] = Account{"nobody"}.human();
154 params[jss::role] = "user";
155 params[jss::ledger] = "current";
156 params[jss::ledger_hash] = "ABCDEF";
157 auto const result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
158 BEAST_EXPECT(result[jss::error] == "invalidParams");
159 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
160 BEAST_EXPECT(
161 result[jss::error_message] ==
162 "Exactly one of 'ledger', 'ledger_hash', or 'ledger_index' can "
163 "be specified.");
164 }
165
166 {
167 // invalid ledger
168 json::Value params;
169 params[jss::account] = Account{"nobody"}.human();
170 params[jss::role] = "user";
171 params[jss::ledger] = json::ValueType::Object;
172 auto const result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
173 BEAST_EXPECT(result[jss::error] == "invalidParams");
174 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
175 BEAST_EXPECT(
176 result[jss::error_message] == "Invalid field 'ledger', not string or number.");
177 }
178 }
179
180 void
181 testBasic(bool user, bool problems)
182 {
183 testcase << "Request noripple_check for " << (user ? "user" : "gateway") << " role, expect"
184 << (problems ? "" : " no") << " problems";
185
186 using namespace test::jtx;
187 Env env{*this};
188
189 auto const gw = Account{"gw"};
190 auto const alice = Account{"alice"};
191
192 env.fund(XRP(10000), gw, alice);
193 if ((user && problems) || (!user && !problems))
194 {
195 env(fset(alice, asfDefaultRipple));
196 env(trust(alice, gw["USD"](100)));
197 }
198 else
199 {
200 env(fclear(alice, asfDefaultRipple));
201 env(trust(alice, gw["USD"](100), gw, tfSetNoRipple));
202 }
203 env.close();
204
205 json::Value params;
206 params[jss::account] = alice.human();
207 params[jss::role] = (user ? "user" : "gateway");
208 params[jss::ledger] = "current";
209 auto result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
210
211 auto const pa = result["problems"];
212 if (!BEAST_EXPECT(pa.isArray()))
213 return;
214
215 BEAST_EXPECT(!result.isMember(jss::transactions));
216 if (problems)
217 {
218 if (!BEAST_EXPECT(pa.size() == 2))
219 return;
220
221 if (user)
222 {
223 BEAST_EXPECT(pa[0u].asString().starts_with("You appear to have set"));
224 BEAST_EXPECT(pa[1u].asString().starts_with("You should probably set"));
225 }
226 else
227 {
228 BEAST_EXPECT(pa[0u].asString().starts_with("You should immediately set"));
229 BEAST_EXPECT(pa[1u].asString().starts_with("You should clear"));
230 }
231 }
232 else
233 {
234 BEAST_EXPECT(pa.size() == 0);
235 }
236
237 // now make a second request asking for the relevant transactions this
238 // time.
239 params[jss::transactions] = true;
240 result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
241
242 auto const txs = result[jss::transactions];
243 if (problems)
244 {
245 if (!BEAST_EXPECT(result[jss::transactions].isArray()))
246 return;
247 if (!BEAST_EXPECT(txs.size() == (user ? 1 : 2)))
248 return;
249
250 if (!user)
251 {
252 BEAST_EXPECT(txs[0u][jss::Account] == alice.human());
253 BEAST_EXPECT(txs[0u][jss::TransactionType] == jss::AccountSet);
254 }
255
256 BEAST_EXPECT(result[jss::transactions][txs.size() - 1][jss::Account] == alice.human());
257 BEAST_EXPECT(
258 result[jss::transactions][txs.size() - 1][jss::TransactionType] == jss::TrustSet);
259 BEAST_EXPECT(
260 result[jss::transactions][txs.size() - 1][jss::LimitAmount] ==
261 gw["USD"](100).value().getJson(JsonOptions::Values::None));
262 }
263 else
264 {
265 BEAST_EXPECT(txs.size() == 0);
266 }
267 }
268
269public:
270 void
271 run() override
272 {
273 testBadInput();
274 for (auto const user : {true, false})
275 {
276 for (auto const problem : {true, false})
277 testBasic(user, problem);
278 }
279 }
280};
281
283{
284 void
285 testLimits(bool admin)
286 {
287 testcase << "Check limits in returned data, " << (admin ? "admin" : "non-admin");
288
289 using namespace test::jtx;
290
291 Env env{*this, admin ? envconfig() : envconfig(noAdmin)};
292
293 auto const alice = Account{"alice"};
294 env.fund(XRP(100000), alice);
295 env(fset(alice, asfDefaultRipple));
296 env.close();
297
298 auto const checkBalance = [&env] {
299 // this is endpoint drop prevention. Non admin ports will drop
300 // requests if they are coming too fast, so we manipulate the
301 // resource manager here to reset the endpoint balance (for
302 // localhost) if we get too close to the drop limit. It would
303 // be better if we could add this functionality to Env somehow
304 // or otherwise disable endpoint charging for certain test
305 // cases.
306 using namespace xrpl::resource;
307 using namespace std::chrono;
308 using namespace beast::ip;
309 auto c = env.app().getResourceManager().newInboundEndpoint(
311
312 // if we go above the warning threshold, reset
313 if (c.balance() > kWarningThreshold)
314 {
316 c.entry().localBalance =
318 }
319 };
320
321 for (auto i = 0; i < xrpl::rpc::tuning::kNoRippleCheck.rmax + 5; ++i)
322 {
323 if (!admin)
324 checkBalance();
325
326 auto const& txq = env.app().getTxQ();
327 auto const gw = Account{"gw" + std::to_string(i)};
328 env.memoize(gw);
329 auto const baseFee = env.current()->fees().base;
330 env(pay(env.master, gw, XRP(1000)),
331 Seq(kAutofill),
332 Fee(toDrops(txq.getMetrics(*env.current()).openLedgerFeeLevel, baseFee) + 1),
333 Sig(kAutofill));
334 env(fset(gw, asfDefaultRipple),
335 Seq(kAutofill),
336 Fee(toDrops(txq.getMetrics(*env.current()).openLedgerFeeLevel, baseFee) + 1),
337 Sig(kAutofill));
338 env(trust(alice, gw["USD"](10)),
339 Fee(toDrops(txq.getMetrics(*env.current()).openLedgerFeeLevel, baseFee) + 1));
340 env.close();
341 }
342
343 // default limit value
344 json::Value params;
345 params[jss::account] = alice.human();
346 params[jss::role] = "user";
347 params[jss::ledger] = "current";
348 auto result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
349
350 BEAST_EXPECT(result["problems"].size() == 301);
351
352 // one below minimum
353 params[jss::limit] = 9;
354 result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
355 BEAST_EXPECT(result["problems"].size() == (admin ? 10 : 11));
356
357 // at minimum
358 params[jss::limit] = 10;
359 result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
360 BEAST_EXPECT(result["problems"].size() == 11);
361
362 // at max
363 params[jss::limit] = 400;
364 result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
365 BEAST_EXPECT(result["problems"].size() == 401);
366
367 // at max+1
368 params[jss::limit] = 401;
369 result = env.rpc("json", "noripple_check", to_string(params))[jss::result];
370 BEAST_EXPECT(result["problems"].size() == (admin ? 402 : 401));
371 }
372
373public:
374 void
375 run() override
376 {
377 for (auto const admin : {true, false})
378 testLimits(admin);
379 }
380};
381
383
384// These tests that deal with limit amounts are slow because of the
385// offer/account setup, so making them manual -- the additional coverage
386// provided by them is minimal
387
389
390} // namespace xrpl
Abstract interface to a clock.
static Endpoint fromString(std::string const &s)
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
Sampling function using exponential decay to provide a continuous value.
void run() override
Runs the suite.
void run() override
Runs the suite.
void testBasic(bool user, bool problems)
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
@ Null
'null' value
Definition json_value.h:26
static constexpr auto kWarningThreshold
Tunable constants.
static constexpr LimitRange kNoRippleCheck
Limits for the no_ripple_check command.
The api_version numbers this server serves.
Definition ApiVersion.h:26
char const * getEnvLocalhostAddr()
Definition envconfig.h:16
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcActNotFound
Definition ErrorCodes.h:53
@ RpcActMalformed
Definition ErrorCodes.h:73
@ RpcInvalidParams
Definition ErrorCodes.h:67
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
XRPAmount toDrops(FeeLevel< T > const &level, XRPAmount baseFee)
Definition TxQ.h:1011
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
BEAST_DEFINE_TESTSUITE_MANUAL_PRIO(NoRippleCheckLimits, rpc, xrpl, 1)
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
T size(T... args)
T to_string(T... args)