Skip to content

Commit

Permalink
Add error handling to RPC process without json_block (#2377)
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermelawless authored and Russel Waters committed Nov 1, 2019
1 parent 387cebd commit 5897cfb
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 17 deletions.
24 changes: 17 additions & 7 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,23 +258,33 @@ nano::amount nano::json_handler::amount_impl ()

std::shared_ptr<nano::block> nano::json_handler::block_impl (bool signature_work_required)
{
std::shared_ptr<nano::block> result;
std::shared_ptr<nano::block> result{ nullptr };
if (!ec)
{
std::string block_text (request.get<std::string> ("block"));
boost::property_tree::ptree block_l;
std::stringstream block_stream (block_text);
boost::property_tree::read_json (block_stream, block_l);
if (!signature_work_required)
try
{
block_l.put ("signature", "0");
block_l.put ("work", "0");
boost::property_tree::read_json (block_stream, block_l);
}
result = nano::deserialize_block_json (block_l);
if (result == nullptr)
catch (...)
{
ec = nano::error_blocks::invalid_block;
}
if (!ec)
{
if (!signature_work_required)
{
block_l.put ("signature", "0");
block_l.put ("work", "0");
}
result = nano::deserialize_block_json (block_l);
if (result == nullptr)
{
ec = nano::error_blocks::invalid_block;
}
}
}
return result;
}
Expand Down
84 changes: 74 additions & 10 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1691,20 +1691,84 @@ TEST (rpc, process_block)
std::string json;
send.serialize_json (json);
request.put ("block", json);
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
system.deadline_set (10s);
while (system.nodes[0]->latest (nano::test_genesis_key.pub) != send.hash ())
{
ASSERT_NO_ERROR (system.poll ());
}
std::string send_hash (response.json.get<std::string> ("hash"));
ASSERT_EQ (send.hash ().to_string (), send_hash);
}
ASSERT_EQ (200, response.status);
system.deadline_set (10s);
while (system.nodes[0]->latest (nano::test_genesis_key.pub) != send.hash ())
request.put ("json_block", true);
{
ASSERT_NO_ERROR (system.poll ());
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
std::error_code ec (nano::error_blocks::invalid_block);
ASSERT_EQ (ec.message (), response.json.get<std::string> ("error"));
}
}

TEST (rpc, process_json_block)
{
nano::system system (24000, 1);
scoped_io_thread_name_change scoped_thread_name_io;
nano::keypair key;
auto latest (system.nodes[0]->latest (nano::test_genesis_key.pub));
auto & node1 (*system.nodes[0]);
nano::send_block send (latest, key.pub, 100, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *node1.work_generate_blocking (latest));
enable_ipc_transport_tcp (node1.config.ipc_config.transport_tcp);
nano::node_rpc_config node_rpc_config;
nano::ipc::ipc_server ipc_server (node1, node_rpc_config);
nano::rpc_config rpc_config (true);
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", "process");
boost::property_tree::ptree block_node;
send.serialize_json (block_node);
request.add_child ("block", block_node);
{
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
std::error_code ec (nano::error_blocks::invalid_block);
ASSERT_EQ (ec.message (), response.json.get<std::string> ("error"));
}
request.put ("json_block", true);
{
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
system.deadline_set (10s);
while (system.nodes[0]->latest (nano::test_genesis_key.pub) != send.hash ())
{
ASSERT_NO_ERROR (system.poll ());
}
std::string send_hash (response.json.get<std::string> ("hash"));
ASSERT_EQ (send.hash ().to_string (), send_hash);
}
std::string send_hash (response.json.get<std::string> ("hash"));
ASSERT_EQ (send.hash ().to_string (), send_hash);
}

TEST (rpc, process_block_with_work_watcher)
Expand Down

0 comments on commit 5897cfb

Please sign in to comment.