xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
xrpld
rpc
detail
Handler.h
1
#pragma once
2
3
#include <xrpld/rpc/RPCHandler.h>
4
#include <xrpld/rpc/Role.h>
5
#include <xrpld/rpc/detail/Tuning.h>
6
7
#include <xrpl/basics/Log.h>
8
#include <xrpl/basics/StringUtilities.h>
9
#include <xrpl/json/json_value.h>
10
#include <xrpl/protocol/ApiVersion.h>
11
#include <xrpl/protocol/ErrorCodes.h>
12
#include <xrpl/protocol/jss.h>
13
#include <xrpl/server/NetworkOPs.h>
14
15
#include <rpcspec/Errors.hpp>
16
17
#include <
span
>
18
#include <
string_view
>
19
20
namespace
xrpl::rpc
{
21
22
// Under what condition can we call this RPC?
23
enum class
Condition
{
24
NoCondition
= 0,
25
NeedsNetworkConnection
= 1,
26
NeedsCurrentLedger
= 1 << 1,
27
NeedsClosedLedger
= 1 << 2,
28
};
29
30
struct
Handler
31
{
48
class
Method
49
{
50
public
:
51
using
Function
= ::rpc::Status (*)(
JsonContext
&,
json::Value
&);
52
59
template
<Function Fn>
60
static
constexpr
Method
61
of
() noexcept
62
{
63
return
Method
{Fn};
64
}
65
73
::rpc::Status
74
operator()
(
JsonContext
& context,
json::Value
& result)
const
75
{
76
return
fn_
(context, result);
77
}
78
79
private
:
80
constexpr
explicit
Method
(
Function
fn) noexcept :
fn_
(fn)
81
{
82
}
83
84
Function
fn_
;
85
};
86
87
std::string_view
name
;
88
Method
valueMethod
;
89
Role
role
;
90
rpc::Condition
condition
;
91
92
unsigned
minApiVer
=
kApiMinimumSupportedVersion
;
93
unsigned
maxApiVer
=
kApiMaximumValidVersion
;
94
95
// Whether the command-line client accepts this method as a command. The
96
// exceptions are methods whose arguments have no positional form. A field
97
// rather than a comment, so that RPCCall_test can check it against the
98
// command-line table in both directions.
99
bool
hasCommandLineForm
=
true
;
100
};
101
113
Handler
const
*
114
getHandler
(
unsigned
int
version,
bool
betaEnabled,
std::string_view
name);
115
119
template
<
class
Value>
120
json::Value
121
makeObjectValue
(Value
const
& value,
json::StaticString
const
& field = jss::message)
122
{
123
json::Value
result(
json::ValueType::Object
);
124
result[field] = value;
125
return
result;
126
}
127
135
std::span<NullTerminatedView const>
136
getHandlerNames
();
137
138
template
<
class
T>
139
ErrorCodeI
140
conditionMet
(
Condition
conditionRequired, T& context)
141
{
142
if
(context.app.getOPs().isAmendmentBlocked() && (conditionRequired !=
Condition::NoCondition
))
143
{
144
return
RpcAmendmentBlocked
;
145
}
146
147
if
(context.app.getOPs().isUNLBlocked() && (conditionRequired !=
Condition::NoCondition
))
148
{
149
return
RpcExpiredValidatorList
;
150
}
151
152
if
((conditionRequired !=
Condition::NoCondition
) &&
153
(context.netOps.getOperatingMode() <
OperatingMode::SYNCING
))
154
{
155
JLOG(context.j.info()) <<
"Insufficient network mode for RPC: "
156
<< context.netOps.strOperatingMode();
157
158
if
(context.apiVersion == 1)
159
return
RpcNoNetwork
;
160
return
RpcNotSynced
;
161
}
162
163
if
(!context.app.config().standalone() && conditionRequired !=
Condition::NoCondition
)
164
{
165
if
(context.ledgerMaster.getValidatedLedgerAge() >
tuning::kMaxValidatedLedgerAge
)
166
{
167
if
(context.apiVersion == 1)
168
return
RpcNoCurrent
;
169
return
RpcNotSynced
;
170
}
171
172
auto
const
cID = context.ledgerMaster.getCurrentLedgerIndex();
173
auto
const
vID = context.ledgerMaster.getValidLedgerIndex();
174
175
if
(cID + 10 < vID)
176
{
177
JLOG(context.j.debug()) <<
"Current ledger ID("
<< cID
178
<<
") is less than validated ledger ID("
<< vID <<
")"
;
179
if
(context.apiVersion == 1)
180
return
RpcNoCurrent
;
181
return
RpcNotSynced
;
182
}
183
}
184
185
if
((conditionRequired !=
Condition::NoCondition
) && !context.ledgerMaster.getClosedLedger())
186
{
187
if
(context.apiVersion == 1)
188
return
RpcNoClosed
;
189
return
RpcNotSynced
;
190
}
191
192
return
RpcSuccess
;
193
}
194
195
}
// namespace xrpl::rpc
std::string_view
json::StaticString
Lightweight wrapper to tag static string.
Definition
json_value.h:52
json::Value
Represents a JSON value.
Definition
json_value.h:121
xrpl::rpc::Handler::Method
The function a handler dispatches to.
Definition
Handler.h:49
xrpl::rpc::Handler::Method::fn_
Function fn_
Definition
Handler.h:84
xrpl::rpc::Handler::Method::of
static constexpr Method of() noexcept
Build a Method that calls a given function.
Definition
Handler.h:61
xrpl::rpc::Handler::Method::Method
constexpr Method(Function fn) noexcept
Definition
Handler.h:80
xrpl::rpc::Handler::Method::Function
::rpc::Status(*)(JsonContext &, json::Value &) Function
Definition
Handler.h:51
xrpl::rpc::Handler::Method::operator()
::rpc::Status operator()(JsonContext &context, json::Value &result) const
Call the function.
Definition
Handler.h:74
json::ValueType::Object
@ Object
object value (collection of name/value pairs).
Definition
json_value.h:33
xrpl::rpc::tuning::kMaxValidatedLedgerAge
constexpr auto kMaxValidatedLedgerAge
Definition
src/xrpld/rpc/detail/Tuning.h:69
xrpl::rpc
The api_version numbers this server serves.
Definition
ApiVersion.h:26
xrpl::rpc::conditionMet
ErrorCodeI conditionMet(Condition conditionRequired, T &context)
Definition
Handler.h:140
xrpl::rpc::getHandlerNames
std::span< NullTerminatedView const > getHandlerNames()
Return the names of all methods, sorted and without duplicates.
Definition
Handler.cpp:657
xrpl::rpc::makeObjectValue
json::Value makeObjectValue(Value const &value, json::StaticString const &field=jss::message)
Return a json::ValueType::Object with a single entry.
Definition
Handler.h:121
xrpl::rpc::getHandler
Handler const * getHandler(unsigned version, bool betaEnabled, std::string_view name)
Definition
Handler.cpp:638
xrpl::rpc::kApiMaximumValidVersion
static constexpr auto kApiMaximumValidVersion
Definition
ApiVersion.h:37
xrpl::rpc::kApiMinimumSupportedVersion
static constexpr auto kApiMinimumSupportedVersion
Definition
ApiVersion.h:32
xrpl::rpc::Condition
Condition
Definition
Handler.h:23
xrpl::rpc::Condition::NeedsCurrentLedger
@ NeedsCurrentLedger
Definition
Handler.h:26
xrpl::rpc::Condition::NeedsNetworkConnection
@ NeedsNetworkConnection
Definition
Handler.h:25
xrpl::rpc::Condition::NeedsClosedLedger
@ NeedsClosedLedger
Definition
Handler.h:27
xrpl::rpc::Condition::NoCondition
@ NoCondition
Definition
Handler.h:24
xrpl::ErrorCodeI
ErrorCodeI
Definition
ErrorCodes.h:23
xrpl::RpcNoClosed
@ RpcNoClosed
Definition
ErrorCodes.h:47
xrpl::RpcNoNetwork
@ RpcNoNetwork
Definition
ErrorCodes.h:49
xrpl::RpcAmendmentBlocked
@ RpcAmendmentBlocked
Definition
ErrorCodes.h:44
xrpl::RpcSuccess
@ RpcSuccess
Definition
ErrorCodes.h:27
xrpl::RpcExpiredValidatorList
@ RpcExpiredValidatorList
Definition
ErrorCodes.h:120
xrpl::RpcNotSynced
@ RpcNotSynced
Definition
ErrorCodes.h:50
xrpl::RpcNoCurrent
@ RpcNoCurrent
Definition
ErrorCodes.h:48
xrpl::Role
Role
Indicates the level of administrative permission to grant.
Definition
Role.h:27
xrpl::OperatingMode::SYNCING
@ SYNCING
fallen slightly behind
Definition
NetworkOPs.h:63
span
string_view
xrpl::rpc::Handler
Definition
Handler.h:31
xrpl::rpc::Handler::role
Role role
Definition
Handler.h:89
xrpl::rpc::Handler::hasCommandLineForm
bool hasCommandLineForm
Definition
Handler.h:99
xrpl::rpc::Handler::valueMethod
Method valueMethod
Definition
Handler.h:88
xrpl::rpc::Handler::name
std::string_view name
Definition
Handler.h:87
xrpl::rpc::Handler::minApiVer
unsigned minApiVer
Definition
Handler.h:92
xrpl::rpc::Handler::maxApiVer
unsigned maxApiVer
Definition
Handler.h:93
xrpl::rpc::Handler::condition
rpc::Condition condition
Definition
Handler.h:90
xrpl::rpc::JsonContext
Definition
Context.h:41
Generated by
1.17.0