From 76d911ca21e833bc8d0c68d8bcff0c7e95cc14ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sat, 9 Mar 2024 15:06:28 +0100 Subject: [PATCH] FIX TRENDED CALCULATION --- nano/core_test/vote_processor.cpp | 13 ++++++++++++- nano/node/node.cpp | 13 ++++--------- nano/node/node.hpp | 1 - nano/node/online_reps.cpp | 2 +- nano/secure/common.cpp | 4 +++- nano/secure/common.hpp | 2 +- 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/nano/core_test/vote_processor.cpp b/nano/core_test/vote_processor.cpp index 2b286723e0..1a97e388f7 100644 --- a/nano/core_test/vote_processor.cpp +++ b/nano/core_test/vote_processor.cpp @@ -151,9 +151,20 @@ TEST (vote_processor, weights) system.wallet (0)->send_sync (nano::dev::genesis_key.pub, key1.pub, level1); system.wallet (0)->send_sync (nano::dev::genesis_key.pub, key2.pub, level2); + while (true) + { + std::cout << "*******************" << std::endl; + std::cout << "total: " << total << std::endl; + std::cout << "online: " << node.online_reps.online () << std::endl; + std::cout << "trended: " << node.online_reps.trended () << std::endl; + std::cout << "reps: " << node.rep_crawler.representative_count () << std::endl; + std::cout << "*******************" << std::endl; + WAIT (1s); + } + // Wait for representatives ASSERT_TIMELY_EQ (10s, node.ledger.cache.rep_weights.get_rep_amounts ().size (), 4); - ASSERT_TIMELY_EQ (5s, node.online_reps.online (), total); + ASSERT_TIMELY_EQ (5s, node.online_reps.trended (), total); // Wait for rep tiers to be updated node.stats.clear (); diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 9706566ecf..bb7ce793ae 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -591,7 +591,7 @@ void nano::node::start () rep_crawler.start (); } ongoing_peer_store (); - ongoing_online_weight_calculation_queue (); + ongoing_online_weight_calculation (); bool tcp_enabled = false; if (config.tcp_incoming_connections_max > 0 && !(flags.disable_bootstrap_listener && flags.disable_tcp_realtime)) @@ -1145,12 +1145,13 @@ bool nano::node::block_confirmed_or_being_confirmed (nano::block_hash const & ha return block_confirmed_or_being_confirmed (store.tx_begin_read (), hash_a); } -void nano::node::ongoing_online_weight_calculation_queue () +void nano::node::ongoing_online_weight_calculation () { std::weak_ptr node_w (shared_from_this ()); - workers.add_timed_task (std::chrono::steady_clock::now () + (std::chrono::seconds (network_params.node.weight_period)), [node_w] () { + workers.add_timed_task (std::chrono::steady_clock::now () + (network_params.node.weight_period), [node_w] () { if (auto node_l = node_w.lock ()) { + node_l->online_reps.sample (); node_l->ongoing_online_weight_calculation (); } }); @@ -1161,12 +1162,6 @@ bool nano::node::online () const return rep_crawler.total_weight () > online_reps.delta (); } -void nano::node::ongoing_online_weight_calculation () -{ - online_reps.sample (); - ongoing_online_weight_calculation_queue (); -} - void nano::node::receive_confirmed (store::transaction const & block_transaction_a, nano::block_hash const & hash_a, nano::account const & destination_a) { nano::unique_lock lk{ wallets.mutex }; diff --git a/nano/node/node.hpp b/nano/node/node.hpp index e762fbffce..6bb9a434f6 100644 --- a/nano/node/node.hpp +++ b/nano/node/node.hpp @@ -119,7 +119,6 @@ class node final : public std::enable_shared_from_this bool block_confirmed_or_being_confirmed (nano::block_hash const &); void do_rpc_callback (boost::asio::ip::tcp::resolver::iterator i_a, std::string const &, uint16_t, std::shared_ptr const &, std::shared_ptr const &, std::shared_ptr const &); void ongoing_online_weight_calculation (); - void ongoing_online_weight_calculation_queue (); bool online () const; bool init_error () const; std::pair get_bootstrap_weights () const; diff --git a/nano/node/online_reps.cpp b/nano/node/online_reps.cpp index 3fdd2442dd..2d1a0b4ca4 100644 --- a/nano/node/online_reps.cpp +++ b/nano/node/online_reps.cpp @@ -23,7 +23,7 @@ void nano::online_reps::observe (nano::account const & rep_a) auto now = std::chrono::steady_clock::now (); auto new_insert = reps.get ().erase (rep_a) == 0; reps.insert ({ now, rep_a }); - auto cutoff = reps.get ().lower_bound (now - std::chrono::seconds (config.network_params.node.weight_period)); + auto cutoff = reps.get ().lower_bound (now - config.network_params.node.weight_period); auto trimmed = reps.get ().begin () != cutoff; reps.get ().erase (reps.get ().begin (), cutoff); if (new_insert || trimmed) diff --git a/nano/secure/common.cpp b/nano/secure/common.cpp index 091e45fcc4..73af07afba 100644 --- a/nano/secure/common.cpp +++ b/nano/secure/common.cpp @@ -18,6 +18,8 @@ #include #include +using namespace std::chrono_literals; + nano::networks nano::network_constants::active_network = nano::networks::ACTIVE_NETWORK; namespace @@ -170,7 +172,7 @@ nano::node_constants::node_constants (nano::network_constants & network_constant unchecked_cleaning_interval = std::chrono::minutes (30); process_confirmed_interval = network_constants.is_dev_network () ? std::chrono::milliseconds (50) : std::chrono::milliseconds (500); max_weight_samples = (network_constants.is_live_network () || network_constants.is_test_network ()) ? 4032 : 288; - weight_period = 5 * 60; // 5 minutes + weight_period = network_constants.is_dev_network () ? 1s : 5min; } nano::voting_constants::voting_constants (nano::network_constants & network_constants) : diff --git a/nano/secure/common.hpp b/nano/secure/common.hpp index 9ce6858258..1b919d1a62 100644 --- a/nano/secure/common.hpp +++ b/nano/secure/common.hpp @@ -345,7 +345,7 @@ class node_constants /** The maximum amount of samples for a 2 week period on live or 1 day on beta */ uint64_t max_weight_samples; - uint64_t weight_period; + std::chrono::seconds weight_period; }; /** Voting related constants whose value depends on the active network */