Skip to content

Commit

Permalink
Adding information on confirmed blocks for RPC confirmation_active
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermelawless committed Mar 2, 2020
1 parent f684f03 commit 0732da0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
18 changes: 14 additions & 4 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,7 @@ void nano::json_handler::chain (bool successors)
void nano::json_handler::confirmation_active ()
{
uint64_t announcements (0);
uint64_t confirmed (0);
boost::optional<std::string> announcements_text (request.get_optional<std::string> ("announcements"));
if (announcements_text.is_initialized ())
{
Expand All @@ -1766,15 +1767,24 @@ void nano::json_handler::confirmation_active ()
nano::lock_guard<std::mutex> lock (node.active.mutex);
for (auto i (node.active.roots.begin ()), n (node.active.roots.end ()); i != n; ++i)
{
if (i->election->confirmation_request_count >= announcements && !i->election->confirmed ())
if (i->election->confirmation_request_count >= announcements)
{
boost::property_tree::ptree entry;
entry.put ("", i->root.to_string ());
elections.push_back (std::make_pair ("", entry));
if (!i->election->confirmed ())
{
boost::property_tree::ptree entry;
entry.put ("", i->root.to_string ());
elections.push_back (std::make_pair ("", entry));
}
else
{
++confirmed;
}
}
}
}
response_l.add_child ("confirmations", elections);
response_l.put ("unconfirmed", elections.size ());
response_l.put ("confirmed", confirmed);
response_errors ();
}

Expand Down
50 changes: 50 additions & 0 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8152,6 +8152,56 @@ TEST (rpc, node_telemetry_self)
}
}

TEST (rpc, confirmation_active)
{
nano::system system;
nano::node_config node_config;
node_config.ipc_config.transport_tcp.enabled = true;
node_config.ipc_config.transport_tcp.port = nano::get_available_port ();
nano::node_flags node_flags;
node_flags.disable_request_loop = true;
auto & node1 (*system.add_node (node_config, node_flags));
scoped_io_thread_name_change scoped_thread_name_io;
nano::node_rpc_config node_rpc_config;
nano::ipc::ipc_server ipc_server (node1, node_rpc_config);
nano::rpc_config rpc_config (nano::get_available_port (), true);
rpc_config.rpc_process.ipc_port = node1.config.ipc_config.transport_tcp.port;
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 ();

nano::genesis genesis;
auto send1 (std::make_shared<nano::send_block> (genesis.hash (), nano::public_key (), nano::genesis_amount - 100, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (genesis.hash ())));
auto send2 (std::make_shared<nano::send_block> (send1->hash (), nano::public_key (), nano::genesis_amount - 200, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (send1->hash ())));
node1.process_active (send1);
node1.process_active (send2);
node1.block_processor.flush ();
ASSERT_EQ (2, node1.active.size ());
{
nano::lock_guard<std::mutex> guard (node1.active.mutex);
auto info (node1.active.roots.find (send1->qualified_root ()));
ASSERT_NE (node1.active.roots.end (), info);
info->election->confirm_once ();
}

boost::property_tree::ptree request;
request.put ("action", "confirmation_active");
{
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 & confirmations (response.json.get_child ("confirmations"));
ASSERT_EQ (1, confirmations.size ());
ASSERT_EQ (send2->qualified_root ().to_string (), confirmations.front ().second.get<std::string> (""));
ASSERT_EQ (1, response.json.get<unsigned> ("unconfirmed"));
ASSERT_EQ (1, response.json.get<unsigned> ("confirmed"));
}
}

TEST (rpc, confirmation_info)
{
nano::system system;
Expand Down

0 comments on commit 0732da0

Please sign in to comment.