rippled
LedgerData.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/LedgerToJson.h>
21 #include <ripple/ledger/ReadView.h>
22 #include <ripple/protocol/ErrorCodes.h>
23 #include <ripple/protocol/jss.h>
24 #include <ripple/protocol/LedgerFormats.h>
25 #include <ripple/rpc/impl/RPCHelpers.h>
26 #include <ripple/rpc/impl/Tuning.h>
27 #include <ripple/rpc/Context.h>
28 #include <ripple/rpc/Role.h>
29 
30 namespace ripple {
31 
32 // Get state nodes from a ledger
33 // Inputs:
34 // limit: integer, maximum number of entries
35 // marker: opaque, resume point
36 // binary: boolean, format
37 // type: string // optional, defaults to all ledger node types
38 // Outputs:
39 // ledger_hash: chosen ledger's hash
40 // ledger_index: chosen ledger's index
41 // state: array of state nodes
42 // marker: resume point, if any
44 {
46  auto const& params = context.params;
47 
48  auto jvResult = RPC::lookupLedger(lpLedger, context);
49  if (!lpLedger)
50  return jvResult;
51 
52  bool const isMarker = params.isMember (jss::marker);
54  if (isMarker)
55  {
56  Json::Value const& jMarker = params[jss::marker];
57  if (! (jMarker.isString () && key.SetHex (jMarker.asString ())))
58  return RPC::expected_field_error (jss::marker, "valid");
59  }
60 
61  bool const isBinary = params[jss::binary].asBool();
62 
63  int limit = -1;
64  if (params.isMember (jss::limit))
65  {
66  Json::Value const& jLimit = params[jss::limit];
67  if (!jLimit.isIntegral ())
68  return RPC::expected_field_error (jss::limit, "integer");
69 
70  limit = jLimit.asInt ();
71  }
72 
73  auto maxLimit = RPC::Tuning::pageLength(isBinary);
74  if ((limit < 0) || ((limit > maxLimit) && (! isUnlimited (context.role))))
75  limit = maxLimit;
76 
77  jvResult[jss::ledger_hash] = to_string (lpLedger->info().hash);
78  jvResult[jss::ledger_index] = lpLedger->info().seq;
79 
80  if (! isMarker)
81  {
82  // Return base ledger data on first query
83  jvResult[jss::ledger] = getJson (
84  LedgerFill (*lpLedger, isBinary ?
85  LedgerFill::Options::binary : 0));
86  }
87 
88  auto [rpcStatus, type] = RPC::chooseLedgerEntryType(params);
89  if (rpcStatus)
90  {
91  jvResult.clear();
92  rpcStatus.inject(jvResult);
93  return jvResult;
94  }
95  Json::Value& nodes = jvResult[jss::state];
96 
97  auto e = lpLedger->sles.end();
98  for (auto i = lpLedger->sles.upper_bound(key); i != e; ++i)
99  {
100  auto sle = lpLedger->read(keylet::unchecked((*i)->key()));
101  if (limit-- <= 0)
102  {
103  // Stop processing before the current key.
104  auto k = sle->key();
105  jvResult[jss::marker] = to_string(--k);
106  break;
107  }
108 
109  if (type == ltINVALID || sle->getType () == type)
110  {
111  if (isBinary)
112  {
113  Json::Value& entry = nodes.append (Json::objectValue);
114  entry[jss::data] = serializeHex(*sle);
115  entry[jss::index] = to_string(sle->key());
116  }
117  else
118  {
119  Json::Value& entry = nodes.append (sle->getJson (JsonOptions::none));
120  entry[jss::index] = to_string(sle->key());
121  }
122  }
123  }
124 
125  return jvResult;
126 }
127 
128 } // ripple
ripple::ReadView::info
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
ripple::RPC::JsonContext
Definition: Context.h:52
std::shared_ptr
STL class.
Json::Value::isString
bool isString() const
Definition: json_value.cpp:1049
ripple::LedgerInfo::hash
uint256 hash
Definition: ReadView.h:95
ripple::ReadView::key_type
uint256 key_type
Definition: ReadView.h:193
ripple::ReadView::sles_type::upper_bound
iterator upper_bound(key_type const &key) const
Definition: ReadView.cpp:154
ripple::LedgerInfo::seq
LedgerIndex seq
Definition: ReadView.h:87
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:41
ripple::RPC::Context::role
Role role
Definition: Context.h:47
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:484
ripple::RPC::expected_field_error
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition: ErrorCodes.h:286
ripple::base_uint< 256 >
ripple::ltINVALID
@ ltINVALID
Definition: LedgerFormats.h:50
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:907
ripple::serializeHex
std::string serializeHex(STObject const &o)
Serialize an object to a hex string.
Definition: LedgerToJson.h:79
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:45
ripple::JsonOptions::none
@ none
ripple::ReadView::sles_type::end
iterator const & end() const
Definition: ReadView.cpp:145
ripple::base_uint::SetHex
bool SetHex(const char *psz, bool bStrict=false)
Parse a hex string into a base_uint The input can be:
Definition: base_uint.h:362
ripple::doLedgerData
Json::Value doLedgerData(RPC::JsonContext &)
Definition: LedgerData.cpp:43
ripple::ReadView::sles
sles_type sles
Iterable range of ledger state items.
Definition: ReadView.h:398
ripple::RPC::Tuning::pageLength
constexpr int pageLength(bool isBinary)
Maximum number of pages in a LedgerData response.
Definition: rpc/impl/Tuning.h:68
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::keylet::unchecked
Keylet unchecked(uint256 const &key)
Any ledger entry.
Definition: Indexes.cpp:313
ripple::isUnlimited
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition: Role.cpp:86
ripple::getJson
Json::Value getJson(LedgerFill const &fill)
Return a new Json::Value representing the ledger with given options.
Definition: LedgerToJson.cpp:272
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RPC::chooseLedgerEntryType
std::pair< RPC::Status, LedgerEntryType > chooseLedgerEntryType(Json::Value const &params)
Definition: RPCHelpers.cpp:790
Json::Value::isIntegral
bool isIntegral() const
Definition: json_value.cpp:1026
Json::Value::asInt
Int asInt() const
Definition: json_value.cpp:516
ripple::LedgerFill
Definition: LedgerToJson.h:32
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:63
Json::Value
Represents a JSON value.
Definition: json_value.h:141
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:482