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
23#include <xrpl/json/json_value.h>
24#include <xrpl/protocol/ErrorCodes.h>
25#include <xrpl/protocol/PublicKey.h>
26#include <xrpl/protocol/RPCErr.h>
27#include <xrpl/protocol/jss.h>
28
29#include <optional>
30#include <string>
31#include <utility>
32
33namespace 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 std::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 std::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
Represents a JSON value.
Definition json_value.h:149
Value & append(Value const &value)
Append value to array at the end.
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:44
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:45
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition ErrorCodes.h:349
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:283
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
@ rpcPUBLIC_MALFORMED
Definition ErrorCodes.h:117
Json::Value doPeerReservationsAdd(RPC::JsonContext &)
Json::Value doPeerReservationsList(RPC::JsonContext &)
Json::Value rpcError(int iError)
Definition RPCErr.cpp:31
Json::Value doPeerReservationsDel(RPC::JsonContext &)
Application & app
Definition Context.h:41