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