Add blocks_not_found in RPC blocks_info response rather than an error (#1950)

* Add blocks_not_found in RPC blocks_info response rather than an error

* Lambda capture
This commit is contained in:
Guilherme Lawless 2019-06-03 16:33:33 +01:00 committed by Wesley Shillingford
commit 6559c101da
2 changed files with 93 additions and 43 deletions

View file

@ -1028,9 +1028,11 @@ void nano::json_handler::blocks_info ()
const bool pending = request.get<bool> ("pending", false);
const bool source = request.get<bool> ("source", false);
const bool json_block_l = request.get<bool> ("json_block", false);
const bool include_not_found = request.get<bool> ("include_not_found", false);
std::vector<std::string> hashes;
boost::property_tree::ptree blocks;
boost::property_tree::ptree blocks_not_found;
auto transaction (node.store.tx_begin_read ());
for (boost::property_tree::ptree::value_type & hashes : request.get_child ("hashes"))
{
@ -1098,6 +1100,12 @@ void nano::json_handler::blocks_info ()
}
blocks.push_back (std::make_pair (hash_text, entry));
}
else if (include_not_found)
{
boost::property_tree::ptree entry;
entry.put ("", hash_text);
blocks_not_found.push_back (std::make_pair ("", entry));
}
else
{
ec = nano::error_blocks::not_found;
@ -1109,7 +1117,14 @@ void nano::json_handler::blocks_info ()
}
}
}
if (!ec)
{
response_l.add_child ("blocks", blocks);
if (include_not_found)
{
response_l.add_child ("blocks_not_found", blocks_not_found);
}
}
response_errors ();
}

View file

@ -4467,20 +4467,7 @@ TEST (rpc, blocks_info)
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", "blocks_info");
boost::property_tree::ptree entry;
boost::property_tree::ptree peers_l;
entry.put ("", system.nodes[0]->latest (nano::genesis_account).to_string ());
peers_l.push_back (std::make_pair ("", entry));
request.add_child ("hashes", peers_l);
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 check_blocks = [&system](test_response & response) {
for (auto & blocks : response.json.get_child ("blocks"))
{
std::string hash_text (blocks.first);
@ -4499,23 +4486,71 @@ TEST (rpc, blocks_info)
ASSERT_EQ (nano::genesis_amount.convert_to<std::string> (), balance_text);
ASSERT_TRUE (blocks.second.get<bool> ("confirmed")); // Genesis block is confirmed by default
}
// Test for optional values
request.put ("source", "true");
request.put ("pending", "1");
test_response response2 (request, rpc.config.port, system.io_ctx);
};
boost::property_tree::ptree request;
request.put ("action", "blocks_info");
boost::property_tree::ptree entry;
boost::property_tree::ptree hashes;
entry.put ("", system.nodes[0]->latest (nano::genesis_account).to_string ());
hashes.push_back (std::make_pair ("", entry));
request.add_child ("hashes", hashes);
{
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response2.status == 0)
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response2.status);
for (auto & blocks : response2.json.get_child ("blocks"))
ASSERT_EQ (200, response.status);
check_blocks (response);
}
std::string random_hash = nano::block_hash ().to_string ();
entry.put ("", random_hash);
hashes.push_back (std::make_pair ("", entry));
request.erase ("hashes");
request.add_child ("hashes", hashes);
{
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 ("Block not found", response.json.get<std::string> ("error"));
}
request.put ("include_not_found", "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);
check_blocks (response);
auto & blocks_not_found (response.json.get_child ("blocks_not_found"));
ASSERT_EQ (1, blocks_not_found.size ());
ASSERT_EQ (random_hash, blocks_not_found.begin ()->second.get<std::string> (""));
}
request.put ("source", "true");
request.put ("pending", "1");
{
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);
for (auto & blocks : response.json.get_child ("blocks"))
{
std::string source (blocks.second.get<std::string> ("source_account"));
ASSERT_EQ ("0", source);
std::string pending (blocks.second.get<std::string> ("pending"));
ASSERT_EQ ("0", pending);
}
}
}
TEST (rpc, blocks_info_subtype)
@ -4540,15 +4575,15 @@ TEST (rpc, blocks_info_subtype)
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "blocks_info");
boost::property_tree::ptree peers_l;
boost::property_tree::ptree hashes;
boost::property_tree::ptree entry;
entry.put ("", send->hash ().to_string ());
peers_l.push_back (std::make_pair ("", entry));
hashes.push_back (std::make_pair ("", entry));
entry.put ("", receive->hash ().to_string ());
peers_l.push_back (std::make_pair ("", entry));
hashes.push_back (std::make_pair ("", entry));
entry.put ("", change->hash ().to_string ());
peers_l.push_back (std::make_pair ("", entry));
request.add_child ("hashes", peers_l);
hashes.push_back (std::make_pair ("", entry));
request.add_child ("hashes", hashes);
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)