xrpld
Loading...
Searching...
No Matches
NoRipple_test.cpp
1
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/TestHelpers.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/balance.h> // IWYU pragma: keep
7#include <test/jtx/flags.h>
8#include <test/jtx/paths.h>
9#include <test/jtx/pay.h>
10#include <test/jtx/ter.h>
11#include <test/jtx/trust.h>
12
13#include <xrpl/beast/unit_test/suite.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/Feature.h>
19#include <xrpl/protocol/TER.h>
20#include <xrpl/protocol/TxFlags.h>
21#include <xrpl/protocol/jss.h>
22
23#include <string>
24
25namespace xrpl::test {
26
28{
29public:
30 void
32 {
33 testcase("Set and clear noripple");
34
35 using namespace jtx;
36 Env env(*this);
37
38 auto const gw = Account("gateway");
39 auto const alice = Account("alice");
40
41 env.fund(XRP(10000), gw, alice);
42
43 auto const usd = gw["USD"];
44
45 json::Value accountGw;
46 accountGw[jss::account] = gw.human();
47 json::Value accountAlice;
48 accountAlice[jss::account] = alice.human();
49
50 for (auto const setOrClear : {true, false})
51 {
52 // Create a trust line with no-ripple flag setting
53 env(trust(gw, usd(100), alice, setOrClear ? tfSetNoRipple : tfClearNoRipple));
54 env.close();
55
56 // Check no-ripple flag on sender 'gateway'
57 json::Value lines{env.rpc("json", "account_lines", to_string(accountGw))};
58 auto const& gwLine0 = lines[jss::result][jss::lines][0u];
59 BEAST_EXPECT(gwLine0[jss::no_ripple].asBool() == setOrClear);
60
61 // Check no-ripple peer flag on destination 'alice'
62 lines = env.rpc("json", "account_lines", to_string(accountAlice));
63 auto const& aline0 = lines[jss::result][jss::lines][0u];
64 BEAST_EXPECT(aline0[jss::no_ripple_peer].asBool() == setOrClear);
65 }
66 }
67
68 void
70 {
71 testcase("Set noripple on a line with negative balance");
72
73 using namespace jtx;
74 auto const gw = Account("gateway");
75 auto const alice = Account("alice");
76 auto const bob = Account("bob");
77 auto const carol = Account("carol");
78
79 Env env(*this, features);
80
81 env.fund(XRP(10000), gw, alice, bob, carol);
82 env.close();
83
84 env.trust(alice["USD"](100), bob);
85 env.trust(bob["USD"](100), carol);
86 env.close();
87
88 // After this payment alice has a -50 USD balance with bob, and
89 // bob has a -50 USD balance with carol. So neither alice nor
90 // bob should be able to clear the noRipple flag.
91 env(pay(alice, carol, carol["USD"](50)), Path(bob));
92 env.close();
93
94 TER const terNeg{TER{tecNO_PERMISSION}};
95
96 env(trust(alice, bob["USD"](100), bob, tfSetNoRipple), Ter(terNeg));
97 env(trust(bob, carol["USD"](100), carol, tfSetNoRipple), Ter(terNeg));
98 env.close();
99
100 json::Value params;
101 params[jss::source_account] = alice.human();
102 params[jss::destination_account] = carol.human();
103 params[jss::destination_amount] = [] {
104 json::Value destAmt;
105 destAmt[jss::currency] = "USD";
106 destAmt[jss::value] = "1";
107 destAmt[jss::issuer] = Account("carol").human();
108 return destAmt;
109 }();
110
111 auto const resp = env.rpc("json", "ripple_path_find", to_string(params));
112 BEAST_EXPECT(resp[jss::result][jss::alternatives].size() == 1);
113
114 auto const getAccountLines = [&env](Account const& acct) {
115 auto const r = jtx::getAccountLines(env, acct);
116 return r[jss::lines];
117 };
118 {
119 auto const aliceLines = getAccountLines(alice);
120 BEAST_EXPECT(aliceLines.size() == 1);
121 BEAST_EXPECT(aliceLines[0u][jss::no_ripple].asBool() == false);
122
123 auto const bobLines = getAccountLines(bob);
124 BEAST_EXPECT(bobLines.size() == 2);
125 BEAST_EXPECT(bobLines[0u][jss::no_ripple].asBool() == false);
126 BEAST_EXPECT(bobLines[1u][jss::no_ripple].asBool() == false);
127 }
128
129 // Now carol sends the 50 USD back to alice. Then alice and
130 // bob can set the noRipple flag.
131 env(pay(carol, alice, alice["USD"](50)), Path(bob));
132 env.close();
133
134 env(trust(alice, bob["USD"](100), bob, tfSetNoRipple));
135 env(trust(bob, carol["USD"](100), carol, tfSetNoRipple));
136 env.close();
137 {
138 auto const aliceLines = getAccountLines(alice);
139 BEAST_EXPECT(aliceLines.size() == 1);
140 BEAST_EXPECT(aliceLines[0u].isMember(jss::no_ripple));
141
142 auto const bobLines = getAccountLines(bob);
143 BEAST_EXPECT(bobLines.size() == 2);
144 BEAST_EXPECT(bobLines[0u].isMember(jss::no_ripple_peer));
145 BEAST_EXPECT(bobLines[1u].isMember(jss::no_ripple));
146 }
147 }
148
149 void
151 {
152 testcase("pairwise NoRipple");
153
154 using namespace jtx;
155 Env env(*this, features);
156
157 auto const alice = Account("alice");
158 auto const bob = Account("bob");
159 auto const carol = Account("carol");
160
161 env.fund(XRP(10000), alice, bob, carol);
162
163 env(trust(bob, alice["USD"](100)));
164 env(trust(carol, bob["USD"](100)));
165
166 env(trust(bob, alice["USD"](100), alice, tfSetNoRipple));
167 env(trust(bob, carol["USD"](100), carol, tfSetNoRipple));
168 env.close();
169
170 json::Value params;
171 params[jss::source_account] = alice.human();
172 params[jss::destination_account] = carol.human();
173 params[jss::destination_amount] = [] {
174 json::Value destAmt;
175 destAmt[jss::currency] = "USD";
176 destAmt[jss::value] = "1";
177 destAmt[jss::issuer] = Account("carol").human();
178 return destAmt;
179 }();
180
181 json::Value const resp{env.rpc("json", "ripple_path_find", to_string(params))};
182 BEAST_EXPECT(resp[jss::result][jss::alternatives].size() == 0);
183
184 env(pay(alice, carol, bob["USD"](50)), Ter(tecPATH_DRY));
185 }
186
187 void
188 testDefaultRipple(FeatureBitset features, unsigned int apiVersion)
189 {
190 testcase(
191 "Set default ripple on an account and check new trustlines "
192 "Version " +
193 std::to_string(apiVersion));
194
195 using namespace jtx;
196 Env env(*this, features);
197
198 auto const gw = Account("gateway");
199 auto const alice = Account("alice");
200 auto const bob = Account("bob");
201
202 env.fund(XRP(10000), gw, noripple(alice, bob));
203
204 env(fset(bob, asfDefaultRipple));
205
206 auto const usd = gw["USD"];
207
208 env(trust(gw, usd(100), alice, 0));
209 env(trust(gw, usd(100), bob, 0));
210 json::Value params;
211 params[jss::api_version] = apiVersion;
212
213 {
214 params[jss::account] = gw.human();
215 params[jss::peer] = alice.human();
216
217 auto lines = env.rpc("json", "account_lines", to_string(params));
218 auto const& line0 = lines[jss::result][jss::lines][0u];
219 BEAST_EXPECT(line0[jss::no_ripple_peer].asBool() == true);
220 }
221 {
222 params[jss::account] = alice.human();
223 params[jss::peer] = gw.human();
224
225 auto lines = env.rpc("json", "account_lines", to_string(params));
226 auto const& line0 = lines[jss::result][jss::lines][0u];
227 BEAST_EXPECT(line0[jss::no_ripple].asBool() == true);
228 }
229 {
230 params[jss::account] = gw.human();
231 params[jss::peer] = bob.human();
232
233 auto lines = env.rpc("json", "account_lines", to_string(params));
234 auto const& line0 = lines[jss::result][jss::lines][0u];
235 BEAST_EXPECT(line0[jss::no_ripple].asBool() == false);
236 }
237 {
238 params[jss::account] = bob.human();
239 params[jss::peer] = gw.human();
240
241 auto lines = env.rpc("json", "account_lines", to_string(params));
242 auto const& line0 = lines[jss::result][jss::lines][0u];
243 BEAST_EXPECT(line0[jss::no_ripple_peer].asBool() == false);
244 }
245 {
246 // test for transactions
247 {
248 params[jss::account] = bob.human();
249 params[jss::role] = "gateway";
250 params[jss::transactions] = "asdf";
251
252 auto lines = env.rpc("json", "noripple_check", to_string(params));
253 if (apiVersion < 2u)
254 {
255 BEAST_EXPECT(lines[jss::result][jss::status] == "success");
256 }
257 else
258 {
259 BEAST_EXPECT(lines[jss::result][jss::error] == "invalidParams");
260 BEAST_EXPECT(lines[jss::result][jss::error_code] == RpcInvalidParams);
261 BEAST_EXPECT(
262 lines[jss::result][jss::error_message] == "Invalid field 'transactions'.");
263 }
264 }
265 }
266 }
267
268 void
269 run() override
270 {
272
273 auto const withFeatsTests = [this](FeatureBitset features) {
275 [&, this](unsigned testVersion) { testDefaultRipple(features, testVersion); });
276 testNegativeBalance(features);
277 testPairwise(features);
278 };
279 using namespace jtx;
280 auto const sa = testableAmendments();
281 withFeatsTests(sa - featurePermissionedDEX);
282 withFeatsTests(sa);
283 }
284};
285
287
288} // namespace xrpl::test
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
void testPairwise(FeatureBitset features)
void run() override
Runs the suite.
void testDefaultRipple(FeatureBitset features, unsigned int apiVersion)
void testNegativeBalance(FeatureBitset features)
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
A transaction testing environment.
Definition Env.h:161
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:1058
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:354
Add a path.
Definition paths.h:47
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
The api_version numbers this server serves.
Definition ApiVersion.h:26
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
FeatureBitset testableAmendments()
Definition Env.h:92
std::array< Account, 1+sizeof...(Args)> noripple(Account const &account, Args const &... args)
Designate accounts as no-ripple in Env::fund.
Definition Env.h:86
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Definition trust.cpp:18
json::Value getAccountLines(Env &env, AccountID const &acctId)
json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
Definition flags.cpp:15
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcInvalidParams
Definition ErrorCodes.h:67
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
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecPATH_DRY
Definition TER.h:305
@ tecNO_PERMISSION
Definition TER.h:316
T to_string(T... args)