RPC unchecked json_block option (#2341)

This commit is contained in:
Guilherme Lawless 2019-10-15 12:55:54 +01:00 committed by GitHub
commit 3642fdc5a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 108 additions and 3 deletions

View file

@ -3905,6 +3905,7 @@ void nano::json_handler::stop ()
void nano::json_handler::unchecked ()
{
const bool json_block_l = request.get<bool> ("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);
}

View file

@ -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<nano::state_block> (key.pub, 0, key.pub, 1, key.pub, key.prv, key.pub, *system.work.generate (key.pub)));
auto open2 (std::make_shared<nano::state_block> (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<std::string> ("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<nano::state_block> (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<uint64_t> ("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<std::string> ("type"));
auto timestamp (response.json.get<uint64_t> ("modified_timestamp"));
ASSERT_LE (timestamp, nano::seconds_since_epoch ());
}
}
TEST (rpc, unopened)
{
nano::system system (24000, 1);