rippled
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 <ripple/app/ledger/LedgerMaster.h>
24 #include <ripple/app/misc/NetworkOPs.h>
25 #include <ripple/core/Config.h>
26 #include <ripple/rpc/RPCHandler.h>
27 #include <ripple/rpc/Status.h>
28 #include <ripple/rpc/impl/RPCHelpers.h>
29 #include <ripple/rpc/impl/Tuning.h>
30 #include <vector>
31 
32 namespace Json {
33 class Object;
34 }
35 
36 namespace ripple {
37 namespace RPC {
38 
39 // Under what condition can we call this RPC?
40 enum Condition {
45 };
46 
47 struct Handler
48 {
49  template <class JsonValue>
50  using Method = std::function<Status(JsonContext&, JsonValue&)>;
51 
52  const char* name_;
56 
59 };
60 
61 Handler const*
62 getHandler(unsigned int version, bool betaEnabled, std::string const&);
63 
65 template <class Value>
68  Value const& value,
69  Json::StaticString const& field = jss::message)
70 {
72  result[field] = value;
73  return result;
74 }
75 
79 
80 template <class T>
82 conditionMet(Condition condition_required, T& context)
83 {
84  if (context.app.config().reporting())
85  {
86  if (condition_required == NEEDS_CURRENT_LEDGER)
87  {
88  return rpcNO_CURRENT;
89  }
90  else if (condition_required == NEEDS_CLOSED_LEDGER)
91  {
92  return rpcNO_CLOSED;
93  }
94  else
95  {
96  return rpcSUCCESS;
97  }
98  }
99 
100  if (context.app.getOPs().isAmendmentBlocked() &&
101  (condition_required != NO_CONDITION))
102  {
103  return rpcAMENDMENT_BLOCKED;
104  }
105 
106  if (context.app.getOPs().isUNLBlocked() &&
107  (condition_required != NO_CONDITION))
108  {
110  }
111 
112  if ((condition_required != NO_CONDITION) &&
113  (context.netOps.getOperatingMode() < OperatingMode::SYNCING))
114  {
115  JLOG(context.j.info()) << "Insufficient network mode for RPC: "
116  << context.netOps.strOperatingMode();
117 
118  if (context.apiVersion == 1)
119  return rpcNO_NETWORK;
120  return rpcNOT_SYNCED;
121  }
122 
123  if (!context.app.config().standalone() &&
124  condition_required != NO_CONDITION)
125  {
126  if (context.ledgerMaster.getValidatedLedgerAge() >
128  {
129  if (context.apiVersion == 1)
130  return rpcNO_CURRENT;
131  return rpcNOT_SYNCED;
132  }
133 
134  auto const cID = context.ledgerMaster.getCurrentLedgerIndex();
135  auto const vID = context.ledgerMaster.getValidLedgerIndex();
136 
137  if (cID + 10 < vID)
138  {
139  JLOG(context.j.debug())
140  << "Current ledger ID(" << cID
141  << ") is less than validated ledger ID(" << vID << ")";
142  if (context.apiVersion == 1)
143  return rpcNO_CURRENT;
144  return rpcNOT_SYNCED;
145  }
146  }
147 
148  if ((condition_required != NO_CONDITION) &&
149  !context.ledgerMaster.getClosedLedger())
150  {
151  if (context.apiVersion == 1)
152  return rpcNO_CLOSED;
153  return rpcNOT_SYNCED;
154  }
155 
156  return rpcSUCCESS;
157 }
158 
159 } // namespace RPC
160 } // namespace ripple
161 
162 #endif
ripple::rpcNO_NETWORK
@ rpcNO_NETWORK
Definition: ErrorCodes.h:66
ripple::RPC::JsonContext
Definition: Context.h:53
std::string
STL class.
ripple::RPC::getHandler
Handler const * getHandler(unsigned version, bool betaEnabled, std::string const &name)
Definition: Handler.cpp:307
ripple::RPC::getHandlerNames
std::set< char const * > getHandlerNames()
Return names of all methods.
Definition: Handler.cpp:313
ripple::RPC::Handler::role_
Role role_
Definition: Handler.h:54
vector
std::function
ripple::RPC::Handler::maxApiVer_
unsigned maxApiVer_
Definition: Handler.h:58
ripple::RPC::Handler::name_
const char * name_
Definition: Handler.h:52
ripple::RPC::NEEDS_NETWORK_CONNECTION
@ NEEDS_NETWORK_CONNECTION
Definition: Handler.h:42
ripple::error_code_i
error_code_i
Definition: ErrorCodes.h:40
ripple::OperatingMode::SYNCING
@ SYNCING
fallen slightly behind
ripple::rpcAMENDMENT_BLOCKED
@ rpcAMENDMENT_BLOCKED
Definition: ErrorCodes.h:61
Json
JSON (JavaScript Object Notation).
Definition: DeliverMax.h:28
ripple::rpcSUCCESS
@ rpcSUCCESS
Definition: ErrorCodes.h:44
ripple::RPC::Handler
Definition: Handler.h:47
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::RPC::NEEDS_CURRENT_LEDGER
@ NEEDS_CURRENT_LEDGER
Definition: Handler.h:43
ripple::RPC::Handler::condition_
RPC::Condition condition_
Definition: Handler.h:55
ripple::RPC::NO_CONDITION
@ NO_CONDITION
Definition: Handler.h:41
ripple::rpcNO_CURRENT
@ rpcNO_CURRENT
Definition: ErrorCodes.h:65
ripple::RPC::NEEDS_CLOSED_LEDGER
@ NEEDS_CLOSED_LEDGER
Definition: Handler.h:44
ripple::RPC::Condition
Condition
Definition: Handler.h:40
ripple::RPC::Status
Status represents the results of an operation that might fail.
Definition: Status.h:39
ripple::RPC::apiMinimumSupportedVersion
constexpr unsigned int apiMinimumSupportedVersion
Definition: RPCHelpers.h:234
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RPC::Handler::valueMethod_
Method< Json::Value > valueMethod_
Definition: Handler.h:53
ripple::RPC::apiMaximumValidVersion
constexpr unsigned int apiMaximumValidVersion
Definition: RPCHelpers.h:238
ripple::RPC::conditionMet
error_code_i conditionMet(Condition condition_required, T &context)
Definition: Handler.h:82
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:60
ripple::RPC::Handler::minApiVer_
unsigned minApiVer_
Definition: Handler.h:57
ripple::RPC::makeObjectValue
Json::Value makeObjectValue(Value const &value, Json::StaticString const &field=jss::message)
Return a Json::objectValue with a single entry.
Definition: Handler.h:67
ripple::rpcNO_CLOSED
@ rpcNO_CLOSED
Definition: ErrorCodes.h:64
std::set
STL class.
ripple::Role
Role
Indicates the level of administrative permission to grant.
Definition: Role.h:43
ripple::RPC::Tuning::maxValidatedLedgerAge
constexpr auto maxValidatedLedgerAge
Definition: rpc/impl/Tuning.h:65
ripple::rpcNOT_SYNCED
@ rpcNOT_SYNCED
Definition: ErrorCodes.h:67
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::rpcEXPIRED_VALIDATOR_LIST
@ rpcEXPIRED_VALIDATOR_LIST
Definition: ErrorCodes.h:137