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 <optional>
29 #include <string>
30 #include <utility>
31 
32 namespace ripple {
33 
36 {
37  if (context.app.config().reporting())
39 
40  auto const& params = context.params;
41 
42  if (!params.isMember(jss::public_key))
43  return RPC::missing_field_error(jss::public_key);
44 
45  // Returning JSON from every function ruins any attempt to encapsulate
46  // the pattern of "get field F as type T, and diagnose an error if it is
47  // missing or malformed":
48  // - It is costly to copy whole JSON objects around just to check whether an
49  // error code is present.
50  // - It is not as easy to read when cluttered by code to pack and unpack the
51  // JSON object.
52  // - It is not as easy to write when you have to include all the packing and
53  // unpacking code.
54  // Exceptions would be easier to use, but have a terrible cost for control
55  // flow. An error monad is purpose-built for this situation; it is
56  // essentially an optional (the "maybe monad" in Haskell) with a non-unit
57  // type for the failure case to capture more information.
58  if (!params[jss::public_key].isString())
59  return RPC::expected_field_error(jss::public_key, "a string");
60 
61  // Same for the pattern of "if field F is present, make sure it has type T
62  // and get it".
63  std::string desc;
64  if (params.isMember(jss::description))
65  {
66  if (!params[jss::description].isString())
67  return RPC::expected_field_error(jss::description, "a string");
68  desc = params[jss::description].asString();
69  }
70 
71  // channel_verify takes a key in both base58 and hex.
72  // @nikb prefers that we take only base58.
73  std::optional<PublicKey> optPk = parseBase58<PublicKey>(
74  TokenType::NodePublic, params[jss::public_key].asString());
75  if (!optPk)
77  PublicKey const& nodeId = *optPk;
78 
79  auto const previous = context.app.peerReservations().insert_or_assign(
80  PeerReservation{nodeId, desc});
81 
83  if (previous)
84  {
85  result[jss::previous] = previous->toJson();
86  }
87  return result;
88 }
89 
92 {
93  if (context.app.config().reporting())
95 
96  auto const& params = context.params;
97 
98  // We repeat much of the parameter parsing from `doPeerReservationsAdd`.
99  if (!params.isMember(jss::public_key))
100  return RPC::missing_field_error(jss::public_key);
101  if (!params[jss::public_key].isString())
102  return RPC::expected_field_error(jss::public_key, "a string");
103 
104  std::optional<PublicKey> optPk = parseBase58<PublicKey>(
105  TokenType::NodePublic, params[jss::public_key].asString());
106  if (!optPk)
108  PublicKey const& nodeId = *optPk;
109 
110  auto const previous = context.app.peerReservations().erase(nodeId);
111 
113  if (previous)
114  {
115  result[jss::previous] = previous->toJson();
116  }
117  return result;
118 }
119 
122 {
123  if (context.app.config().reporting())
125 
126  auto const& reservations = context.app.peerReservations().list();
127  // Enumerate the reservations in context.app.peerReservations()
128  // as a Json::Value.
130  Json::Value& jaReservations = result[jss::reservations] = Json::arrayValue;
131  for (auto const& reservation : reservations)
132  {
133  jaReservations.append(reservation.toJson());
134  }
135  return result;
136 }
137 
138 } // namespace ripple
ripple::PeerReservationTable::erase
std::optional< PeerReservation > erase(PublicKey const &nodeId)
Definition: PeerReservationTable.cpp:118
ripple::RPC::JsonContext
Definition: Context.h:53
std::string
STL class.
ripple::rpcError
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
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:328
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:262
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:337
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::doPeerReservationsList
Json::Value doPeerReservationsList(RPC::JsonContext &)
Definition: Reservations.cpp:121
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:52
ripple::doPeerReservationsDel
Json::Value doPeerReservationsDel(RPC::JsonContext &)
Definition: Reservations.cpp:91
ripple::TokenType::NodePublic
@ NodePublic
optional
ripple::PeerReservationTable::insert_or_assign
std::optional< PeerReservation > insert_or_assign(PeerReservation const &reservation)
Definition: PeerReservationTable.cpp:84
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::doPeerReservationsAdd
Json::Value doPeerReservationsAdd(RPC::JsonContext &)
Definition: Reservations.cpp:35
Json::Value
Represents a JSON value.
Definition: json_value.h:145
string