update proxy conditions

This commit is contained in:
Nathan Nichols
2021-05-13 12:22:13 -05:00
parent 262cadf514
commit 68d0d3373b
4 changed files with 17 additions and 116 deletions

View File

@@ -18,43 +18,22 @@
//==============================================================================
#include <boost/json.hpp>
#include <reporting/ReportingETL.h>
namespace ripple {
boost::json::object
forwardToP2p(boost::json::object const& request, ReportingETL& etl)
{
return etl.getETLLoadBalancer().forwardToP2p(request);
}
std::unique_ptr<org::xrpl::rpc::v1::XRPLedgerAPIService::Stub>
getP2pForwardingStub(ReportingETL& etl)
{
return etl
.getETLLoadBalancer()
.getP2pForwardingStub();
}
#include <reporting/server/session.h>
// We only forward requests where ledger_index is "current" or "closed"
// otherwise, attempt to handle here
bool
shouldForwardToP2p(boost::json::object const& request)
{
if(request.contains("forward") && request.at("forward").is_bool())
return request.at("forward").as_bool();
std::string strCommand = request.contains("command")
? request.at("command").as_string().c_str()
: request.at("method").as_string().c_str();
BOOST_LOG_TRIVIAL(info) << "COMMAND:" << strCommand;
BOOST_LOG_TRIVIAL(info) << "REQUEST:" << request;
auto handler = forwardCommands.find(strCommand) != forwardCommands.end();
if (!handler)
{
BOOST_LOG_TRIVIAL(error)
<< "Error getting handler. command = " << strCommand;
return false;
}
if (forwardCommands.find(strCommand) != forwardCommands.end())
return true;
if (request.contains("ledger_index"))
{
@@ -66,7 +45,5 @@ shouldForwardToP2p(boost::json::object const& request)
}
}
return true;
return false;
}
} // namespace ripple

View File

@@ -20,93 +20,12 @@
#ifndef RIPPLE_APP_REPORTING_P2PPROXY_H_INCLUDED
#define RIPPLE_APP_REPORTING_P2PPROXY_H_INCLUDED
#include <boost/beast/websocket.hpp>
#include "org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h"
#include <grpcpp/grpcpp.h>
namespace ripple {
/// Forward a JSON request to a p2p node and return the response
/// @param context context of the request
/// @return response from p2p node
Json::Value
forwardToP2p(RPC::JsonContext& context);
#include <boost/json.hpp>
/// Whether a request should be forwarded, based on request parameters
/// @param context context of the request
/// @param request request json
/// @return true if should be forwarded
bool
shouldForwardToP2p(RPC::JsonContext& context);
shouldForwardToP2p(boost::json::object const& request);
template <class Request>
bool
needCurrentOrClosed(Request& request)
{
// These are the only gRPC requests that specify a ledger
if constexpr (
std::is_same<Request, org::xrpl::rpc::v1::GetAccountInfoRequest>::
value ||
std::is_same<Request, org::xrpl::rpc::v1::GetLedgerRequest>::value ||
std::is_same<Request, org::xrpl::rpc::v1::GetLedgerDataRequest>::
value ||
std::is_same<Request, org::xrpl::rpc::v1::GetLedgerEntryRequest>::value)
{
if (request.ledger().ledger_case() ==
org::xrpl::rpc::v1::LedgerSpecifier::LedgerCase::kShortcut)
{
if (request.ledger().shortcut() !=
org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_VALIDATED &&
request.ledger().shortcut() !=
org::xrpl::rpc::v1::LedgerSpecifier::SHORTCUT_UNSPECIFIED)
return true;
}
}
// GetLedgerDiff specifies two ledgers
else if constexpr (std::is_same<
Request,
org::xrpl::rpc::v1::GetLedgerDiffRequest>::value)
{
auto help = [](auto specifier) {
if (specifier.ledger_case() ==
org::xrpl::rpc::v1::LedgerSpecifier::LedgerCase::kShortcut)
{
if (specifier.shortcut() !=
org::xrpl::rpc::v1::LedgerSpecifier::
SHORTCUT_VALIDATED &&
specifier.shortcut() !=
org::xrpl::rpc::v1::LedgerSpecifier::
SHORTCUT_UNSPECIFIED)
return true;
}
return false;
};
return help(request.base_ledger()) || help(request.desired_ledger());
}
return false;
}
/// Whether a request should be forwarded, based on request parameters
/// @param context context of the request
/// @condition required condition for the request
/// @return true if should be forwarded
template <class Request>
bool
shouldForwardToP2p(RPC::GRPCContext<Request>& context, RPC::Condition condition)
{
if (!context.app.config().reporting())
return false;
if (condition == RPC::NEEDS_CURRENT_LEDGER ||
condition == RPC::NEEDS_CLOSED_LEDGER)
return true;
return needCurrentOrClosed(context.params);
}
/// Get stub used to forward gRPC requests to a p2p node
/// @param context context of the request
/// @return stub to forward requests
std::unique_ptr<org::xrpl::rpc::v1::XRPLedgerAPIService::Stub>
getP2pForwardingStub(RPC::Context& context);
} // namespace ripple
#endif

View File

@@ -1,4 +1,5 @@
#include <reporting/server/session.h>
#include <reporting/P2pProxy.h>
void
fail(boost::beast::error_code ec, char const* what)
@@ -17,7 +18,7 @@ buildResponse(
BOOST_LOG_TRIVIAL(info) << "Received rpc command : " << request;
boost::json::object response;
if (forwardCommands.find(command) != forwardCommands.end())
if (shouldForwardToP2p(request))
return etl.getETLLoadBalancer().forwardToP2p(request);
switch (commandMap[command])

View File

@@ -73,7 +73,11 @@ static std::unordered_map<std::string, RPCCommand> commandMap{
static std::unordered_set<std::string> forwardCommands{
"submit",
"fee"
"submit_multisigned",
"fee",
"path_find",
"ripple_path_find",
"manifest"
};
boost::json::object