1#include <xrpld/app/ledger/LedgerMaster.h>
2#include <xrpld/app/ledger/LedgerToJson.h>
3#include <xrpld/app/ledger/OpenLedger.h>
4#include <xrpld/app/main/Application.h>
5#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
7#include <xrpl/protocol/RPCErr.h>
9#include <boost/algorithm/string/case_conv.hpp>
17isValidatedOld(LedgerMaster&
ledgerMaster,
bool standalone)
30 Context
const& context,
34 if (!ledgerHash.parseHex(hash.
asString()))
36 return getLedger(ledger, ledgerHash, context);
44 Context
const& context,
47 auto const index = indexValue.
asString();
49 if (index ==
"current" || index.empty())
52 if (index ==
"validated")
55 if (index ==
"closed")
71 auto& params = context.params;
72 auto const hasLedger = context.params.isMember(jss::ledger);
73 auto const hasHash = context.params.isMember(jss::ledger_hash);
74 auto const hasIndex = context.params.isMember(jss::ledger_index);
76 if ((hasLedger + hasHash + hasIndex) > 1)
84 "Exactly one of 'ledger', 'ledger_hash', or "
85 "'ledger_index' can be specified."};
89 "Exactly one of 'ledger_hash' or "
90 "'ledger_index' can be specified."};
96 auto& legacyLedger = params[jss::ledger];
97 if (!legacyLedger.isString() && !legacyLedger.isUInt() && !legacyLedger.isInt())
101 if (legacyLedger.isString() && legacyLedger.asString().size() == 64)
103 return ledgerFromHash(ledger, legacyLedger, context, jss::ledger);
106 return ledgerFromIndex(ledger, legacyLedger, context, jss::ledger);
111 auto const& ledgerHash = params[jss::ledger_hash];
112 if (!ledgerHash.isString())
114 return ledgerFromHash(ledger, ledgerHash, context, jss::ledger_hash);
119 auto const& ledgerIndex = params[jss::ledger_index];
120 if (!ledgerIndex.isString() && !ledgerIndex.isUInt() && !ledgerIndex.isInt())
125 return ledgerFromIndex(ledger, ledgerIndex, context, jss::ledger_index);
133template <
class T,
class R>
137 R
const& request = context.
params;
163 org::xrpl::rpc::v1::LedgerSpecifier
const& specifier,
168 using LedgerCase = org::xrpl::rpc::v1::LedgerSpecifier::LedgerCase;
169 LedgerCase
const ledgerCase = specifier.ledger_case();
172 case LedgerCase::kHash: {
175 return getLedger(ledger, *hash, context);
179 case LedgerCase::kSequence:
180 return getLedger(ledger, specifier.sequence(), context);
181 case LedgerCase::kShortcut:
183 case LedgerCase::LEDGER_NOT_SET: {
184 auto const shortcut = specifier.shortcut();
185 if (shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_VALIDATED)
190 if (shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_CURRENT ||
191 shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_UNSPECIFIED)
195 if (shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_CLOSED)
210 if (ledger ==
nullptr)
220 if (ledger ==
nullptr)
223 if (cur->header().seq == ledgerIndex)
229 if (ledger ==
nullptr)
258 if (ledger ==
nullptr)
265 XRPL_ASSERT(!ledger->open(),
"xrpl::RPC::getLedger : validated is not open");
272 XRPL_ASSERT(ledger->open(),
"xrpl::RPC::getLedger : current is open");
277 XRPL_ASSERT(!ledger->open(),
"xrpl::RPC::getLedger : closed is not open");
284 if (ledger ==
nullptr)
291 static auto const minSequenceGap = 10;
342 auto& info = ledger->header();
346 result[jss::ledger_hash] =
to_string(info.hash);
347 result[jss::ledger_index] = info.seq;
351 result[jss::ledger_current_index] = info.seq;
362 if (
auto status =
lookupLedger(ledger, context, result))
363 status.inject(result);
378 if ((
static_cast<int>(hasHash) +
static_cast<int>(hasIndex)) != 1)
382 "Exactly one of 'ledger_hash' or "
383 "'ledger_index' can be specified."));
389 if (!jsonHash.isString() || !ledgerHash.
parseHex(jsonHash.asString()))
395 if (!jsonIndex.isInt() && !jsonIndex.isUInt())
406 ledgerIndex = jsonIndex.asInt();
409 if (ledgerIndex >= ledger->header().seq)
411 if (ledgerIndex <= 0)
417 auto neededHash =
hashOfSeq(*ledger, ledgerIndex, j);
423 auto refHash =
hashOfSeq(*ledger, refIndex, j);
424 XRPL_ASSERT(refHash,
"xrpl::RPC::getOrAcquireLedger : nonzero ledger hash");
445 jvResult[jss::acquiring] = il->getJson(0);
453 neededHash =
hashOfSeq(*ledger, ledgerIndex, j);
455 XRPL_ASSERT(neededHash,
"xrpl::RPC::getOrAcquireLedger : nonzero needed hash");
456 ledgerHash = neededHash ? *neededHash : beast::zero;
Lightweight wrapper to tag static string.
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.
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
virtual Config & config()=0
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 parseHex(std::string_view sv)
Parse a hex string into a base_uint.
static std::optional< base_uint > fromVoidChecked(T const &from)
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
Status
Return codes from Backend operations.
auto constexpr maxValidatedLedgerAge
std::string expected_field_message(std::string const &name, std::string const &type)
Status ledgerFromSpecifier(T &ledger, org::xrpl::rpc::v1::LedgerSpecifier const &specifier, Context const &context)
Retrieves a ledger based on a LedgerSpecifier.
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.
Json::Value expected_field_error(std::string const &name, std::string const &type)
Json::Value make_param_error(std::string const &message)
Returns a new json object that indicates invalid parameters.
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.
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
Status getLedger(T &ledger, uint256 const &ledgerHash, Context const &context)
Retrieves a ledger by its hash.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
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)
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.
LedgerIndex getCandidateLedger(LedgerIndex requested)
Find a ledger index from which we could easily get the requested ledger.
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Json::Value rpcError(error_code_i iError)
@ 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.