xrpld
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/SpecBridge.hpp>
8#include <xrpld/rpc/detail/Tuning.h>
9
10#include <xrpl/basics/base_uint.h>
11#include <xrpl/json/json_value.h>
12#include <xrpl/ledger/ReadView.h>
13#include <xrpl/protocol/ErrorCodes.h>
14#include <xrpl/protocol/Indexes.h>
15#include <xrpl/protocol/LedgerFormats.h>
16#include <xrpl/protocol/Serializer.h>
17#include <xrpl/protocol/jss.h>
18#include <xrpl/protocol/serialize.h>
19
20#include <grpcpp/support/status.h>
21#include <org/xrpl/rpc/v1/get_ledger_data.pb.h>
22
23#include <memory>
24#include <utility>
25
26namespace xrpl {
27
28// Get state nodes from a ledger
29// Inputs:
30// limit: integer, maximum number of entries
31// marker: opaque, resume point
32// binary: boolean, format
33// type: string // optional, defaults to all ledger node types
34// Outputs:
35// ledger_hash: chosen ledger's hash
36// ledger_index: chosen ledger's index
37// state: array of state nodes
38// marker: resume point, if any
39json::Value
41{
43 auto const& params = context.params;
44
45 auto jvResult = rpc::lookupLedger(lpLedger, context);
46 if (!lpLedger)
47 return jvResult;
48
49 bool const isMarker = params.isMember(jss::marker);
51 if (isMarker)
52 {
53 json::Value const& jMarker = params[jss::marker];
54 if (!(jMarker.isString() && key.parseHex(jMarker.asString())))
55 return rpc::expectedFieldError(jss::marker, "valid");
56 }
57
58 bool isBinary = false;
59 if (params.isMember(jss::binary))
60 {
61 if (!params[jss::binary].isBool())
62 return rpc::expectedFieldError(jss::binary, "boolean");
63 isBinary = params[jss::binary].asBool();
64 }
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::expectedFieldError(jss::limit, "integer");
72
73 limit = jLimit.asInt();
74 }
75
76 auto const 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->header().hash);
81 jvResult[jss::ledger_index] = lpLedger->header().seq;
82
83 if (!isMarker)
84 {
85 // Return base ledger data on first query
86 jvResult[jss::ledger] = getJson(LedgerFill(
87 *lpLedger, &context, isBinary ? static_cast<int>(LedgerFill::Options::Binary) : 0));
88 }
89
90 auto [rpcStatus, type] = rpc::chooseLedgerEntryType(params);
91 if (rpcStatus)
92 {
93 jvResult.clear();
94 rpc::injectSpecError(jvResult, rpcStatus);
95 return jvResult;
96 }
97 json::Value& nodes = jvResult[jss::state];
98 if (nodes.type() == json::ValueType::Null)
99 {
101 }
102
103 auto const e = lpLedger->sles.end();
104 for (auto i = lpLedger->sles.upperBound(key); i != e; ++i)
105 {
106 auto const sle = lpLedger->read(keylet::unchecked((*i)->key()));
107 if (limit-- <= 0)
108 {
109 // Stop processing before the current key.
110 auto k = sle->key();
111 jvResult[jss::marker] = to_string(--k);
112 break;
113 }
114
115 if (type == ltANY || sle->getType() == type)
116 {
117 if (isBinary)
118 {
120 entry[jss::data] = serializeHex(*sle);
121 entry[jss::index] = to_string(sle->key());
122 }
123 else
124 {
125 json::Value& entry = nodes.append(sle->getJson(JsonOptions::Values::None));
126 entry[jss::index] = to_string(sle->key());
127 }
128 }
129 }
130
131 return jvResult;
132}
133
136{
137 org::xrpl::rpc::v1::GetLedgerDataRequest const& request = context.params;
138 org::xrpl::rpc::v1::GetLedgerDataResponse response;
139 grpc::Status const status = grpc::Status::OK;
140
142 if (auto const status = rpc::ledgerFromRequest(ledger, context))
143 {
144 grpc::Status errorStatus;
145 if (status == RpcInvalidParams)
146 {
147 errorStatus = grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, status.message);
148 }
149 else
150 {
151 errorStatus = 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().empty())
162 {
163 grpc::Status const errorStatus{grpc::StatusCode::INVALID_ARGUMENT, "marker malformed"};
164 return {response, errorStatus};
165 }
166
167 auto e = ledger->sles.end();
168 if (!request.end_marker().empty())
169 {
170 auto const key = UInt256::fromVoidChecked(request.end_marker());
171
172 if (!key)
173 return {response, {grpc::StatusCode::INVALID_ARGUMENT, "end marker malformed"}};
174
175 if (*key < startKey)
176 return {response, {grpc::StatusCode::INVALID_ARGUMENT, "end marker out of range"}};
177
178 e = ledger->sles.upperBound(*key);
179 }
180
181 int maxLimit = rpc::tuning::pageLength(true);
182
183 for (auto i = ledger->sles.upperBound(startKey); i != e; ++i)
184 {
185 auto const sle = ledger->read(keylet::unchecked((*i)->key()));
186 if (maxLimit-- <= 0)
187 {
188 // Stop processing before the current key.
189 auto k = sle->key();
190 --k;
191 response.set_marker(k.data(), k.size());
192 break;
193 }
194 auto stateObject = response.mutable_ledger_objects()->add_objects();
195 Serializer s;
196 sle->add(s);
197 stateObject->set_data(s.peekData().data(), s.getLength());
198 stateObject->set_key(sle->key().data(), sle->key().size());
199 }
200 return {response, status};
201}
202
203} // namespace xrpl
Represents a JSON value.
Definition json_value.h:121
bool isIntegral() const
bool isString() const
Value & append(Value const &value)
Append value to array at the end.
ValueType type() const
std::string asString() const
Returns the unquoted string value.
Int asInt() const
static std::optional< BaseUInt > fromVoidChecked(T const &from)
Definition base_uint.h:347
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:526
UInt256 key_type
Definition ReadView.h:45
Blob const & peekData() const
Definition Serializer.h:273
int getLength() const
Definition Serializer.h:304
T data(T... args)
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
@ Null
'null' value
Definition json_value.h:26
Keylet unchecked(UInt256 const &key) noexcept
Any ledger entry.
Definition Indexes.cpp:409
constexpr int pageLength(bool isBinary)
Maximum number of pages in a LedgerData response.
json::Value expectedFieldError(std::string const &name, std::string const &type)
Definition ErrorCodes.h:309
::rpc::Status ledgerFromRequest(T &ledger, GRPCContext< R > const &context)
Retrieves a ledger from a gRPC request context.
::rpc::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.
std::pair<::rpc::Status, LedgerEntryType > chooseLedgerEntryType(json::Value const &params)
Chooses the ledger entry type based on RPC parameters.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcInvalidParams
Definition ErrorCodes.h:67
json::Value doLedgerData(rpc::JsonContext &)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
BaseUInt< 256 > UInt256
Definition base_uint.h:581
std::string serializeHex(STObject const &o)
Serialize an object to a hex string.
Definition serialize.h:28
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 > const &context)
@ 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:115
RequestType params
Definition Context.h:59
json::Value params
Definition Context.h:51