rippled
CrawlShards.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2018 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/net/RPCErr.h>
22 #include <ripple/nodestore/DatabaseShard.h>
23 #include <ripple/overlay/Overlay.h>
24 #include <ripple/protocol/ErrorCodes.h>
25 #include <ripple/protocol/jss.h>
26 #include <ripple/resource/Fees.h>
27 #include <ripple/rpc/Context.h>
28 
29 namespace ripple {
30 
44 {
45  if (context.app.config().reporting())
47 
48  if (context.role != Role::ADMIN)
49  return rpcError(rpcNO_PERMISSION);
50 
51  std::uint32_t hops{0};
52  if (auto const& jv = context.params[jss::limit])
53  {
54  if (!(jv.isUInt() || (jv.isInt() && jv.asInt() >= 0)))
55  {
56  return RPC::expected_field_error(jss::limit, "unsigned integer");
57  }
58 
59  hops = std::min(jv.asUInt(), csHopLimit);
60  }
61 
62  bool const pubKey{
63  context.params.isMember(jss::public_key) &&
64  context.params[jss::public_key].asBool()};
65 
66  // Collect shard info from peers connected to this server
67  Json::Value jvResult{context.app.overlay().crawlShards(pubKey, hops)};
68 
69  // Collect shard info from this server
70  if (auto shardStore = context.app.getShardStore())
71  {
72  if (pubKey)
73  jvResult[jss::public_key] = toBase58(
74  TokenType::NodePublic, context.app.nodeIdentity().first);
75  jvResult[jss::complete_shards] = shardStore->getCompleteShards();
76  }
77 
78  if (hops == 0)
80  else
82 
83  return jvResult;
84 }
85 
86 } // namespace ripple
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::csHopLimit
static constexpr std::uint32_t csHopLimit
Definition: ripple/overlay/Peer.h:36
ripple::Resource::feeMediumBurdenRPC
const Charge feeMediumBurdenRPC
ripple::RPC::Context::loadType
Resource::Charge & loadType
Definition: Context.h:43
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:29
ripple::Application::getShardStore
virtual NodeStore::DatabaseShard * getShardStore()=0
ripple::RPC::Context::role
Role role
Definition: Context.h:47
ripple::rpcREPORTING_UNSUPPORTED
@ rpcREPORTING_UNSUPPORTED
Definition: ErrorCodes.h:141
ripple::RPC::expected_field_error
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition: ErrorCodes.h:309
Json::Value::asBool
bool asBool() const
Definition: json_value.cpp:619
ripple::doCrawlShards
Json::Value doCrawlShards(RPC::JsonContext &context)
RPC command that reports stored shards by nodes.
Definition: CrawlShards.cpp:43
ripple::Config::reporting
bool reporting() const
Definition: Config.h:267
ripple::Role::ADMIN
@ ADMIN
ripple::Application::config
virtual Config & config()=0
ripple::Application::nodeIdentity
virtual std::pair< PublicKey, SecretKey > const & nodeIdentity()=0
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:932
std::uint32_t
ripple::Overlay::crawlShards
virtual Json::Value crawlShards(bool pubKey, std::uint32_t hops)=0
Returns information reported to the crawl shard RPC command.
ripple::rpcError
Json::Value rpcError(int iError, Json::Value jvResult)
Definition: RPCErr.cpp:29
std::min
T min(T... args)
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::rpcNO_PERMISSION
@ rpcNO_PERMISSION
Definition: ErrorCodes.h:53
ripple::Application::overlay
virtual Overlay & overlay()=0
ripple::TokenType::NodePublic
@ NodePublic
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::Resource::feeHighBurdenRPC
const Charge feeHighBurdenRPC
Json::Value
Represents a JSON value.
Definition: json_value.h:145