rippled
RPCHelpers.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012=2014 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_RPC_RPCHELPERS_H_INCLUDED
21 #define RIPPLE_RPC_RPCHELPERS_H_INCLUDED
22 
23 #include <ripple/beast/core/SemanticVersion.h>
24 #include <ripple/proto/org/xrpl/rpc/v1/xrp_ledger.pb.h>
25 #include <ripple/protocol/TxMeta.h>
26 
27 #include <ripple/app/misc/NetworkOPs.h>
28 #include <ripple/app/misc/TxQ.h>
29 #include <ripple/basics/Expected.h>
30 #include <ripple/basics/SubmitSync.h>
31 #include <ripple/protocol/SecretKey.h>
32 #include <ripple/rpc/Context.h>
33 #include <ripple/rpc/Status.h>
34 #include <ripple/rpc/impl/Tuning.h>
35 #include <optional>
36 #include <variant>
37 
38 namespace Json {
39 class Value;
40 }
41 
42 namespace ripple {
43 
44 class ReadView;
45 class Transaction;
46 
47 namespace RPC {
48 
49 struct JsonContext;
50 
54 
55 // --> strIdent: public key, account ID, or regular seed.
56 // --> bStrict: Only allow account id or public key.
57 //
58 // Returns a Json::objectValue, containing error information if there was one.
61  AccountID& result,
62  std::string const& strIdent,
63  bool bStrict = false);
64 
73  AccountID& result,
74  std::string const& strIdent,
75  bool bStrict = false);
76 
82 getStartHint(std::shared_ptr<SLE const> const& sle, AccountID const& accountID);
83 
90 bool
92  ReadView const& ledger,
93  std::shared_ptr<SLE const> const& sle,
94  AccountID const& accountID);
95 
105 bool
107  ReadView const& ledger,
108  AccountID const& account,
109  std::optional<std::vector<LedgerEntryType>> const& typeFilter,
110  uint256 dirIndex,
111  uint256 entryIndex,
112  std::uint32_t const limit,
113  Json::Value& jvResult);
114 
119 template <class T>
120 Status
121 getLedger(T& ledger, uint256 const& ledgerHash, Context& context);
122 
127 template <class T>
128 Status
129 getLedger(T& ledger, uint32_t ledgerIndex, Context& context);
130 
136 template <class T>
137 Status
138 getLedger(T& ledger, LedgerShortcut shortcut, Context& context);
139 
148 
154 Status
157  JsonContext&,
158  Json::Value& result);
159 
160 template <class T, class R>
161 Status
162 ledgerFromRequest(T& ledger, GRPCContext<R>& context);
163 
164 template <class T>
165 Status
167  T& ledger,
168  org::xrpl::rpc::v1::LedgerSpecifier const& specifier,
169  Context& context);
170 
172 parseAccountIds(Json::Value const& jvArray);
173 
174 bool
175 isHexTxID(std::string const& txid);
176 
185 void
186 injectSLE(Json::Value& jv, SLE const& sle);
187 
195  unsigned int& limit,
196  Tuning::LimitRange const&,
197  JsonContext const&);
198 
200 getSeedFromRPC(Json::Value const& params, Json::Value& error);
201 
203 parseRippleLibSeed(Json::Value const& params);
204 
211 
234 constexpr unsigned int apiInvalidVersion = 0;
235 constexpr unsigned int apiVersionIfUnspecified = 1;
236 constexpr unsigned int apiMinimumSupportedVersion = 1;
237 constexpr unsigned int apiMaximumSupportedVersion = 2;
238 constexpr unsigned int apiCommandLineVersion = 1; // TODO Bump to 2 later
239 constexpr unsigned int apiBetaVersion = 3;
240 constexpr unsigned int apiMaximumValidVersion = apiBetaVersion;
241 
246 
247 template <class Object>
248 void
249 setVersion(Object& parent, unsigned int apiVersion, bool betaEnabled)
250 {
251  assert(apiVersion != apiInvalidVersion);
252  auto&& object = addObject(parent, jss::version);
253  if (apiVersion == apiVersionIfUnspecified)
254  {
255  object[jss::first] = firstVersion.print();
256  object[jss::good] = goodVersion.print();
257  object[jss::last] = lastVersion.print();
258  }
259  else
260  {
261  object[jss::first] = apiMinimumSupportedVersion;
262  object[jss::last] =
264  }
265 }
266 
268 chooseLedgerEntryType(Json::Value const& params);
269 
284 unsigned int
285 getAPIVersionNumber(const Json::Value& value, bool betaEnabled);
286 
291 
294  Json::Value const& params,
295  Json::Value& error,
296  unsigned int apiVersion = apiVersionIfUnspecified);
303 getSubmitSyncMode(Json::Value const& params);
304 
305 } // namespace RPC
306 } // namespace ripple
307 
308 #endif
ripple::RPC::apiInvalidVersion
constexpr unsigned int apiInvalidVersion
API version numbers used in later API versions.
Definition: RPCHelpers.h:234
ripple::RPC::apiVersionIfUnspecified
constexpr unsigned int apiVersionIfUnspecified
Definition: RPCHelpers.h:235
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::RPC::CURRENT
@ CURRENT
Definition: RPCHelpers.h:131
ripple::STLedgerEntry
Definition: STLedgerEntry.h:30
ripple::RPC::firstVersion
const beast::SemanticVersion firstVersion("1.0.0")
API version numbers used in API version 1.
Definition: RPCHelpers.h:208
ripple::RPC::goodVersion
const beast::SemanticVersion goodVersion("1.0.0")
Definition: RPCHelpers.h:209
std::string
STL class.
std::shared_ptr
STL class.
ripple::RPC::isHexTxID
bool isHexTxID(std::string const &txid)
std::unordered_set
STL class.
ripple::RPC::injectSLE
void injectSLE(Json::Value &jv, SLE const &sle)
Inject JSON describing ledger entry.
Definition: RPCHelpers.cpp:674
std::pair
ripple::RPC::LedgerShortcut
LedgerShortcut
Definition: RPCHelpers.h:131
ripple::RPC::getAccountObjects
bool getAccountObjects(ReadView const &ledger, AccountID const &account, std::optional< std::vector< LedgerEntryType >> const &typeFilter, uint256 dirIndex, uint256 entryIndex, std::uint32_t const limit, Json::Value &jvResult)
Gathers all objects for an account in a ledger.
Definition: RPCHelpers.cpp:152
ripple::RPC::getAPIVersionNumber
unsigned int getAPIVersionNumber(Json::Value const &jv, bool betaEnabled)
Retrieve the api version number from the json value.
Definition: RPCHelpers.cpp:992
std::vector
STL class.
beast::SemanticVersion::print
std::string print() const
Produce a string from semantic version components.
Definition: SemanticVersion.cpp:236
ripple::RPC::readLimitField
std::optional< Json::Value > readLimitField(unsigned int &limit, Tuning::LimitRange const &range, JsonContext const &context)
Retrieve the limit value from a JsonContext, or set a default - then restrict the limit by max and mi...
Definition: RPCHelpers.cpp:699
beast::SemanticVersion
A Semantic Version number.
Definition: SemanticVersion.h:35
ripple::RPC::apiCommandLineVersion
constexpr unsigned int apiCommandLineVersion
Definition: RPCHelpers.h:238
ripple::RPC::getLedgerByContext
std::variant< std::shared_ptr< Ledger const >, Json::Value > getLedgerByContext(RPC::JsonContext &context)
Return a ledger based on ledger_hash or ledger_index, or an RPC error.
Definition: RPCHelpers.cpp:1013
ripple::RPC::apiBetaVersion
constexpr unsigned int apiBetaVersion
Definition: RPCHelpers.h:239
ripple::RPC::lookupLedger
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext &context, Json::Value &result)
Look up a ledger from a request and fill a Json::Result with the data representing a ledger.
Definition: RPCHelpers.cpp:623
ripple::error_code_i
error_code_i
Definition: ErrorCodes.h:40
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::RPC::keypairForSignature
std::pair< PublicKey, SecretKey > keypairForSignature(Json::Value const &params, Json::Value &error, unsigned int apiVersion)
Definition: RPCHelpers.cpp:796
ripple::RPC::parseRippleLibSeed
std::optional< Seed > parseRippleLibSeed(Json::Value const &value)
Definition: RPCHelpers.cpp:718
Json
JSON (JavaScript Object Notation).
Definition: DeliverMax.h:28
ripple::Expected
Definition: Expected.h:132
ripple::RPC::accountFromStringWithCode
error_code_i accountFromStringWithCode(AccountID &result, std::string const &strIdent, bool bStrict)
Decode account ID from string.
Definition: RPCHelpers.cpp:59
ripple::RPC::VALIDATED
@ VALIDATED
Definition: RPCHelpers.h:131
ripple::RPC::CLOSED
@ CLOSED
Definition: RPCHelpers.h:131
std::uint64_t
ripple::NodeStore::Status
Status
Return codes from Backend operations.
Definition: nodestore/Types.h:45
ripple::RPC::apiMinimumSupportedVersion
constexpr unsigned int apiMinimumSupportedVersion
Definition: RPCHelpers.h:236
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RPC::getLedger
Status getLedger(T &ledger, uint256 const &ledgerHash, Context &context)
Get ledger by hash If there is no error in the return value, the ledger pointer will have been filled...
Definition: RPCHelpers.cpp:480
ripple::RPC::apiMaximumValidVersion
constexpr unsigned int apiMaximumValidVersion
Definition: RPCHelpers.h:240
ripple::RPC::chooseLedgerEntryType
std::pair< RPC::Status, LedgerEntryType > chooseLedgerEntryType(Json::Value const &params)
Definition: RPCHelpers.cpp:932
ripple::RPC::getStartHint
std::uint64_t getStartHint(std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
Definition: RPCHelpers.cpp:97
ripple::RPC::getSeedFromRPC
std::optional< Seed > getSeedFromRPC(Json::Value const &params, Json::Value &error)
Definition: RPCHelpers.cpp:737
ripple::RPC::getSubmitSyncMode
ripple::Expected< RPC::SubmitSync, Json::Value > getSubmitSyncMode(Json::Value const &params)
Helper to parse submit_mode parameter to RPC submit.
Definition: RPCHelpers.cpp:1130
ripple::RPC::apiMaximumSupportedVersion
constexpr unsigned int apiMaximumSupportedVersion
Definition: RPCHelpers.h:237
ripple::RPC::ledgerFromSpecifier
Status ledgerFromSpecifier(T &ledger, org::xrpl::rpc::v1::LedgerSpecifier const &specifier, Context &context)
Definition: RPCHelpers.cpp:421
optional
ripple::RPC::parseAccountIds
hash_set< AccountID > parseAccountIds(Json::Value const &jvArray)
Definition: RPCHelpers.cpp:658
ripple::RPC::accountFromStringStrict
std::optional< AccountID > accountFromStringStrict(std::string const &account)
Get an AccountID from an account ID or public key.
Definition: RPCHelpers.cpp:43
ripple::RPC::lastVersion
const beast::SemanticVersion lastVersion("1.0.0")
Definition: RPCHelpers.h:210
ripple::RPC::ledgerFromRequest
Status ledgerFromRequest(T &ledger, GRPCContext< R > &context)
Definition: RPCHelpers.cpp:395
ripple::RPC::accountFromString
Json::Value accountFromString(AccountID &result, std::string const &strIdent, bool bStrict)
Definition: RPCHelpers.cpp:87
ripple::RPC::isRelatedToAccount
bool isRelatedToAccount(ReadView const &ledger, std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Tests if a SLE is owned by accountID.
Definition: RPCHelpers.cpp:114
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::RPC::setVersion
void setVersion(Object &parent, unsigned int apiVersion, bool betaEnabled)
Definition: RPCHelpers.h:249
variant