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/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
19namespace xrpl::rpc {
20
21// Under what condition can we call this RPC?
28
29struct Handler
30{
47 class Method
48 {
49 public:
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
84 };
85
90
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
112Handler const*
113getHandler(unsigned int version, bool betaEnabled, std::string_view name);
114
118template <class Value>
120makeObjectValue(Value const& value, json::StaticString const& field = jss::message)
121{
123 result[field] = value;
124 return result;
125}
126
136
137template <class T>
139conditionMet(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 {
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
Lightweight wrapper to tag static string.
Definition json_value.h:48
Represents a JSON value.
Definition json_value.h:117
The function a handler dispatches to.
Definition Handler.h:48
static constexpr Method of() noexcept
Build a Method that calls a given function.
Definition Handler.h:60
constexpr Method(Function fn) noexcept
Definition Handler.h:79
Status(*)(JsonContext &, json::Value &) Function
Definition Handler.h:50
Status operator()(JsonContext &context, json::Value &result) const
Call the function.
Definition Handler.h:73
@ Object
object value (collection of name/value pairs).
Definition json_value.h:29
API version numbers used in later API versions.
Definition ApiVersion.h:36
ErrorCodeI conditionMet(Condition conditionRequired, T &context)
Definition Handler.h:139
std::span< NullTerminatedView const > getHandlerNames()
Return the names of all methods, sorted and without duplicates.
Definition Handler.cpp:691
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
Handler const * getHandler(unsigned version, bool betaEnabled, std::string_view name)
Definition Handler.cpp:672
static constexpr auto kApiMaximumValidVersion
Definition ApiVersion.h:47
static constexpr auto kApiMinimumSupportedVersion
Definition ApiVersion.h:42
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:98
Method valueMethod
Definition Handler.h:87
std::string_view name
Definition Handler.h:86
unsigned minApiVer
Definition Handler.h:91
unsigned maxApiVer
Definition Handler.h:92
rpc::Condition condition
Definition Handler.h:89
Status represents the results of an operation that might fail.
Definition Status.h:27