xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
xrpld
rpc
handlers
admin
peer
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
15
namespace
xrpl
{
16
17
json::Value
18
doPeerReservationsAdd
(
rpc::JsonContext
& context)
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.
53
std::optional<PublicKey>
optPk =
54
parseBase58<PublicKey>
(
TokenType::NodePublic
, params[jss::public_key].asString());
55
if
(!optPk)
56
return
rpcError
(
RpcPublicMalformed
);
57
PublicKey
const
& nodeId = *optPk;
58
59
auto
const
previous = context.
app
.
getPeerReservations
().
insertOrAssign
(
60
PeerReservation
{.nodeId = nodeId, .description = desc});
61
62
json::Value
result{
json::ValueType::Object
};
63
if
(previous)
64
{
65
result[jss::previous] = previous->toJson();
66
}
67
return
result;
68
}
69
70
}
// namespace xrpl
std::string
json::Value
Represents a JSON value.
Definition
json_value.h:117
xrpl::PeerReservationTable::insertOrAssign
std::optional< PeerReservation > insertOrAssign(PeerReservation const &reservation)
Definition
PeerReservationTable.cpp:63
xrpl::PublicKey
A public key.
Definition
PublicKey.h:53
xrpl::ServiceRegistry::getPeerReservations
virtual PeerReservationTable & getPeerReservations()=0
json::ValueType::Object
@ Object
object value (collection of name/value pairs).
Definition
json_value.h:29
xrpl::rpc::expectedFieldError
json::Value expectedFieldError(std::string const &name, std::string const &type)
Definition
ErrorCodes.h:309
xrpl::rpc::missingFieldError
json::Value missingFieldError(std::string const &name)
Definition
ErrorCodes.h:243
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::RpcPublicMalformed
@ RpcPublicMalformed
Definition
ErrorCodes.h:100
xrpl::parseBase58
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition
AccountID.cpp:105
xrpl::rpcError
json::Value rpcError(ErrorCodeI iError)
Definition
RPCErr.cpp:13
xrpl::doPeerReservationsAdd
json::Value doPeerReservationsAdd(rpc::JsonContext &context)
Definition
PeerReservationsAdd.cpp:18
xrpl::TokenType::NodePublic
@ NodePublic
Definition
tokens.h:21
optional
string
xrpl::PeerReservation
Definition
PeerReservationTable.h:21
xrpl::rpc::Context::app
Application & app
Definition
Context.h:29
xrpl::rpc::JsonContext
Definition
Context.h:41
xrpl::rpc::JsonContext::params
json::Value params
Definition
Context.h:51
Generated by
1.17.0