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/GRPCHandlers.h>
27 #include <ripple/rpc/Role.h>
28 #include <ripple/rpc/impl/GRPCHelpers.h>
29 #include <ripple/rpc/impl/RPCHelpers.h>
30 #include <ripple/rpc/impl/Tuning.h>
31 
32 namespace ripple {
33 
34 // Get state nodes from a ledger
35 // Inputs:
36 // limit: integer, maximum number of entries
37 // marker: opaque, resume point
38 // binary: boolean, format
39 // type: string // optional, defaults to all ledger node types
40 // Outputs:
41 // ledger_hash: chosen ledger's hash
42 // ledger_index: chosen ledger's index
43 // state: array of state nodes
44 // marker: resume point, if any
47 {
49  auto const& params = context.params;
50 
51  auto jvResult = RPC::lookupLedger(lpLedger, context);
52  if (!lpLedger)
53  return jvResult;
54 
55  bool const isMarker = params.isMember(jss::marker);
57  if (isMarker)
58  {
59  Json::Value const& jMarker = params[jss::marker];
60  if (!(jMarker.isString() && key.parseHex(jMarker.asString())))
61  return RPC::expected_field_error(jss::marker, "valid");
62  }
63 
64  bool const isBinary = params[jss::binary].asBool();
65 
66  int limit = -1;
67  if (params.isMember(jss::limit))
68  {
69  Json::Value const& jLimit = params[jss::limit];
70  if (!jLimit.isIntegral())
71  return RPC::expected_field_error(jss::limit, "integer");
72 
73  limit = jLimit.asInt();
74  }
75 
76  auto maxLimit = RPC::Tuning::pageLength(isBinary);
77  if ((limit < 0) || ((limit > maxLimit) && (!isUnlimited(context.role))))
78  limit = maxLimit;
79 
80  jvResult[jss::ledger_hash] = to_string(lpLedger->info().hash);
81  jvResult[jss::ledger_index] = lpLedger->info().seq;
82 
83  if (!isMarker)
84  {
85  // Return base ledger data on first query
86  jvResult[jss::ledger] = getJson(LedgerFill(
87  *lpLedger, &context, isBinary ? LedgerFill::Options::binary : 0));
88  }
89 
90  auto [rpcStatus, type] = RPC::chooseLedgerEntryType(params);
91  if (rpcStatus)
92  {
93  jvResult.clear();
94  rpcStatus.inject(jvResult);
95  return jvResult;
96  }
97  Json::Value& nodes = jvResult[jss::state];
98 
99  auto e = lpLedger->sles.end();
100  for (auto i = lpLedger->sles.upper_bound(key); i != e; ++i)
101  {
102  auto sle = lpLedger->read(keylet::unchecked((*i)->key()));
103  if (limit-- <= 0)
104  {
105  // Stop processing before the current key.
106  auto k = sle->key();
107  jvResult[jss::marker] = to_string(--k);
108  break;
109  }
110 
111  if (type == ltANY || sle->getType() == type)
112  {
113  if (isBinary)
114  {
115  Json::Value& entry = nodes.append(Json::objectValue);
116  entry[jss::data] = serializeHex(*sle);
117  entry[jss::index] = to_string(sle->key());
118  }
119  else
120  {
121  Json::Value& entry =
122  nodes.append(sle->getJson(JsonOptions::none));
123  entry[jss::index] = to_string(sle->key());
124  }
125  }
126  }
127 
128  return jvResult;
129 }
130 
134 {
135  org::xrpl::rpc::v1::GetLedgerDataRequest& request = context.params;
136  org::xrpl::rpc::v1::GetLedgerDataResponse response;
137  grpc::Status status = grpc::Status::OK;
138 
140  if (auto status = RPC::ledgerFromRequest(ledger, context))
141  {
142  grpc::Status errorStatus;
143  if (status.toErrorCode() == rpcINVALID_PARAMS)
144  {
145  errorStatus = grpc::Status(
146  grpc::StatusCode::INVALID_ARGUMENT, status.message());
147  }
148  else
149  {
150  errorStatus =
151  grpc::Status(grpc::StatusCode::NOT_FOUND, status.message());
152  }
153  return {response, errorStatus};
154  }
155 
156  uint256 startKey;
157  if (auto key = uint256::fromVoidChecked(request.marker()))
158  {
159  startKey = *key;
160  }
161  else if (request.marker().size() != 0)
162  {
163  grpc::Status errorStatus{
164  grpc::StatusCode::INVALID_ARGUMENT, "marker malformed"};
165  return {response, errorStatus};
166  }
167 
168  auto e = ledger->sles.end();
169  if (auto key = uint256::fromVoidChecked(request.end_marker()))
170  {
171  e = ledger->sles.upper_bound(*key);
172  }
173  else if (request.end_marker().size() != 0)
174  {
175  grpc::Status errorStatus{
176  grpc::StatusCode::INVALID_ARGUMENT, "end marker malformed"};
177  return {response, errorStatus};
178  }
179 
180  int maxLimit = RPC::Tuning::pageLength(true);
181 
182  for (auto i = ledger->sles.upper_bound(startKey); i != e; ++i)
183  {
184  auto sle = ledger->read(keylet::unchecked((*i)->key()));
185  if (maxLimit-- <= 0)
186  {
187  // Stop processing before the current key.
188  auto k = sle->key();
189  --k;
190  response.set_marker(k.data(), k.size());
191  break;
192  }
193  auto stateObject = response.mutable_ledger_objects()->add_objects();
194  Serializer s;
195  sle->add(s);
196  stateObject->set_data(s.peekData().data(), s.getLength());
197  stateObject->set_key(sle->key().data(), sle->key().size());
198  }
199  return {response, status};
200 }
201 
202 } // namespace ripple
ripple::ReadView::info
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
ripple::RPC::JsonContext
Definition: Context.h:53
std::shared_ptr
STL class.
ripple::rpcINVALID_PARAMS
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
Json::Value::isString
bool isString() const
Definition: json_value.cpp:1009
ripple::ltANY
@ ltANY
A special type, matching any ledger entry type.
Definition: LedgerFormats.h:164
std::pair
ripple::LedgerInfo::hash
uint256 hash
Definition: ReadView.h:100
ripple::ReadView::key_type
uint256 key_type
Definition: ReadView.h:198
ripple::ReadView::sles_type::upper_bound
iterator upper_bound(key_type const &key) const
Definition: ReadView.cpp:143
ripple::LedgerInfo::seq
LedgerIndex seq
Definition: ReadView.h:92
ripple::RPC::Context::role
Role role
Definition: Context.h:47
ripple::base_uint< 256 >::fromVoidChecked
static std::optional< base_uint > fromVoidChecked(T const &from)
Definition: base_uint.h:312
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:540
ripple::RPC::expected_field_error
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition: ErrorCodes.h:309
ripple::base_uint< 256 >
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:85
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 end() const
Definition: ReadView.cpp:137
ripple::RPC::GRPCContext
Definition: Context.h:70
ripple::doLedgerData
Json::Value doLedgerData(RPC::JsonContext &)
Definition: LedgerData.cpp:46
ripple::ReadView::sles
sles_type sles
Iterable range of ledger state items.
Definition: ReadView.h:387
ripple::RPC::Tuning::pageLength
constexpr int pageLength(bool isBinary)
Maximum number of pages in a LedgerData response.
Definition: rpc/impl/Tuning.h:69
ripple::doLedgerDataGrpc
std::pair< org::xrpl::rpc::v1::GetLedgerDataResponse, grpc::Status > doLedgerDataGrpc(RPC::GRPCContext< org::xrpl::rpc::v1::GetLedgerDataRequest > &context)
Definition: LedgerData.cpp:132
ripple::keylet::unchecked
Keylet unchecked(uint256 const &key) noexcept
Any ledger entry.
Definition: Indexes.cpp:294
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::Serializer
Definition: Serializer.h:39
ripple::getJson
Json::Value getJson(LedgerFill const &fill)
Return a new Json::Value representing the ledger with given options.
Definition: LedgerToJson.cpp:291
ripple::RPC::GRPCContext::params
RequestType params
Definition: Context.h:72
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:840
ripple::Serializer::peekData
Blob const & peekData() const
Definition: Serializer.h:168
Json::Value::isIntegral
bool isIntegral() const
Definition: json_value.cpp:991
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:38
Json::Value::asInt
Int asInt() const
Definition: json_value.cpp:503
ripple::base_uint::parseHex
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:475
ripple::Serializer::getLength
int getLength() const
Definition: Serializer.h:199
ripple::LedgerFill
Definition: LedgerToJson.h:33
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
std::vector::data
T data(T... args)
ripple::RPC::ledgerFromRequest
Status ledgerFromRequest(T &ledger, GRPCContext< R > &context)
Definition: RPCHelpers.cpp:253
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