Add random id to RPC send via IPC if not included (#2892)

* Add random id to RPC send via IPC if not included

* Add a request callback for RPC test setups; add a test case

Co-authored-by: Guilherme Lawless <guilherme@nano.org>
This commit is contained in:
Sergey Kroshnin 2020-08-25 18:24:34 +03:00 committed by GitHub
commit 518467b0b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 0 deletions

View file

@ -59,6 +59,11 @@ void nano::json_handler::process_request (bool unsafe_a)
{
std::stringstream istream (body);
boost::property_tree::read_json (istream, request);
if (node_rpc_config.request_callback)
{
debug_assert (nano::network_constants ().is_dev_network ());
node_rpc_config.request_callback (request);
}
action = request.get<std::string> ("action");
auto no_arg_func_iter = ipc_json_handler_no_arg_funcs.find (action);
if (no_arg_func_iter != ipc_json_handler_no_arg_funcs.cend ())

View file

@ -3,6 +3,8 @@
#include <nano/lib/tomlconfig.hpp>
#include <nano/node/node_rpc_config.hpp>
#include <boost/property_tree/ptree.hpp>
nano::error nano::node_rpc_config::serialize_json (nano::jsonconfig & json) const
{
json.put ("version", json_version ());
@ -54,3 +56,9 @@ nano::error nano::node_rpc_config::deserialize_json (bool & upgraded_a, nano::js
return json.get_error ();
}
void nano::node_rpc_config::set_request_callback (std::function<void(boost::property_tree::ptree const &)> callback_a)
{
debug_assert (nano::network_constants ().is_dev_network ());
request_callback = std::move (callback_a);
}

View file

@ -2,6 +2,8 @@
#include <nano/lib/rpcconfig.hpp>
#include <boost/property_tree/ptree_fwd.hpp>
#include <string>
namespace boost
@ -36,5 +38,9 @@ public:
{
return 1;
}
// Used in tests to ensure requests are modified in specific cases
void set_request_callback (std::function<void(boost::property_tree::ptree const &)>);
std::function<void(boost::property_tree::ptree const &)> request_callback;
};
}

View file

@ -1,6 +1,8 @@
#include <nano/crypto_lib/random_pool.hpp>
#include <nano/lib/errors.hpp>
#include <nano/lib/json_error_response.hpp>
#include <nano/lib/logger_mt.hpp>
#include <nano/lib/numbers.hpp>
#include <nano/lib/rpc_handler_interface.hpp>
#include <nano/lib/rpcconfig.hpp>
#include <nano/rpc/rpc_handler.hpp>
@ -99,6 +101,18 @@ void nano::rpc_handler::process_request (nano::rpc_handler_request_params const
error = true;
}
}
// Add random id to RPC send via IPC if not included
else if (action == "send" && request.find ("id") == request.not_found ())
{
nano::uint128_union random_id;
nano::random_pool::generate_block (random_id.bytes.data (), random_id.bytes.size ());
std::string random_id_text;
random_id.encode_hex (random_id_text);
request.put ("id", random_id_text);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request);
body = ostream.str ();
}
}
if (!error)

View file

@ -612,6 +612,32 @@ TEST (rpc, send_epoch_2)
}
}
TEST (rpc, send_ipc_random_id)
{
nano::system system;
auto node = add_ipc_enabled_node (system);
scoped_io_thread_name_change scoped_thread_name_io;
nano::node_rpc_config node_rpc_config;
std::atomic<bool> got_request{ false };
node_rpc_config.set_request_callback ([&got_request](boost::property_tree::ptree const & request_a) {
EXPECT_TRUE (request_a.count ("id"));
got_request = true;
});
nano::ipc::ipc_server ipc_server (*node, node_rpc_config);
nano::rpc_config rpc_config (nano::get_available_port (), true);
rpc_config.rpc_process.ipc_port = node->config.ipc_config.transport_tcp.port;
nano::ipc_rpc_processor ipc_rpc_processor (system.io_ctx, rpc_config);
nano::rpc rpc (system.io_ctx, rpc_config, ipc_rpc_processor);
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "send");
test_response response (request, rpc.config.port, system.io_ctx);
ASSERT_TIMELY (10s, response.status != 0);
ASSERT_EQ (1, response.json.count ("error"));
ASSERT_EQ ("Unable to parse JSON", response.json.get<std::string> ("error"));
ASSERT_TRUE (got_request);
}
TEST (rpc, stop)
{
nano::system system;