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 if (!ledgerHash.parseHex(hash.
asString()))
32 return getLedger(ledger, ledgerHash, context);
39 auto const index = indexValue.
asString();
41 if (index ==
"current" || index.empty())
42 return getLedger(ledger, LedgerShortcut::Current, context);
44 if (index ==
"validated")
45 return getLedger(ledger, LedgerShortcut::Validated, context);
47 if (index ==
"closed")
48 return getLedger(ledger, LedgerShortcut::Closed, context);
63 auto& params = context.params;
64 auto const hasLedger = context.params.isMember(jss::ledger);
65 auto const hasHash = context.params.isMember(jss::ledger_hash);
66 auto const hasIndex = context.params.isMember(jss::ledger_index);
68 if ((hasLedger + hasHash + hasIndex) > 1)
75 "Exactly one of 'ledger', 'ledger_hash', or "
76 "'ledger_index' can be specified."};
79 "Exactly one of 'ledger_hash' or "
80 "'ledger_index' can be specified."};
86 auto& legacyLedger = params[jss::ledger];
87 if (!legacyLedger.isString() && !legacyLedger.isUInt() && !legacyLedger.isInt())
91 if (legacyLedger.isString() && legacyLedger.asString().size() == 64)
92 return ledgerFromHash(ledger, legacyLedger, context, jss::ledger);
94 return ledgerFromIndex(ledger, legacyLedger, context, jss::ledger);
99 auto const& ledgerHash = params[jss::ledger_hash];
100 if (!ledgerHash.isString())
102 return ledgerFromHash(ledger, ledgerHash, context, jss::ledger_hash);
107 auto const& ledgerIndex = params[jss::ledger_index];
108 if (!ledgerIndex.isString() && !ledgerIndex.isUInt() && !ledgerIndex.isInt())
112 return ledgerFromIndex(ledger, ledgerIndex, context, jss::ledger_index);
116 return getLedger(ledger, LedgerShortcut::Current, context);
120template <
class T,
class R>
124 R
const& request = context.
params;
146 using LedgerCase = org::xrpl::rpc::v1::LedgerSpecifier::LedgerCase;
147 LedgerCase ledgerCase = specifier.ledger_case();
150 case LedgerCase::kHash: {
153 return getLedger(ledger, *hash, context);
157 case LedgerCase::kSequence:
158 return getLedger(ledger, specifier.sequence(), context);
159 case LedgerCase::kShortcut:
161 case LedgerCase::LEDGER_NOT_SET: {
162 auto const shortcut = specifier.shortcut();
163 if (shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_VALIDATED)
165 return getLedger(ledger, LedgerShortcut::Validated, context);
169 if (shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_CURRENT ||
170 shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_UNSPECIFIED)
172 return getLedger(ledger, LedgerShortcut::Current, context);
174 else if (shortcut == org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_CLOSED)
176 return getLedger(ledger, LedgerShortcut::Closed, context);
190 if (ledger ==
nullptr)
200 if (ledger ==
nullptr)
203 if (cur->header().seq == ledgerIndex)
209 if (ledger ==
nullptr)
235 if (shortcut == LedgerShortcut::Validated)
238 if (ledger ==
nullptr)
245 XRPL_ASSERT(!ledger->open(),
"xrpl::RPC::getLedger : validated is not open");
249 if (shortcut == LedgerShortcut::Current)
252 XRPL_ASSERT(ledger->open(),
"xrpl::RPC::getLedger : current is open");
254 else if (shortcut == LedgerShortcut::Closed)
257 XRPL_ASSERT(!ledger->open(),
"xrpl::RPC::getLedger : closed is not open");
264 if (ledger ==
nullptr)
271 static auto const minSequenceGap = 10;
319 auto& info = ledger->header();
323 result[jss::ledger_hash] =
to_string(info.hash);
324 result[jss::ledger_index] = info.seq;
328 result[jss::ledger_current_index] = info.seq;
339 if (
auto status =
lookupLedger(ledger, context, result))
340 status.inject(result);
355 if ((hasHash + hasIndex) != 1)
359 "'ledger_index' can be specified."));
365 if (!jsonHash.isString() || !ledgerHash.
parseHex(jsonHash.asString()))
371 if (!jsonIndex.isInt() && !jsonIndex.isUInt())
382 ledgerIndex = jsonIndex.asInt();
385 if (ledgerIndex >= ledger->header().seq)
387 if (ledgerIndex <= 0)
390 auto const j = context.
app.
journal(
"RPCHandler");
393 auto neededHash =
hashOfSeq(*ledger, ledgerIndex, j);
399 auto refHash =
hashOfSeq(*ledger, refIndex, j);
400 XRPL_ASSERT(refHash,
"xrpl::RPC::getOrAcquireLedger : nonzero ledger hash");
421 jvResult[jss::acquiring] = il->getJson(0);
429 neededHash =
hashOfSeq(*ledger, ledgerIndex, j);
431 XRPL_ASSERT(neededHash,
"xrpl::RPC::getOrAcquireLedger : nonzero needed hash");
432 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 InboundLedgers & getInboundLedgers()=0
virtual LedgerMaster & getLedgerMaster()=0
virtual beast::Journal journal(std::string const &name)=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)
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)
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.