rippled
AccountObjects.cpp
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 #include <ripple/app/main/Application.h>
21 #include <ripple/json/json_writer.h>
22 #include <ripple/ledger/ReadView.h>
23 #include <ripple/net/RPCErr.h>
24 #include <ripple/protocol/ErrorCodes.h>
25 #include <ripple/protocol/Indexes.h>
26 #include <ripple/protocol/LedgerFormats.h>
27 #include <ripple/protocol/jss.h>
28 #include <ripple/resource/Fees.h>
29 #include <ripple/rpc/Context.h>
30 #include <ripple/rpc/impl/RPCHelpers.h>
31 #include <ripple/rpc/impl/Tuning.h>
32 
33 #include <sstream>
34 #include <string>
35 
36 namespace ripple {
37 
51 {
52  auto const& params = context.params;
53  if (!params.isMember(jss::account))
54  return RPC::missing_field_error(jss::account);
55 
57  auto result = RPC::lookupLedger(ledger, context);
58  if (ledger == nullptr)
59  return result;
60 
61  AccountID accountID;
62  {
63  auto const strIdent = params[jss::account].asString();
64  if (auto jv = RPC::accountFromString(accountID, strIdent))
65  {
66  for (auto it = jv.begin(); it != jv.end(); ++it)
67  result[it.memberName()] = *it;
68 
69  return result;
70  }
71  }
72 
73  if (!ledger->exists(keylet::account(accountID)))
74  return rpcError(rpcACT_NOT_FOUND);
75 
77 
78  if (params.isMember(jss::deletion_blockers_only) &&
79  params[jss::deletion_blockers_only].asBool())
80  {
81  struct
82  {
83  Json::StaticString name;
84  LedgerEntryType type;
85  } static constexpr deletionBlockers[] = {
86  {jss::check, ltCHECK},
87  {jss::escrow, ltESCROW},
88  {jss::payment_channel, ltPAYCHAN},
89  {jss::state, ltRIPPLE_STATE}};
90 
91  typeFilter.emplace();
92  typeFilter->reserve(std::size(deletionBlockers));
93 
94  for (auto [name, type] : deletionBlockers)
95  {
96  if (params.isMember(jss::type) && name != params[jss::type])
97  {
98  continue;
99  }
100 
101  typeFilter->push_back(type);
102  }
103  }
104  else
105  {
106  auto [rpcStatus, type] = RPC::chooseLedgerEntryType(params);
107 
108  if (rpcStatus)
109  {
110  result.clear();
111  rpcStatus.inject(result);
112  return result;
113  }
114  else if (type != ltANY)
115  {
116  typeFilter = std::vector<LedgerEntryType>({type});
117  }
118  }
119 
120  unsigned int limit;
121  if (auto err = readLimitField(limit, RPC::Tuning::accountObjects, context))
122  return *err;
123 
124  uint256 dirIndex;
125  uint256 entryIndex;
126  if (params.isMember(jss::marker))
127  {
128  auto const& marker = params[jss::marker];
129  if (!marker.isString())
130  return RPC::expected_field_error(jss::marker, "string");
131 
132  std::stringstream ss(marker.asString());
133  std::string s;
134  if (!std::getline(ss, s, ','))
135  return RPC::invalid_field_error(jss::marker);
136 
137  if (!dirIndex.parseHex(s))
138  return RPC::invalid_field_error(jss::marker);
139 
140  if (!std::getline(ss, s, ','))
141  return RPC::invalid_field_error(jss::marker);
142 
143  if (!entryIndex.parseHex(s))
144  return RPC::invalid_field_error(jss::marker);
145  }
146 
148  *ledger,
149  accountID,
150  typeFilter,
151  dirIndex,
152  entryIndex,
153  limit,
154  result))
155  {
156  result[jss::account_objects] = Json::arrayValue;
157  }
158 
159  result[jss::account] = context.app.accountIDCache().toBase58(accountID);
161  return result;
162 }
163 
164 } // namespace ripple
sstream
ripple::RPC::JsonContext
Definition: Context.h:53
std::string
STL class.
std::shared_ptr
STL class.
ripple::rpcError
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
ripple::ltANY
@ ltANY
A special type, matching any ledger entry type.
Definition: LedgerFormats.h:164
ripple::Resource::feeMediumBurdenRPC
const Charge feeMediumBurdenRPC
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
ripple::doAccountObjects
Json::Value doAccountObjects(RPC::JsonContext &context)
General RPC command that can retrieve objects in the account root.
Definition: AccountObjects.cpp:50
ripple::RPC::Context::loadType
Resource::Charge & loadType
Definition: Context.h:43
std::vector
STL class.
std::size
T size(T... args)
std::optional::emplace
T emplace(T... args)
std::stringstream
STL class.
ripple::ltCHECK
@ ltCHECK
A ledger object which describes a check.
Definition: LedgerFormats.h:136
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:582
ripple::Application::accountIDCache
virtual AccountIDCache const & accountIDCache() const =0
ripple::RPC::expected_field_error
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition: ErrorCodes.h:309
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:243
ripple::base_uint< 160, detail::AccountIDTag >
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:130
ripple::ltESCROW
@ ltESCROW
A ledger object describing a single escrow.
Definition: LedgerFormats.h:124
ripple::rpcACT_NOT_FOUND
@ rpcACT_NOT_FOUND
Definition: ErrorCodes.h:70
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
ripple::ReadView::exists
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
ripple::RPC::Tuning::accountObjects
static constexpr LimitRange accountObjects
Limits for the account_objects command.
Definition: rpc/impl/Tuning.h:43
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::LedgerEntryType
LedgerEntryType
Identifiers for on-ledger objects.
Definition: LedgerFormats.h:53
ripple::RPC::chooseLedgerEntryType
std::pair< RPC::Status, LedgerEntryType > chooseLedgerEntryType(Json::Value const &params)
Definition: RPCHelpers.cpp:882
std::getline
T getline(T... args)
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:60
std::optional
ripple::ltRIPPLE_STATE
@ ltRIPPLE_STATE
A ledger object which describes a bidirectional trust line.
Definition: LedgerFormats.h:74
ripple::base_uint::parseHex
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:475
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::RPC::invalid_field_error
Json::Value invalid_field_error(std::string const &name)
Definition: ErrorCodes.h:285
ripple::RPC::accountFromString
Json::Value accountFromString(AccountID &result, std::string const &strIdent, bool bStrict)
Definition: RPCHelpers.cpp:84
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::ltPAYCHAN
@ ltPAYCHAN
A ledger object describing a single unidirectional XRP payment channel.
Definition: LedgerFormats.h:130
ripple::RPC::getAccountObjects
bool getAccountObjects(ReadView const &ledger, AccountID const &account, std::optional< std::vector< LedgerEntryType >> const &typeFilter, uint256 dirIndex, uint256 const &entryIndex, std::uint32_t const limit, Json::Value &jvResult)
Gathers all objects for an account in a ledger.
Definition: RPCHelpers.cpp:135
ripple::AccountIDCache::toBase58
std::string toBase58(AccountID const &) const
Return ripple::toBase58 for the AccountID.
Definition: AccountID.cpp:134
string