rippled
Loading...
Searching...
No Matches
Discrepancy_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2016 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/jtx.h>
21#include <test/jtx/Env.h>
22#include <test/jtx/PathSet.h>
23
24#include <xrpl/beast/core/LexicalCast.h>
25#include <xrpl/beast/unit_test.h>
26#include <xrpl/protocol/Feature.h>
27#include <xrpl/protocol/SField.h>
28#include <xrpl/protocol/jss.h>
29
30namespace ripple {
31
33{
34 // This is a legacy test ported from js/coffee. The ledger
35 // state was originally setup via a saved ledger file and the relevant
36 // entries have since been converted to the equivalent jtx/Env setup.
37 // A payment with path and sendmax is made and the transaction is queried
38 // to verify that the net of balance changes match the fee charged.
39 void
41 {
42 testcase("Discrepancy test : XRP Discrepancy");
43 using namespace test::jtx;
44 Env env{*this, features};
45
46 Account A1{"A1"};
47 Account A2{"A2"};
48 Account A3{"A3"};
49 Account A4{"A4"};
50 Account A5{"A5"};
51 Account A6{"A6"};
52 Account A7{"A7"};
53
54 env.fund(XRP(2000), A1);
55 env.fund(XRP(1000), A2, A6, A7);
56 env.fund(XRP(5000), A3);
57 env.fund(XRP(1000000), A4);
58 env.fund(XRP(600000), A5);
59 env.close();
60
61 env(trust(A1, A3["CNY"](200000)));
62 env(pay(A3, A1, A3["CNY"](31)));
63 env.close();
64
65 env(trust(A1, A2["JPY"](1000000)));
66 env(pay(A2, A1, A2["JPY"](729117)));
67 env.close();
68
69 env(trust(A4, A2["JPY"](10000000)));
70 env(pay(A2, A4, A2["JPY"](470056)));
71 env.close();
72
73 env(trust(A5, A3["CNY"](50000)));
74 env(pay(A3, A5, A3["CNY"](8683)));
75 env.close();
76
77 env(trust(A6, A3["CNY"](3000)));
78 env(pay(A3, A6, A3["CNY"](293)));
79 env.close();
80
81 env(trust(A7, A6["CNY"](50000)));
82 env(pay(A6, A7, A6["CNY"](261)));
83 env.close();
84
85 env(offer(A4, XRP(49147), A2["JPY"](34501)));
86 env(offer(A5, A3["CNY"](3150), XRP(80086)));
87 env(offer(A7, XRP(1233), A6["CNY"](25)));
88 env.close();
89
90 test::PathSet payPaths{
91 test::Path{A2["JPY"], A2},
92 test::Path{XRP, A2["JPY"], A2},
93 test::Path{A6, XRP, A2["JPY"], A2}};
94
95 env(pay(A1, A1, A2["JPY"](1000)),
96 json(payPaths.json()),
97 txflags(tfPartialPayment),
98 sendmax(A3["CNY"](56)));
99 env.close();
100
101 Json::Value jrq2;
102 jrq2[jss::binary] = false;
103 jrq2[jss::transaction] =
104 env.tx()->getJson(JsonOptions::none)[jss::hash];
105 jrq2[jss::id] = 3;
106 auto jrr = env.rpc("json", "tx", to_string(jrq2))[jss::result];
107 uint64_t fee{jrr[jss::Fee].asUInt()};
108 auto meta = jrr[jss::meta];
109 uint64_t sumPrev{0};
110 uint64_t sumFinal{0};
111 BEAST_EXPECT(meta[sfAffectedNodes.fieldName].size() == 9);
112 for (auto const& an : meta[sfAffectedNodes.fieldName])
113 {
114 Json::Value node;
115 if (an.isMember(sfCreatedNode.fieldName))
116 node = an[sfCreatedNode.fieldName];
117 else if (an.isMember(sfModifiedNode.fieldName))
118 node = an[sfModifiedNode.fieldName];
119 else if (an.isMember(sfDeletedNode.fieldName))
120 node = an[sfDeletedNode.fieldName];
121
122 if (node && node[sfLedgerEntryType.fieldName] == jss::AccountRoot)
123 {
124 Json::Value prevFields =
125 node.isMember(sfPreviousFields.fieldName)
126 ? node[sfPreviousFields.fieldName]
127 : node[sfNewFields.fieldName];
128 Json::Value finalFields = node.isMember(sfFinalFields.fieldName)
129 ? node[sfFinalFields.fieldName]
130 : node[sfNewFields.fieldName];
131 if (prevFields)
132 sumPrev += beast::lexicalCastThrow<std::uint64_t>(
133 prevFields[sfBalance.fieldName].asString());
134 if (finalFields)
135 sumFinal += beast::lexicalCastThrow<std::uint64_t>(
136 finalFields[sfBalance.fieldName].asString());
137 }
138 }
139 // the difference in balances (final and prev) should be the
140 // fee charged
141 BEAST_EXPECT(sumPrev - sumFinal == fee);
142 }
143
144public:
145 void
146 run() override
147 {
148 using namespace test::jtx;
149 auto const sa = supported_amendments();
150 testXRPDiscrepancy(sa - featureFlowCross - featurePermissionedDEX);
151 testXRPDiscrepancy(sa - featurePermissionedDEX);
153 }
154};
155
156BEAST_DEFINE_TESTSUITE(Discrepancy, app, ripple);
157
158} // namespace ripple
Represents a JSON value.
Definition: json_value.h:149
UInt asUInt() const
Definition: json_value.cpp:558
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:482
bool isMember(char const *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:962
A testsuite class.
Definition: suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:155
void testXRPDiscrepancy(FeatureBitset features)
void run() override
Runs the suite.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
constexpr std::uint32_t tfPartialPayment
Definition: TxFlags.h:108
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630