diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index f139d752..48fd15a0 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include diff --git a/nano/core_test/voting.cpp b/nano/core_test/voting.cpp index c1122bc8..3c08618a 100644 --- a/nano/core_test/voting.cpp +++ b/nano/core_test/voting.cpp @@ -1,7 +1,8 @@ #include #include #include -#include +#include +#include #include #include #include diff --git a/nano/lib/processing_queue.hpp b/nano/lib/processing_queue.hpp index e9f31737..4570c952 100644 --- a/nano/lib/processing_queue.hpp +++ b/nano/lib/processing_queue.hpp @@ -108,7 +108,7 @@ public: } public: // Container info - std::unique_ptr collect_container_info (std::string const & name) + std::unique_ptr collect_container_info (std::string const & name) const { nano::lock_guard guard{ mutex }; diff --git a/nano/node/CMakeLists.txt b/nano/node/CMakeLists.txt index f3283f22..3e3dd1dd 100644 --- a/nano/node/CMakeLists.txt +++ b/nano/node/CMakeLists.txt @@ -166,11 +166,13 @@ add_library( unchecked_map.hpp vote_cache.hpp vote_cache.cpp + vote_generator.hpp + vote_generator.cpp vote_processor.hpp vote_processor.cpp + vote_spacing.hpp + vote_spacing.cpp vote_with_weight_info.hpp - voting.hpp - voting.cpp wallet.hpp wallet.cpp websocket.hpp diff --git a/nano/node/active_transactions.hpp b/nano/node/active_transactions.hpp index 0ef5dcbd..da475cfd 100644 --- a/nano/node/active_transactions.hpp +++ b/nano/node/active_transactions.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -17,6 +16,7 @@ #include #include #include +#include #include namespace mi = boost::multi_index; @@ -32,7 +32,14 @@ class election; class vote; class confirmation_height_processor; class stats; +} +namespace nano::store +{ +class read_transaction; +} +namespace nano +{ class recently_confirmed_cache final { public: diff --git a/nano/node/backlog_population.cpp b/nano/node/backlog_population.cpp index 6b2a4515..f459b506 100644 --- a/nano/node/backlog_population.cpp +++ b/nano/node/backlog_population.cpp @@ -2,7 +2,9 @@ #include #include #include +#include #include +#include nano::backlog_population::backlog_population (const config & config_a, nano::store::component & store_a, nano::stats & stats_a) : config_m{ config_a }, diff --git a/nano/node/election.cpp b/nano/node/election.cpp index 3349d016..865593ab 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 6a3dbd8a..57e52b3d 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -4,8 +4,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -177,8 +178,10 @@ nano::node::node (std::shared_ptr io_ctx_a, std::filesy vote_uniquer{}, confirmation_height_processor (ledger, write_database_queue, config.conf_height_processor_batch_min_time, logger, node_initialized_latch, flags.confirmation_height_processor_mode), vote_cache{ config.vote_cache, stats }, - generator{ config, *this, ledger, wallets, vote_processor, history, network, stats, logger, /* non-final */ false }, - final_generator{ config, *this, ledger, wallets, vote_processor, history, network, stats, logger, /* final */ true }, + generator_impl{ std::make_unique (config, *this, ledger, wallets, vote_processor, history, network, stats, logger, /* non-final */ false) }, + generator{ *generator_impl }, + final_generator_impl{ std::make_unique (config, *this, ledger, wallets, vote_processor, history, network, stats, logger, /* final */ true) }, + final_generator{ *final_generator_impl }, active{ *this, confirmation_height_processor, block_processor }, scheduler_impl{ std::make_unique (*this) }, scheduler{ *scheduler_impl }, @@ -547,8 +550,8 @@ std::unique_ptr nano::collect_container_info (no composite->add_component (collect_container_info (node.aggregator, "request_aggregator")); composite->add_component (node.scheduler.collect_container_info ("election_scheduler")); composite->add_component (node.vote_cache.collect_container_info ("vote_cache")); - composite->add_component (collect_container_info (node.generator, "vote_generator")); - composite->add_component (collect_container_info (node.final_generator, "vote_generator_final")); + composite->add_component (node.generator.collect_container_info ("vote_generator")); + composite->add_component (node.final_generator.collect_container_info ("vote_generator_final")); composite->add_component (node.ascendboot.collect_container_info ("bootstrap_ascending")); composite->add_component (node.unchecked.collect_container_info ("unchecked")); composite->add_component (node.local_block_broadcaster.collect_container_info ("local_block_broadcaster")); diff --git a/nano/node/node.hpp b/nano/node/node.hpp index 5f62ba54..6d807463 100644 --- a/nano/node/node.hpp +++ b/nano/node/node.hpp @@ -172,8 +172,10 @@ public: nano::vote_uniquer vote_uniquer; nano::confirmation_height_processor confirmation_height_processor; nano::vote_cache vote_cache; - nano::vote_generator generator; - nano::vote_generator final_generator; + std::unique_ptr generator_impl; + nano::vote_generator & generator; + std::unique_ptr final_generator_impl; + nano::vote_generator & final_generator; nano::active_transactions active; private: // Placed here to maintain initialization order diff --git a/nano/node/node_observers.hpp b/nano/node/node_observers.hpp index 083be5c3..d1b074f2 100644 --- a/nano/node/node_observers.hpp +++ b/nano/node/node_observers.hpp @@ -9,6 +9,14 @@ namespace nano { class election_status; class telemetry; +} +namespace nano::transport +{ +class channel; +} + +namespace nano +{ class node_observers final { public: diff --git a/nano/node/request_aggregator.cpp b/nano/node/request_aggregator.cpp index 62d37146..1c399bc9 100644 --- a/nano/node/request_aggregator.cpp +++ b/nano/node/request_aggregator.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/nano/node/scheduler/priority.hpp b/nano/node/scheduler/priority.hpp index e232b3d9..d8a7b2da 100644 --- a/nano/node/scheduler/priority.hpp +++ b/nano/node/scheduler/priority.hpp @@ -17,6 +17,10 @@ class block; class container_info_component; class node; } +namespace nano::store +{ +class transaction; +} namespace nano::scheduler { diff --git a/nano/node/voting.cpp b/nano/node/vote_generator.cpp similarity index 84% rename from nano/node/voting.cpp rename to nano/node/vote_generator.cpp index 5101ca51..addc7219 100644 --- a/nano/node/voting.cpp +++ b/nano/node/vote_generator.cpp @@ -5,52 +5,15 @@ #include #include #include +#include #include -#include +#include #include #include #include #include -void nano::vote_spacing::trim () -{ - recent.get ().erase (recent.get ().begin (), recent.get ().upper_bound (std::chrono::steady_clock::now () - delay)); -} - -bool nano::vote_spacing::votable (nano::root const & root_a, nano::block_hash const & hash_a) const -{ - bool result = true; - for (auto range = recent.get ().equal_range (root_a); result && range.first != range.second; ++range.first) - { - auto & item = *range.first; - result = hash_a == item.hash || item.time < std::chrono::steady_clock::now () - delay; - } - return result; -} - -void nano::vote_spacing::flag (nano::root const & root_a, nano::block_hash const & hash_a) -{ - trim (); - auto now = std::chrono::steady_clock::now (); - auto existing = recent.get ().find (root_a); - if (existing != recent.end ()) - { - recent.get ().modify (existing, [now] (entry & entry) { - entry.time = now; - }); - } - else - { - recent.insert ({ root_a, now, hash_a }); - } -} - -std::size_t nano::vote_spacing::size () const -{ - return recent.size (); -} - nano::vote_generator::vote_generator (nano::node_config const & config_a, nano::node & node_a, nano::ledger & ledger_a, nano::wallets & wallets_a, nano::vote_processor & vote_processor_a, nano::local_vote_history & history_a, nano::network & network_a, nano::stats & stats_a, nano::logger & logger_a, bool is_final_a) : config (config_a), node (node_a), @@ -58,7 +21,8 @@ nano::vote_generator::vote_generator (nano::node_config const & config_a, nano:: wallets (wallets_a), vote_processor (vote_processor_a), history (history_a), - spacing{ config_a.network_params.voting.delay }, + spacing_impl{ std::make_unique (config_a.network_params.voting.delay) }, + spacing{ *spacing_impl }, network (network_a), stats (stats_a), logger (logger_a), @@ -318,20 +282,20 @@ void nano::vote_generator::run () } } -std::unique_ptr nano::collect_container_info (nano::vote_generator & vote_generator, std::string const & name) +std::unique_ptr nano::vote_generator::collect_container_info (std::string const & name) const { std::size_t candidates_count = 0; std::size_t requests_count = 0; { - nano::lock_guard guard{ vote_generator.mutex }; - candidates_count = vote_generator.candidates.size (); - requests_count = vote_generator.requests.size (); + nano::lock_guard guard{ mutex }; + candidates_count = candidates.size (); + requests_count = requests.size (); } - auto sizeof_candidate_element = sizeof (decltype (vote_generator.candidates)::value_type); - auto sizeof_request_element = sizeof (decltype (vote_generator.requests)::value_type); + auto sizeof_candidate_element = sizeof (decltype (candidates)::value_type); + auto sizeof_request_element = sizeof (decltype (requests)::value_type); auto composite = std::make_unique (name); composite->add_component (std::make_unique (container_info{ "candidates", candidates_count, sizeof_candidate_element })); composite->add_component (std::make_unique (container_info{ "requests", requests_count, sizeof_request_element })); - composite->add_component (vote_generator.vote_generation_queue.collect_container_info ("vote_generation_queue")); + composite->add_component (vote_generation_queue.collect_container_info ("vote_generation_queue")); return composite; } diff --git a/nano/node/voting.hpp b/nano/node/vote_generator.hpp similarity index 75% rename from nano/node/voting.hpp rename to nano/node/vote_generator.hpp index 0ed940e3..4fc3d65e 100644 --- a/nano/node/voting.hpp +++ b/nano/node/vote_generator.hpp @@ -29,42 +29,16 @@ class node; class node_config; class stats; class vote_processor; +class vote_spacing; class wallets; -namespace transport +} +namespace nano::transport { - class channel; +class channel; } -class vote_spacing final +namespace nano { - class entry - { - public: - nano::root root; - std::chrono::steady_clock::time_point time; - nano::block_hash hash; - }; - - boost::multi_index_container, - mi::member>, - mi::ordered_non_unique, - mi::member>>> - recent; - std::chrono::milliseconds const delay; - void trim (); - -public: - vote_spacing (std::chrono::milliseconds const & delay) : - delay{ delay } - { - } - bool votable (nano::root const & root_a, nano::block_hash const & hash_a) const; - void flag (nano::root const & root_a, nano::block_hash const & hash_a); - std::size_t size () const; -}; - class vote_generator final { private: @@ -85,6 +59,8 @@ public: void start (); void stop (); + std::unique_ptr collect_container_info (std::string const & name) const; + private: void run (); void broadcast (nano::unique_lock &); @@ -109,7 +85,8 @@ private: // Dependencies nano::wallets & wallets; nano::vote_processor & vote_processor; nano::local_vote_history & history; - nano::vote_spacing spacing; + std::unique_ptr spacing_impl; + nano::vote_spacing & spacing; nano::network & network; nano::stats & stats; nano::logger & logger; @@ -126,9 +103,5 @@ private: std::deque candidates; std::atomic stopped{ false }; std::thread thread; - - friend std::unique_ptr collect_container_info (vote_generator & vote_generator, std::string const & name); }; - -std::unique_ptr collect_container_info (vote_generator & generator, std::string const & name); } diff --git a/nano/node/vote_spacing.cpp b/nano/node/vote_spacing.cpp new file mode 100644 index 00000000..201a9d7b --- /dev/null +++ b/nano/node/vote_spacing.cpp @@ -0,0 +1,39 @@ +#include + +void nano::vote_spacing::trim () +{ + recent.get ().erase (recent.get ().begin (), recent.get ().upper_bound (std::chrono::steady_clock::now () - delay)); +} + +bool nano::vote_spacing::votable (nano::root const & root_a, nano::block_hash const & hash_a) const +{ + bool result = true; + for (auto range = recent.get ().equal_range (root_a); result && range.first != range.second; ++range.first) + { + auto & item = *range.first; + result = hash_a == item.hash || item.time < std::chrono::steady_clock::now () - delay; + } + return result; +} + +void nano::vote_spacing::flag (nano::root const & root_a, nano::block_hash const & hash_a) +{ + trim (); + auto now = std::chrono::steady_clock::now (); + auto existing = recent.get ().find (root_a); + if (existing != recent.end ()) + { + recent.get ().modify (existing, [now] (entry & entry) { + entry.time = now; + }); + } + else + { + recent.insert ({ root_a, now, hash_a }); + } +} + +std::size_t nano::vote_spacing::size () const +{ + return recent.size (); +} diff --git a/nano/node/vote_spacing.hpp b/nano/node/vote_spacing.hpp new file mode 100644 index 00000000..f46cd635 --- /dev/null +++ b/nano/node/vote_spacing.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include + +#include +#include +#include +#include + +#include + +namespace mi = boost::multi_index; + +namespace nano +{ +class vote_spacing final +{ + class entry + { + public: + nano::root root; + std::chrono::steady_clock::time_point time; + nano::block_hash hash; + }; + + boost::multi_index_container, + mi::member>, + mi::ordered_non_unique, + mi::member>>> + recent; + std::chrono::milliseconds const delay; + void trim (); + +public: + vote_spacing (std::chrono::milliseconds const & delay) : + delay{ delay } + { + } + bool votable (nano::root const & root_a, nano::block_hash const & hash_a) const; + void flag (nano::root const & root_a, nano::block_hash const & hash_a); + std::size_t size () const; +}; +} diff --git a/nano/test_common/ledger.hpp b/nano/test_common/ledger.hpp index 6c81f46b..e968f4d1 100644 --- a/nano/test_common/ledger.hpp +++ b/nano/test_common/ledger.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include namespace nano