xrpld
Loading...
Searching...
No Matches
Discrepancy_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/PathSet.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/jtx_json.h>
6#include <test/jtx/offer.h>
7#include <test/jtx/pay.h>
8#include <test/jtx/sendmax.h>
9#include <test/jtx/trust.h>
10#include <test/jtx/txflags.h>
11
12#include <xrpl/beast/core/LexicalCast.h>
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/Feature.h>
17#include <xrpl/protocol/SField.h>
18#include <xrpl/protocol/TxFlags.h>
19#include <xrpl/protocol/jss.h>
20
21#include <cstdint>
22
23namespace xrpl {
24
26{
27 // This is a legacy test ported from js/coffee. The ledger
28 // state was originally setup via a saved ledger file and the relevant
29 // entries have since been converted to the equivalent jtx/Env setup.
30 // A payment with path and sendmax is made and the transaction is queried
31 // to verify that the net of balance changes match the fee charged.
32 void
34 {
35 testcase("Discrepancy test : XRP Discrepancy");
36 using namespace test::jtx;
37 Env env{*this, features};
38
39 Account const a1{"A1"};
40 Account const a2{"A2"};
41 Account const a3{"A3"};
42 Account const a4{"A4"};
43 Account const a5{"A5"};
44 Account const a6{"A6"};
45 Account const a7{"A7"};
46
47 env.fund(XRP(2000), a1);
48 env.fund(XRP(1000), a2, a6, a7);
49 env.fund(XRP(5000), a3);
50 env.fund(XRP(1000000), a4);
51 env.fund(XRP(600000), a5);
52 env.close();
53
54 env(trust(a1, a3["CNY"](200000)));
55 env(pay(a3, a1, a3["CNY"](31)));
56 env.close();
57
58 env(trust(a1, a2["JPY"](1000000)));
59 env(pay(a2, a1, a2["JPY"](729117)));
60 env.close();
61
62 env(trust(a4, a2["JPY"](10000000)));
63 env(pay(a2, a4, a2["JPY"](470056)));
64 env.close();
65
66 env(trust(a5, a3["CNY"](50000)));
67 env(pay(a3, a5, a3["CNY"](8683)));
68 env.close();
69
70 env(trust(a6, a3["CNY"](3000)));
71 env(pay(a3, a6, a3["CNY"](293)));
72 env.close();
73
74 env(trust(a7, a6["CNY"](50000)));
75 env(pay(a6, a7, a6["CNY"](261)));
76 env.close();
77
78 env(offer(a4, XRP(49147), a2["JPY"](34501)));
79 env(offer(a5, a3["CNY"](3150), XRP(80086)));
80 env(offer(a7, XRP(1233), a6["CNY"](25)));
81 env.close();
82
83 test::PathSet const payPaths{
84 test::TestPath{a2["JPY"], a2},
85 test::TestPath{XRP, a2["JPY"], a2},
86 test::TestPath{a6, XRP, a2["JPY"], a2}};
87
88 env(pay(a1, a1, a2["JPY"](1000)),
89 Json(payPaths.json()),
90 Txflags(tfPartialPayment),
91 Sendmax(a3["CNY"](56)));
92 env.close();
93
94 json::Value jrq2;
95 jrq2[jss::binary] = false;
96 jrq2[jss::transaction] = env.tx()->getJson(JsonOptions::Values::None)[jss::hash];
97 jrq2[jss::id] = 3;
98 auto jrr = env.rpc("json", "tx", to_string(jrq2))[jss::result];
99 uint64_t const fee{jrr[jss::Fee].asUInt()};
100 auto meta = jrr[jss::meta];
101 uint64_t sumPrev{0};
102 uint64_t sumFinal{0};
103 BEAST_EXPECT(meta[sfAffectedNodes.fieldName].size() == 9);
104 for (auto const& an : meta[sfAffectedNodes.fieldName])
105 {
106 json::Value node;
107 if (an.isMember(sfCreatedNode.fieldName))
108 {
109 node = an[sfCreatedNode.fieldName];
110 }
111 else if (an.isMember(sfModifiedNode.fieldName))
112 {
113 node = an[sfModifiedNode.fieldName];
114 }
115 else if (an.isMember(sfDeletedNode.fieldName))
116 {
117 node = an[sfDeletedNode.fieldName];
118 }
119
120 if (node && node[sfLedgerEntryType.fieldName] == jss::AccountRoot)
121 {
122 json::Value prevFields = node.isMember(sfPreviousFields.fieldName)
123 ? node[sfPreviousFields.fieldName]
124 : node[sfNewFields.fieldName];
125 json::Value finalFields = node.isMember(sfFinalFields.fieldName)
126 ? node[sfFinalFields.fieldName]
127 : node[sfNewFields.fieldName];
128 if (prevFields)
129 {
131 prevFields[sfBalance.fieldName].asString());
132 }
133 if (finalFields)
134 {
136 finalFields[sfBalance.fieldName].asString());
137 }
138 }
139 }
140 // the difference in balances (final and prev) should be the
141 // fee charged
142 BEAST_EXPECT(sumPrev - sumFinal == fee);
143 }
144
145public:
146 void
147 run() override
148 {
149 using namespace test::jtx;
150 auto const sa = testableAmendments();
151 testXRPDiscrepancy(sa - featurePermissionedDEX);
153 }
154};
155
156BEAST_DEFINE_TESTSUITE(Discrepancy, app, xrpl);
157
158} // namespace xrpl
A testsuite class.
Definition suite.h:50
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:149
Represents a JSON value.
Definition json_value.h:130
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
void testXRPDiscrepancy(FeatureBitset features)
void run() override
Runs the suite.
json::Value json() const
Definition PathSet.h:170
Out lexicalCastThrow(In in)
Convert from one type to another, throw on error.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)