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 xrpl {
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->header().hash);
63 jvResult[jss::ledger_index] = lpLedger->header().seq;
64
65 if (!isMarker)
66 {
67 // Return base ledger data on first query
68 jvResult[jss::ledger] =
69 getJson(LedgerFill(*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 = nodes.append(sle->getJson(JsonOptions::none));
108 entry[jss::index] = to_string(sle->key());
109 }
110 }
111 }
112
113 return jvResult;
114}
115
118{
119 org::xrpl::rpc::v1::GetLedgerDataRequest const& request = context.params;
120 org::xrpl::rpc::v1::GetLedgerDataResponse response;
121 grpc::Status const status = grpc::Status::OK;
122
124 if (auto status = RPC::ledgerFromRequest(ledger, context))
125 {
126 grpc::Status errorStatus;
127 if (status.toErrorCode() == rpcINVALID_PARAMS)
128 {
129 errorStatus = grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, status.message());
130 }
131 else
132 {
133 errorStatus = grpc::Status(grpc::StatusCode::NOT_FOUND, status.message());
134 }
135 return {response, errorStatus};
136 }
137
138 uint256 startKey;
139 if (auto key = uint256::fromVoidChecked(request.marker()))
140 {
141 startKey = *key;
142 }
143 else if (!request.marker().empty())
144 {
145 grpc::Status const errorStatus{grpc::StatusCode::INVALID_ARGUMENT, "marker malformed"};
146 return {response, errorStatus};
147 }
148
149 auto e = ledger->sles.end();
150 if (!request.end_marker().empty())
151 {
152 auto const key = uint256::fromVoidChecked(request.end_marker());
153
154 if (!key)
155 return {response, {grpc::StatusCode::INVALID_ARGUMENT, "end marker malformed"}};
156
157 if (*key < startKey)
158 return {response, {grpc::StatusCode::INVALID_ARGUMENT, "end marker out of range"}};
159
160 e = ledger->sles.upper_bound(*key);
161 }
162
163 int maxLimit = RPC::Tuning::pageLength(true);
164
165 for (auto i = ledger->sles.upper_bound(startKey); i != e; ++i)
166 {
167 auto sle = ledger->read(keylet::unchecked((*i)->key()));
168 if (maxLimit-- <= 0)
169 {
170 // Stop processing before the current key.
171 auto k = sle->key();
172 --k;
173 response.set_marker(k.data(), k.size());
174 break;
175 }
176 auto stateObject = response.mutable_ledger_objects()->add_objects();
177 Serializer s;
178 sle->add(s);
179 stateObject->set_data(s.peekData().data(), s.getLength());
180 stateObject->set_key(sle->key().data(), sle->key().size());
181 }
182 return {response, status};
183}
184
185} // namespace xrpl
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:35
Blob const & peekData() const
Definition Serializer.h:176
int getLength() const
Definition Serializer.h:207
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:476
static std::optional< base_uint > fromVoidChecked(T const &from)
Definition base_uint.h:300
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.
std::pair< RPC::Status, LedgerEntryType > chooseLedgerEntryType(Json::Value const &params)
Chooses the ledger entry type based on RPC parameters.
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition ErrorCodes.h:297
Status ledgerFromRequest(T &ledger, GRPCContext< R > const &context)
Retrieves a ledger from a gRPC request context.
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext const &context, Json::Value &result)
Looks up a ledger from a request and fills a Json::Value with ledger data.
Keylet unchecked(uint256 const &key) noexcept
Any ledger entry.
Definition Indexes.cpp:330
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Json::Value doLedgerData(RPC::JsonContext &)
Json::Value getJson(LedgerFill const &fill)
Return a new Json::Value representing the ledger with given options.
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
std::pair< org::xrpl::rpc::v1::GetLedgerDataResponse, grpc::Status > doLedgerDataGrpc(RPC::GRPCContext< org::xrpl::rpc::v1::GetLedgerDataRequest > &context)
std::string serializeHex(STObject const &o)
Serialize an object to a hex string.
Definition serialize.h:21
@ ltANY
A special type, matching any ledger entry type.
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition Role.cpp:98
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:64
RequestType params
Definition Context.h:51
Json::Value params
Definition Context.h:43