rippled
P2pProxy.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2020 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_APP_REPORTING_P2PPROXY_H_INCLUDED
21 #define RIPPLE_APP_REPORTING_P2PPROXY_H_INCLUDED
22 
23 #include <ripple/app/main/Application.h>
24 #include <ripple/rpc/Context.h>
25 #include <ripple/rpc/impl/Handler.h>
26 
27 #include <boost/beast/websocket.hpp>
28 
29 #include "org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h"
30 #include <grpcpp/grpcpp.h>
31 
32 namespace ripple {
37 forwardToP2p(RPC::JsonContext& context);
38 
42 bool
43 shouldForwardToP2p(RPC::JsonContext& context);
44 
45 template <class Request>
46 bool
47 needCurrentOrClosed(Request& request)
48 {
49  // These are the only gRPC requests that specify a ledger
50  if constexpr (
52  value ||
55  value ||
57  {
58  if (request.ledger().ledger_case() ==
59  org::xrpl::rpc::v1::LedgerSpecifier::LedgerCase::kShortcut)
60  {
61  if (request.ledger().shortcut() !=
62  org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_VALIDATED &&
63  request.ledger().shortcut() !=
64  org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_UNSPECIFIED)
65  return true;
66  }
67  }
68  // GetLedgerDiff specifies two ledgers
69  else if constexpr (std::is_same<
70  Request,
71  org::xrpl::rpc::v1::GetLedgerDiffRequest>::value)
72  {
73  auto help = [](auto specifier) {
74  if (specifier.ledger_case() ==
75  org::xrpl::rpc::v1::LedgerSpecifier::LedgerCase::kShortcut)
76  {
77  if (specifier.shortcut() !=
78  org::xrpl::rpc::v1::LedgerSpecifier::
79  SHORTCUT_VALIDATED &&
80  specifier.shortcut() !=
81  org::xrpl::rpc::v1::LedgerSpecifier::
82  SHORTCUT_UNSPECIFIED)
83  return true;
84  }
85  return false;
86  };
87  return help(request.base_ledger()) || help(request.desired_ledger());
88  }
89  return false;
90 }
91 
96 template <class Request>
97 bool
99 {
100  if (!context.app.config().reporting())
101  return false;
102  if (condition == RPC::NEEDS_CURRENT_LEDGER ||
103  condition == RPC::NEEDS_CLOSED_LEDGER)
104  return true;
105 
106  return needCurrentOrClosed(context.params);
107 }
108 
113 getP2pForwardingStub(RPC::Context& context);
114 
115 } // namespace ripple
116 #endif
std::is_same
ripple::needCurrentOrClosed
bool needCurrentOrClosed(Request &request)
Definition: P2pProxy.h:47
ripple::Config::reporting
bool reporting() const
Definition: Config.h:267
ripple::RPC::NEEDS_CURRENT_LEDGER
@ NEEDS_CURRENT_LEDGER
Definition: Handler.h:42
ripple::Application::config
virtual Config & config()=0
ripple::RPC::GRPCContext
Definition: Context.h:70
ripple::getP2pForwardingStub
std::unique_ptr< org::xrpl::rpc::v1::XRPLedgerAPIService::Stub > getP2pForwardingStub(RPC::Context &context)
Get stub used to forward gRPC requests to a p2p node.
Definition: P2pProxy.cpp:35
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
ripple::RPC::NEEDS_CLOSED_LEDGER
@ NEEDS_CLOSED_LEDGER
Definition: Handler.h:43
ripple::RPC::Condition
Condition
Definition: Handler.h:39
ripple::forwardToP2p
Json::Value forwardToP2p(RPC::JsonContext &context)
Forward a JSON request to a p2p node and return the response.
Definition: P2pProxy.cpp:28
ripple::RPC::GRPCContext::params
RequestType params
Definition: Context.h:72
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::shouldForwardToP2p
bool shouldForwardToP2p(RPC::JsonContext &context)
Whether a request should be forwarded, based on request parameters.
Definition: P2pProxy.cpp:45
std::unique_ptr< org::xrpl::rpc::v1::XRPLedgerAPIService::Stub >
Json::Value
Represents a JSON value.
Definition: json_value.h:145