xrpld
Loading...
Searching...
No Matches
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
20namespace xrpl::rpc {
21
22// Under what condition can we call this RPC?
29
30struct 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
85 };
86
91
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
113Handler const*
114getHandler(unsigned int version, bool betaEnabled, std::string_view name);
115
119template <class Value>
121makeObjectValue(Value const& value, json::StaticString const& field = jss::message)
122{
124 result[field] = value;
125 return result;
126}
127
137
138template <class T>
140conditionMet(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 {
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
Lightweight wrapper to tag static string.
Definition json_value.h:52
Represents a JSON value.
Definition json_value.h:121
The function a handler dispatches to.
Definition Handler.h:49
static constexpr Method of() noexcept
Build a Method that calls a given function.
Definition Handler.h:61
constexpr Method(Function fn) noexcept
Definition Handler.h:80
::rpc::Status(*)(JsonContext &, json::Value &) Function
Definition Handler.h:51
::rpc::Status operator()(JsonContext &context, json::Value &result) const
Call the function.
Definition Handler.h:74
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
The api_version numbers this server serves.
Definition ApiVersion.h:26
ErrorCodeI conditionMet(Condition conditionRequired, T &context)
Definition Handler.h:140
std::span< NullTerminatedView const > getHandlerNames()
Return the names of all methods, sorted and without duplicates.
Definition Handler.cpp:657
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
Handler const * getHandler(unsigned version, bool betaEnabled, std::string_view name)
Definition Handler.cpp:638
static constexpr auto kApiMaximumValidVersion
Definition ApiVersion.h:37
static constexpr auto kApiMinimumSupportedVersion
Definition ApiVersion.h:32
ErrorCodeI
Definition ErrorCodes.h:23
@ RpcNoClosed
Definition ErrorCodes.h:47
@ RpcNoNetwork
Definition ErrorCodes.h:49
@ RpcAmendmentBlocked
Definition ErrorCodes.h:44
@ RpcSuccess
Definition ErrorCodes.h:27
@ RpcExpiredValidatorList
Definition ErrorCodes.h:120
@ RpcNotSynced
Definition ErrorCodes.h:50
@ RpcNoCurrent
Definition ErrorCodes.h:48
Role
Indicates the level of administrative permission to grant.
Definition Role.h:27
@ SYNCING
fallen slightly behind
Definition NetworkOPs.h:63
bool hasCommandLineForm
Definition Handler.h:99
Method valueMethod
Definition Handler.h:88
std::string_view name
Definition Handler.h:87
unsigned minApiVer
Definition Handler.h:92
unsigned maxApiVer
Definition Handler.h:93
rpc::Condition condition
Definition Handler.h:90