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