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