From d09a9c06ea2bb96f4c9790433c32560c7ee1546b Mon Sep 17 00:00:00 2001 From: Guilherme Lawless Date: Wed, 22 Jan 2020 12:55:07 +0000 Subject: [PATCH 1/2] Drop peers and online weight after 1 week of inactivity --- nano/node/node.cpp | 29 +++++++++++++++++++++++++++++ nano/node/node.hpp | 1 + 2 files changed, 30 insertions(+) diff --git a/nano/node/node.cpp b/nano/node/node.cpp index a9a44ea760..e3f434092e 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -625,6 +625,7 @@ nano::process_return nano::node::process_local (std::shared_ptr blo void nano::node::start () { + long_inactivity_cleanup (); network.start (); add_initial_peers (); if (!flags.disable_legacy_bootstrap) @@ -768,6 +769,34 @@ nano::uint128_t nano::node::minimum_principal_weight (nano::uint128_t const & on return online_stake / network_params.network.principal_weight_factor; } +void nano::node::long_inactivity_cleanup () +{ + bool perform_cleanup = false; + auto transaction (store.tx_begin_write ()); + if (store.online_weight_count (transaction) > 0) + { + auto i (store.online_weight_begin (transaction)); + auto sample (store.online_weight_begin (transaction)); + auto n (store.online_weight_end ()); + while (++i != n) + { + ++sample; + } + assert (sample != n); + auto one_week_ago = (std::chrono::system_clock::now () - std::chrono::hours (7 * 24)).time_since_epoch ().count (); + if (sample->first < one_week_ago) + { + perform_cleanup = true; + } + } + if (perform_cleanup) + { + store.online_weight_clear (transaction); + store.peer_clear (transaction); + logger.always_log ("Removed records of peers and online weight after a long period of inactivity"); + } +} + void nano::node::ongoing_rep_calculation () { auto now (std::chrono::steady_clock::now ()); diff --git a/nano/node/node.hpp b/nano/node/node.hpp index b8985a5d75..b4956b601b 100644 --- a/nano/node/node.hpp +++ b/nano/node/node.hpp @@ -115,6 +115,7 @@ class node final : public std::enable_shared_from_this nano::block_hash rep_block (nano::account const &); nano::uint128_t minimum_principal_weight (); nano::uint128_t minimum_principal_weight (nano::uint128_t const &); + void long_inactivity_cleanup (); void ongoing_rep_calculation (); void ongoing_bootstrap (); void ongoing_store_flush (); From 6c2d37c16fff88d8a4e50f0c9dc465e5d63a4f82 Mon Sep 17 00:00:00 2001 From: Guilherme Lawless Date: Fri, 24 Jan 2020 14:25:15 +0000 Subject: [PATCH 2/2] Making method private and simplifying condition check (Wesley review) --- nano/node/node.cpp | 7 ++----- nano/node/node.hpp | 4 +++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/nano/node/node.cpp b/nano/node/node.cpp index e3f434092e..6f7f20b85d 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -783,11 +783,8 @@ void nano::node::long_inactivity_cleanup () ++sample; } assert (sample != n); - auto one_week_ago = (std::chrono::system_clock::now () - std::chrono::hours (7 * 24)).time_since_epoch ().count (); - if (sample->first < one_week_ago) - { - perform_cleanup = true; - } + auto const one_week_ago = (std::chrono::system_clock::now () - std::chrono::hours (7 * 24)).time_since_epoch ().count (); + perform_cleanup = sample->first < one_week_ago; } if (perform_cleanup) { diff --git a/nano/node/node.hpp b/nano/node/node.hpp index b4956b601b..e8afee1d39 100644 --- a/nano/node/node.hpp +++ b/nano/node/node.hpp @@ -115,7 +115,6 @@ class node final : public std::enable_shared_from_this nano::block_hash rep_block (nano::account const &); nano::uint128_t minimum_principal_weight (); nano::uint128_t minimum_principal_weight (nano::uint128_t const &); - void long_inactivity_cleanup (); void ongoing_rep_calculation (); void ongoing_bootstrap (); void ongoing_store_flush (); @@ -195,6 +194,9 @@ class node final : public std::enable_shared_from_this std::atomic stopped{ false }; static double constexpr price_max = 16.0; static double constexpr free_cutoff = 1024.0; + +private: + void long_inactivity_cleanup (); }; std::unique_ptr collect_container_info (node & node, const std::string & name);