1#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
3#include <xrpld/app/ledger/InboundLedger.h>
4#include <xrpld/app/ledger/InboundLedgers.h>
5#include <xrpld/app/ledger/LedgerMaster.h>
6#include <xrpld/app/ledger/LedgerToJson.h>
7#include <xrpld/app/main/Application.h>
8#include <xrpld/rpc/Context.h>
9#include <xrpld/rpc/Status.h>
10#include <xrpld/rpc/detail/Tuning.h>
12#include <xrpl/basics/base_uint.h>
13#include <xrpl/beast/core/LexicalCast.h>
14#include <xrpl/beast/utility/Zero.h>
15#include <xrpl/beast/utility/instrumentation.h>
16#include <xrpl/json/json_value.h>
17#include <xrpl/ledger/View.h>
18#include <xrpl/protocol/ErrorCodes.h>
19#include <xrpl/protocol/LedgerShortcut.h>
20#include <xrpl/protocol/RPCErr.h>
21#include <xrpl/protocol/RippleLedgerHash.h>
22#include <xrpl/protocol/jss.h>
24#include <org/xrpl/rpc/v1/get_ledger.pb.h>
25#include <org/xrpl/rpc/v1/get_ledger_data.pb.h>
26#include <org/xrpl/rpc/v1/get_ledger_entry.pb.h>
27#include <org/xrpl/rpc/v1/ledger.pb.h>
38isValidatedOld(
LedgerMaster& ledgerMaster,
bool standalone)
52 json::StaticString
const fieldName)
55 if (!ledgerHash.parseHex(hash.
asString()))
57 return getLedger(ledger, ledgerHash, context);
64 json::Value indexValue,
66 json::StaticString
const fieldName)
68 auto const index = indexValue.
asString();
70 if (index ==
"current" || index.empty())
73 if (index ==
"validated")
76 if (index ==
"closed")
79 std::uint32_t iVal = 0;
92 auto& params = context.params;
93 auto const hasLedger = context.params.isMember(jss::ledger);
94 auto const hasHash = context.params.isMember(jss::ledger_hash);
95 auto const hasIndex = context.params.isMember(jss::ledger_index);
97 if ((hasLedger + hasHash + hasIndex) > 1)
105 "Exactly one of 'ledger', 'ledger_hash', or "
106 "'ledger_index' can be specified."};
110 "Exactly one of 'ledger_hash' or "
111 "'ledger_index' can be specified."};
117 auto& legacyLedger = params[jss::ledger];
118 if (!legacyLedger.isString() && !legacyLedger.isUInt() && !legacyLedger.isInt())
122 if (legacyLedger.isString() && legacyLedger.asString().size() == 64)
124 return ledgerFromHash(ledger, legacyLedger, context, jss::ledger);
127 return ledgerFromIndex(ledger, legacyLedger, context, jss::ledger);
132 auto const& ledgerHash = params[jss::ledger_hash];
133 if (!ledgerHash.isString())
135 return ledgerFromHash(ledger, ledgerHash, context, jss::ledger_hash);
140 auto const& ledgerIndex = params[jss::ledger_index];
141 if (!ledgerIndex.isString() && !ledgerIndex.isUInt() && !ledgerIndex.isInt())
145 return ledgerFromIndex(ledger, ledgerIndex, context, jss::ledger_index);
153template <
class T,
class R>
157 R
const& request = context.
params;
183 org::xrpl::rpc::v1::LedgerSpecifier
const& specifier,
188 using LedgerCase = org::xrpl::rpc::v1::LedgerSpecifier::LedgerCase;
189 LedgerCase
const ledgerCase = specifier.ledger_case();
192 case LedgerCase::kHash: {
195 return getLedger(ledger, *hash, context);
199 case LedgerCase::kSequence:
200 return getLedger(ledger, specifier.sequence(), context);
201 case LedgerCase::kShortcut:
203 case LedgerCase::LEDGER_NOT_SET: {
204 auto const shortcut = specifier.shortcut();
205 if (shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_VALIDATED)
210 if (shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_CURRENT ||
211 shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_UNSPECIFIED)
215 if (shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_CLOSED)
230 if (ledger ==
nullptr)
240 if (ledger ==
nullptr)
243 if (cur->header().seq == ledgerIndex)
249 if (ledger ==
nullptr)
278 if (ledger ==
nullptr)
285 XRPL_ASSERT(!ledger->open(),
"xrpl::rpc::getLedger : validated is not open");
292 XRPL_ASSERT(ledger->open(),
"xrpl::rpc::getLedger : current is open");
297 XRPL_ASSERT(!ledger->open(),
"xrpl::rpc::getLedger : closed is not open");
304 if (ledger ==
nullptr)
311 static auto const kMinSequenceGap = 10;
338 org::xrpl::rpc::v1::LedgerSpecifier
const&,
369 auto& info = ledger->header();
373 result[jss::ledger_hash] =
to_string(info.hash);
374 result[jss::ledger_index] = info.seq;
378 result[jss::ledger_current_index] = info.seq;
389 if (
auto status =
lookupLedger(ledger, context, result))
390 status.inject(result);
395std::expected<std::shared_ptr<Ledger const>,
json::Value>
405 if ((
static_cast<int>(hasHash) +
static_cast<int>(hasIndex)) != 1)
409 "Exactly one of 'ledger_hash' or "
410 "'ledger_index' can be specified."));
416 if (!jsonHash.isString() || !ledgerHash.
parseHex(jsonHash.asString()))
422 if (!jsonIndex.isInt() && !jsonIndex.isUInt())
433 ledgerIndex = jsonIndex.asInt();
434 auto ledger = ledgerMaster.getValidatedLedger();
436 if (ledgerIndex >= ledger->header().seq)
438 if (ledgerIndex <= 0)
444 auto neededHash =
hashOfSeq(*ledger, ledgerIndex, j);
450 auto refHash =
hashOfSeq(*ledger, refIndex, j);
451 XRPL_ASSERT(refHash,
"xrpl::rpc::getOrAcquireLedger : nonzero ledger hash");
454 ledger = ledgerMaster.getLedgerByHash(*refHash);
474 jvResult[jss::acquiring] = il->getJson(0);
482 neededHash =
hashOfSeq(*ledger, ledgerIndex, j);
484 XRPL_ASSERT(neededHash,
"xrpl::rpc::getOrAcquireLedger : nonzero needed hash");
495 ledger = ledgerMaster.getLedgerByHash(ledgerHash);
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
virtual Config & config()=0
static std::optional< BaseUInt > fromVoidChecked(T const &from)
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
virtual std::shared_ptr< Ledger const > acquire(uint256 const &hash, std::uint32_t seq, InboundLedger::Reason)=0
virtual std::shared_ptr< InboundLedger > find(LedgerHash const &hash)=0
std::shared_ptr< Ledger const > getLedgerBySeq(std::uint32_t index)
std::shared_ptr< Ledger const > getClosedLedger()
std::shared_ptr< Ledger const > getValidatedLedger()
bool isValidated(ReadView const &ledger)
LedgerIndex getValidLedgerIndex()
std::shared_ptr< ReadView const > getCurrentLedger()
std::shared_ptr< Ledger const > getLedgerByHash(uint256 const &hash)
virtual beast::Journal getJournal(std::string const &name)=0
virtual InboundLedgers & getInboundLedgers()=0
virtual LedgerMaster & getLedgerMaster()=0
constexpr bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
constexpr auto kMaxValidatedLedgerAge
API version numbers used in later API versions.
json::Value expectedFieldError(std::string const &name, std::string const &type)
std::expected< std::shared_ptr< Ledger const >, json::Value > getOrAcquireLedger(rpc::JsonContext const &context)
Retrieves or acquires a ledger based on the parameters provided in the given JsonContext.
Status ledgerFromRequest(T &ledger, GRPCContext< R > const &context)
Retrieves a ledger from a gRPC request context.
Status getLedger(T &ledger, uint256 const &ledgerHash, Context const &context)
Retrieves a ledger by its hash.
json::Value makeParamError(std::string const &message)
Returns a new json object that indicates invalid parameters.
Status ledgerFromSpecifier(T &ledger, org::xrpl::rpc::v1::LedgerSpecifier const &specifier, Context const &context)
Retrieves a ledger based on a LedgerSpecifier.
std::string expectedFieldMessage(std::string const &name, std::string const &type)
json::Value makeError(ErrorCodeI code)
Returns a new json object that reflects the error code.
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.
LedgerShortcut
Enumeration of ledger shortcuts for specifying which ledger to use.
@ Closed
The most recently closed ledger (may not be validated).
@ Current
The current working ledger (open, not yet closed).
@ Validated
The most recently validated ledger.
std::string to_string(BaseUInt< Bits, Tag > const &a)
LedgerIndex getCandidateLedger(LedgerIndex requested)
Find a ledger index from which we could easily get the requested ledger.
json::Value rpcError(ErrorCodeI iError)
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
json::Value getJson(LedgerFill const &fill)
Return a new json::Value representing the ledger with given options.
@ LedgerMaster
ledger master data for signing
The context of information needed to call an RPC.
LedgerMaster & ledgerMaster
Status represents the results of an operation that might fail.
static constexpr Code kOK