rippled
Peers.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/misc/LoadFeeTrack.h>
21 #include <ripple/core/TimeKeeper.h>
22 #include <ripple/net/RPCErr.h>
23 #include <ripple/overlay/Cluster.h>
24 #include <ripple/overlay/Overlay.h>
25 #include <ripple/protocol/ErrorCodes.h>
26 #include <ripple/protocol/jss.h>
27 #include <ripple/rpc/Context.h>
28 
29 namespace ripple {
30 
33 {
34  if (context.app.config().reporting())
36 
38 
39  jvResult[jss::peers] = context.app.overlay().json();
40 
41  // Legacy support
42  if (context.apiVersion == 1)
43  {
44  for (auto& p : jvResult[jss::peers])
45  {
46  if (p.isMember(jss::track))
47  {
48  auto const s = p[jss::track].asString();
49 
50  if (s == "diverged")
51  p["sanity"] = "insane";
52  else if (s == "unknown")
53  p["sanity"] = "unknown";
54  }
55  }
56  }
57 
58  auto const now = context.app.timeKeeper().now();
59  auto const self = context.app.nodeIdentity().first;
60 
61  Json::Value& cluster = (jvResult[jss::cluster] = Json::objectValue);
62  std::uint32_t ref = context.app.getFeeTrack().getLoadBase();
63 
64  context.app.cluster().for_each(
65  [&cluster, now, ref, &self](ClusterNode const& node) {
66  if (node.identity() == self)
67  return;
68 
69  Json::Value& json =
70  cluster[toBase58(TokenType::NodePublic, node.identity())];
71 
72  if (!node.name().empty())
73  json[jss::tag] = node.name();
74 
75  if ((node.getLoadFee() != ref) && (node.getLoadFee() != 0))
76  json[jss::fee] = static_cast<double>(node.getLoadFee()) / ref;
77 
78  if (node.getReportTime() != NetClock::time_point{})
79  json[jss::age] = (node.getReportTime() >= now)
80  ? 0
81  : (now - node.getReportTime()).count();
82  });
83 
84  return jvResult;
85 }
86 
87 } // namespace ripple
ripple::ClusterNode
Definition: ClusterNode.h:30
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::Application::cluster
virtual Cluster & cluster()=0
ripple::rpcError
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
ripple::ClusterNode::name
std::string const & name() const
Definition: ClusterNode.h:45
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:104
ripple::LoadFeeTrack::getLoadBase
std::uint32_t getLoadBase() const
Definition: LoadFeeTrack.h:89
ripple::Application::timeKeeper
virtual TimeKeeper & timeKeeper()=0
ripple::doPeers
Json::Value doPeers(RPC::JsonContext &)
Definition: Peers.cpp:32
ripple::rpcREPORTING_UNSUPPORTED
@ rpcREPORTING_UNSUPPORTED
Definition: ErrorCodes.h:141
ripple::Application::getFeeTrack
virtual LoadFeeTrack & getFeeTrack()=0
ripple::Config::reporting
bool reporting() const
Definition: Config.h:337
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
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
std::chrono::time_point
std::uint32_t
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Overlay::json
virtual Json::Value json()=0
Return diagnostics on the status of all peers.
ripple::RPC::Context::apiVersion
unsigned int apiVersion
Definition: Context.h:50
ripple::TimeKeeper::now
virtual time_point now() const override=0
Returns the estimate of wall time, in network time.
ripple::Application::overlay
virtual Overlay & overlay()=0
std::string::empty
T empty(T... args)
ripple::TokenType::NodePublic
@ NodePublic
ripple::Cluster::for_each
void for_each(std::function< void(ClusterNode const &)> func) const
Invokes the callback once for every cluster node.
Definition: Cluster.cpp:84
ripple::ClusterNode::getReportTime
NetClock::time_point getReportTime() const
Definition: ClusterNode.h:57
ripple::ClusterNode::identity
PublicKey const & identity() const
Definition: ClusterNode.h:63
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::ClusterNode::getLoadFee
std::uint32_t getLoadFee() const
Definition: ClusterNode.h:51
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469