From 3642fdc5a64477b6f56e2a0710825a5e723fa3dd Mon Sep 17 00:00:00 2001 From: Guilherme Lawless Date: Tue, 15 Oct 2019 12:55:54 +0100 Subject: [PATCH] RPC unchecked json_block option (#2341) --- nano/node/json_handler.cpp | 16 +++++-- nano/rpc_test/rpc.cpp | 95 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 3 deletions(-) diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index 22061de4..8df2e02c 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -3905,6 +3905,7 @@ void nano::json_handler::stop () void nano::json_handler::unchecked () { + const bool json_block_l = request.get ("json_block", false); auto count (count_optional_impl ()); if (!ec) { @@ -3913,9 +3914,18 @@ void nano::json_handler::unchecked () for (auto i (node.store.unchecked_begin (transaction)), n (node.store.unchecked_end ()); i != n && unchecked.size () < count; ++i) { nano::unchecked_info const & info (i->second); - std::string contents; - info.block->serialize_json (contents); - unchecked.put (info.block->hash ().to_string (), contents); + if (json_block_l) + { + boost::property_tree::ptree block_node_l; + info.block->serialize_json (block_node_l); + unchecked.add_child (info.block->hash ().to_string (), block_node_l); + } + else + { + std::string contents; + info.block->serialize_json (contents); + unchecked.put (info.block->hash ().to_string (), contents); + } } response_l.add_child ("blocks", unchecked); } diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 07f5605a..bd829529 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -6294,6 +6294,101 @@ TEST (rpc, stats_clear) ASSERT_LE (system.nodes[0]->stats.last_reset ().count (), 5); } +TEST (rpc, unchecked) +{ + nano::system system (24000, 1); + nano::keypair key; + auto & node (*system.nodes[0]); + enable_ipc_transport_tcp (node.config.ipc_config.transport_tcp); + nano::node_rpc_config node_rpc_config; + nano::ipc::ipc_server ipc_server (node, 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 (); + auto open (std::make_shared (key.pub, 0, key.pub, 1, key.pub, key.prv, key.pub, *system.work.generate (key.pub))); + auto open2 (std::make_shared (key.pub, 0, key.pub, 2, key.pub, key.prv, key.pub, *system.work.generate (key.pub))); + node.process_active (open); + node.process_active (open2); + node.block_processor.flush (); + boost::property_tree::ptree request; + request.put ("action", "unchecked"); + request.put ("count", 2); + { + 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); + auto & blocks (response.json.get_child ("blocks")); + ASSERT_EQ (2, blocks.size ()); + ASSERT_EQ (1, blocks.count (open->hash ().to_string ())); + ASSERT_EQ (1, blocks.count (open2->hash ().to_string ())); + } + 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); + auto & blocks (response.json.get_child ("blocks")); + ASSERT_EQ (2, blocks.size ()); + auto & open_block (blocks.get_child (open->hash ().to_string ())); + ASSERT_EQ ("state", open_block.get ("type")); + } +} + +TEST (rpc, unchecked_get) +{ + nano::system system (24000, 1); + nano::keypair key; + auto & node (*system.nodes[0]); + enable_ipc_transport_tcp (node.config.ipc_config.transport_tcp); + nano::node_rpc_config node_rpc_config; + nano::ipc::ipc_server ipc_server (node, 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 (); + auto open (std::make_shared (key.pub, 0, key.pub, 1, key.pub, key.prv, key.pub, *system.work.generate (key.pub))); + node.process_active (open); + node.block_processor.flush (); + boost::property_tree::ptree request; + request.put ("action", "unchecked_get"); + request.put ("hash", open->hash ().to_string ()); + { + 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); + ASSERT_EQ (1, response.json.count ("contents")); + auto timestamp (response.json.get ("modified_timestamp")); + ASSERT_LE (timestamp, nano::seconds_since_epoch ()); + } + 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); + auto & contents (response.json.get_child ("contents")); + ASSERT_EQ ("state", contents.get ("type")); + auto timestamp (response.json.get ("modified_timestamp")); + ASSERT_LE (timestamp, nano::seconds_since_epoch ()); + } +} + TEST (rpc, unopened) { nano::system system (24000, 1);