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 (
53  value ||
55  {
56  if (request.ledger().ledger_case() ==
57  org::xrpl::rpc::v1::LedgerSpecifier::LedgerCase::kShortcut)
58  {
59  if (request.ledger().shortcut() !=
60  org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_VALIDATED &&
61  request.ledger().shortcut() !=
62  org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_UNSPECIFIED)
63  return true;
64  }
65  }
66  // GetLedgerDiff specifies two ledgers
67  else if constexpr (std::is_same<
68  Request,
69  org::xrpl::rpc::v1::GetLedgerDiffRequest>::value)
70  {
71  auto help = [](auto specifier) {
72  if (specifier.ledger_case() ==
73  org::xrpl::rpc::v1::LedgerSpecifier::LedgerCase::kShortcut)
74  {
75  if (specifier.shortcut() !=
76  org::xrpl::rpc::v1::LedgerSpecifier::
77  SHORTCUT_VALIDATED &&
78  specifier.shortcut() !=
79  org::xrpl::rpc::v1::LedgerSpecifier::
80  SHORTCUT_UNSPECIFIED)
81  return true;
82  }
83  return false;
84  };
85  return help(request.base_ledger()) || help(request.desired_ledger());
86  }
87  return false;
88 }
89 
94 template <class Request>
95 bool
97 {
98  if (!context.app.config().reporting())
99  return false;
100  if (condition == RPC::NEEDS_CURRENT_LEDGER ||
101  condition == RPC::NEEDS_CLOSED_LEDGER)
102  return true;
103 
104  return needCurrentOrClosed(context.params);
105 }
106 
111 getP2pForwardingStub(RPC::Context& context);
112 
113 } // namespace ripple
114 #endif
std::is_same
ripple::needCurrentOrClosed
bool needCurrentOrClosed(Request &request)
Definition: P2pProxy.h:47
ripple::Config::reporting
bool reporting() const
Definition: Config.h:337
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