rippled
Reservations.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2019 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/json/json_value.h>
21 #include <ripple/net/RPCErr.h>
22 #include <ripple/protocol/ErrorCodes.h>
23 #include <ripple/protocol/PublicKey.h>
24 #include <ripple/protocol/jss.h>
25 #include <ripple/rpc/Context.h>
26 #include <ripple/rpc/handlers/Handlers.h>
27 
28 #include <boost/optional.hpp>
29 
30 #include <string>
31 #include <utility>
32 
33 namespace ripple {
34 
37 {
38  if (context.app.config().reporting())
40 
41  auto const& params = context.params;
42 
43  if (!params.isMember(jss::public_key))
44  return RPC::missing_field_error(jss::public_key);
45 
46  // Returning JSON from every function ruins any attempt to encapsulate
47  // the pattern of "get field F as type T, and diagnose an error if it is
48  // missing or malformed":
49  // - It is costly to copy whole JSON objects around just to check whether an
50  // error code is present.
51  // - It is not as easy to read when cluttered by code to pack and unpack the
52  // JSON object.
53  // - It is not as easy to write when you have to include all the packing and
54  // unpacking code.
55  // Exceptions would be easier to use, but have a terrible cost for control
56  // flow. An error monad is purpose-built for this situation; it is
57  // essentially an optional (the "maybe monad" in Haskell) with a non-unit
58  // type for the failure case to capture more information.
59  if (!params[jss::public_key].isString())
60  return RPC::expected_field_error(jss::public_key, "a string");
61 
62  // Same for the pattern of "if field F is present, make sure it has type T
63  // and get it".
64  std::string desc;
65  if (params.isMember(jss::description))
66  {
67  if (!params[jss::description].isString())
68  return RPC::expected_field_error(jss::description, "a string");
69  desc = params[jss::description].asString();
70  }
71 
72  // channel_verify takes a key in both base58 and hex.
73  // @nikb prefers that we take only base58.
74  boost::optional<PublicKey> optPk = parseBase58<PublicKey>(
75  TokenType::NodePublic, params[jss::public_key].asString());
76  if (!optPk)
78  PublicKey const& nodeId = *optPk;
79 
80  auto const previous = context.app.peerReservations().insert_or_assign(
81  PeerReservation{nodeId, desc});
82 
84  if (previous)
85  {
86  result[jss::previous] = previous->toJson();
87  }
88  return result;
89 }
90 
93 {
94  if (context.app.config().reporting())
96 
97  auto const& params = context.params;
98 
99  // We repeat much of the parameter parsing from `doPeerReservationsAdd`.
100  if (!params.isMember(jss::public_key))
101  return RPC::missing_field_error(jss::public_key);
102  if (!params[jss::public_key].isString())
103  return RPC::expected_field_error(jss::public_key, "a string");
104 
105  boost::optional<PublicKey> optPk = parseBase58<PublicKey>(
106  TokenType::NodePublic, params[jss::public_key].asString());
107  if (!optPk)
109  PublicKey const& nodeId = *optPk;
110 
111  auto const previous = context.app.peerReservations().erase(nodeId);
112 
114  if (previous)
115  {
116  result[jss::previous] = previous->toJson();
117  }
118  return result;
119 }
120 
123 {
124  if (context.app.config().reporting())
126 
127  auto const& reservations = context.app.peerReservations().list();
128  // Enumerate the reservations in context.app.peerReservations()
129  // as a Json::Value.
131  Json::Value& jaReservations = result[jss::reservations] = Json::arrayValue;
132  for (auto const& reservation : reservations)
133  {
134  jaReservations.append(reservation.toJson());
135  }
136  return result;
137 }
138 
139 } // namespace ripple
ripple::RPC::JsonContext
Definition: Context.h:53
std::string
STL class.
utility
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
ripple::Application::peerReservations
virtual PeerReservationTable & peerReservations()=0
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
ripple::PeerReservationTable::erase
auto erase(PublicKey const &nodeId) -> boost::optional< PeerReservation >
Definition: PeerReservationTable.cpp:148
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:243
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:882
ripple::Config::reporting
bool reporting() const
Definition: Config.h:267
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::Application::config
virtual Config & config()=0
ripple::rpcPUBLIC_MALFORMED
@ rpcPUBLIC_MALFORMED
Definition: ErrorCodes.h:117
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
ripple::PeerReservation
Definition: PeerReservationTable.h:43
ripple::rpcError
Json::Value rpcError(int iError, Json::Value jvResult)
Definition: RPCErr.cpp:29
ripple::doPeerReservationsList
Json::Value doPeerReservationsList(RPC::JsonContext &)
Definition: Reservations.cpp:122
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerReservationTable::list
std::vector< PeerReservation > list() const
Definition: PeerReservationTable.cpp:51
ripple::doPeerReservationsDel
Json::Value doPeerReservationsDel(RPC::JsonContext &)
Definition: Reservations.cpp:92
ripple::TokenType::NodePublic
@ NodePublic
ripple::PeerReservationTable::insert_or_assign
auto insert_or_assign(PeerReservation const &reservation) -> boost::optional< PeerReservation >
Definition: PeerReservationTable.cpp:108
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::doPeerReservationsAdd
Json::Value doPeerReservationsAdd(RPC::JsonContext &)
Definition: Reservations.cpp:36
Json::Value
Represents a JSON value.
Definition: json_value.h:145
string