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 <test/jtx.h>
21 #include <test/jtx/Env.h>
22 #include <test/jtx/envconfig.h>
23 #include <ripple/protocol/jss.h>
24 #include <boost/container/static_vector.hpp>
25 #include <algorithm>
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 = env.client()
41  .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 = env.client()
51  .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 testRequest()
58  {
59  testcase("Basic request");
60  using namespace test::jtx;
61  Env env {*this};
62 
63  // create enough transactions to provide some
64  // history...
65  size_t const numAccounts = 20;
66  boost::container::static_vector<Account, numAccounts> accounts;
67  for(size_t i = 0; i<numAccounts; ++i)
68  {
69  accounts.emplace_back("A" + std::to_string(i));
70  auto const& acct=accounts.back();
71  env.fund(XRP(10000), acct);
72  env.close();
73  if(i > 0)
74  {
75  auto const& prev=accounts[i-1];
76  env.trust(acct["USD"](1000), prev);
77  env(pay(acct, prev, acct["USD"](5)));
78  }
79  env(offer(acct, XRP(100), acct["USD"](1)));
80  env.close();
81 
82  // verify the latest transaction in env (offer)
83  // is available in tx_history.
85  params[jss::start] = 0;
86  auto result =
87  env.client().invoke("tx_history", params)[jss::result];
88  if(! BEAST_EXPECT(result[jss::txs].isArray() &&
89  result[jss::txs].size() > 0))
90  return;
91 
92  // search for a tx in history matching the last offer
93  bool const txFound = [&] {
94  auto const toFind = env.tx()->getJson(JsonOptions::none);
95  for (auto tx : result[jss::txs])
96  {
97  tx.removeMember(jss::inLedger);
98  tx.removeMember(jss::ledger_index);
99  if (toFind == tx)
100  return true;
101  }
102  return false;
103  }();
104  BEAST_EXPECT(txFound);
105  }
106 
107  unsigned int start = 0;
108  unsigned int total = 0;
109  // also summarize the transaction types in this map
111  while(start < 120)
112  {
114  params[jss::start] = start;
115  auto result =
116  env.client().invoke("tx_history", params)[jss::result];
117  if(! BEAST_EXPECT(result[jss::txs].isArray() &&
118  result[jss::txs].size() > 0))
119  break;
120  total += result[jss::txs].size();
121  start += 20;
122  for (auto const& t : result[jss::txs])
123  {
124  typeCounts[t[sfTransactionType.fieldName].asString()]++;
125  }
126  }
127  BEAST_EXPECT(total == 117);
128  BEAST_EXPECT(typeCounts[jss::AccountSet.c_str()] == 20);
129  BEAST_EXPECT(typeCounts[jss::TrustSet.c_str()] == 19);
130  BEAST_EXPECT(typeCounts[jss::Payment.c_str()] == 58);
131  BEAST_EXPECT(typeCounts[jss::OfferCreate.c_str()] == 20);
132 
133  // also, try a request with max non-admin start value
134  {
136  params[jss::start] = 10000; //limited to <= 10000 for non admin
137  auto const result = env.client()
138  .invoke("tx_history", params)[jss::result];
139  BEAST_EXPECT(result[jss::status] == "success");
140  BEAST_EXPECT(result[jss::index] == 10000);
141  }
142  }
143 
144 public:
145  void run () override
146  {
147  testBadInput();
148  testRequest();
149  }
150 };
151 
152 BEAST_DEFINE_TESTSUITE (TransactionHistory, rpc, ripple);
153 
154 } // ripple
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::SField::fieldName
const std::string fieldName
Definition: SField.h:136
ripple::TransactionHistory_test
Definition: TransactionHistory_test.cpp:29
algorithm
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:45
ripple::JsonOptions::none
@ none
std::to_string
T to_string(T... args)
ripple::sfTransactionType
const SF_U16 sfTransactionType(access, STI_UINT16, 2, "TransactionType")
Definition: SField.h:331
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:57
ripple::TransactionHistory_test::run
void run() override
Definition: TransactionHistory_test.cpp:145
std::unordered_map
STL class.
Json::Value
Represents a JSON value.
Definition: json_value.h:141