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/RPCLedgerHelpers.h>
7#include <xrpld/rpc/detail/Tuning.h>
8
9#include <xrpl/ledger/ReadView.h>
10#include <xrpl/protocol/ErrorCodes.h>
11#include <xrpl/protocol/LedgerFormats.h>
12#include <xrpl/protocol/jss.h>
13
14namespace ripple {
15
16// Get state nodes from a ledger
17// Inputs:
18// limit: integer, maximum number of entries
19// marker: opaque, resume point
20// binary: boolean, format
21// type: string // optional, defaults to all ledger node types
22// Outputs:
23// ledger_hash: chosen ledger's hash
24// ledger_index: chosen ledger's index
25// state: array of state nodes
26// marker: resume point, if any
29{
31 auto const& params = context.params;
32
33 auto jvResult = RPC::lookupLedger(lpLedger, context);
34 if (!lpLedger)
35 return jvResult;
36
37 bool const isMarker = params.isMember(jss::marker);
39 if (isMarker)
40 {
41 Json::Value const& jMarker = params[jss::marker];
42 if (!(jMarker.isString() && key.parseHex(jMarker.asString())))
43 return RPC::expected_field_error(jss::marker, "valid");
44 }
45
46 bool const isBinary = params[jss::binary].asBool();
47
48 int limit = -1;
49 if (params.isMember(jss::limit))
50 {
51 Json::Value const& jLimit = params[jss::limit];
52 if (!jLimit.isIntegral())
53 return RPC::expected_field_error(jss::limit, "integer");
54
55 limit = jLimit.asInt();
56 }
57
58 auto maxLimit = RPC::Tuning::pageLength(isBinary);
59 if ((limit < 0) || ((limit > maxLimit) && (!isUnlimited(context.role))))
60 limit = maxLimit;
61
62 jvResult[jss::ledger_hash] = to_string(lpLedger->info().hash);
63 jvResult[jss::ledger_index] = lpLedger->info().seq;
64
65 if (!isMarker)
66 {
67 // Return base ledger data on first query
68 jvResult[jss::ledger] = getJson(LedgerFill(
69 *lpLedger, &context, isBinary ? LedgerFill::Options::binary : 0));
70 }
71
72 auto [rpcStatus, type] = RPC::chooseLedgerEntryType(params);
73 if (rpcStatus)
74 {
75 jvResult.clear();
76 rpcStatus.inject(jvResult);
77 return jvResult;
78 }
79 Json::Value& nodes = jvResult[jss::state];
80 if (nodes.type() == Json::nullValue)
81 {
83 }
84
85 auto e = lpLedger->sles.end();
86 for (auto i = lpLedger->sles.upper_bound(key); i != e; ++i)
87 {
88 auto sle = lpLedger->read(keylet::unchecked((*i)->key()));
89 if (limit-- <= 0)
90 {
91 // Stop processing before the current key.
92 auto k = sle->key();
93 jvResult[jss::marker] = to_string(--k);
94 break;
95 }
96
97 if (type == ltANY || sle->getType() == type)
98 {
99 if (isBinary)
100 {
101 Json::Value& entry = nodes.append(Json::objectValue);
102 entry[jss::data] = serializeHex(*sle);
103 entry[jss::index] = to_string(sle->key());
104 }
105 else
106 {
107 Json::Value& entry =
108 nodes.append(sle->getJson(JsonOptions::none));
109 entry[jss::index] = to_string(sle->key());
110 }
111 }
112 }
113
114 return jvResult;
115}
116
120{
121 org::xrpl::rpc::v1::GetLedgerDataRequest& request = context.params;
122 org::xrpl::rpc::v1::GetLedgerDataResponse response;
123 grpc::Status status = grpc::Status::OK;
124
126 if (auto status = RPC::ledgerFromRequest(ledger, context))
127 {
128 grpc::Status errorStatus;
129 if (status.toErrorCode() == rpcINVALID_PARAMS)
130 {
131 errorStatus = grpc::Status(
132 grpc::StatusCode::INVALID_ARGUMENT, status.message());
133 }
134 else
135 {
136 errorStatus =
137 grpc::Status(grpc::StatusCode::NOT_FOUND, status.message());
138 }
139 return {response, errorStatus};
140 }
141
142 uint256 startKey;
143 if (auto key = uint256::fromVoidChecked(request.marker()))
144 {
145 startKey = *key;
146 }
147 else if (request.marker().size() != 0)
148 {
149 grpc::Status errorStatus{
150 grpc::StatusCode::INVALID_ARGUMENT, "marker malformed"};
151 return {response, errorStatus};
152 }
153
154 auto e = ledger->sles.end();
155 if (request.end_marker().size() != 0)
156 {
157 auto const key = uint256::fromVoidChecked(request.end_marker());
158
159 if (!key)
160 return {
161 response,
162 {grpc::StatusCode::INVALID_ARGUMENT, "end marker malformed"}};
163
164 if (*key < startKey)
165 return {
166 response,
167 {grpc::StatusCode::INVALID_ARGUMENT,
168 "end marker out of range"}};
169
170 e = ledger->sles.upper_bound(*key);
171 }
172
173 int maxLimit = RPC::Tuning::pageLength(true);
174
175 for (auto i = ledger->sles.upper_bound(startKey); i != e; ++i)
176 {
177 auto sle = ledger->read(keylet::unchecked((*i)->key()));
178 if (maxLimit-- <= 0)
179 {
180 // Stop processing before the current key.
181 auto k = sle->key();
182 --k;
183 response.set_marker(k.data(), k.size());
184 break;
185 }
186 auto stateObject = response.mutable_ledger_objects()->add_objects();
187 Serializer s;
188 sle->add(s);
189 stateObject->set_data(s.peekData().data(), s.getLength());
190 stateObject->set_key(sle->key().data(), sle->key().size());
191 }
192 return {response, status};
193}
194
195} // namespace ripple
Represents a JSON value.
Definition json_value.h:131
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:20
@ arrayValue
array value (ordered list)
Definition json_value.h:26
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:27
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)