xrpld
Loading...
Searching...
No Matches
DeliveredAmount_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/TestHelpers.h>
4#include <test/jtx/WSClient.h>
5#include <test/jtx/amount.h>
6#include <test/jtx/balance.h>
7#include <test/jtx/envconfig.h>
8#include <test/jtx/mpt.h>
9#include <test/jtx/pay.h>
10#include <test/jtx/sendmax.h>
11#include <test/jtx/ter.h>
12#include <test/jtx/txflags.h>
13
14#include <xrpl/basics/chrono.h>
15#include <xrpl/beast/unit_test/suite.h>
16#include <xrpl/json/json_value.h>
17#include <xrpl/json/to_string.h>
18#include <xrpl/protocol/AccountID.h>
19#include <xrpl/protocol/Feature.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/TER.h>
22#include <xrpl/protocol/TxFlags.h>
23#include <xrpl/protocol/jss.h>
24
25#include <chrono>
26#include <string>
27#include <utility>
28
29namespace xrpl::test {
30
31// Helper class to track the expected number `delivered_amount` results.
33{
34 // If the test occurs before or after the switch time
36 // number of payments expected 'delivered_amount' available
38 // Number of payments with field with `delivered_amount` set to the
39 // string "unavailable"
41 // Number of payments with no `delivered_amount` field
43
44 // Increment one of the expected numExpected{Available_, Unavailable_,
45 // NotSet_} values. Which value to kIncrement depends on: 1) If the ledger is
46 // before or after the switch time 2) If the tx is a partial payment 3) If
47 // the payment is successful or not
48 void
49 adjCounters(bool success, bool partial)
50 {
51 if (!success)
52 {
54 return;
55 }
57 {
58 if (partial)
59 {
61 }
62 else
63 {
65 }
66 return;
67 }
68 // normal case: after switch time & successful transaction
70 }
71
72public:
73 explicit CheckDeliveredAmount(bool afterSwitchTime) : afterSwitchTime_(afterSwitchTime)
74 {
75 }
76
77 void
79 {
80 adjCounters(true, false);
81 }
82
83 void
85 {
86 adjCounters(false, false);
87 }
88 void
90 {
91 adjCounters(true, true);
92 }
93
94 // After all the txns are checked, all the `numExpected` variables should be
95 // zero. The `checkTxn` function decrements these variables.
96 [[nodiscard]] bool
98 {
99 return (numExpectedAvailable_ == 0) && (numExpectedNotSet_ == 0) &&
101 }
102
103 // Check if the transaction has `delivered_amount` in the metaData as
104 // expected from our rules. Decrements the appropriate `numExpected`
105 // variable. After all the txns are checked, all the `numExpected` variables
106 // should be zero.
107 bool
108 checkTxn(json::Value const& t, json::Value const& metaData)
109 {
110 if (t[jss::TransactionType].asString() != jss::Payment)
111 return true;
112
113 bool const isSet = metaData.isMember(jss::delivered_amount);
114 bool isSetUnavailable = false;
115 bool isSetAvailable = false;
116 if (isSet)
117 {
118 if (metaData[jss::delivered_amount] != "unavailable")
119 {
120 isSetAvailable = true;
121 }
122 else
123 {
124 isSetUnavailable = true;
125 }
126 }
127 if (isSetAvailable)
128 {
130 }
131 else if (isSetUnavailable)
132 {
134 }
135 else if (!isSet)
136 {
138 }
139
140 if (isSet)
141 {
142 if (metaData.isMember(sfDeliveredAmount.jsonName))
143 {
144 if (metaData[jss::delivered_amount] != metaData[sfDeliveredAmount.jsonName])
145 return false;
146 }
147 else
148 {
150 {
151 if (metaData[jss::delivered_amount] != t[jss::Amount])
152 return false;
153 }
154 else
155 {
156 if (metaData[jss::delivered_amount] != "unavailable")
157 return false;
158 }
159 }
160 }
161
162 if (metaData[sfTransactionResult.jsonName] != "tesSUCCESS")
163 {
164 if (isSet)
165 return false;
166 }
167 else
168 {
170 {
171 if (!isSetAvailable)
172 return false;
173 }
174 else
175 {
176 if (metaData.isMember(sfDeliveredAmount.jsonName))
177 {
178 if (!isSetAvailable)
179 return false;
180 }
181 else
182 {
183 if (!isSetUnavailable)
184 return false;
185 }
186 }
187 }
188 return true;
189 }
190};
191
193{
194 void
196 {
197 testcase("Ledger Request Subscribe DeliveredAmount");
198
199 using namespace test::jtx;
200 using namespace std::chrono_literals;
201
202 Account const alice("alice");
203 Account const bob("bob");
204 Account const carol("carol");
205 auto const gw = Account("gateway");
206 auto const usd = gw["USD"];
207
208 for (bool const afterSwitchTime : {true, false})
209 {
210 auto cfg = envconfig();
211 cfg->fees.referenceFee = 10;
212 Env env(*this, std::move(cfg));
213 env.fund(XRP(10000), alice, bob, carol, gw);
214 env.trust(usd(1000), alice, bob, carol);
215 if (afterSwitchTime)
216 {
217 env.close(NetClock::time_point{446000000s});
218 }
219 else
220 {
221 env.close();
222 }
223
224 CheckDeliveredAmount checkDeliveredAmount{afterSwitchTime};
225 {
226 // add payments, but do no close until subscribed
227
228 // normal payments
229 env(pay(gw, alice, usd(50)));
230 checkDeliveredAmount.adjCountersSuccess();
231 env(pay(gw, alice, XRP(50)));
232 checkDeliveredAmount.adjCountersSuccess();
233
234 // partial payment
235 env(pay(gw, bob, usd(9999999)), Txflags(tfPartialPayment));
236 checkDeliveredAmount.adjCountersPartialPayment();
237 env.require(Balance(bob, usd(1000)));
238
239 // failed payment
240 env(pay(bob, carol, usd(9999999)), Ter(tecPATH_PARTIAL));
241 checkDeliveredAmount.adjCountersFail();
242 env.require(Balance(carol, usd(0)));
243 }
244
245 auto wsc = makeWSClient(env.app().config());
246
247 {
248 json::Value stream;
249 // RPC subscribe to ledger stream
250 stream[jss::streams] = json::ValueType::Array;
251 stream[jss::streams].append("ledger");
252 stream[jss::accounts] = json::ValueType::Array;
253 stream[jss::accounts].append(toBase58(alice.id()));
254 stream[jss::accounts].append(toBase58(bob.id()));
255 stream[jss::accounts].append(toBase58(carol.id()));
256 auto jv = wsc->invoke("subscribe", stream);
257 if (wsc->version() == 2)
258 {
259 BEAST_EXPECT(hasEnvelope2(jv));
260 BEAST_EXPECT(jv.isMember(jss::id) && jv[jss::id] == 5);
261 }
262 BEAST_EXPECT(jv[jss::result][jss::ledger_index] == 3);
263 }
264 {
265 env.close();
266 // Check stream update
267 while (true)
268 {
269 auto const r = wsc->findMsg(
270 1s, [&](auto const& jv) { return jv[jss::ledger_index] == 4; });
271 if (!r)
272 break;
273
274 if (!r->isMember(jss::transaction))
275 continue;
276
277 BEAST_EXPECT(
278 checkDeliveredAmount.checkTxn((*r)[jss::transaction], (*r)[jss::meta]));
279 }
280 }
281 BEAST_EXPECT(checkDeliveredAmount.checkExpectedCounters());
282 }
283 }
284 void
286 {
287 testcase("Ledger Request RPC DeliveredAmount");
288
289 using namespace test::jtx;
290 using namespace std::chrono_literals;
291
292 Account const alice("alice");
293 Account const bob("bob");
294 Account const carol("carol");
295 auto const gw = Account("gateway");
296 auto const usd = gw["USD"];
297
298 for (bool const afterSwitchTime : {true, false})
299 {
300 auto cfg = envconfig();
301 cfg->fees.referenceFee = 10;
302 Env env(*this, std::move(cfg));
303 env.fund(XRP(10000), alice, bob, carol, gw);
304 env.trust(usd(1000), alice, bob, carol);
305 if (afterSwitchTime)
306 {
307 env.close(NetClock::time_point{446000000s});
308 }
309 else
310 {
311 env.close();
312 }
313
314 CheckDeliveredAmount checkDeliveredAmount{afterSwitchTime};
315 // normal payments
316 env(pay(gw, alice, usd(50)));
317 checkDeliveredAmount.adjCountersSuccess();
318 env(pay(gw, alice, XRP(50)));
319 checkDeliveredAmount.adjCountersSuccess();
320
321 // partial payment
322 env(pay(gw, bob, usd(9999999)), Txflags(tfPartialPayment));
323 checkDeliveredAmount.adjCountersPartialPayment();
324 env.require(Balance(bob, usd(1000)));
325
326 // failed payment
327 env(pay(gw, carol, usd(9999999)), Ter(tecPATH_PARTIAL));
328 checkDeliveredAmount.adjCountersFail();
329 env.require(Balance(carol, usd(0)));
330
331 env.close();
332 json::Value jvParams;
333 jvParams[jss::ledger_index] = 4u;
334 jvParams[jss::transactions] = true;
335 jvParams[jss::expand] = true;
336 auto const jtxn = env.rpc(
337 "json", "ledger", to_string(jvParams))[jss::result][jss::ledger][jss::transactions];
338 for (auto const& t : jtxn)
339 BEAST_EXPECT(checkDeliveredAmount.checkTxn(t, t[jss::metaData]));
340 BEAST_EXPECT(checkDeliveredAmount.checkExpectedCounters());
341 }
342 }
343
344 void
346 {
347 testcase("MPT DeliveredAmount");
348
349 using namespace jtx;
350 Account const alice("alice");
351 Account const carol("carol");
352 Account const bob("bob");
353 Env env{*this, features};
354
355 MPTTester mptAlice(env, alice, {.holders = {bob, carol}, .close = false});
356
357 mptAlice.create(
358 {.transferFee = 25000, .ownerCount = 1, .holderCount = 0, .flags = tfMPTCanTransfer});
359 auto const mpt = mptAlice["MPT"];
360
361 mptAlice.authorize({.account = bob});
362 mptAlice.authorize({.account = carol});
363
364 // issuer to holder
365 mptAlice.pay(alice, bob, 10000);
366
367 // holder to holder
368 env(pay(bob, carol, mptAlice.mpt(1000)), Txflags(tfPartialPayment));
369 env.close();
370
371 // Get the hash for the most recent transaction.
372 std::string txHash{env.tx()->getJson(JsonOptions::Values::None)[jss::hash].asString()};
373 json::Value meta = env.rpc("tx", txHash)[jss::result][jss::meta];
374
375 if (features[fixMPTDeliveredAmount])
376 {
377 BEAST_EXPECT(
378 meta[sfDeliveredAmount.jsonName] ==
380 BEAST_EXPECT(
381 meta[jss::delivered_amount] ==
383 }
384 else
385 {
386 BEAST_EXPECT(!meta.isMember(sfDeliveredAmount.jsonName));
387 BEAST_EXPECT(meta[jss::delivered_amount] = json::Value("unavailable"));
388 }
389
390 env(pay(bob, carol, mpt(1000)), Sendmax(mpt(1200)), Txflags(tfPartialPayment));
391 env.close();
392
393 txHash = env.tx()->getJson(JsonOptions::Values::None)[jss::hash].asString();
394 meta = env.rpc("tx", txHash)[jss::result][jss::meta];
395
396 if (features[fixMPTDeliveredAmount])
397 {
398 BEAST_EXPECT(
399 meta[sfDeliveredAmount.jsonName] ==
401 BEAST_EXPECT(
402 meta[jss::delivered_amount] ==
404 }
405 else
406 {
407 BEAST_EXPECT(!meta.isMember(sfDeliveredAmount.jsonName));
408 BEAST_EXPECT(meta[jss::delivered_amount] = json::Value("unavailable"));
409 }
410 }
411
412public:
413 void
414 run() override
415 {
416 using namespace test::jtx;
418
421
422 testMPTDeliveredAmountRPC(all - fixMPTDeliveredAmount - featureMPTokensV2);
424 }
425};
426
427BEAST_DEFINE_TESTSUITE(DeliveredAmount, rpc, xrpl);
428
429} // 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
bool isMember(char const *key) const
Return true if the object has a member named key.
virtual Config & config()=0
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
bool checkTxn(json::Value const &t, json::Value const &metaData)
void adjCounters(bool success, bool partial)
void run() override
Runs the suite.
void testMPTDeliveredAmountRPC(FeatureBitset features)
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
AccountID id() const
Returns the Account ID.
A balance matches.
Definition balance.h:24
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
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
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
Definition Env.cpp:560
void require(Args const &... args)
Check a set of requirements.
Definition Env.h:766
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
Definition mpt.h:512
void create(MPTCreate const &arg=MPTCreate{}, std::source_location const &loc=std::source_location::current())
Definition mpt.cpp:346
Sets the SendMax on a JTx.
Definition sendmax.h:16
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
Set the flags on a JTx.
Definition txflags.h:14
@ 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...
FeatureBitset testableAmendments()
Definition Env.h:92
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:38
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
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
@ tecPATH_PARTIAL
Definition TER.h:293