1#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
3#include <xrpld/app/ledger/InboundLedger.h>
4#include <xrpld/app/ledger/LedgerMaster.h>
5#include <xrpld/app/ledger/LedgerToJson.h>
6#include <xrpld/app/main/Application.h>
7#include <xrpld/rpc/Context.h>
8#include <xrpld/rpc/Status.h>
9#include <xrpld/rpc/detail/Tuning.h>
11#include <xrpl/basics/base_uint.h>
12#include <xrpl/beast/core/LexicalCast.h>
13#include <xrpl/beast/utility/Zero.h>
14#include <xrpl/beast/utility/instrumentation.h>
15#include <xrpl/json/json_value.h>
16#include <xrpl/ledger/View.h>
17#include <xrpl/protocol/ErrorCodes.h>
18#include <xrpl/protocol/LedgerShortcut.h>
19#include <xrpl/protocol/RPCErr.h>
20#include <xrpl/protocol/RippleLedgerHash.h>
21#include <xrpl/protocol/jss.h>
23#include <org/xrpl/rpc/v1/ledger.pb.h>
34isValidatedOld(
LedgerMaster& ledgerMaster,
bool standalone)
48 json::StaticString
const fieldName)
51 if (!ledgerHash.parseHex(hash.
asString()))
53 return getLedger(ledger, ledgerHash, context);
60 json::Value indexValue,
62 json::StaticString
const fieldName)
64 auto const index = indexValue.
asString();
66 if (index ==
"current" || index.empty())
69 if (index ==
"validated")
72 if (index ==
"closed")
75 std::uint32_t iVal = 0;
88 auto& params = context.params;
89 auto const hasLedger = context.params.isMember(jss::ledger);
90 auto const hasHash = context.params.isMember(jss::ledger_hash);
91 auto const hasIndex = context.params.isMember(jss::ledger_index);
93 if ((hasLedger + hasHash + hasIndex) > 1)
101 "Exactly one of 'ledger', 'ledger_hash', or "
102 "'ledger_index' can be specified."};
106 "Exactly one of 'ledger_hash' or "
107 "'ledger_index' can be specified."};
113 auto& legacyLedger = params[jss::ledger];
114 if (!legacyLedger.isString() && !legacyLedger.isUInt() && !legacyLedger.isInt())
118 if (legacyLedger.isString() && legacyLedger.asString().size() == 64)
120 return ledgerFromHash(ledger, legacyLedger, context, jss::ledger);
123 return ledgerFromIndex(ledger, legacyLedger, context, jss::ledger);
128 auto const& ledgerHash = params[jss::ledger_hash];
129 if (!ledgerHash.isString())
131 return ledgerFromHash(ledger, ledgerHash, context, jss::ledger_hash);
136 auto const& ledgerIndex = params[jss::ledger_index];
137 if (!ledgerIndex.isString() && !ledgerIndex.isUInt() && !ledgerIndex.isInt())
141 return ledgerFromIndex(ledger, ledgerIndex, context, jss::ledger_index);
149template <
class T,
class R>
153 R
const& request = context.
params;
179 org::xrpl::rpc::v1::LedgerSpecifier
const& specifier,
184 using LedgerCase = org::xrpl::rpc::v1::LedgerSpecifier::LedgerCase;
185 LedgerCase
const ledgerCase = specifier.ledger_case();
188 case LedgerCase::kHash: {
191 return getLedger(ledger, *hash, context);
195 case LedgerCase::kSequence:
196 return getLedger(ledger, specifier.sequence(), context);
197 case LedgerCase::kShortcut:
199 case LedgerCase::LEDGER_NOT_SET: {
200 auto const shortcut = specifier.shortcut();
201 if (shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_VALIDATED)
206 if (shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_CURRENT ||
207 shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_UNSPECIFIED)
211 if (shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_CLOSED)
226 if (ledger ==
nullptr)
236 if (ledger ==
nullptr)
239 if (cur->header().seq == ledgerIndex)
245 if (ledger ==
nullptr)
274 if (ledger ==
nullptr)
281 XRPL_ASSERT(!ledger->open(),
"xrpl::RPC::getLedger : validated is not open");
288 XRPL_ASSERT(ledger->open(),
"xrpl::RPC::getLedger : current is open");
293 XRPL_ASSERT(!ledger->open(),
"xrpl::RPC::getLedger : closed is not open");
300 if (ledger ==
nullptr)
307 static auto const kMinSequenceGap = 10;
358 auto& info = ledger->header();
362 result[jss::ledger_hash] =
to_string(info.hash);
363 result[jss::ledger_index] = info.seq;
367 result[jss::ledger_current_index] = info.seq;
378 if (
auto status =
lookupLedger(ledger, context, result))
379 status.inject(result);
384std::expected<std::shared_ptr<Ledger const>,
json::Value>
394 if ((
static_cast<int>(hasHash) +
static_cast<int>(hasIndex)) != 1)
398 "Exactly one of 'ledger_hash' or "
399 "'ledger_index' can be specified."));
405 if (!jsonHash.isString() || !ledgerHash.
parseHex(jsonHash.asString()))
411 if (!jsonIndex.isInt() && !jsonIndex.isUInt())
422 ledgerIndex = jsonIndex.asInt();
423 auto ledger = ledgerMaster.getValidatedLedger();
425 if (ledgerIndex >= ledger->header().seq)
427 if (ledgerIndex <= 0)
433 auto neededHash =
hashOfSeq(*ledger, ledgerIndex, j);
439 auto refHash =
hashOfSeq(*ledger, refIndex, j);
440 XRPL_ASSERT(refHash,
"xrpl::RPC::getOrAcquireLedger : nonzero ledger hash");
443 ledger = ledgerMaster.getLedgerByHash(*refHash);
463 jvResult[jss::acquiring] = il->getJson(0);
471 neededHash =
hashOfSeq(*ledger, ledgerIndex, j);
473 XRPL_ASSERT(neededHash,
"xrpl::RPC::getOrAcquireLedger : nonzero needed hash");
474 ledgerHash = neededHash ? *neededHash : beast::kZero;
484 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
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
constexpr auto kMaxValidatedLedgerAge
API version numbers used in later API versions.
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 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.
Status ledgerFromSpecifier(T &ledger, org::xrpl::rpc::v1::LedgerSpecifier const &specifier, Context const &context)
Retrieves a ledger based on a LedgerSpecifier.
json::Value makeError(ErrorCodeI code)
Returns a new json object that reflects the error code.
Status ledgerFromRequest(T &ledger, GRPCContext< R > const &context)
Retrieves a ledger from a gRPC request context.
std::string expectedFieldMessage(std::string const &name, std::string const &type)
json::Value makeParamError(std::string const &message)
Returns a new json object that indicates invalid parameters.
Status getLedger(T &ledger, uint256 const &ledgerHash, Context const &context)
Retrieves a ledger by its hash.
json::Value expectedFieldError(std::string const &name, std::string const &type)
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