From 5897cfbe5be0af604450f4f5a10fcfd5a2d3e2d3 Mon Sep 17 00:00:00 2001 From: Guilherme Lawless Date: Thu, 31 Oct 2019 22:15:36 +0000 Subject: [PATCH] Add error handling to RPC process without json_block (#2377) --- nano/node/json_handler.cpp | 24 +++++++---- nano/rpc_test/rpc.cpp | 84 +++++++++++++++++++++++++++++++++----- 2 files changed, 91 insertions(+), 17 deletions(-) diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index 87e7b71572..1656e2007c 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -258,23 +258,33 @@ nano::amount nano::json_handler::amount_impl () std::shared_ptr nano::json_handler::block_impl (bool signature_work_required) { - std::shared_ptr result; + std::shared_ptr result{ nullptr }; if (!ec) { std::string block_text (request.get ("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; } diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index bd8295290b..0e401763e8 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -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 ("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 ("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 ("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 ("hash")); + ASSERT_EQ (send.hash ().to_string (), send_hash); } - std::string send_hash (response.json.get ("hash")); - ASSERT_EQ (send.hash ().to_string (), send_hash); } TEST (rpc, process_block_with_work_watcher)