Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC endpoint for election statistics #4533

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,44 @@ void nano::json_handler::confirmation_active ()
response_errors ();
}

void nano::json_handler::election_statistics ()
{
auto active_elections = node.active.list_active ();
unsigned normal_count = 0;
unsigned hinted_count = 0;
unsigned optimistic_count = 0;
unsigned total_count = 0;

for (auto const & election : active_elections)
{
total_count++;
switch (election->behavior ())
{
case election_behavior::normal:
normal_count++;
break;
case election_behavior::hinted:
hinted_count++;
break;
case election_behavior::optimistic:
optimistic_count++;
break;
}
}

auto utilization_percentage = (static_cast<double> (total_count * 100) / node.config.active_elections_size);
std::stringstream stream;
stream << std::fixed << std::setprecision (2) << utilization_percentage;

response_l.put ("normal", normal_count);
response_l.put ("hinted", hinted_count);
response_l.put ("optimistic", optimistic_count);
response_l.put ("total", total_count);
response_l.put ("aec_utilization_percentage", stream.str ());

response_errors ();
}

void nano::json_handler::confirmation_history ()
{
boost::property_tree::ptree elections;
Expand Down Expand Up @@ -5327,6 +5365,7 @@ ipc_json_handler_no_arg_func_map create_ipc_json_handler_no_arg_func_map ()
no_arg_funcs.emplace ("delegators", &nano::json_handler::delegators);
no_arg_funcs.emplace ("delegators_count", &nano::json_handler::delegators_count);
no_arg_funcs.emplace ("deterministic_key", &nano::json_handler::deterministic_key);
no_arg_funcs.emplace ("election_statistics", &nano::json_handler::election_statistics);
no_arg_funcs.emplace ("epoch_upgrade", &nano::json_handler::epoch_upgrade);
no_arg_funcs.emplace ("frontiers", &nano::json_handler::frontiers);
no_arg_funcs.emplace ("frontier_count", &nano::json_handler::account_count);
Expand Down
1 change: 1 addition & 0 deletions nano/node/json_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class json_handler : public std::enable_shared_from_this<nano::json_handler>
void accounts_pending ();
void accounts_receivable ();
void active_difficulty ();
void election_statistics ();
void available_supply ();
void block_info ();
void block_confirm ();
Expand Down
15 changes: 15 additions & 0 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6866,3 +6866,18 @@ TEST (rpc, confirmation_info)
ASSERT_EQ (0, response.get<unsigned> ("total_tally"));
}
}

TEST (rpc, election_statistics)
{
nano::test::system system;
auto node1 = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node1);
boost::property_tree::ptree request1;
request1.put ("action", "election_statistics");
auto response1 (wait_response (system, rpc_ctx, request1));
ASSERT_EQ ("0", response1.get<std::string> ("normal"));
ASSERT_EQ ("0", response1.get<std::string> ("hinted"));
ASSERT_EQ ("0", response1.get<std::string> ("optimistic"));
ASSERT_EQ ("0", response1.get<std::string> ("total"));
ASSERT_EQ ("0.00", response1.get<std::string> ("aec_utilization_percentage"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could at least start a single election which would ensure reported values work as expected

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pwojcikdev I'm not sure how to do this. If I start a normal election it might get confirmed before the RPC request is executed.

}
Loading