rippled
Loading...
Searching...
No Matches
LedgerData.cpp
1#include <xrpld/app/ledger/LedgerToJson.h>
2#include <xrpld/rpc/Context.h>
3#include <xrpld/rpc/GRPCHandlers.h>
4#include <xrpld/rpc/Role.h>
5#include <xrpld/rpc/detail/RPCHelpers.h>
6#include <xrpld/rpc/detail/Tuning.h>
7
8#include <xrpl/ledger/ReadView.h>
9#include <xrpl/protocol/ErrorCodes.h>
10#include <xrpl/protocol/LedgerFormats.h>
11#include <xrpl/protocol/jss.h>
12
13namespace ripple {
14
15// Get state nodes from a ledger
16// Inputs:
17// limit: integer, maximum number of entries
18// marker: opaque, resume point
19// binary: boolean, format
20// type: string // optional, defaults to all ledger node types
21// Outputs:
22// ledger_hash: chosen ledger's hash
23// ledger_index: chosen ledger's index
24// state: array of state nodes
25// marker: resume point, if any
28{
30 auto const& params = context.params;
31
32 auto jvResult = RPC::lookupLedger(lpLedger, context);
33 if (!lpLedger)
34 return jvResult;
35
36 bool const isMarker = params.isMember(jss::marker);
38 if (isMarker)
39 {
40 Json::Value const& jMarker = params[jss::marker];
41 if (!(jMarker.isString() && key.parseHex(jMarker.asString())))
42 return RPC::expected_field_error(jss::marker, "valid");
43 }
44
45 bool const isBinary = params[jss::binary].asBool();
46
47 int limit = -1;
48 if (params.isMember(jss::limit))
49 {
50 Json::Value const& jLimit = params[jss::limit];
51 if (!jLimit.isIntegral())
52 return RPC::expected_field_error(jss::limit, "integer");
53
54 limit = jLimit.asInt();
55 }
56
57 auto maxLimit = RPC::Tuning::pageLength(isBinary);
58 if ((limit < 0) || ((limit > maxLimit) && (!isUnlimited(context.role))))
59 limit = maxLimit;
60
61 jvResult[jss::ledger_hash] = to_string(lpLedger->info().hash);
62 jvResult[jss::ledger_index] = lpLedger->info().seq;
63
64 if (!isMarker)
65 {
66 // Return base ledger data on first query
67 jvResult[jss::ledger] = getJson(LedgerFill(
68 *lpLedger, &context, isBinary ? LedgerFill::Options::binary : 0));
69 }
70
71 auto [rpcStatus, type] = RPC::chooseLedgerEntryType(params);
72 if (rpcStatus)
73 {
74 jvResult.clear();
75 rpcStatus.inject(jvResult);
76 return jvResult;
77 }
78 Json::Value& nodes = jvResult[jss::state];
79 if (nodes.type() == Json::nullValue)
80 {
82 }
83
84 auto e = lpLedger->sles.end();
85 for (auto i = lpLedger->sles.upper_bound(key); i != e; ++i)
86 {
87 auto sle = lpLedger->read(keylet::unchecked((*i)->key()));
88 if (limit-- <= 0)
89 {
90 // Stop processing before the current key.
91 auto k = sle->key();
92 jvResult[jss::marker] = to_string(--k);
93 break;
94 }
95
96 if (type == ltANY || sle->getType() == type)
97 {
98 if (isBinary)
99 {
100 Json::Value& entry = nodes.append(Json::objectValue);
101 entry[jss::data] = serializeHex(*sle);
102 entry[jss::index] = to_string(sle->key());
103 }
104 else
105 {
106 Json::Value& entry =
107 nodes.append(sle->getJson(JsonOptions::none));
108 entry[jss::index] = to_string(sle->key());
109 }
110 }
111 }
112
113 return jvResult;
114}
115
119{
120 org::xrpl::rpc::v1::GetLedgerDataRequest& request = context.params;
121 org::xrpl::rpc::v1::GetLedgerDataResponse response;
122 grpc::Status status = grpc::Status::OK;
123
125 if (auto status = RPC::ledgerFromRequest(ledger, context))
126 {
127 grpc::Status errorStatus;
128 if (status.toErrorCode() == rpcINVALID_PARAMS)
129 {
130 errorStatus = grpc::Status(
131 grpc::StatusCode::INVALID_ARGUMENT, status.message());
132 }
133 else
134 {
135 errorStatus =
136 grpc::Status(grpc::StatusCode::NOT_FOUND, status.message());
137 }
138 return {response, errorStatus};
139 }
140
141 uint256 startKey;
142 if (auto key = uint256::fromVoidChecked(request.marker()))
143 {
144 startKey = *key;
145 }
146 else if (request.marker().size() != 0)
147 {
148 grpc::Status errorStatus{
149 grpc::StatusCode::INVALID_ARGUMENT, "marker malformed"};
150 return {response, errorStatus};
151 }
152
153 auto e = ledger->sles.end();
154 if (request.end_marker().size() != 0)
155 {
156 auto const key = uint256::fromVoidChecked(request.end_marker());
157
158 if (!key)
159 return {
160 response,
161 {grpc::StatusCode::INVALID_ARGUMENT, "end marker malformed"}};
162
163 if (*key < startKey)
164 return {
165 response,
166 {grpc::StatusCode::INVALID_ARGUMENT,
167 "end marker out of range"}};
168
169 e = ledger->sles.upper_bound(*key);
170 }
171
172 int maxLimit = RPC::Tuning::pageLength(true);
173
174 for (auto i = ledger->sles.upper_bound(startKey); i != e; ++i)
175 {
176 auto sle = ledger->read(keylet::unchecked((*i)->key()));
177 if (maxLimit-- <= 0)
178 {
179 // Stop processing before the current key.
180 auto k = sle->key();
181 --k;
182 response.set_marker(k.data(), k.size());
183 break;
184 }
185 auto stateObject = response.mutable_ledger_objects()->add_objects();
186 Serializer s;
187 sle->add(s);
188 stateObject->set_data(s.peekData().data(), s.getLength());
189 stateObject->set_key(sle->key().data(), sle->key().size());
190 }
191 return {response, status};
192}
193
194} // namespace ripple
Represents a JSON value.
Definition json_value.h:130
Value & append(Value const &value)
Append value to array at the end.
Int asInt() const
bool isString() const
ValueType type() const
std::string asString() const
Returns the unquoted string value.
bool isIntegral() const
uint256 key_type
Definition ReadView.h:37
Blob const & peekData() const
Definition Serializer.h:183
int getLength() const
Definition Serializer.h:214
static std::optional< base_uint > fromVoidChecked(T const &from)
Definition base_uint.h:307
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:484
T data(T... args)
@ nullValue
'null' value
Definition json_value.h:19
@ arrayValue
array value (ordered list)
Definition json_value.h:25
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:26
int constexpr pageLength(bool isBinary)
Maximum number of pages in a LedgerData response.
Status ledgerFromRequest(T &ledger, GRPCContext< R > &context)
std::pair< RPC::Status, LedgerEntryType > chooseLedgerEntryType(Json::Value const &params)
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition ErrorCodes.h:330
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.
Keylet unchecked(uint256 const &key) noexcept
Any ledger entry.
Definition Indexes.cpp:349
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:65
std::string serializeHex(STObject const &o)
Serialize an object to a hex string.
Definition serialize.h:22
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition Role.cpp:106
Json::Value doLedgerData(RPC::JsonContext &)
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
@ ltANY
A special type, matching any ledger entry type.
Json::Value getJson(LedgerFill const &fill)
Return a new Json::Value representing the ledger with given options.
std::pair< org::xrpl::rpc::v1::GetLedgerDataResponse, grpc::Status > doLedgerDataGrpc(RPC::GRPCContext< org::xrpl::rpc::v1::GetLedgerDataRequest > &context)