rippled
TransactionHistory_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2017 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 <ripple/protocol/jss.h>
21 #include <boost/container/static_vector.hpp>
22 #include <algorithm>
23 #include <test/jtx.h>
24 #include <test/jtx/Env.h>
25 #include <test/jtx/envconfig.h>
26 
27 namespace ripple {
28 
29 class TransactionHistory_test : public beast::unit_test::suite
30 {
31  void
33  {
34  testcase("Invalid request params");
35  using namespace test::jtx;
36  Env env{*this, envconfig(no_admin)};
37 
38  {
39  // no params
40  auto const result =
41  env.client().invoke("tx_history", {})[jss::result];
42  BEAST_EXPECT(result[jss::error] == "invalidParams");
43  BEAST_EXPECT(result[jss::status] == "error");
44  }
45 
46  {
47  // test at 1 greater than the allowed non-admin limit
49  params[jss::start] = 10001; // limited to <= 10000 for non admin
50  auto const result =
51  env.client().invoke("tx_history", params)[jss::result];
52  BEAST_EXPECT(result[jss::error] == "noPermission");
53  BEAST_EXPECT(result[jss::status] == "error");
54  }
55  }
56 
57  void
59  {
60  testcase("Basic request");
61  using namespace test::jtx;
62  Env env{*this};
63 
64  // create enough transactions to provide some
65  // history...
66  size_t const numAccounts = 20;
67  boost::container::static_vector<Account, numAccounts> accounts;
68  for (size_t i = 0; i < numAccounts; ++i)
69  {
70  accounts.emplace_back("A" + std::to_string(i));
71  auto const& acct = accounts.back();
72  env.fund(XRP(10000), acct);
73  env.close();
74  if (i > 0)
75  {
76  auto const& prev = accounts[i - 1];
77  env.trust(acct["USD"](1000), prev);
78  env(pay(acct, prev, acct["USD"](5)));
79  }
80  env(offer(acct, XRP(100), acct["USD"](1)));
81  env.close();
82 
83  // verify the latest transaction in env (offer)
84  // is available in tx_history.
86  params[jss::start] = 0;
87  auto result =
88  env.client().invoke("tx_history", params)[jss::result];
89  if (!BEAST_EXPECT(
90  result[jss::txs].isArray() && result[jss::txs].size() > 0))
91  return;
92 
93  // search for a tx in history matching the last offer
94  bool const txFound = [&] {
95  auto const toFind = env.tx()->getJson(JsonOptions::none);
96  for (auto tx : result[jss::txs])
97  {
98  tx.removeMember(jss::inLedger);
99  tx.removeMember(jss::ledger_index);
100  if (toFind == tx)
101  return true;
102  }
103  return false;
104  }();
105  BEAST_EXPECT(txFound);
106  }
107 
108  unsigned int start = 0;
109  unsigned int total = 0;
110  // also summarize the transaction types in this map
112  while (start < 120)
113  {
115  params[jss::start] = start;
116  auto result =
117  env.client().invoke("tx_history", params)[jss::result];
118  if (!BEAST_EXPECT(
119  result[jss::txs].isArray() && result[jss::txs].size() > 0))
120  break;
121  total += result[jss::txs].size();
122  start += 20;
123  for (auto const& t : result[jss::txs])
124  {
125  typeCounts[t[sfTransactionType.fieldName].asString()]++;
126  }
127  }
128  BEAST_EXPECT(total == 117);
129  BEAST_EXPECT(typeCounts[jss::AccountSet.c_str()] == 20);
130  BEAST_EXPECT(typeCounts[jss::TrustSet.c_str()] == 19);
131  BEAST_EXPECT(typeCounts[jss::Payment.c_str()] == 58);
132  BEAST_EXPECT(typeCounts[jss::OfferCreate.c_str()] == 20);
133 
134  // also, try a request with max non-admin start value
135  {
137  params[jss::start] = 10000; // limited to <= 10000 for non admin
138  auto const result =
139  env.client().invoke("tx_history", params)[jss::result];
140  BEAST_EXPECT(result[jss::status] == "success");
141  BEAST_EXPECT(result[jss::index] == 10000);
142  }
143  }
144 
145 public:
146  void
147  run() override
148  {
149  testBadInput();
150  testRequest();
151  }
152 };
153 
154 BEAST_DEFINE_TESTSUITE(TransactionHistory, rpc, ripple);
155 
156 } // namespace ripple
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::SField::fieldName
const std::string fieldName
Definition: SField.h:132
ripple::TransactionHistory_test
Definition: TransactionHistory_test.cpp:29
algorithm
ripple::sfTransactionType
const SF_UINT16 sfTransactionType
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::JsonOptions::none
@ none
std::to_string
T to_string(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TransactionHistory_test::testBadInput
void testBadInput()
Definition: TransactionHistory_test.cpp:32
ripple::TransactionHistory_test::testRequest
void testRequest()
Definition: TransactionHistory_test.cpp:58
ripple::TransactionHistory_test::run
void run() override
Definition: TransactionHistory_test.cpp:147
std::unordered_map
STL class.
Json::Value
Represents a JSON value.
Definition: json_value.h:145