rippled
ripple
app
reporting
P2pProxy.cpp
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
#include <ripple/app/reporting/P2pProxy.h>
21
#include <ripple/app/reporting/ReportingETL.h>
22
#include <ripple/json/json_reader.h>
23
#include <ripple/json/json_writer.h>
24
25
namespace
ripple
{
26
27
Json::Value
28
forwardToP2p
(
RPC::JsonContext
& context)
29
{
30
return
context.
app
.
getReportingETL
().
getETLLoadBalancer
().
forwardToP2p
(
31
context);
32
}
33
34
std::unique_ptr<org::xrpl::rpc::v1::XRPLedgerAPIService::Stub>
35
getP2pForwardingStub
(
RPC::Context
& context)
36
{
37
return
context.
app
.
getReportingETL
()
38
.
getETLLoadBalancer
()
39
.
getP2pForwardingStub
();
40
}
41
42
// We only forward requests where ledger_index is "current" or "closed"
43
// otherwise, attempt to handle here
44
bool
45
shouldForwardToP2p
(
RPC::JsonContext
& context)
46
{
47
if
(!context.
app
.
config
().
reporting
())
48
return
false
;
49
50
Json::Value
& params = context.
params
;
51
std::string
strCommand = params.
isMember
(jss::command)
52
? params[jss::command].
asString
()
53
: params[jss::method].
asString
();
54
55
JLOG(context.
j
.
trace
()) <<
"COMMAND:"
<< strCommand;
56
JLOG(context.
j
.
trace
()) <<
"REQUEST:"
<< params;
57
auto
handler =
RPC::getHandler
(
58
context.
apiVersion
, context.
app
.
config
().
BETA_RPC_API
, strCommand);
59
if
(!handler)
60
{
61
JLOG(context.
j
.
error
())
62
<<
"Error getting handler. command = "
<< strCommand;
63
return
false
;
64
}
65
66
if
(handler->condition_ ==
RPC::NEEDS_CURRENT_LEDGER
||
67
handler->condition_ ==
RPC::NEEDS_CLOSED_LEDGER
)
68
{
69
return
true
;
70
}
71
72
if
(params.
isMember
(jss::ledger_index))
73
{
74
auto
indexValue = params[jss::ledger_index];
75
if
(indexValue.isString())
76
{
77
auto
index = indexValue.
asString
();
78
return
index ==
"current"
|| index ==
"closed"
;
79
}
80
}
81
return
false
;
82
}
83
84
}
// namespace ripple
ripple::RPC::JsonContext
Definition:
Context.h:53
std::string
STL class.
beast::Journal::trace
Stream trace() const
Severity stream access functions.
Definition:
Journal.h:309
ripple::ETLLoadBalancer::forwardToP2p
Json::Value forwardToP2p(RPC::JsonContext &context) const
Forward a JSON RPC request to a randomly selected p2p node.
Definition:
ETLSource.cpp:755
ripple::RPC::getHandler
Handler const * getHandler(unsigned version, bool betaEnabled, std::string const &name)
Definition:
Handler.cpp:243
ripple::ETLLoadBalancer::getP2pForwardingStub
std::unique_ptr< org::xrpl::rpc::v1::XRPLedgerAPIService::Stub > getP2pForwardingStub() const
Randomly select a p2p node to forward a gRPC request to.
Definition:
ETLSource.cpp:733
ripple::RPC::Context::j
const beast::Journal j
Definition:
Context.h:41
ripple::Config::reporting
bool reporting() const
Definition:
Config.h:337
ripple::Application::getReportingETL
virtual ReportingETL & getReportingETL()=0
ripple::RPC::NEEDS_CURRENT_LEDGER
@ NEEDS_CURRENT_LEDGER
Definition:
Handler.h:42
ripple::Application::config
virtual Config & config()=0
ripple::ReportingETL::getETLLoadBalancer
ETLLoadBalancer & getETLLoadBalancer()
Definition:
ReportingETL.h:356
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
beast::Journal::error
Stream error() const
Definition:
Journal.h:333
ripple::Config::BETA_RPC_API
bool BETA_RPC_API
Definition:
Config.h:293
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition:
json_value.cpp:932
ripple::RPC::NEEDS_CLOSED_LEDGER
@ NEEDS_CLOSED_LEDGER
Definition:
Handler.h:43
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
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
ripple::RPC::Context::apiVersion
unsigned int apiVersion
Definition:
Context.h:50
std::unique_ptr< org::xrpl::rpc::v1::XRPLedgerAPIService::Stub >
ripple::RPC::JsonContext::params
Json::Value params
Definition:
Context.h:64
ripple::RPC::Context
The context of information needed to call an RPC.
Definition:
Context.h:39
Json::Value
Represents a JSON value.
Definition:
json_value.h:145
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition:
json_value.cpp:469
Generated by
1.8.17