rippled
TxHistory.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/main/Application.h>
21 #include <ripple/app/misc/Transaction.h>
22 #include <ripple/core/DatabaseCon.h>
23 #include <ripple/core/SociDB.h>
24 #include <ripple/net/RPCErr.h>
25 #include <ripple/protocol/ErrorCodes.h>
26 #include <ripple/protocol/jss.h>
27 #include <ripple/resource/Fees.h>
28 #include <ripple/rpc/Context.h>
29 #include <ripple/rpc/Role.h>
30 #include <boost/format.hpp>
31 
32 namespace ripple {
33 
34 // {
35 // start: <index>
36 // }
38 {
40 
41  if (!context.params.isMember (jss::start))
42  return rpcError (rpcINVALID_PARAMS);
43 
44  unsigned int startIndex = context.params[jss::start].asUInt ();
45 
46  if ((startIndex > 10000) && (! isUnlimited (context.role)))
47  return rpcError (rpcNO_PERMISSION);
48 
49  Json::Value obj;
50  Json::Value txs;
51 
52  obj[jss::index] = startIndex;
53 
54  std::string sql =
55  boost::str (boost::format (
56  "SELECT LedgerSeq, Status, RawTxn "
57  "FROM Transactions ORDER BY LedgerSeq desc LIMIT %u,20;")
58  % startIndex);
59 
60  {
61  auto db = context.app.getTxnDB ().checkoutDb ();
62 
63  boost::optional<std::uint64_t> ledgerSeq;
64  boost::optional<std::string> status;
65  soci::blob sociRawTxnBlob (*db);
66  soci::indicator rti;
67  Blob rawTxn;
68 
69  soci::statement st = (db->prepare << sql,
70  soci::into (ledgerSeq),
71  soci::into (status),
72  soci::into (sociRawTxnBlob, rti));
73 
74  st.execute ();
75  while (st.fetch ())
76  {
77  if (soci::i_ok == rti)
78  convert(sociRawTxnBlob, rawTxn);
79  else
80  rawTxn.clear ();
81 
82  if (auto trans = Transaction::transactionFromSQL (
83  ledgerSeq, status, rawTxn, context.app))
84  txs.append (trans->getJson (JsonOptions::none));
85  }
86  }
87 
88  obj[jss::txs] = txs;
89 
90  return obj;
91 }
92 
93 } // ripple
ripple::Transaction::transactionFromSQL
static Transaction::pointer transactionFromSQL(boost::optional< std::uint64_t > const &ledgerSeq, boost::optional< std::string > const &status, Blob const &rawTxn, Application &app)
Definition: Transaction.cpp:83
ripple::RPC::JsonContext
Definition: Context.h:52
std::string
STL class.
ripple::rpcINVALID_PARAMS
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:85
ripple::Resource::feeMediumBurdenRPC
const Charge feeMediumBurdenRPC
ripple::convert
void convert(soci::blob &from, std::vector< std::uint8_t > &to)
Definition: SociDB.cpp:150
ripple::RPC::Context::loadType
Resource::Charge & loadType
Definition: Context.h:43
std::vector< unsigned char >
ripple::RPC::Context::role
Role role
Definition: Context.h:47
std::vector::clear
T clear(T... args)
ripple::doTxHistory
Json::Value doTxHistory(RPC::JsonContext &)
Definition: TxHistory.cpp:37
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:907
ripple::DatabaseCon::checkoutDb
LockedSociSession checkoutDb()
Definition: DatabaseCon.h:123
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:961
ripple::rpcError
Json::Value rpcError(int iError, Json::Value jvResult)
Definition: RPCErr.cpp:28
ripple::isUnlimited
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition: Role.cpp:86
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::rpcNO_PERMISSION
@ rpcNO_PERMISSION
Definition: ErrorCodes.h:54
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:555
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:63
Json::Value
Represents a JSON value.
Definition: json_value.h:141
ripple::Application::getTxnDB
virtual DatabaseCon & getTxnDB()=0