rippled
AccountTxOld.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2014 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/app/ledger/LedgerMaster.h>
21 #include <ripple/app/main/Application.h>
22 #include <ripple/app/misc/NetworkOPs.h>
23 #include <ripple/app/misc/Transaction.h>
24 #include <ripple/ledger/ReadView.h>
25 #include <ripple/net/RPCErr.h>
26 #include <ripple/protocol/ErrorCodes.h>
27 #include <ripple/protocol/jss.h>
28 #include <ripple/resource/Fees.h>
29 #include <ripple/rpc/Context.h>
30 #include <ripple/rpc/DeliveredAmount.h>
31 #include <ripple/rpc/Role.h>
32 #include <ripple/rpc/impl/RPCHelpers.h>
33 
34 namespace ripple {
35 
36 // {
37 // account: account,
38 // ledger_index_min: ledger_index,
39 // ledger_index_max: ledger_index,
40 // binary: boolean, // optional, defaults to false
41 // count: boolean, // optional, defaults to false
42 // descending: boolean, // optional, defaults to false
43 // offset: integer, // optional, defaults to 0
44 // limit: integer // optional
45 // }
48 {
49  std::uint32_t offset = context.params.isMember(jss::offset)
50  ? context.params[jss::offset].asUInt()
51  : 0;
52  int limit = context.params.isMember(jss::limit)
53  ? context.params[jss::limit].asUInt()
54  : -1;
55  bool bBinary = context.params.isMember(jss::binary) &&
56  context.params[jss::binary].asBool();
57  bool bDescending = context.params.isMember(jss::descending) &&
58  context.params[jss::descending].asBool();
59  bool bCount = context.params.isMember(jss::count) &&
60  context.params[jss::count].asBool();
61  std::uint32_t uLedgerMin;
62  std::uint32_t uLedgerMax;
63  std::uint32_t uValidatedMin;
64  std::uint32_t uValidatedMax;
65  bool bValidated =
66  context.ledgerMaster.getValidatedRange(uValidatedMin, uValidatedMax);
67 
68  if (!context.params.isMember(jss::account))
70 
71  auto const raAccount =
72  parseBase58<AccountID>(context.params[jss::account].asString());
73  if (!raAccount)
74  return rpcError(rpcACT_MALFORMED);
75 
76  if (offset > 3000)
78 
80 
81  // DEPRECATED
82  if (context.params.isMember(jss::ledger_min))
83  {
84  context.params[jss::ledger_index_min] = context.params[jss::ledger_min];
85  bDescending = true;
86  }
87 
88  // DEPRECATED
89  if (context.params.isMember(jss::ledger_max))
90  {
91  context.params[jss::ledger_index_max] = context.params[jss::ledger_max];
92  bDescending = true;
93  }
94 
95  if (context.params.isMember(jss::ledger_index_min) ||
96  context.params.isMember(jss::ledger_index_max))
97  {
98  std::int64_t iLedgerMin = context.params.isMember(jss::ledger_index_min)
99  ? context.params[jss::ledger_index_min].asInt()
100  : -1;
101  std::int64_t iLedgerMax = context.params.isMember(jss::ledger_index_max)
102  ? context.params[jss::ledger_index_max].asInt()
103  : -1;
104 
105  if (!bValidated && (iLedgerMin == -1 || iLedgerMax == -1))
106  {
107  // Don't have a validated ledger range.
108  if (context.apiVersion == 1)
110  return rpcError(rpcNOT_SYNCED);
111  }
112 
113  uLedgerMin = iLedgerMin == -1 ? uValidatedMin : iLedgerMin;
114  uLedgerMax = iLedgerMax == -1 ? uValidatedMax : iLedgerMax;
115 
116  if (uLedgerMax < uLedgerMin)
117  {
118  if (context.apiVersion == 1)
120  return rpcError(rpcNOT_SYNCED);
121  }
122  }
123  else
124  {
126  auto ret = RPC::lookupLedger(ledger, context);
127 
128  if (!ledger)
129  return ret;
130 
131  if (!ret[jss::validated].asBool() ||
132  (ledger->info().seq > uValidatedMax) ||
133  (ledger->info().seq < uValidatedMin))
134  {
136  }
137 
138  uLedgerMin = uLedgerMax = ledger->info().seq;
139  }
140 
141  int count = 0;
142 
143 #ifndef DEBUG
144 
145  try
146  {
147 #endif
148 
150 
151  ret[jss::account] = context.app.accountIDCache().toBase58(*raAccount);
152  Json::Value& jvTxns = (ret[jss::transactions] = Json::arrayValue);
153 
154  if (bBinary)
155  {
156  auto txns = context.netOps.getAccountTxsB(
157  *raAccount,
158  uLedgerMin,
159  uLedgerMax,
160  bDescending,
161  offset,
162  limit,
163  isUnlimited(context.role));
164 
165  for (auto it = txns.begin(), end = txns.end(); it != end; ++it)
166  {
167  ++count;
168  Json::Value& jvObj = jvTxns.append(Json::objectValue);
169 
170  std::uint32_t uLedgerIndex = std::get<2>(*it);
171  jvObj[jss::tx_blob] = strHex(std::get<0>(*it));
172  jvObj[jss::meta] = strHex(std::get<1>(*it));
173  jvObj[jss::ledger_index] = uLedgerIndex;
174  jvObj[jss::validated] = bValidated &&
175  uValidatedMin <= uLedgerIndex &&
176  uValidatedMax >= uLedgerIndex;
177  }
178  }
179  else
180  {
181  auto txns = context.netOps.getAccountTxs(
182  *raAccount,
183  uLedgerMin,
184  uLedgerMax,
185  bDescending,
186  offset,
187  limit,
188  isUnlimited(context.role));
189 
190  for (auto it = txns.begin(), end = txns.end(); it != end; ++it)
191  {
192  ++count;
193  Json::Value& jvObj = jvTxns.append(Json::objectValue);
194 
195  if (it->first)
196  jvObj[jss::tx] =
197  it->first->getJson(JsonOptions::include_date);
198 
199  if (it->second)
200  {
201  std::uint32_t uLedgerIndex = it->second->getLgrSeq();
202 
203  auto meta = it->second->getJson(JsonOptions::none);
204  insertDeliveredAmount(
205  meta, context, it->first, *it->second);
206  jvObj[jss::meta] = std::move(meta);
207 
208  jvObj[jss::validated] = bValidated &&
209  uValidatedMin <= uLedgerIndex &&
210  uValidatedMax >= uLedgerIndex;
211  }
212  }
213  }
214 
215  // Add information about the original query
216  ret[jss::ledger_index_min] = uLedgerMin;
217  ret[jss::ledger_index_max] = uLedgerMax;
218  ret[jss::validated] = bValidated && uValidatedMin <= uLedgerMin &&
219  uValidatedMax >= uLedgerMax;
220  ret[jss::offset] = offset;
221 
222  // We no longer return the full count but only the count of returned
223  // transactions. Computing this count was two expensive and this API is
224  // deprecated anyway.
225  if (bCount)
226  ret[jss::count] = count;
227 
228  if (context.params.isMember(jss::limit))
229  ret[jss::limit] = limit;
230 
231  return ret;
232 #ifndef DEBUG
233  }
234  catch (std::exception const&)
235  {
236  return rpcError(rpcINTERNAL);
237  }
238 
239 #endif
240 }
241 
242 } // namespace ripple
ripple::ReadView::info
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
ripple::LedgerMaster::getValidatedRange
bool getValidatedRange(std::uint32_t &minVal, std::uint32_t &maxVal)
Definition: LedgerMaster.cpp:610
ripple::doAccountTxOld
Json::Value doAccountTxOld(RPC::JsonContext &context)
Definition: AccountTxOld.cpp:47
ripple::JsonOptions::include_date
@ include_date
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::rpcLGR_IDXS_INVALID
@ rpcLGR_IDXS_INVALID
Definition: ErrorCodes.h:112
std::shared_ptr
STL class.
ripple::rpcINVALID_PARAMS
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
std::exception
STL class.
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
ripple::RPC::Context::loadType
Resource::Charge & loadType
Definition: Context.h:43
ripple::RPC::Context::ledgerMaster
LedgerMaster & ledgerMaster
Definition: Context.h:45
ripple::LedgerInfo::seq
LedgerIndex seq
Definition: ReadView.h:92
ripple::RPC::Context::role
Role role
Definition: Context.h:47
ripple::rpcATX_DEPRECATED
@ rpcATX_DEPRECATED
Definition: ErrorCodes.h:127
ripple::RPC::lookupLedger
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext &context, Json::Value &result)
Look up a ledger from a request and fill a Json::Result with the data representing a ledger.
Definition: RPCHelpers.cpp:487
ripple::Application::accountIDCache
virtual AccountIDCache const & accountIDCache() const =0
Json::Value::asBool
bool asBool() const
Definition: json_value.cpp:619
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:882
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::JsonOptions::none
@ none
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:932
std::uint32_t
ripple::rpcError
Json::Value rpcError(int iError, Json::Value jvResult)
Definition: RPCErr.cpp:29
ripple::RPC::Context::netOps
NetworkOPs & netOps
Definition: Context.h:44
ripple::rpcINTERNAL
@ rpcINTERNAL
Definition: ErrorCodes.h:130
ripple::NetworkOPs::getAccountTxs
virtual AccountTxs getAccountTxs(AccountID const &account, std::int32_t minLedger, std::int32_t maxLedger, bool descending, std::uint32_t offset, int limit, bool bUnlimited)=0
ripple::isUnlimited
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition: Role.cpp:94
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::rpcACT_MALFORMED
@ rpcACT_MALFORMED
Definition: ErrorCodes.h:90
ripple::rpcLGR_NOT_VALIDATED
@ rpcLGR_NOT_VALIDATED
Definition: ErrorCodes.h:73
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:545
ripple::RPC::Context::apiVersion
unsigned int apiVersion
Definition: Context.h:50
Json::Value::asInt
Int asInt() const
Definition: json_value.cpp:503
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:45
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::Resource::feeHighBurdenRPC
const Charge feeHighBurdenRPC
ripple::rpcNOT_SYNCED
@ rpcNOT_SYNCED
Definition: ErrorCodes.h:67
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::AccountIDCache::toBase58
std::string toBase58(AccountID const &) const
Return ripple::toBase58 for the AccountID.
Definition: AccountID.cpp:134
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469
ripple::NetworkOPs::getAccountTxsB
virtual MetaTxsList getAccountTxsB(AccountID const &account, std::int32_t minLedger, std::int32_t maxLedger, bool descending, std::uint32_t offset, int limit, bool bUnlimited)=0