rippled
Loading...
Searching...
No Matches
PeerReservationsAdd.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
13namespace xrpl {
14
17{
18 auto const& params = context.params;
19
20 if (!params.isMember(jss::public_key))
21 return RPC::missing_field_error(jss::public_key);
22
23 // Returning JSON from every function ruins any attempt to encapsulate
24 // the pattern of "get field F as type T, and diagnose an error if it is
25 // missing or malformed":
26 // - It is costly to copy whole JSON objects around just to check whether an
27 // error code is present.
28 // - It is not as easy to read when cluttered by code to pack and unpack the
29 // JSON object.
30 // - It is not as easy to write when you have to include all the packing and
31 // unpacking code.
32 // Exceptions would be easier to use, but have a terrible cost for control
33 // flow. An error monad is purpose-built for this situation; it is
34 // essentially an optional (the "maybe monad" in Haskell) with a non-unit
35 // type for the failure case to capture more information.
36 if (!params[jss::public_key].isString())
37 return RPC::expected_field_error(jss::public_key, "a string");
38
39 // Same for the pattern of "if field F is present, make sure it has type T
40 // and get it".
41 std::string desc;
42 if (params.isMember(jss::description))
43 {
44 if (!params[jss::description].isString())
45 return RPC::expected_field_error(jss::description, "a string");
46 desc = params[jss::description].asString();
47 }
48
49 // channel_verify takes a key in both base58 and hex.
50 // @nikb prefers that we take only base58.
52 parseBase58<PublicKey>(TokenType::NodePublic, params[jss::public_key].asString());
53 if (!optPk)
55 PublicKey const& nodeId = *optPk;
56
57 auto const previous =
59
61 if (previous)
62 {
63 result[jss::previous] = previous->toJson();
64 }
65 return result;
66}
67
68} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
std::optional< PeerReservation > insert_or_assign(PeerReservation const &reservation)
A public key.
Definition PublicKey.h:42
virtual PeerReservationTable & getPeerReservations()=0
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:26
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition ErrorCodes.h:297
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:231
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Json::Value doPeerReservationsAdd(RPC::JsonContext &context)
Json::Value rpcError(error_code_i iError)
Definition RPCErr.cpp:12
@ rpcPUBLIC_MALFORMED
Definition ErrorCodes.h:97
Application & app
Definition Context.h:21
Json::Value params
Definition Context.h:43