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/Tuning.h>
29 #include <vector>
30 
31 namespace Json {
32 class Object;
33 }
34 
35 namespace ripple {
36 namespace RPC {
37 
38 // Under what condition can we call this RPC?
39 enum Condition {
44 };
45 
46 struct Handler
47 {
48  template <class JsonValue>
49  using Method = std::function<Status(JsonContext&, JsonValue&)>;
50 
51  const char* name_;
55 };
56 
57 Handler const*
58 getHandler(unsigned int version, bool betaEnabled, std::string const&);
59 
61 template <class Value>
64  Value const& value,
65  Json::StaticString const& field = jss::message)
66 {
68  result[field] = value;
69  return result;
70 }
71 
75 
76 template <class T>
78 conditionMet(Condition condition_required, T& context)
79 {
80  if (context.app.config().reporting())
81  {
82  if (condition_required == NEEDS_CURRENT_LEDGER)
83  {
84  return rpcNO_CURRENT;
85  }
86  else if (condition_required == NEEDS_CLOSED_LEDGER)
87  {
88  return rpcNO_CLOSED;
89  }
90  else
91  {
92  return rpcSUCCESS;
93  }
94  }
95 
96  if (context.app.getOPs().isAmendmentBlocked() &&
97  (condition_required & NEEDS_CURRENT_LEDGER ||
98  condition_required & NEEDS_CLOSED_LEDGER))
99  {
100  return rpcAMENDMENT_BLOCKED;
101  }
102 
103  if (context.app.getOPs().isUNLBlocked() &&
104  (condition_required & NEEDS_CURRENT_LEDGER ||
105  condition_required & NEEDS_CLOSED_LEDGER))
106  {
108  }
109 
110  if ((condition_required & NEEDS_NETWORK_CONNECTION) &&
111  (context.netOps.getOperatingMode() < OperatingMode::SYNCING))
112  {
113  JLOG(context.j.info()) << "Insufficient network mode for RPC: "
114  << context.netOps.strOperatingMode();
115 
116  if (context.apiVersion == 1)
117  return rpcNO_NETWORK;
118  return rpcNOT_SYNCED;
119  }
120 
121  if (!context.app.config().standalone() &&
122  condition_required & NEEDS_CURRENT_LEDGER)
123  {
124  if (context.ledgerMaster.getValidatedLedgerAge() >
126  {
127  if (context.apiVersion == 1)
128  return rpcNO_CURRENT;
129  return rpcNOT_SYNCED;
130  }
131 
132  auto const cID = context.ledgerMaster.getCurrentLedgerIndex();
133  auto const vID = context.ledgerMaster.getValidLedgerIndex();
134 
135  if (cID + 10 < vID)
136  {
137  JLOG(context.j.debug())
138  << "Current ledger ID(" << cID
139  << ") is less than validated ledger ID(" << vID << ")";
140  if (context.apiVersion == 1)
141  return rpcNO_CURRENT;
142  return rpcNOT_SYNCED;
143  }
144  }
145 
146  if ((condition_required & NEEDS_CLOSED_LEDGER) &&
147  !context.ledgerMaster.getClosedLedger())
148  {
149  if (context.apiVersion == 1)
150  return rpcNO_CLOSED;
151  return rpcNOT_SYNCED;
152  }
153 
154  return rpcSUCCESS;
155 }
156 
157 } // namespace RPC
158 } // namespace ripple
159 
160 #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:243
ripple::RPC::Handler::role_
Role role_
Definition: Handler.h:53
vector
std::function
ripple::RPC::Handler::name_
const char * name_
Definition: Handler.h:51
ripple::RPC::NEEDS_NETWORK_CONNECTION
@ NEEDS_NETWORK_CONNECTION
Definition: Handler.h:41
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: json_reader.cpp:27
ripple::rpcSUCCESS
@ rpcSUCCESS
Definition: ErrorCodes.h:44
ripple::RPC::Handler
Definition: Handler.h:46
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:42
ripple::RPC::Handler::condition_
RPC::Condition condition_
Definition: Handler.h:54
ripple::RPC::NO_CONDITION
@ NO_CONDITION
Definition: Handler.h:40
ripple::rpcNO_CURRENT
@ rpcNO_CURRENT
Definition: ErrorCodes.h:65
ripple::RPC::NEEDS_CLOSED_LEDGER
@ NEEDS_CLOSED_LEDGER
Definition: Handler.h:43
ripple::RPC::Condition
Condition
Definition: Handler.h:39
ripple::RPC::Status
Status represents the results of an operation that might fail.
Definition: Status.h:39
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:52
ripple::RPC::conditionMet
error_code_i conditionMet(Condition condition_required, T &context)
Definition: Handler.h:78
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:60
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:63
ripple::rpcNO_CLOSED
@ rpcNO_CLOSED
Definition: ErrorCodes.h:64
ripple::RPC::getHandlerNames
std::vector< char const * > getHandlerNames()
Return names of all methods.
Definition: Handler.cpp:249
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