From 7f33efdd38a6683f3b1bc09877ecaef68a94ab4d Mon Sep 17 00:00:00 2001 From: clemahieu Date: Tue, 7 May 2024 17:40:45 +0100 Subject: [PATCH] Move recently_cemented_cache and recently_confirmed_cache to their own files. (#4606) --- nano/node/CMakeLists.txt | 4 + nano/node/active_elections.cpp | 104 ------------------------- nano/node/active_elections.hpp | 68 +--------------- nano/node/recently_cemented_cache.cpp | 42 ++++++++++ nano/node/recently_cemented_cache.hpp | 38 +++++++++ nano/node/recently_confirmed_cache.cpp | 66 ++++++++++++++++ nano/node/recently_confirmed_cache.hpp | 63 +++++++++++++++ 7 files changed, 215 insertions(+), 170 deletions(-) create mode 100644 nano/node/recently_cemented_cache.cpp create mode 100644 nano/node/recently_cemented_cache.hpp create mode 100644 nano/node/recently_confirmed_cache.cpp create mode 100644 nano/node/recently_confirmed_cache.hpp diff --git a/nano/node/CMakeLists.txt b/nano/node/CMakeLists.txt index d392f82284..e91bab49af 100644 --- a/nano/node/CMakeLists.txt +++ b/nano/node/CMakeLists.txt @@ -127,6 +127,10 @@ add_library( portmapping.cpp process_live_dispatcher.cpp process_live_dispatcher.hpp + recently_cemented_cache.cpp + recently_cemented_cache.hpp + recently_confirmed_cache.cpp + recently_confirmed_cache.hpp repcrawler.hpp repcrawler.cpp rep_tiers.hpp diff --git a/nano/node/active_elections.cpp b/nano/node/active_elections.cpp index 8be7dc3c18..4f28c06665 100644 --- a/nano/node/active_elections.cpp +++ b/nano/node/active_elections.cpp @@ -662,110 +662,6 @@ std::unique_ptr nano::collect_container_info (ac return composite; } -/* - * class recently_confirmed - */ - -nano::recently_confirmed_cache::recently_confirmed_cache (std::size_t max_size_a) : - max_size{ max_size_a } -{ -} - -void nano::recently_confirmed_cache::put (const nano::qualified_root & root, const nano::block_hash & hash) -{ - nano::lock_guard guard{ mutex }; - confirmed.get ().emplace_back (root, hash); - if (confirmed.size () > max_size) - { - confirmed.get ().pop_front (); - } -} - -void nano::recently_confirmed_cache::erase (const nano::block_hash & hash) -{ - nano::lock_guard guard{ mutex }; - confirmed.get ().erase (hash); -} - -void nano::recently_confirmed_cache::clear () -{ - nano::lock_guard guard{ mutex }; - confirmed.clear (); -} - -bool nano::recently_confirmed_cache::exists (const nano::block_hash & hash) const -{ - nano::lock_guard guard{ mutex }; - return confirmed.get ().find (hash) != confirmed.get ().end (); -} - -bool nano::recently_confirmed_cache::exists (const nano::qualified_root & root) const -{ - nano::lock_guard guard{ mutex }; - return confirmed.get ().find (root) != confirmed.get ().end (); -} - -std::size_t nano::recently_confirmed_cache::size () const -{ - nano::lock_guard guard{ mutex }; - return confirmed.size (); -} - -nano::recently_confirmed_cache::entry_t nano::recently_confirmed_cache::back () const -{ - nano::lock_guard guard{ mutex }; - return confirmed.back (); -} - -std::unique_ptr nano::recently_confirmed_cache::collect_container_info (const std::string & name) -{ - nano::unique_lock lock{ mutex }; - - auto composite = std::make_unique (name); - composite->add_component (std::make_unique (container_info{ "confirmed", confirmed.size (), sizeof (decltype (confirmed)::value_type) })); - return composite; -} - -/* - * class recently_cemented - */ - -nano::recently_cemented_cache::recently_cemented_cache (std::size_t max_size_a) : - max_size{ max_size_a } -{ -} - -void nano::recently_cemented_cache::put (const nano::election_status & status) -{ - nano::lock_guard guard{ mutex }; - cemented.push_back (status); - if (cemented.size () > max_size) - { - cemented.pop_front (); - } -} - -nano::recently_cemented_cache::queue_t nano::recently_cemented_cache::list () const -{ - nano::lock_guard guard{ mutex }; - return cemented; -} - -std::size_t nano::recently_cemented_cache::size () const -{ - nano::lock_guard guard{ mutex }; - return cemented.size (); -} - -std::unique_ptr nano::recently_cemented_cache::collect_container_info (const std::string & name) -{ - nano::unique_lock lock{ mutex }; - - auto composite = std::make_unique (name); - composite->add_component (std::make_unique (container_info{ "cemented", cemented.size (), sizeof (decltype (cemented)::value_type) })); - return composite; -} - /* * active_elections_config */ diff --git a/nano/node/active_elections.hpp b/nano/node/active_elections.hpp index 9ebff5365f..493f3a76aa 100644 --- a/nano/node/active_elections.hpp +++ b/nano/node/active_elections.hpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include @@ -62,72 +64,6 @@ class active_elections_config final std::size_t confirmation_cache{ 65536 }; }; -class recently_confirmed_cache final -{ -public: - using entry_t = std::pair; - - explicit recently_confirmed_cache (std::size_t max_size); - - void put (nano::qualified_root const &, nano::block_hash const &); - void erase (nano::block_hash const &); - void clear (); - std::size_t size () const; - - bool exists (nano::qualified_root const &) const; - bool exists (nano::block_hash const &) const; - -public: // Tests - entry_t back () const; - -private: - // clang-format off - class tag_hash {}; - class tag_root {}; - class tag_sequence {}; - - using ordered_recent_confirmations = boost::multi_index_container>, - mi::hashed_unique, - mi::member>, - mi::hashed_unique, - mi::member>>>; - // clang-format on - ordered_recent_confirmations confirmed; - - std::size_t const max_size; - - mutable nano::mutex mutex; - -public: // Container info - std::unique_ptr collect_container_info (std::string const &); -}; - -/* - * Helper container for storing recently cemented elections (a block from election might be confirmed but not yet cemented by confirmation height processor) - */ -class recently_cemented_cache final -{ -public: - using queue_t = std::deque; - - explicit recently_cemented_cache (std::size_t max_size); - - void put (nano::election_status const &); - queue_t list () const; - std::size_t size () const; - -private: - queue_t cemented; - std::size_t const max_size; - - mutable nano::mutex mutex; - -public: // Container info - std::unique_ptr collect_container_info (std::string const &); -}; - /** * Core class for determining consensus * Holds all active blocks i.e. recently added blocks that need confirmation diff --git a/nano/node/recently_cemented_cache.cpp b/nano/node/recently_cemented_cache.cpp new file mode 100644 index 0000000000..d9d5539725 --- /dev/null +++ b/nano/node/recently_cemented_cache.cpp @@ -0,0 +1,42 @@ +#include +#include + +/* + * class recently_cemented + */ + +nano::recently_cemented_cache::recently_cemented_cache (std::size_t max_size_a) : + max_size{ max_size_a } +{ +} + +void nano::recently_cemented_cache::put (const nano::election_status & status) +{ + nano::lock_guard guard{ mutex }; + cemented.push_back (status); + if (cemented.size () > max_size) + { + cemented.pop_front (); + } +} + +nano::recently_cemented_cache::queue_t nano::recently_cemented_cache::list () const +{ + nano::lock_guard guard{ mutex }; + return cemented; +} + +std::size_t nano::recently_cemented_cache::size () const +{ + nano::lock_guard guard{ mutex }; + return cemented.size (); +} + +std::unique_ptr nano::recently_cemented_cache::collect_container_info (const std::string & name) +{ + nano::unique_lock lock{ mutex }; + + auto composite = std::make_unique (name); + composite->add_component (std::make_unique (container_info{ "cemented", cemented.size (), sizeof (decltype (cemented)::value_type) })); + return composite; +} diff --git a/nano/node/recently_cemented_cache.hpp b/nano/node/recently_cemented_cache.hpp new file mode 100644 index 0000000000..c2f382d673 --- /dev/null +++ b/nano/node/recently_cemented_cache.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +#include + +namespace nano +{ +class container_info_component; +} + +namespace nano +{ +/* + * Helper container for storing recently cemented elections (a block from election might be confirmed but not yet cemented by confirmation height processor) + */ +class recently_cemented_cache final +{ +public: + using queue_t = std::deque; + + explicit recently_cemented_cache (std::size_t max_size); + + void put (nano::election_status const &); + queue_t list () const; + std::size_t size () const; + +private: + queue_t cemented; + std::size_t const max_size; + + mutable nano::mutex mutex; + +public: // Container info + std::unique_ptr collect_container_info (std::string const &); +}; +} diff --git a/nano/node/recently_confirmed_cache.cpp b/nano/node/recently_confirmed_cache.cpp new file mode 100644 index 0000000000..cebffb2b26 --- /dev/null +++ b/nano/node/recently_confirmed_cache.cpp @@ -0,0 +1,66 @@ +#include +#include + +/* + * class recently_confirmed + */ + +nano::recently_confirmed_cache::recently_confirmed_cache (std::size_t max_size_a) : + max_size{ max_size_a } +{ +} + +void nano::recently_confirmed_cache::put (const nano::qualified_root & root, const nano::block_hash & hash) +{ + nano::lock_guard guard{ mutex }; + confirmed.get ().emplace_back (root, hash); + if (confirmed.size () > max_size) + { + confirmed.get ().pop_front (); + } +} + +void nano::recently_confirmed_cache::erase (const nano::block_hash & hash) +{ + nano::lock_guard guard{ mutex }; + confirmed.get ().erase (hash); +} + +void nano::recently_confirmed_cache::clear () +{ + nano::lock_guard guard{ mutex }; + confirmed.clear (); +} + +bool nano::recently_confirmed_cache::exists (const nano::block_hash & hash) const +{ + nano::lock_guard guard{ mutex }; + return confirmed.get ().find (hash) != confirmed.get ().end (); +} + +bool nano::recently_confirmed_cache::exists (const nano::qualified_root & root) const +{ + nano::lock_guard guard{ mutex }; + return confirmed.get ().find (root) != confirmed.get ().end (); +} + +std::size_t nano::recently_confirmed_cache::size () const +{ + nano::lock_guard guard{ mutex }; + return confirmed.size (); +} + +nano::recently_confirmed_cache::entry_t nano::recently_confirmed_cache::back () const +{ + nano::lock_guard guard{ mutex }; + return confirmed.back (); +} + +std::unique_ptr nano::recently_confirmed_cache::collect_container_info (const std::string & name) +{ + nano::unique_lock lock{ mutex }; + + auto composite = std::make_unique (name); + composite->add_component (std::make_unique (container_info{ "confirmed", confirmed.size (), sizeof (decltype (confirmed)::value_type) })); + return composite; +} diff --git a/nano/node/recently_confirmed_cache.hpp b/nano/node/recently_confirmed_cache.hpp new file mode 100644 index 0000000000..5b8249d906 --- /dev/null +++ b/nano/node/recently_confirmed_cache.hpp @@ -0,0 +1,63 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace mi = boost::multi_index; + +namespace nano +{ +class container_info_component; +} + +namespace nano +{ +class recently_confirmed_cache final +{ +public: + using entry_t = std::pair; + + explicit recently_confirmed_cache (std::size_t max_size); + + void put (nano::qualified_root const &, nano::block_hash const &); + void erase (nano::block_hash const &); + void clear (); + std::size_t size () const; + + bool exists (nano::qualified_root const &) const; + bool exists (nano::block_hash const &) const; + +public: // Tests + entry_t back () const; + +private: + // clang-format off + class tag_hash {}; + class tag_root {}; + class tag_sequence {}; + + using ordered_recent_confirmations = boost::multi_index_container>, + mi::hashed_unique, + mi::member>, + mi::hashed_unique, + mi::member>>>; + // clang-format on + ordered_recent_confirmations confirmed; + + std::size_t const max_size; + + mutable nano::mutex mutex; + +public: // Container info + std::unique_ptr collect_container_info (std::string const &); +}; +}