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/LedgerFormats.h>
24 #include <ripple/protocol/jss.h>
25 #include <ripple/rpc/Context.h>
26 #include <ripple/rpc/Role.h>
27 #include <ripple/rpc/impl/RPCHelpers.h>
28 #include <ripple/rpc/impl/Tuning.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
45 {
47  auto const& params = context.params;
48 
49  auto jvResult = RPC::lookupLedger(lpLedger, context);
50  if (!lpLedger)
51  return jvResult;
52 
53  bool const isMarker = params.isMember(jss::marker);
55  if (isMarker)
56  {
57  Json::Value const& jMarker = params[jss::marker];
58  if (!(jMarker.isString() && key.SetHex(jMarker.asString())))
59  return RPC::expected_field_error(jss::marker, "valid");
60  }
61 
62  bool const isBinary = params[jss::binary].asBool();
63 
64  int limit = -1;
65  if (params.isMember(jss::limit))
66  {
67  Json::Value const& jLimit = params[jss::limit];
68  if (!jLimit.isIntegral())
69  return RPC::expected_field_error(jss::limit, "integer");
70 
71  limit = jLimit.asInt();
72  }
73 
74  auto maxLimit = RPC::Tuning::pageLength(isBinary);
75  if ((limit < 0) || ((limit > maxLimit) && (!isUnlimited(context.role))))
76  limit = maxLimit;
77 
78  jvResult[jss::ledger_hash] = to_string(lpLedger->info().hash);
79  jvResult[jss::ledger_index] = lpLedger->info().seq;
80 
81  if (!isMarker)
82  {
83  // Return base ledger data on first query
84  jvResult[jss::ledger] = getJson(
85  LedgerFill(*lpLedger, isBinary ? 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 =
120  nodes.append(sle->getJson(JsonOptions::none));
121  entry[jss::index] = to_string(sle->key());
122  }
123  }
124  }
125 
126  return jvResult;
127 }
128 
129 } // namespace 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:1009
ripple::LedgerInfo::hash
uint256 hash
Definition: ReadView.h:96
ripple::ReadView::key_type
uint256 key_type
Definition: ReadView.h:194
ripple::ReadView::sles_type::upper_bound
iterator upper_bound(key_type const &key) const
Definition: ReadView.cpp:146
ripple::LedgerInfo::seq
LedgerIndex seq
Definition: ReadView.h:88
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:481
ripple::RPC::expected_field_error
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition: ErrorCodes.h:302
ripple::base_uint< 256 >
ripple::ltINVALID
@ ltINVALID
Definition: LedgerFormats.h:49
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:882
ripple::serializeHex
std::string serializeHex(STObject const &o)
Serialize an object to a hex string.
Definition: LedgerToJson.h:82
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::JsonOptions::none
@ none
ripple::ReadView::sles_type::end
iterator const & end() const
Definition: ReadView.cpp:138
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:406
ripple::doLedgerData
Json::Value doLedgerData(RPC::JsonContext &)
Definition: LedgerData.cpp:44
ripple::ReadView::sles
sles_type sles
Iterable range of ledger state items.
Definition: ReadView.h:383
ripple::RPC::Tuning::pageLength
constexpr int pageLength(bool isBinary)
Maximum number of pages in a LedgerData response.
Definition: rpc/impl/Tuning.h:69
ripple::keylet::unchecked
Keylet unchecked(uint256 const &key) noexcept
Any ledger entry.
Definition: Indexes.cpp:270
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::isUnlimited
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition: Role.cpp:94
ripple::getJson
Json::Value getJson(LedgerFill const &fill)
Return a new Json::Value representing the ledger with given options.
Definition: LedgerToJson.cpp:283
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:781
Json::Value::isIntegral
bool isIntegral() const
Definition: json_value.cpp:991
Json::Value::asInt
Int asInt() const
Definition: json_value.cpp:503
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:145
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469