rippled
Loading...
Searching...
No Matches
Handler.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_RPC_HANDLER_H_INCLUDED
21#define RIPPLE_RPC_HANDLER_H_INCLUDED
22
23#include <xrpld/app/ledger/LedgerMaster.h>
24#include <xrpld/app/misc/NetworkOPs.h>
25#include <xrpld/core/Config.h>
26#include <xrpld/rpc/RPCHandler.h>
27#include <xrpld/rpc/Status.h>
28#include <xrpld/rpc/detail/RPCHelpers.h>
29#include <xrpld/rpc/detail/Tuning.h>
30#include <vector>
31
32namespace Json {
33class Object;
34}
35
36namespace ripple {
37namespace RPC {
38
39// Under what condition can we call this RPC?
45};
46
47struct Handler
48{
49 template <class JsonValue>
50 using Method = std::function<Status(JsonContext&, JsonValue&)>;
51
52 const char* name_;
56
59};
60
61Handler const*
62getHandler(unsigned int version, bool betaEnabled, std::string const&);
63
65template <class Value>
68 Value const& value,
69 Json::StaticString const& field = jss::message)
70{
72 result[field] = value;
73 return result;
74}
75
79
80template <class T>
82conditionMet(Condition condition_required, T& context)
83{
84 if (context.app.getOPs().isAmendmentBlocked() &&
85 (condition_required != NO_CONDITION))
86 {
88 }
89
90 if (context.app.getOPs().isUNLBlocked() &&
91 (condition_required != NO_CONDITION))
92 {
94 }
95
96 if ((condition_required != NO_CONDITION) &&
97 (context.netOps.getOperatingMode() < OperatingMode::SYNCING))
98 {
99 JLOG(context.j.info()) << "Insufficient network mode for RPC: "
100 << context.netOps.strOperatingMode();
101
102 if (context.apiVersion == 1)
103 return rpcNO_NETWORK;
104 return rpcNOT_SYNCED;
105 }
106
107 if (!context.app.config().standalone() &&
108 condition_required != NO_CONDITION)
109 {
110 if (context.ledgerMaster.getValidatedLedgerAge() >
112 {
113 if (context.apiVersion == 1)
114 return rpcNO_CURRENT;
115 return rpcNOT_SYNCED;
116 }
117
118 auto const cID = context.ledgerMaster.getCurrentLedgerIndex();
119 auto const vID = context.ledgerMaster.getValidLedgerIndex();
120
121 if (cID + 10 < vID)
122 {
123 JLOG(context.j.debug())
124 << "Current ledger ID(" << cID
125 << ") is less than validated ledger ID(" << vID << ")";
126 if (context.apiVersion == 1)
127 return rpcNO_CURRENT;
128 return rpcNOT_SYNCED;
129 }
130 }
131
132 if ((condition_required != NO_CONDITION) &&
133 !context.ledgerMaster.getClosedLedger())
134 {
135 if (context.apiVersion == 1)
136 return rpcNO_CLOSED;
137 return rpcNOT_SYNCED;
138 }
139
140 return rpcSUCCESS;
141}
142
143} // namespace RPC
144} // namespace ripple
145
146#endif
Lightweight wrapper to tag static string.
Definition: json_value.h:61
Represents a JSON value.
Definition: json_value.h:147
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
@ NEEDS_CURRENT_LEDGER
Definition: Handler.h:43
@ NEEDS_NETWORK_CONNECTION
Definition: Handler.h:42
@ NO_CONDITION
Definition: Handler.h:41
@ NEEDS_CLOSED_LEDGER
Definition: Handler.h:44
std::set< char const * > getHandlerNames()
Return names of all methods.
Definition: Handler.cpp:315
Json::Value makeObjectValue(Value const &value, Json::StaticString const &field=jss::message)
Return a Json::objectValue with a single entry.
Definition: Handler.h:67
static constexpr auto apiMaximumValidVersion
Definition: ApiVersion.h:63
Handler const * getHandler(unsigned version, bool betaEnabled, std::string const &name)
Definition: Handler.cpp:309
error_code_i conditionMet(Condition condition_required, T &context)
Definition: Handler.h:82
static constexpr auto apiMinimumSupportedVersion
Definition: ApiVersion.h:57
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
error_code_i
Definition: ErrorCodes.h:40
@ rpcNO_NETWORK
Definition: ErrorCodes.h:66
@ rpcAMENDMENT_BLOCKED
Definition: ErrorCodes.h:61
@ rpcSUCCESS
Definition: ErrorCodes.h:44
@ rpcNO_CURRENT
Definition: ErrorCodes.h:65
@ rpcEXPIRED_VALIDATOR_LIST
Definition: ErrorCodes.h:137
@ rpcNO_CLOSED
Definition: ErrorCodes.h:64
@ rpcNOT_SYNCED
Definition: ErrorCodes.h:67
@ SYNCING
fallen slightly behind
Role
Indicates the level of administrative permission to grant.
Definition: Role.h:43
RPC::Condition condition_
Definition: Handler.h:55
Method< Json::Value > valueMethod_
Definition: Handler.h:53
const char * name_
Definition: Handler.h:52
unsigned maxApiVer_
Definition: Handler.h:58
unsigned minApiVer_
Definition: Handler.h:57
Status represents the results of an operation that might fail.
Definition: Status.h:40