From ab693253a3ba48bc044b3f519ef3d768d4d0665a Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:18:03 +0200 Subject: [PATCH 1/3] Add election age information to statistics --- nano/node/election.hpp | 4 ++++ nano/node/json_handler.cpp | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/nano/node/election.hpp b/nano/node/election.hpp index 6f8b61ca6f..834af89e07 100644 --- a/nano/node/election.hpp +++ b/nano/node/election.hpp @@ -118,6 +118,10 @@ class election final : public std::enable_shared_from_this nano::vote_info get_last_vote (nano::account const & account); void set_last_vote (nano::account const & account, nano::vote_info vote_info); nano::election_status get_status () const; + std::chrono::steady_clock::time_point get_election_start () const + { + return election_start; + } private: // Dependencies nano::node & node; diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index 7d226c4656..758c57cc6c 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -2009,10 +2009,21 @@ void nano::json_handler::election_statistics () unsigned hinted_count = 0; unsigned optimistic_count = 0; unsigned total_count = 0; + std::chrono::steady_clock::duration total_age{}; + auto now = std::chrono::steady_clock::now (); + std::chrono::steady_clock::time_point oldest_election_start = now; for (auto const & election : active_elections) { total_count++; + auto election_start = election->get_election_start (); + auto age = now - election_start; + total_age += age; + if (election_start < oldest_election_start) + { + oldest_election_start = election_start; + } + switch (election->behavior ()) { case election_behavior::normal: @@ -2028,14 +2039,20 @@ void nano::json_handler::election_statistics () } auto utilization_percentage = (static_cast (total_count * 100) / node.config.active_elections_size); - std::stringstream stream; - stream << std::fixed << std::setprecision (2) << utilization_percentage; + auto max_election_age = std::chrono::duration_cast (now - oldest_election_start).count (); + double average_election_age = total_count ? std::chrono::duration (total_age).count () / total_count : 0; + + std::stringstream stream_utilization, stream_average_age; + stream_utilization << std::fixed << std::setprecision (2) << utilization_percentage; + stream_average_age << std::fixed << std::setprecision (2) << average_election_age; 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_l.put ("aec_utilization_percentage", stream_utilization.str ()); + response_l.put ("max_election_age", max_election_age); + response_l.put ("average_election_age", stream_average_age.str ()); response_errors (); } From 8bcd91352c045905bfca612faa48897158147edf Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:37:52 +0200 Subject: [PATCH 2/3] Updated unit test --- nano/rpc_test/rpc.cpp | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 3b9168a000..8cb7a8b79e 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -6870,14 +6870,37 @@ TEST (rpc, confirmation_info) TEST (rpc, election_statistics) { nano::test::system system; - auto node1 = add_ipc_enabled_node (system); + nano::node_config node_config; + node_config.ipc_config.transport_tcp.enabled = true; + node_config.ipc_config.transport_tcp.port = system.get_available_port (); + nano::node_flags node_flags; + node_flags.disable_request_loop = true; + auto node1 (system.add_node (node_config, node_flags)); 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 ("normal")); - ASSERT_EQ ("0", response1.get ("hinted")); - ASSERT_EQ ("0", response1.get ("optimistic")); - ASSERT_EQ ("0", response1.get ("total")); - ASSERT_EQ ("0.00", response1.get ("aec_utilization_percentage")); + + nano::block_builder builder; + auto send1 = builder + .send () + .previous (nano::dev::genesis->hash ()) + .destination (nano::public_key ()) + .balance (nano::dev::constants.genesis_amount - 100) + .sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub) + .work (*system.work.generate (nano::dev::genesis->hash ())) + .build (); + node1->process_active (send1); + ASSERT_TRUE (nano::test::start_elections (system, *node1, { send1 })); + ASSERT_EQ (1, node1->active.size ()); + + boost::property_tree::ptree request; + request.put ("action", "election_statistics"); + { + auto response (wait_response (system, rpc_ctx, request)); + ASSERT_EQ ("1", response.get ("normal")); + ASSERT_EQ ("0", response.get ("hinted")); + ASSERT_EQ ("0", response.get ("optimistic")); + ASSERT_EQ ("1", response.get ("total")); + ASSERT_NE ("0.00", response.get ("aec_utilization_percentage")); + ASSERT_NO_THROW (response.get ("max_election_age")); + ASSERT_NO_THROW (response.get ("average_election_age")); + } } From 2151c2727a502b415b8c0e080fd228010bd07c94 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Fri, 5 Apr 2024 13:57:39 +0200 Subject: [PATCH 3/3] Fixed unit test --- nano/rpc_test/rpc.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 8cb7a8b79e..1dfa930969 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -6888,6 +6888,7 @@ TEST (rpc, election_statistics) .work (*system.work.generate (nano::dev::genesis->hash ())) .build (); node1->process_active (send1); + ASSERT_TIMELY (5s, !node1->active.empty ()); ASSERT_TRUE (nano::test::start_elections (system, *node1, { send1 })); ASSERT_EQ (1, node1->active.size ());