xrpld
Loading...
Searching...
No Matches
TransactionHistory_test.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/amount.h>
4#include <test/jtx/envconfig.h>
5#include <test/jtx/offer.h>
6#include <test/jtx/pay.h>
7
8#include <xrpl/beast/unit_test/suite.h>
9#include <xrpl/json/json_value.h>
10#include <xrpl/protocol/ErrorCodes.h>
11#include <xrpl/protocol/SField.h>
12#include <xrpl/protocol/jss.h>
13
14#include <boost/container/static_vector.hpp>
15
16#include <cstddef>
17#include <string>
18#include <unordered_map>
19
20namespace xrpl {
21
23{
24 void
26 {
27 testcase("Invalid request params");
28 using namespace test::jtx;
29 Env env{*this, envconfig(noAdmin)};
30
31 {
32 // no params
33 auto const result = env.client().invoke("tx_history", {})[jss::result];
34 BEAST_EXPECT(result[jss::status] == "error");
35 BEAST_EXPECT(result[jss::error] == "invalidParams");
36 BEAST_EXPECT(result[jss::error_code] == RpcInvalidParams);
37 BEAST_EXPECT(result[jss::error_message] == "Invalid parameters.");
38 }
39
40 {
41 // test at 1 greater than the allowed non-admin limit
43 params[jss::start] = 10001; // limited to <= 10000 for non admin
44 auto const result = env.client().invoke("tx_history", params)[jss::result];
45 BEAST_EXPECT(result[jss::status] == "error");
46 BEAST_EXPECT(result[jss::error] == "noPermission");
47 BEAST_EXPECT(result[jss::error_code] == RpcNoPermission);
48 BEAST_EXPECT(
49 result[jss::error_message] == "You don't have permission for this command.");
50 }
51 }
52
53 void
55 {
56 testcase("Command retired from API v2");
57 using namespace test::jtx;
58 Env env{*this, envconfig(noAdmin)};
59
61 params[jss::api_version] = 2;
62 auto const result = env.client().invoke("tx_history", params)[jss::result];
63 BEAST_EXPECT(result[jss::status] == "error");
64 BEAST_EXPECT(result[jss::error] == "unknownCmd");
65 BEAST_EXPECT(result[jss::error_code] == RpcUnknownCommand);
66 BEAST_EXPECT(result[jss::error_message] == "Unknown method.");
67 }
68
69 void
71 {
72 testcase("Basic request");
73 using namespace test::jtx;
74 Env env{*this};
75
76 // create enough transactions to provide some
77 // history...
78 size_t const numAccounts = 20;
79 boost::container::static_vector<Account, numAccounts> accounts;
80 for (size_t i = 0; i < numAccounts; ++i)
81 {
82 accounts.emplace_back("A" + std::to_string(i));
83 auto const& acct = accounts.back();
84 env.fund(XRP(10000), acct);
85 env.close();
86 if (i > 0)
87 {
88 auto const& prev = accounts[i - 1];
89 env.trust(acct["USD"](1000), prev);
90 env(pay(acct, prev, acct["USD"](5)));
91 }
92 env(offer(acct, XRP(100), acct["USD"](1)));
93 env.close();
94
95 // verify the latest transaction in env (offer)
96 // is available in tx_history.
98 params[jss::start] = 0;
99 auto result = env.client().invoke("tx_history", params)[jss::result];
100 if (!BEAST_EXPECT(result[jss::txs].isArray() && result[jss::txs].size() > 0))
101 return;
102
103 // search for a tx in history matching the last offer
104 bool const txFound = [&] {
105 auto const toFind = env.tx()->getJson(JsonOptions::Values::None);
106 for (auto tx : result[jss::txs])
107 {
108 tx.removeMember(jss::inLedger);
109 tx.removeMember(jss::ledger_index);
110 if (toFind == tx)
111 return true;
112 }
113 return false;
114 }();
115 BEAST_EXPECT(txFound);
116 }
117
118 unsigned int start = 0;
119 unsigned int total = 0;
120 // also summarize the transaction types in this map
122 while (start < 120)
123 {
125 params[jss::start] = start;
126 auto result = env.client().invoke("tx_history", params)[jss::result];
127 if (!BEAST_EXPECT(result[jss::txs].isArray() && result[jss::txs].size() > 0))
128 break;
129 total += result[jss::txs].size();
130 start += 20;
131 for (auto const& t : result[jss::txs])
132 {
133 typeCounts[t[sfTransactionType.fieldName].asString()]++;
134 }
135 }
136 BEAST_EXPECT(total == 117);
137 BEAST_EXPECT(typeCounts[jss::AccountSet.cStr()] == 20);
138 BEAST_EXPECT(typeCounts[jss::TrustSet.cStr()] == 19);
139 BEAST_EXPECT(typeCounts[jss::Payment.cStr()] == 58);
140 BEAST_EXPECT(typeCounts[jss::OfferCreate.cStr()] == 20);
141
142 // also, try a request with max non-admin start value
143 {
145 params[jss::start] = 10000; // limited to <= 10000 for non admin
146 auto const result = env.client().invoke("tx_history", params)[jss::result];
147 BEAST_EXPECT(result[jss::status] == "success");
148 BEAST_EXPECT(result[jss::index] == 10000);
149 }
150 }
151
152public:
153 void
154 run() override
155 {
156 testBadInput();
157 testRequest();
159 }
160};
161
162BEAST_DEFINE_TESTSUITE(TransactionHistory, rpc, xrpl);
163
164} // namespace xrpl
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
UInt size() const
Number of values in array or object.
void run() override
Runs the suite.
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
The api_version numbers this server serves.
Definition ApiVersion.h:26
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcInvalidParams
Definition ErrorCodes.h:67
@ RpcNoPermission
Definition ErrorCodes.h:36
@ RpcUnknownCommand
Definition ErrorCodes.h:68
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
T to_string(T... args)