xrpld
Loading...
Searching...
No Matches
RobustTransaction_test.cpp
1#include <test/jtx/Env.h>
2#include <test/jtx/TestHelpers.h>
3#include <test/jtx/WSClient.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/balance.h> // IWYU pragma: keep
6#include <test/jtx/flags.h>
7#include <test/jtx/pay.h>
8
9#include <xrpl/beast/unit_test/suite.h>
10#include <xrpl/core/JobQueue.h>
11#include <xrpl/json/json_value.h>
12#include <xrpl/protocol/SField.h>
13#include <xrpl/protocol/Seed.h>
14#include <xrpl/protocol/jss.h>
15
16#include <chrono>
17
18namespace xrpl::test {
19
21{
22public:
23 void
25 {
26 using namespace std::chrono_literals;
27 using namespace jtx;
28 Env env(*this);
29 env.fund(XRP(10000), "alice", "bob");
30 env.close();
31 auto wsc = makeWSClient(env.app().config());
32
33 {
34 // RPC subscribe to transactions stream
35 json::Value jv;
36 jv[jss::streams] = json::ValueType::Array;
37 jv[jss::streams].append("transactions");
38 jv = wsc->invoke("subscribe", jv);
39 BEAST_EXPECT(jv[jss::status] == "success");
40 if (wsc->version() == 2)
41 {
42 BEAST_EXPECT(hasEnvelope2(jv));
43 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
44 }
45 }
46
47 {
48 // Submit past ledger sequence transaction
49 json::Value payment;
50 payment[jss::secret] = toBase58(generateSeed("alice"));
51 payment[jss::tx_json] = pay("alice", "bob", XRP(1));
52 payment[jss::tx_json][sfLastLedgerSequence.fieldName] = 1;
53 auto jv = wsc->invoke("submit", payment);
54 if (wsc->version() == 2)
55 {
56 BEAST_EXPECT(hasEnvelope2(jv));
57 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
58 }
59 BEAST_EXPECT(jv[jss::result][jss::engine_result] == "tefMAX_LEDGER");
60
61 // Submit past sequence transaction
62 payment[jss::tx_json] = pay("alice", "bob", XRP(1));
63 payment[jss::tx_json][sfSequence.fieldName] = env.seq("alice") - 1;
64 jv = wsc->invoke("submit", payment);
65 if (wsc->version() == 2)
66 {
67 BEAST_EXPECT(hasEnvelope2(jv));
68 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
69 }
70 BEAST_EXPECT(jv[jss::result][jss::engine_result] == "tefPAST_SEQ");
71
72 // Submit future sequence transaction
73 payment[jss::tx_json][sfSequence.fieldName] = env.seq("alice") + 1;
74 jv = wsc->invoke("submit", payment);
75 if (wsc->version() == 2)
76 {
77 BEAST_EXPECT(hasEnvelope2(jv));
78 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
79 }
80 BEAST_EXPECT(jv[jss::result][jss::engine_result] == "terPRE_SEQ");
81
82 // Submit transaction to bridge the sequence gap
83 payment[jss::tx_json][sfSequence.fieldName] = env.seq("alice");
84 jv = wsc->invoke("submit", payment);
85 if (wsc->version() == 2)
86 {
87 BEAST_EXPECT(hasEnvelope2(jv));
88 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
89 }
90 BEAST_EXPECT(jv[jss::result][jss::engine_result] == "tesSUCCESS");
91
92 // Wait for the jobqueue to process everything
93 env.app().getJobQueue().rendezvous();
94
95 // Finalize transactions
96 jv = wsc->invoke("ledger_accept");
97 if (wsc->version() == 2)
98 {
99 BEAST_EXPECT(hasEnvelope2(jv));
100 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
101 }
102 BEAST_EXPECT(jv[jss::result].isMember(jss::ledger_current_index));
103 }
104
105 {
106 // Check balances
107 BEAST_EXPECT(wsc->findMsg(5s, [&](auto const& jv) {
108 auto const& ff = jv[jss::meta]["AffectedNodes"][1u]["ModifiedNode"]["FinalFields"];
109 return ff[jss::Account] == Account("bob").human() && ff["Balance"] == "10001000000";
110 }));
111
112 BEAST_EXPECT(wsc->findMsg(5s, [&](auto const& jv) {
113 auto const& ff = jv[jss::meta]["AffectedNodes"][1u]["ModifiedNode"]["FinalFields"];
114 return ff[jss::Account] == Account("bob").human() && ff["Balance"] == "10002000000";
115 }));
116 }
117
118 {
119 // RPC unsubscribe to transactions stream
120 json::Value jv;
121 jv[jss::streams] = json::ValueType::Array;
122 jv[jss::streams].append("transactions");
123 jv = wsc->invoke("unsubscribe", jv);
124 if (wsc->version() == 2)
125 {
126 BEAST_EXPECT(hasEnvelope2(jv));
127 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
128 }
129 BEAST_EXPECT(jv[jss::status] == "success");
130 }
131 }
132
133 /*
134 Submit a normal payment. Client disconnects after the proposed
135 transaction result is received.
136
137 Client reconnects in the future. During this time it is presumed that the
138 transaction should have succeeded.
139
140 Upon reconnection, recent account transaction history is loaded.
141 The submitted transaction should be detected, and the transaction should
142 ultimately succeed.
143 */
144 void
146 {
147 using namespace jtx;
148 Env env(*this);
149 env.fund(XRP(10000), "alice", "bob");
150 env.close();
151 auto wsc = makeWSClient(env.app().config());
152
153 {
154 // Submit normal payment
155 json::Value jv;
156 jv[jss::secret] = toBase58(generateSeed("alice"));
157 jv[jss::tx_json] = pay("alice", "bob", XRP(1));
158 jv = wsc->invoke("submit", jv);
159 if (wsc->version() == 2)
160 {
161 BEAST_EXPECT(hasEnvelope2(jv));
162 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
163 }
164 BEAST_EXPECT(jv[jss::result][jss::engine_result] == "tesSUCCESS");
165
166 // Disconnect
167 wsc.reset();
168
169 // Server finalizes transaction
170 env.close();
171 }
172
173 {
174 // RPC account_tx
175 json::Value jv;
176 jv[jss::account] = Account("bob").human();
177 jv[jss::ledger_index_min] = -1;
178 jv[jss::ledger_index_max] = -1;
179 wsc = makeWSClient(env.app().config());
180 jv = wsc->invoke("account_tx", jv);
181 if (wsc->version() == 2)
182 {
183 BEAST_EXPECT(hasEnvelope2(jv));
184 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
185 }
186
187 // Check balance
188 auto ff = jv[jss::result][jss::transactions][0u][jss::meta]["AffectedNodes"][1u]
189 ["ModifiedNode"]["FinalFields"];
190 BEAST_EXPECT(ff[jss::Account] == Account("bob").human());
191 BEAST_EXPECT(ff["Balance"] == "10001000000");
192 }
193 }
194
195 void
197 {
198 using namespace std::chrono_literals;
199 using namespace jtx;
200 Env env(*this);
201 env.fund(XRP(10000), "alice", "bob");
202 env.close();
203 auto wsc = makeWSClient(env.app().config());
204
205 {
206 // Submit normal payment
207 json::Value jv;
208 jv[jss::secret] = toBase58(generateSeed("alice"));
209 jv[jss::tx_json] = pay("alice", "bob", XRP(1));
210 jv = wsc->invoke("submit", jv);
211 if (wsc->version() == 2)
212 {
213 BEAST_EXPECT(hasEnvelope2(jv));
214 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
215 }
216 BEAST_EXPECT(jv[jss::result][jss::engine_result] == "tesSUCCESS");
217
218 // Finalize transaction
219 jv = wsc->invoke("ledger_accept");
220 if (wsc->version() == 2)
221 {
222 BEAST_EXPECT(hasEnvelope2(jv));
223 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
224 }
225 BEAST_EXPECT(jv[jss::result].isMember(jss::ledger_current_index));
226
227 // Wait for the jobqueue to process everything
228 env.app().getJobQueue().rendezvous();
229 }
230
231 {
232 {
233 // RPC subscribe to ledger stream
234 json::Value jv;
235 jv[jss::streams] = json::ValueType::Array;
236 jv[jss::streams].append("ledger");
237 jv = wsc->invoke("subscribe", jv);
238 if (wsc->version() == 2)
239 {
240 BEAST_EXPECT(hasEnvelope2(jv));
241 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
242 }
243 BEAST_EXPECT(jv[jss::status] == "success");
244 }
245
246 // Close ledgers
247 for (auto i = 0; i < 8; ++i)
248 {
249 auto jv = wsc->invoke("ledger_accept");
250 if (wsc->version() == 2)
251 {
252 BEAST_EXPECT(hasEnvelope2(jv));
253 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
254 }
255 BEAST_EXPECT(jv[jss::result].isMember(jss::ledger_current_index));
256
257 // Wait for the jobqueue to process everything
258 env.app().getJobQueue().rendezvous();
259
260 BEAST_EXPECT(wsc->findMsg(
261 5s, [&](auto const& jval) { return jval[jss::type] == "ledgerClosed"; }));
262 }
263
264 {
265 // RPC unsubscribe to ledger stream
266 json::Value jv;
267 jv[jss::streams] = json::ValueType::Array;
268 jv[jss::streams].append("ledger");
269 jv = wsc->invoke("unsubscribe", jv);
270 if (wsc->version() == 2)
271 {
272 BEAST_EXPECT(hasEnvelope2(jv));
273 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
274 }
275 BEAST_EXPECT(jv[jss::status] == "success");
276 }
277 }
278
279 {
280 // Disconnect, reconnect
281 wsc = makeWSClient(env.app().config());
282 {
283 // RPC subscribe to ledger stream
284 json::Value jv;
285 jv[jss::streams] = json::ValueType::Array;
286 jv[jss::streams].append("ledger");
287 jv = wsc->invoke("subscribe", jv);
288 if (wsc->version() == 2)
289 {
290 BEAST_EXPECT(hasEnvelope2(jv));
291 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
292 }
293 BEAST_EXPECT(jv[jss::status] == "success");
294 }
295
296 // Close ledgers
297 for (auto i = 0; i < 2; ++i)
298 {
299 auto jv = wsc->invoke("ledger_accept");
300 if (wsc->version() == 2)
301 {
302 BEAST_EXPECT(hasEnvelope2(jv));
303 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
304 }
305 BEAST_EXPECT(jv[jss::result].isMember(jss::ledger_current_index));
306
307 // Wait for the jobqueue to process everything
308 env.app().getJobQueue().rendezvous();
309
310 BEAST_EXPECT(wsc->findMsg(
311 5s, [&](auto const& jval) { return jval[jss::type] == "ledgerClosed"; }));
312 }
313
314 {
315 // RPC unsubscribe to ledger stream
316 json::Value jv;
317 jv[jss::streams] = json::ValueType::Array;
318 jv[jss::streams].append("ledger");
319 jv = wsc->invoke("unsubscribe", jv);
320 if (wsc->version() == 2)
321 {
322 BEAST_EXPECT(hasEnvelope2(jv));
323 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
324 }
325 BEAST_EXPECT(jv[jss::status] == "success");
326 }
327 }
328
329 {
330 // RPC account_tx
331 json::Value jv;
332 jv[jss::account] = Account("bob").human();
333 jv[jss::ledger_index_min] = -1;
334 jv[jss::ledger_index_max] = -1;
335 wsc = makeWSClient(env.app().config());
336 jv = wsc->invoke("account_tx", jv);
337 if (wsc->version() == 2)
338 {
339 BEAST_EXPECT(hasEnvelope2(jv));
340 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
341 }
342
343 // Check balance
344 auto ff = jv[jss::result][jss::transactions][0u][jss::meta]["AffectedNodes"][1u]
345 ["ModifiedNode"]["FinalFields"];
346 BEAST_EXPECT(ff[jss::Account] == Account("bob").human());
347 BEAST_EXPECT(ff["Balance"] == "10001000000");
348 }
349 }
350
351 void
353 {
354 using namespace std::chrono_literals;
355 using namespace jtx;
356 Env env(*this);
357 env.fund(XRP(10000), "alice");
358 env.close();
359 auto wsc = makeWSClient(env.app().config());
360
361 {
362 // RPC subscribe to accounts_proposed stream
363 json::Value jv;
364 jv[jss::accounts_proposed] = json::ValueType::Array;
365 jv[jss::accounts_proposed].append(Account("alice").human());
366 jv = wsc->invoke("subscribe", jv);
367 if (wsc->version() == 2)
368 {
369 BEAST_EXPECT(hasEnvelope2(jv));
370 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
371 }
372 BEAST_EXPECT(jv[jss::status] == "success");
373 }
374
375 {
376 // Submit account_set transaction
377 json::Value jv;
378 jv[jss::secret] = toBase58(generateSeed("alice"));
379 jv[jss::tx_json] = fset("alice", 0);
380 jv[jss::tx_json][jss::Fee] = static_cast<int>(env.current()->fees().base.drops());
381 jv = wsc->invoke("submit", jv);
382 if (wsc->version() == 2)
383 {
384 BEAST_EXPECT(hasEnvelope2(jv));
385 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
386 }
387 BEAST_EXPECT(jv[jss::result][jss::engine_result] == "tesSUCCESS");
388 }
389
390 {
391 // Check stream update
392 BEAST_EXPECT(wsc->findMsg(5s, [&](auto const& jv) {
393 return jv[jss::transaction][jss::TransactionType] == jss::AccountSet;
394 }));
395 }
396
397 {
398 // RPC unsubscribe to accounts_proposed stream
399 json::Value jv;
400 jv[jss::accounts_proposed] = json::ValueType::Array;
401 jv[jss::accounts_proposed].append(Account("alice").human());
402 jv = wsc->invoke("unsubscribe", jv);
403 if (wsc->version() == 2)
404 {
405 BEAST_EXPECT(hasEnvelope2(jv));
406 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
407 }
408 BEAST_EXPECT(jv[jss::status] == "success");
409 }
410 }
411
412 void
413 run() override
414 {
419 }
420};
421
422BEAST_DEFINE_TESTSUITE(RobustTransaction, rpc, xrpl);
423
424} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
Represents a JSON value.
Definition json_value.h:121
Value & append(Value const &value)
Append value to array at the end.
bool isMember(char const *key) const
Return true if the object has a member named key.
virtual Config & config()=0
void rendezvous()
Block until no jobs running.
Definition JobQueue.cpp:243
virtual JobQueue & getJobQueue()=0
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
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
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition Env.cpp:302
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
@ Array
array value (ordered list)
Definition json_value.h:32
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
bool hasEnvelope2(json::Value const &reply)
Reports whether reply carries the version 2 envelope: jsonrpc equal to rpc::kJsonRpcVersion and rippl...
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)
std::unique_ptr< WSClient > makeWSClient(Config const &cfg, bool v2, unsigned rpcVersion, std::unordered_map< std::string, std::string > const &headers)
Returns a client operating through WebSockets/S.
Definition WSClient.cpp:373
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:58