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