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