diff --git a/nano/core_test/block_store.cpp b/nano/core_test/block_store.cpp index fd9a99e7d2..8584b3bdfa 100644 --- a/nano/core_test/block_store.cpp +++ b/nano/core_test/block_store.cpp @@ -5,13 +5,13 @@ #include #include #include -#include #include #include #include #include -#include -#include +#include +#include +#include #include #include #include diff --git a/nano/core_test/bootstrap_ascending.cpp b/nano/core_test/bootstrap_ascending.cpp index fd3850ccee..c1577ac1a9 100644 --- a/nano/core_test/bootstrap_ascending.cpp +++ b/nano/core_test/bootstrap_ascending.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include diff --git a/nano/core_test/confirmation_height.cpp b/nano/core_test/confirmation_height.cpp index 7a014447ef..b4e1dec8e3 100644 --- a/nano/core_test/confirmation_height.cpp +++ b/nano/core_test/confirmation_height.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include @@ -1166,7 +1166,7 @@ TEST (confirmation_heightDeathTest, rollback_added_block) ASSERT_TRUE (!store->init_error ()); nano::stats stats; nano::ledger ledger (*store, stats, nano::dev::constants); - nano::write_database_queue write_database_queue (false); + nano::store::write_database_queue write_database_queue (false); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::keypair key1; nano::block_builder builder; @@ -1258,7 +1258,7 @@ TEST (confirmation_heightDeathTest, modified_chain) ASSERT_TRUE (!store->init_error ()); nano::stats stats; nano::ledger ledger (*store, stats, nano::dev::constants); - nano::write_database_queue write_database_queue (false); + nano::store::write_database_queue write_database_queue (false); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::keypair key1; nano::block_builder builder; @@ -1283,14 +1283,14 @@ TEST (confirmation_heightDeathTest, modified_chain) { // This reads the blocks in the account, but prevents any writes from occuring yet - auto scoped_write_guard = write_database_queue.wait (nano::writer::testing); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::testing); bounded_processor.process (send); } // Rollback the block and now try to write, the block no longer exists so should bail ledger.rollback (store->tx_begin_write (), send->hash ()); { - auto scoped_write_guard = write_database_queue.wait (nano::writer::confirmation_height); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::confirmation_height); ASSERT_DEATH_IF_SUPPORTED (bounded_processor.cement_blocks (scoped_write_guard), ""); } @@ -1302,14 +1302,14 @@ TEST (confirmation_heightDeathTest, modified_chain) { // This reads the blocks in the account, but prevents any writes from occuring yet - auto scoped_write_guard = write_database_queue.wait (nano::writer::testing); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::testing); unbounded_processor.process (send); } // Rollback the block and now try to write, the block no longer exists so should bail ledger.rollback (store->tx_begin_write (), send->hash ()); { - auto scoped_write_guard = write_database_queue.wait (nano::writer::confirmation_height); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::confirmation_height); ASSERT_DEATH_IF_SUPPORTED (unbounded_processor.cement_blocks (scoped_write_guard), ""); } } @@ -1336,7 +1336,7 @@ TEST (confirmation_heightDeathTest, modified_chain_account_removed) ASSERT_TRUE (!store->init_error ()); nano::stats stats; nano::ledger ledger (*store, stats, nano::dev::constants); - nano::write_database_queue write_database_queue (false); + nano::store::write_database_queue write_database_queue (false); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::keypair key1; nano::block_builder builder; @@ -1372,14 +1372,14 @@ TEST (confirmation_heightDeathTest, modified_chain_account_removed) { // This reads the blocks in the account, but prevents any writes from occuring yet - auto scoped_write_guard = write_database_queue.wait (nano::writer::testing); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::testing); unbounded_processor.process (open); } // Rollback the block and now try to write, the send should be cemented but the account which the open block belongs no longer exists so should bail ledger.rollback (store->tx_begin_write (), open->hash ()); { - auto scoped_write_guard = write_database_queue.wait (nano::writer::confirmation_height); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::confirmation_height); ASSERT_DEATH_IF_SUPPORTED (unbounded_processor.cement_blocks (scoped_write_guard), ""); } @@ -1392,13 +1392,13 @@ TEST (confirmation_heightDeathTest, modified_chain_account_removed) { // This reads the blocks in the account, but prevents any writes from occuring yet - auto scoped_write_guard = write_database_queue.wait (nano::writer::testing); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::testing); bounded_processor.process (open); } // Rollback the block and now try to write, the send should be cemented but the account which the open block belongs no longer exists so should bail ledger.rollback (store->tx_begin_write (), open->hash ()); - auto scoped_write_guard = write_database_queue.wait (nano::writer::confirmation_height); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::confirmation_height); ASSERT_DEATH_IF_SUPPORTED (bounded_processor.cement_blocks (scoped_write_guard), ""); } } @@ -1508,7 +1508,7 @@ TEST (confirmation_height, callback_confirmed_history) node->block_processor.flush (); // The write guard prevents the confirmation height processor doing any writes - auto write_guard = node->write_database_queue.wait (nano::writer::testing); + auto write_guard = node->write_database_queue.wait (nano::store::writer::testing); // Confirm send1 auto election = node->active.election (send1->qualified_root ()); @@ -1521,13 +1521,13 @@ TEST (confirmation_height, callback_confirmed_history) auto transaction = node->store.tx_begin_read (); ASSERT_FALSE (node->ledger.block_confirmed (transaction, send->hash ())); - ASSERT_TIMELY (10s, node->write_database_queue.contains (nano::writer::confirmation_height)); + ASSERT_TIMELY (10s, node->write_database_queue.contains (nano::store::writer::confirmation_height)); // Confirm that no inactive callbacks have been called when the confirmation height processor has already iterated over it, waiting to write ASSERT_EQ (0, node->stats.count (nano::stat::type::confirmation_observer, nano::stat::detail::inactive_conf_height, nano::stat::dir::out)); } - ASSERT_TIMELY (10s, !node->write_database_queue.contains (nano::writer::confirmation_height)); + ASSERT_TIMELY (10s, !node->write_database_queue.contains (nano::store::writer::confirmation_height)); auto transaction = node->store.tx_begin_read (); ASSERT_TRUE (node->ledger.block_confirmed (transaction, send->hash ())); @@ -2047,7 +2047,7 @@ TEST (confirmation_height, unbounded_block_cache_iteration) ASSERT_TRUE (!store->init_error ()); nano::stats stats; nano::ledger ledger (*store, stats, nano::dev::constants); - nano::write_database_queue write_database_queue (false); + nano::store::write_database_queue write_database_queue (false); boost::latch initialized_latch{ 0 }; nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::logging logging; @@ -2081,7 +2081,7 @@ TEST (confirmation_height, unbounded_block_cache_iteration) timer.start (); { // Prevent conf height processor doing any writes, so that we can query is_processing_block correctly - auto write_guard = write_database_queue.wait (nano::writer::testing); + auto write_guard = write_database_queue.wait (nano::store::writer::testing); // Add the frontier block confirmation_height_processor.add (send1); @@ -2113,7 +2113,7 @@ TEST (confirmation_height, pruned_source) nano::stats stats; nano::ledger ledger (*store, stats, nano::dev::constants); ledger.pruning = true; - nano::write_database_queue write_database_queue (false); + nano::store::write_database_queue write_database_queue (false); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::keypair key1, key2; nano::block_builder builder; diff --git a/nano/core_test/ledger.cpp b/nano/core_test/ledger.cpp index db323513d5..a3f25bb22d 100644 --- a/nano/core_test/ledger.cpp +++ b/nano/core_test/ledger.cpp @@ -1,11 +1,11 @@ #include #include #include -#include #include #include #include -#include +#include +#include #include #include #include diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index 0213b57880..7092911073 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -1,11 +1,11 @@ #include #include -#include #include #include #include #include #include +#include #include #include #include @@ -3102,7 +3102,7 @@ TEST (node, block_processor_half_full) .work (*node.work_generate_blocking (send2->hash ())) .build_shared (); // The write guard prevents block processor doing any writes - auto write_guard = node.write_database_queue.wait (nano::writer::testing); + auto write_guard = node.write_database_queue.wait (nano::store::writer::testing); node.block_processor.add (send1); ASSERT_FALSE (node.block_processor.half_full ()); node.block_processor.add (send2); @@ -3499,7 +3499,7 @@ TEST (node, rollback_vote_self) { // The write guard prevents the block processor from performing the rollback - auto write_guard = node.write_database_queue.wait (nano::writer::testing); + auto write_guard = node.write_database_queue.wait (nano::store::writer::testing); ASSERT_EQ (0, election->votes_with_weight ().size ()); // Vote with key to switch the winner diff --git a/nano/core_test/processor_service.cpp b/nano/core_test/processor_service.cpp index 42893a97d0..ba4c73c4fd 100644 --- a/nano/core_test/processor_service.cpp +++ b/nano/core_test/processor_service.cpp @@ -1,9 +1,9 @@ #include #include -#include #include #include #include +#include #include #include diff --git a/nano/core_test/request_aggregator.cpp b/nano/core_test/request_aggregator.cpp index 344b6316b5..b487e0a3e6 100644 --- a/nano/core_test/request_aggregator.cpp +++ b/nano/core_test/request_aggregator.cpp @@ -1,6 +1,6 @@ #include -#include #include +#include #include #include #include diff --git a/nano/core_test/vote_cache.cpp b/nano/core_test/vote_cache.cpp index f94dda0679..c51a299696 100644 --- a/nano/core_test/vote_cache.cpp +++ b/nano/core_test/vote_cache.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include @@ -36,8 +36,8 @@ nano::keypair create_rep (nano::uint128_t weight) TEST (vote_cache, construction) { - nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::voting::cache_config cfg; + nano::voting::cache vote_cache{ cfg }; ASSERT_EQ (0, vote_cache.size ()); ASSERT_TRUE (vote_cache.empty ()); auto hash1 = nano::test::random_hash (); @@ -49,8 +49,8 @@ TEST (vote_cache, construction) */ TEST (vote_cache, insert_one_hash) { - nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::voting::cache_config cfg; + nano::voting::cache vote_cache{ cfg }; vote_cache.rep_weight_query = rep_weight_query (); auto rep1 = create_rep (7); auto hash1 = nano::test::random_hash (); @@ -79,8 +79,8 @@ TEST (vote_cache, insert_one_hash) */ TEST (vote_cache, insert_one_hash_many_votes) { - nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::voting::cache_config cfg; + nano::voting::cache vote_cache{ cfg }; vote_cache.rep_weight_query = rep_weight_query (); auto hash1 = nano::test::random_hash (); auto rep1 = create_rep (7); @@ -114,8 +114,8 @@ TEST (vote_cache, insert_one_hash_many_votes) */ TEST (vote_cache, insert_many_hashes_many_votes) { - nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::voting::cache_config cfg; + nano::voting::cache vote_cache{ cfg }; vote_cache.rep_weight_query = rep_weight_query (); // There will be 3 random hashes to vote for auto hash1 = nano::test::random_hash (); @@ -194,8 +194,8 @@ TEST (vote_cache, insert_many_hashes_many_votes) */ TEST (vote_cache, insert_duplicate) { - nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::voting::cache_config cfg; + nano::voting::cache vote_cache{ cfg }; vote_cache.rep_weight_query = rep_weight_query (); auto hash1 = nano::test::random_hash (); auto rep1 = create_rep (9); @@ -211,8 +211,8 @@ TEST (vote_cache, insert_duplicate) */ TEST (vote_cache, insert_newer) { - nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::voting::cache_config cfg; + nano::voting::cache vote_cache{ cfg }; vote_cache.rep_weight_query = rep_weight_query (); auto hash1 = nano::test::random_hash (); auto rep1 = create_rep (9); @@ -236,8 +236,8 @@ TEST (vote_cache, insert_newer) */ TEST (vote_cache, insert_older) { - nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::voting::cache_config cfg; + nano::voting::cache vote_cache{ cfg }; vote_cache.rep_weight_query = rep_weight_query (); auto hash1 = nano::test::random_hash (); auto rep1 = create_rep (9); @@ -259,8 +259,8 @@ TEST (vote_cache, insert_older) */ TEST (vote_cache, erase) { - nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::voting::cache_config cfg; + nano::voting::cache vote_cache{ cfg }; vote_cache.rep_weight_query = rep_weight_query (); auto hash1 = nano::test::random_hash (); auto hash2 = nano::test::random_hash (); @@ -299,9 +299,9 @@ TEST (vote_cache, erase) TEST (vote_cache, overfill) { // Create a vote cache with max size set to 1024 - nano::vote_cache_config cfg; + nano::voting::cache_config cfg; cfg.max_size = 1024; - nano::vote_cache vote_cache{ cfg }; + nano::voting::cache vote_cache{ cfg }; vote_cache.rep_weight_query = rep_weight_query (); const int count = 16 * 1024; for (int n = 0; n < count; ++n) @@ -324,8 +324,8 @@ TEST (vote_cache, overfill) */ TEST (vote_cache, overfill_entry) { - nano::vote_cache_config cfg; - nano::vote_cache vote_cache{ cfg }; + nano::voting::cache_config cfg; + nano::voting::cache vote_cache{ cfg }; vote_cache.rep_weight_query = rep_weight_query (); const int count = 1024; auto hash1 = nano::test::random_hash (); @@ -336,4 +336,4 @@ TEST (vote_cache, overfill_entry) vote_cache.vote (vote1->hashes.front (), vote1); } ASSERT_EQ (1, vote_cache.size ()); -} \ No newline at end of file +} diff --git a/nano/core_test/vote_processor.cpp b/nano/core_test/vote_processor.cpp index a444719b01..01b8aa9346 100644 --- a/nano/core_test/vote_processor.cpp +++ b/nano/core_test/vote_processor.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include diff --git a/nano/core_test/voting.cpp b/nano/core_test/voting.cpp index 4bc7e2a4df..1b8dc6c043 100644 --- a/nano/core_test/voting.cpp +++ b/nano/core_test/voting.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include @@ -11,7 +11,7 @@ namespace nano { TEST (local_vote_history, basic) { - nano::local_vote_history history{ nano::dev::network_params.voting }; + nano::voting::history history{ nano::dev::network_params.voting }; ASSERT_FALSE (history.exists (1)); ASSERT_FALSE (history.exists (2)); ASSERT_TRUE (history.votes (1).empty ()); @@ -105,7 +105,7 @@ TEST (vote_generator, multiple_representatives) TEST (vote_spacing, basic) { - nano::vote_spacing spacing{ std::chrono::milliseconds{ 100 } }; + nano::voting::spacing spacing{ std::chrono::milliseconds{ 100 } }; nano::root root1{ 1 }; nano::root root2{ 2 }; nano::block_hash hash3{ 3 }; @@ -124,7 +124,7 @@ TEST (vote_spacing, basic) TEST (vote_spacing, prune) { auto length = std::chrono::milliseconds{ 100 }; - nano::vote_spacing spacing{ length }; + nano::voting::spacing spacing{ length }; nano::root root1{ 1 }; nano::root root2{ 2 }; nano::block_hash hash3{ 3 }; diff --git a/nano/node/CMakeLists.txt b/nano/node/CMakeLists.txt index 769d6809cd..35fa8f5522 100644 --- a/nano/node/CMakeLists.txt +++ b/nano/node/CMakeLists.txt @@ -114,8 +114,6 @@ add_library( ledger_walker.cpp logging.hpp logging.cpp - make_store.hpp - make_store.cpp network.hpp network.cpp nodeconfig.hpp @@ -140,8 +138,6 @@ add_library( process_live_dispatcher.hpp repcrawler.hpp repcrawler.cpp - request_aggregator.hpp - request_aggregator.cpp scheduler/bucket.cpp scheduler/bucket.hpp scheduler/buckets.cpp @@ -180,12 +176,18 @@ add_library( transport/transport.cpp unchecked_map.cpp unchecked_map.hpp - vote_cache.hpp - vote_cache.cpp - vote_processor.hpp - vote_processor.cpp - voting.hpp - voting.cpp + voting/aggregator.hpp + voting/aggregator.cpp + voting/cache.hpp + voting/cache.cpp + voting/history.hpp + voting/history.cpp + voting/generator.hpp + voting/generator.cpp + voting/processor.hpp + voting/processor.cpp + voting/spacing.hpp + voting/spacing.cpp wallet.hpp wallet.cpp websocket.hpp @@ -194,8 +196,6 @@ add_library( websocketconfig.cpp websocket_stream.hpp websocket_stream.cpp - write_database_queue.hpp - write_database_queue.cpp messages.hpp messages.cpp xorshift.hpp) diff --git a/nano/node/active_transactions.hpp b/nano/node/active_transactions.hpp index 36acd179e5..d04dfb2407 100644 --- a/nano/node/active_transactions.hpp +++ b/nano/node/active_transactions.hpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include @@ -53,6 +53,7 @@ class recently_confirmed_cache final // clang-format off class tag_root {}; class tag_hash {}; + class tag_sequence {}; using ordered_recent_confirmations = boost::multi_index_container -nano::block_processor::block_processor (nano::node & node_a, nano::write_database_queue & write_database_queue_a) : +nano::block_processor::block_processor (nano::node & node_a, nano::store::write_database_queue & write_database_queue_a) : next_log (std::chrono::steady_clock::now ()), node (node_a), write_database_queue (write_database_queue_a), @@ -262,7 +262,7 @@ void nano::block_processor::add_impl (std::shared_ptr block) auto nano::block_processor::process_batch (nano::unique_lock & lock_a) -> std::deque { std::deque processed; - auto scoped_write_guard = write_database_queue.wait (nano::writer::process_batch); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::process_batch); auto transaction (node.store.tx_begin_write ({ tables::accounts, tables::blocks, tables::frontiers, tables::pending })); nano::timer timer_l; lock_a.lock (); diff --git a/nano/node/blockprocessor.hpp b/nano/node/blockprocessor.hpp index 465bc5f6fd..93f3569b8f 100644 --- a/nano/node/blockprocessor.hpp +++ b/nano/node/blockprocessor.hpp @@ -12,13 +12,13 @@ namespace nano::store { +class write_database_queue; class write_transaction; } namespace nano { class node; -class write_database_queue; /** * Processing blocks is a potentially long IO operation. @@ -27,7 +27,7 @@ class write_database_queue; class block_processor final { public: - explicit block_processor (nano::node &, nano::write_database_queue &); + explicit block_processor (nano::node &, nano::store::write_database_queue &); void stop (); void flush (); std::size_t size (); @@ -68,7 +68,7 @@ class block_processor final std::deque> forced; nano::condition_variable condition; nano::node & node; - nano::write_database_queue & write_database_queue; + nano::store::write_database_queue & write_database_queue; nano::mutex mutex{ mutex_identifier (mutexes::block_processor) }; nano::state_block_signature_verification state_block_signature_verification; std::thread processing_thread; diff --git a/nano/node/confirmation_height_bounded.cpp b/nano/node/confirmation_height_bounded.cpp index a2301f891d..ed2bf51603 100644 --- a/nano/node/confirmation_height_bounded.cpp +++ b/nano/node/confirmation_height_bounded.cpp @@ -2,17 +2,17 @@ #include #include #include -#include #include #include #include #include +#include #include #include -nano::confirmation_height_bounded::confirmation_height_bounded (nano::ledger & ledger_a, nano::write_database_queue & write_database_queue_a, std::chrono::milliseconds batch_separate_pending_min_time_a, nano::logging const & logging_a, nano::logger_mt & logger_a, std::atomic & stopped_a, uint64_t & batch_write_size_a, std::function> const &)> const & notify_observers_callback_a, std::function const & notify_block_already_cemented_observers_callback_a, std::function const & awaiting_processing_size_callback_a) : +nano::confirmation_height_bounded::confirmation_height_bounded (nano::ledger & ledger_a, nano::store::write_database_queue & write_database_queue_a, std::chrono::milliseconds batch_separate_pending_min_time_a, nano::logging const & logging_a, nano::logger_mt & logger_a, std::atomic & stopped_a, uint64_t & batch_write_size_a, std::function> const &)> const & notify_observers_callback_a, std::function const & notify_block_already_cemented_observers_callback_a, std::function const & awaiting_processing_size_callback_a) : ledger (ledger_a), write_database_queue (write_database_queue_a), batch_separate_pending_min_time (batch_separate_pending_min_time_a), @@ -204,14 +204,14 @@ void nano::confirmation_height_bounded::process (std::shared_ptr or if ((max_batch_write_size_reached || should_output || force_write) && !pending_writes.empty ()) { // If nothing is currently using the database write lock then write the cemented pending blocks otherwise continue iterating - if (write_database_queue.process (nano::writer::confirmation_height)) + if (write_database_queue.process (nano::store::writer::confirmation_height)) { auto scoped_write_guard = write_database_queue.pop (); cement_blocks (scoped_write_guard); } else if (force_write) { - auto scoped_write_guard = write_database_queue.wait (nano::writer::confirmation_height); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::confirmation_height); cement_blocks (scoped_write_guard); } } @@ -361,7 +361,7 @@ void nano::confirmation_height_bounded::prepare_iterated_blocks_for_cementing (p } } -void nano::confirmation_height_bounded::cement_blocks (nano::write_guard & scoped_write_guard_a) +void nano::confirmation_height_bounded::cement_blocks (nano::store::write_guard & scoped_write_guard_a) { // Will contain all blocks that have been cemented (bounded by batch_write_size) // and will get run through the cemented observer callback @@ -479,7 +479,7 @@ void nano::confirmation_height_bounded::cement_blocks (nano::write_guard & scope // Only aquire transaction if there are blocks left if (!(last_iteration && pending_writes.size () == 1)) { - scoped_write_guard_a = write_database_queue.wait (nano::writer::confirmation_height); + scoped_write_guard_a = write_database_queue.wait (nano::store::writer::confirmation_height); transaction.renew (); } cemented_batch_timer.restart (); diff --git a/nano/node/confirmation_height_bounded.hpp b/nano/node/confirmation_height_bounded.hpp index c176b41686..362c71e33e 100644 --- a/nano/node/confirmation_height_bounded.hpp +++ b/nano/node/confirmation_height_bounded.hpp @@ -8,22 +8,25 @@ #include +namespace nano::store +{ +class write_database_queue; +class write_guard; +} namespace nano { class ledger; class logging; class logger_mt; -class write_database_queue; -class write_guard; class confirmation_height_bounded final { public: - confirmation_height_bounded (nano::ledger &, nano::write_database_queue &, std::chrono::milliseconds batch_separate_pending_min_time, nano::logging const &, nano::logger_mt &, std::atomic & stopped, uint64_t & batch_write_size, std::function> const &)> const & cemented_callback, std::function const & already_cemented_callback, std::function const & awaiting_processing_size_query); + confirmation_height_bounded (nano::ledger &, nano::store::write_database_queue &, std::chrono::milliseconds batch_separate_pending_min_time, nano::logging const &, nano::logger_mt &, std::atomic & stopped, uint64_t & batch_write_size, std::function> const &)> const & cemented_callback, std::function const & already_cemented_callback, std::function const & awaiting_processing_size_query); bool pending_empty () const; void clear_process_vars (); void process (std::shared_ptr original_block); - void cement_blocks (nano::write_guard & scoped_write_guard_a); + void cement_blocks (nano::store::write_guard & scoped_write_guard_a); private: class top_and_next_hash final @@ -119,7 +122,7 @@ class confirmation_height_bounded final bool iterate (store::read_transaction const &, uint64_t, nano::block_hash const &, boost::circular_buffer_space_optimized &, nano::block_hash &, nano::block_hash const &, boost::circular_buffer_space_optimized &, nano::account const &); nano::ledger & ledger; - nano::write_database_queue & write_database_queue; + nano::store::write_database_queue & write_database_queue; std::chrono::milliseconds batch_separate_pending_min_time; nano::logging const & logging; nano::logger_mt & logger; diff --git a/nano/node/confirmation_height_processor.cpp b/nano/node/confirmation_height_processor.cpp index 6cf9772999..5243a9bb70 100644 --- a/nano/node/confirmation_height_processor.cpp +++ b/nano/node/confirmation_height_processor.cpp @@ -3,13 +3,13 @@ #include #include #include -#include #include #include +#include #include -nano::confirmation_height_processor::confirmation_height_processor (nano::ledger & ledger_a, nano::write_database_queue & write_database_queue_a, std::chrono::milliseconds batch_separate_pending_min_time_a, nano::logging const & logging_a, nano::logger_mt & logger_a, boost::latch & latch, confirmation_height_mode mode_a) : +nano::confirmation_height_processor::confirmation_height_processor (nano::ledger & ledger_a, nano::store::write_database_queue & write_database_queue_a, std::chrono::milliseconds batch_separate_pending_min_time_a, nano::logging const & logging_a, nano::logger_mt & logger_a, boost::latch & latch, confirmation_height_mode mode_a) : ledger (ledger_a), write_database_queue (write_database_queue_a), unbounded_processor ( @@ -105,7 +105,7 @@ void nano::confirmation_height_processor::run (confirmation_height_mode mode_a) { debug_assert (unbounded_processor.pending_empty ()); { - auto scoped_write_guard = write_database_queue.wait (nano::writer::confirmation_height); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::confirmation_height); bounded_processor.cement_blocks (scoped_write_guard); } lock_and_cleanup (); @@ -114,7 +114,7 @@ void nano::confirmation_height_processor::run (confirmation_height_mode mode_a) { debug_assert (bounded_processor.pending_empty ()); { - auto scoped_write_guard = write_database_queue.wait (nano::writer::confirmation_height); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::confirmation_height); unbounded_processor.cement_blocks (scoped_write_guard); } lock_and_cleanup (); diff --git a/nano/node/confirmation_height_processor.hpp b/nano/node/confirmation_height_processor.hpp index 88f2d1a15b..45a9dec4c1 100644 --- a/nano/node/confirmation_height_processor.hpp +++ b/nano/node/confirmation_height_processor.hpp @@ -30,7 +30,7 @@ class write_database_queue; class confirmation_height_processor final { public: - confirmation_height_processor (nano::ledger &, nano::write_database_queue &, std::chrono::milliseconds, nano::logging const &, nano::logger_mt &, boost::latch & initialized_latch, confirmation_height_mode = confirmation_height_mode::automatic); + confirmation_height_processor (nano::ledger &, nano::store::write_database_queue &, std::chrono::milliseconds, nano::logging const &, nano::logger_mt &, boost::latch & initialized_latch, confirmation_height_mode = confirmation_height_mode::automatic); ~confirmation_height_processor (); void pause (); void unpause (); @@ -95,7 +95,7 @@ class confirmation_height_processor final std::vector> block_already_cemented_observers; nano::ledger & ledger; - nano::write_database_queue & write_database_queue; + nano::store::write_database_queue & write_database_queue; /** The maximum amount of blocks to write at once. This is dynamically modified by the bounded processor based on previous write performance **/ uint64_t batch_write_size{ 16384 }; diff --git a/nano/node/confirmation_height_unbounded.cpp b/nano/node/confirmation_height_unbounded.cpp index 9a458b9c8c..b3f587e622 100644 --- a/nano/node/confirmation_height_unbounded.cpp +++ b/nano/node/confirmation_height_unbounded.cpp @@ -2,17 +2,17 @@ #include #include #include -#include #include #include #include #include +#include #include #include -nano::confirmation_height_unbounded::confirmation_height_unbounded (nano::ledger & ledger_a, nano::write_database_queue & write_database_queue_a, std::chrono::milliseconds batch_separate_pending_min_time_a, nano::logging const & logging_a, nano::logger_mt & logger_a, std::atomic & stopped_a, uint64_t & batch_write_size_a, std::function> const &)> const & notify_observers_callback_a, std::function const & notify_block_already_cemented_observers_callback_a, std::function const & awaiting_processing_size_callback_a) : +nano::confirmation_height_unbounded::confirmation_height_unbounded (nano::ledger & ledger_a, nano::store::write_database_queue & write_database_queue_a, std::chrono::milliseconds batch_separate_pending_min_time_a, nano::logging const & logging_a, nano::logger_mt & logger_a, std::atomic & stopped_a, uint64_t & batch_write_size_a, std::function> const &)> const & notify_observers_callback_a, std::function const & notify_block_already_cemented_observers_callback_a, std::function const & awaiting_processing_size_callback_a) : ledger (ledger_a), write_database_queue (write_database_queue_a), batch_separate_pending_min_time (batch_separate_pending_min_time_a), @@ -175,7 +175,7 @@ void nano::confirmation_height_unbounded::process (std::shared_ptr if ((max_write_size_reached || should_output || force_write) && !pending_writes.empty ()) { - if (write_database_queue.process (nano::writer::confirmation_height)) + if (write_database_queue.process (nano::store::writer::confirmation_height)) { auto scoped_write_guard = write_database_queue.pop (); cement_blocks (scoped_write_guard); @@ -183,7 +183,7 @@ void nano::confirmation_height_unbounded::process (std::shared_ptr else if (force_write) { // Unbounded processor has grown too large, force a write - auto scoped_write_guard = write_database_queue.wait (nano::writer::confirmation_height); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::confirmation_height); cement_blocks (scoped_write_guard); } } @@ -365,7 +365,7 @@ void nano::confirmation_height_unbounded::prepare_iterated_blocks_for_cementing } } -void nano::confirmation_height_unbounded::cement_blocks (nano::write_guard & scoped_write_guard_a) +void nano::confirmation_height_unbounded::cement_blocks (nano::store::write_guard & scoped_write_guard_a) { nano::timer cemented_batch_timer; std::vector> cemented_blocks; diff --git a/nano/node/confirmation_height_unbounded.hpp b/nano/node/confirmation_height_unbounded.hpp index 0af353e69a..b3c63dcf5d 100644 --- a/nano/node/confirmation_height_unbounded.hpp +++ b/nano/node/confirmation_height_unbounded.hpp @@ -9,22 +9,26 @@ #include #include +namespace nano::store +{ +class write_database_queue; +class write_guard; +} + namespace nano { class ledger; class logging; class logger_mt; -class write_database_queue; -class write_guard; class confirmation_height_unbounded final { public: - confirmation_height_unbounded (nano::ledger &, nano::write_database_queue &, std::chrono::milliseconds batch_separate_pending_min_time, nano::logging const &, nano::logger_mt &, std::atomic & stopped, uint64_t & batch_write_size, std::function> const &)> const & cemented_callback, std::function const & already_cemented_callback, std::function const & awaiting_processing_size_query); + confirmation_height_unbounded (nano::ledger &, nano::store::write_database_queue &, std::chrono::milliseconds batch_separate_pending_min_time, nano::logging const &, nano::logger_mt &, std::atomic & stopped, uint64_t & batch_write_size, std::function> const &)> const & cemented_callback, std::function const & already_cemented_callback, std::function const & awaiting_processing_size_query); bool pending_empty () const; void clear_process_vars (); void process (std::shared_ptr original_block); - void cement_blocks (nano::write_guard &); + void cement_blocks (nano::store::write_guard &); bool has_iterated_over_block (nano::block_hash const &) const; private: @@ -96,7 +100,7 @@ class confirmation_height_unbounded final void prepare_iterated_blocks_for_cementing (preparation_data &); nano::ledger & ledger; - nano::write_database_queue & write_database_queue; + nano::store::write_database_queue & write_database_queue; std::chrono::milliseconds batch_separate_pending_min_time; nano::logger_mt & logger; std::atomic & stopped; diff --git a/nano/node/node.cpp b/nano/node/node.cpp index bb9d064e0a..346e9b50d5 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -14,7 +13,8 @@ #include #include #include -#include +#include +#include #include @@ -1077,7 +1077,7 @@ void nano::node::ledger_pruning (uint64_t const batch_size_a, bool bootstrap_wei transaction_write_count = 0; if (!pruning_targets.empty () && !stopped) { - auto scoped_write_guard = write_database_queue.wait (nano::writer::pruning); + auto scoped_write_guard = write_database_queue.wait (nano::store::writer::pruning); auto write_transaction (store.tx_begin_write ({ tables::blocks, tables::pruned })); while (!pruning_targets.empty () && transaction_write_count < batch_size_a && !stopped) { diff --git a/nano/node/node.hpp b/nano/node/node.hpp index 8f54915d5d..d5398ed507 100644 --- a/nano/node/node.hpp +++ b/nano/node/node.hpp @@ -28,18 +28,20 @@ #include #include #include -#include #include #include #include #include -#include -#include +#include +#include +#include +#include +#include #include #include -#include #include #include +#include #include #include @@ -140,7 +142,7 @@ class node final : public std::enable_shared_from_this nano::telemetry_data local_telemetry () const; public: - nano::write_database_queue write_database_queue; + nano::store::write_database_queue write_database_queue; boost::asio::io_context & io_ctx; boost::latch node_initialized_latch; nano::node_config config; @@ -171,18 +173,18 @@ class node final : public std::enable_shared_from_this nano::port_mapping port_mapping; nano::online_reps online_reps; nano::rep_crawler rep_crawler; - nano::vote_processor vote_processor; + nano::voting::processor vote_processor; unsigned warmed_up; nano::block_processor block_processor; nano::block_arrival block_arrival; - nano::local_vote_history history; + nano::voting::history history; nano::keypair node_id; nano::block_uniquer block_uniquer; 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; + nano::voting::cache vote_cache; + nano::voting::generator generator; + nano::voting::generator final_generator; nano::active_transactions active; private: // Placed here to maintain initialization order @@ -190,7 +192,7 @@ class node final : public std::enable_shared_from_this public: nano::scheduler::component & scheduler; - nano::request_aggregator aggregator; + nano::voting::aggregator aggregator; nano::wallets wallets; nano::backlog_population backlog; nano::bootstrap_ascending::service ascendboot; diff --git a/nano/node/nodeconfig.hpp b/nano/node/nodeconfig.hpp index 0cc4116270..43c6779faf 100644 --- a/nano/node/nodeconfig.hpp +++ b/nano/node/nodeconfig.hpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include @@ -125,7 +125,7 @@ class node_config unsigned backlog_scan_batch_size{ 10 * 1000 }; /** Number of times per second to run backlog population batches. Number of accounts per single batch is `backlog_scan_batch_size / backlog_scan_frequency` */ unsigned backlog_scan_frequency{ 10 }; - nano::vote_cache_config vote_cache; + nano::voting::cache_config vote_cache; public: std::string serialize_frontiers_confirmation (nano::frontiers_confirmation_mode) const; diff --git a/nano/node/process_live_dispatcher.cpp b/nano/node/process_live_dispatcher.cpp index c7a6d16dbf..3be93b94fd 100644 --- a/nano/node/process_live_dispatcher.cpp +++ b/nano/node/process_live_dispatcher.cpp @@ -2,13 +2,13 @@ #include #include #include -#include +#include #include #include #include #include -nano::process_live_dispatcher::process_live_dispatcher (nano::ledger & ledger, nano::scheduler::priority & scheduler, nano::vote_cache & vote_cache, nano::websocket_server & websocket) : +nano::process_live_dispatcher::process_live_dispatcher (nano::ledger & ledger, nano::scheduler::priority & scheduler, nano::voting::cache & vote_cache, nano::websocket_server & websocket) : ledger{ ledger }, scheduler{ scheduler }, vote_cache{ vote_cache }, diff --git a/nano/node/process_live_dispatcher.hpp b/nano/node/process_live_dispatcher.hpp index ec30d94913..cb0033c3c3 100644 --- a/nano/node/process_live_dispatcher.hpp +++ b/nano/node/process_live_dispatcher.hpp @@ -4,11 +4,14 @@ namespace nano::store { class transaction; } +namespace nano::voting +{ +class cache; +} namespace nano { class ledger; -class vote_cache; class websocket_server; class block_processor; class process_return; @@ -23,7 +26,7 @@ namespace scheduler class process_live_dispatcher { public: - process_live_dispatcher (nano::ledger &, nano::scheduler::priority &, nano::vote_cache &, nano::websocket_server &); + process_live_dispatcher (nano::ledger &, nano::scheduler::priority &, nano::voting::cache &, nano::websocket_server &); void connect (nano::block_processor & block_processor); private: @@ -33,7 +36,7 @@ class process_live_dispatcher nano::ledger & ledger; nano::scheduler::priority & scheduler; - nano::vote_cache & vote_cache; + nano::voting::cache & vote_cache; nano::websocket_server & websocket; }; } diff --git a/nano/node/scheduler/hinted.cpp b/nano/node/scheduler/hinted.cpp index 13a36d91bd..219e9e4f65 100644 --- a/nano/node/scheduler/hinted.cpp +++ b/nano/node/scheduler/hinted.cpp @@ -7,7 +7,7 @@ * hinted */ -nano::scheduler::hinted::hinted (hinted_config const & config_a, nano::node & node_a, nano::vote_cache & vote_cache_a, nano::active_transactions & active_a, nano::online_reps & online_reps_a, nano::stats & stats_a) : +nano::scheduler::hinted::hinted (hinted_config const & config_a, nano::node & node_a, nano::voting::cache & vote_cache_a, nano::active_transactions & active_a, nano::online_reps & online_reps_a, nano::stats & stats_a) : config{ config_a }, node{ node_a }, vote_cache{ vote_cache_a }, @@ -246,4 +246,4 @@ nano::error nano::scheduler::hinted_config::deserialize (nano::tomlconfig & toml } return toml.get_error (); -} \ No newline at end of file +} diff --git a/nano/node/scheduler/hinted.hpp b/nano/node/scheduler/hinted.hpp index a18d11dcb8..e52fbc8704 100644 --- a/nano/node/scheduler/hinted.hpp +++ b/nano/node/scheduler/hinted.hpp @@ -20,9 +20,12 @@ namespace nano class node; class node_config; class active_transactions; -class vote_cache; class online_reps; } +namespace nano::voting +{ +class cache; +} namespace nano::scheduler { @@ -46,7 +49,7 @@ class hinted_config final class hinted final { public: - hinted (hinted_config const &, nano::node &, nano::vote_cache &, nano::active_transactions &, nano::online_reps &, nano::stats &); + hinted (hinted_config const &, nano::node &, nano::voting::cache &, nano::active_transactions &, nano::online_reps &, nano::stats &); ~hinted (); void start (); @@ -68,7 +71,7 @@ class hinted final private: // Dependencies nano::node & node; - nano::vote_cache & vote_cache; + nano::voting::cache & vote_cache; nano::active_transactions & active; nano::online_reps & online_reps; nano::stats & stats; diff --git a/nano/node/voting.hpp b/nano/node/voting.hpp deleted file mode 100644 index 13f113b106..0000000000 --- a/nano/node/voting.hpp +++ /dev/null @@ -1,179 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include -#include -#include - -namespace mi = boost::multi_index; - -namespace nano -{ -class ledger; -class network; -class node_config; -class stats; -class vote_processor; -class wallets; -namespace transport -{ - class channel; -} - -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; -}; - -class local_vote_history final -{ - class local_vote final - { - public: - local_vote (nano::root const & root_a, nano::block_hash const & hash_a, std::shared_ptr const & vote_a) : - root (root_a), - hash (hash_a), - vote (vote_a) - { - } - nano::root root; - nano::block_hash hash; - std::shared_ptr vote; - }; - -public: - local_vote_history (nano::voting_constants const & constants) : - constants{ constants } - { - } - void add (nano::root const & root_a, nano::block_hash const & hash_a, std::shared_ptr const & vote_a); - void erase (nano::root const & root_a); - - std::vector> votes (nano::root const & root_a, nano::block_hash const & hash_a, bool const is_final_a = false) const; - bool exists (nano::root const &) const; - std::size_t size () const; - -private: - // clang-format off - boost::multi_index_container, - mi::member>, - mi::sequenced>>> - history; - // clang-format on - - nano::voting_constants const & constants; - void clean (); - std::vector> votes (nano::root const & root_a) const; - // Only used in Debug - bool consistency_check (nano::root const &) const; - mutable nano::mutex mutex; - - friend std::unique_ptr collect_container_info (local_vote_history & history, std::string const & name); - friend class local_vote_history_basic_Test; -}; - -std::unique_ptr collect_container_info (local_vote_history & history, std::string const & name); - -class vote_generator final -{ -private: - using candidate_t = std::pair; - using request_t = std::pair, std::shared_ptr>; - using queue_entry_t = std::pair; - -public: - vote_generator (nano::node_config const & config_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, bool is_final_a); - ~vote_generator (); - - /** Queue items for vote generation, or broadcast votes already in cache */ - void add (nano::root const &, nano::block_hash const &); - /** Queue blocks for vote generation, returning the number of successful candidates.*/ - std::size_t generate (std::vector> const & blocks_a, std::shared_ptr const & channel_a); - void set_reply_action (std::function const &, std::shared_ptr const &)>); - - void start (); - void stop (); - -private: - void run (); - void broadcast (nano::unique_lock &); - void reply (nano::unique_lock &, request_t &&); - void vote (std::vector const &, std::vector const &, std::function const &)> const &); - void broadcast_action (std::shared_ptr const &) const; - void process_batch (std::deque & batch); - /** - * Check if block is eligible for vote generation, then generates a vote or broadcasts votes already in cache - * @param transaction : needs `tables::final_votes` lock - */ - void process (store::write_transaction const &, nano::root const &, nano::block_hash const &); - -private: - std::function const &, std::shared_ptr &)> reply_action; // must be set only during initialization by using set_reply_action - -private: // Dependencies - nano::node_config const & config; - nano::ledger & ledger; - nano::wallets & wallets; - nano::vote_processor & vote_processor; - nano::local_vote_history & history; - nano::vote_spacing spacing; - nano::network & network; - nano::stats & stats; - -private: - processing_queue vote_generation_queue; - -private: - const bool is_final; - mutable nano::mutex mutex; - nano::condition_variable condition; - static std::size_t constexpr max_requests{ 2048 }; - std::deque requests; - 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/request_aggregator.cpp b/nano/node/voting/aggregator.cpp similarity index 91% rename from nano/node/request_aggregator.cpp rename to nano/node/voting/aggregator.cpp index cb84ba6045..95b89979b5 100644 --- a/nano/node/request_aggregator.cpp +++ b/nano/node/voting/aggregator.cpp @@ -4,13 +4,14 @@ #include #include #include -#include -#include +#include +#include +#include #include #include #include -nano::request_aggregator::request_aggregator (nano::node_config const & config_a, nano::stats & stats_a, nano::vote_generator & generator_a, nano::vote_generator & final_generator_a, nano::local_vote_history & history_a, nano::ledger & ledger_a, nano::wallets & wallets_a, nano::active_transactions & active_a) : +nano::voting::aggregator::aggregator (nano::node_config const & config_a, nano::stats & stats_a, nano::voting::generator & generator_a, nano::voting::generator & final_generator_a, nano::voting::history & history_a, nano::ledger & ledger_a, nano::wallets & wallets_a, nano::active_transactions & active_a) : config{ config_a }, max_delay (config_a.network_params.network.is_dev_network () ? 50 : 300), small_delay (config_a.network_params.network.is_dev_network () ? 10 : 50), @@ -34,7 +35,7 @@ nano::request_aggregator::request_aggregator (nano::node_config const & config_a condition.wait (lock, [&started = started] { return started; }); } -void nano::request_aggregator::add (std::shared_ptr const & channel_a, std::vector> const & hashes_roots_a) +void nano::voting::aggregator::add (std::shared_ptr const & channel_a, std::vector> const & hashes_roots_a) { debug_assert (wallets.reps ().voting > 0); bool error = true; @@ -70,7 +71,7 @@ void nano::request_aggregator::add (std::shared_ptr co stats.inc (nano::stat::type::aggregator, !error ? nano::stat::detail::aggregator_accepted : nano::stat::detail::aggregator_dropped); } -void nano::request_aggregator::run () +void nano::voting::aggregator::run () { nano::thread_role::set (nano::thread_role::name::request_aggregator); nano::unique_lock lock{ mutex }; @@ -124,7 +125,7 @@ void nano::request_aggregator::run () } } -void nano::request_aggregator::stop () +void nano::voting::aggregator::stop () { { nano::lock_guard guard{ mutex }; @@ -137,24 +138,24 @@ void nano::request_aggregator::stop () } } -std::size_t nano::request_aggregator::size () +std::size_t nano::voting::aggregator::size () { nano::unique_lock lock{ mutex }; return requests.size (); } -bool nano::request_aggregator::empty () +bool nano::voting::aggregator::empty () { return size () == 0; } -void nano::request_aggregator::reply_action (std::shared_ptr const & vote_a, std::shared_ptr const & channel_a) const +void nano::voting::aggregator::reply_action (std::shared_ptr const & vote_a, std::shared_ptr const & channel_a) const { nano::confirm_ack confirm{ config.network_params.network, vote_a }; channel_a->send (confirm); } -void nano::request_aggregator::erase_duplicates (std::vector> & requests_a) const +void nano::voting::aggregator::erase_duplicates (std::vector> & requests_a) const { std::sort (requests_a.begin (), requests_a.end (), [] (auto const & pair1, auto const & pair2) { return pair1.first < pair2.first; @@ -165,7 +166,7 @@ void nano::request_aggregator::erase_duplicates (std::vector>, std::vector>> nano::request_aggregator::aggregate (std::vector> const & requests_a, std::shared_ptr & channel_a) const +std::pair>, std::vector>> nano::voting::aggregator::aggregate (std::vector> const & requests_a, std::shared_ptr & channel_a) const { auto transaction (ledger.store.tx_begin_read ()); std::vector> to_generate; @@ -310,7 +311,7 @@ std::pair>, std::vector nano::collect_container_info (nano::request_aggregator & aggregator, std::string const & name) +std::unique_ptr nano::voting::collect_container_info (nano::voting::aggregator & aggregator, std::string const & name) { auto pools_count = aggregator.size (); auto sizeof_element = sizeof (decltype (aggregator.requests)::value_type); diff --git a/nano/node/request_aggregator.hpp b/nano/node/voting/aggregator.hpp similarity index 87% rename from nano/node/request_aggregator.hpp rename to nano/node/voting/aggregator.hpp index 657b7b56f3..beb114e5c7 100644 --- a/nano/node/request_aggregator.hpp +++ b/nano/node/voting/aggregator.hpp @@ -16,16 +16,22 @@ namespace mi = boost::multi_index; +namespace nano::voting +{ +class generator; +class history; +} namespace nano { class active_transactions; class ledger; -class local_vote_history; class node_config; class stats; -class vote_generator; class wallets; +} +namespace nano::voting +{ /** * Pools together confirmation requests, separately for each endpoint. * Requests are added from network messages, and aggregated to minimize bandwidth and vote generation. Example: @@ -34,7 +40,7 @@ class wallets; * * The aggregator will reply with the two cached votes * Votes are generated for uncached hashes. */ -class request_aggregator final +class aggregator final { /** * Holds a buffer of incoming requests from an endpoint. @@ -61,7 +67,7 @@ class request_aggregator final // clang-format on public: - request_aggregator (nano::node_config const & config, nano::stats & stats_a, nano::vote_generator &, nano::vote_generator &, nano::local_vote_history &, nano::ledger &, nano::wallets &, nano::active_transactions &); + aggregator (nano::node_config const & config, nano::stats & stats_a, nano::voting::generator &, nano::voting::generator &, nano::voting::history &, nano::ledger &, nano::wallets &, nano::active_transactions &); /** Add a new request by \p channel_a for hashes \p hashes_roots_a */ void add (std::shared_ptr const & channel_a, std::vector> const & hashes_roots_a); @@ -84,12 +90,12 @@ class request_aggregator final void reply_action (std::shared_ptr const & vote_a, std::shared_ptr const & channel_a) const; nano::stats & stats; - nano::local_vote_history & local_votes; + nano::voting::history & local_votes; nano::ledger & ledger; nano::wallets & wallets; nano::active_transactions & active; - nano::vote_generator & generator; - nano::vote_generator & final_generator; + nano::voting::generator & generator; + nano::voting::generator & final_generator; // clang-format off boost::multi_index_container collect_container_info (request_aggregator &, std::string const &); + friend std::unique_ptr collect_container_info (aggregator &, std::string const &); }; -std::unique_ptr collect_container_info (request_aggregator &, std::string const &); -} +std::unique_ptr collect_container_info (aggregator &, std::string const &); +} // namespace nano::voting diff --git a/nano/node/vote_cache.cpp b/nano/node/voting/cache.cpp similarity index 67% rename from nano/node/vote_cache.cpp rename to nano/node/voting/cache.cpp index 4b3622ad44..b846a21738 100644 --- a/nano/node/vote_cache.cpp +++ b/nano/node/voting/cache.cpp @@ -1,17 +1,17 @@ #include #include -#include +#include /* * entry */ -nano::vote_cache::entry::entry (const nano::block_hash & hash) : +nano::voting::cache::entry::entry (const nano::block_hash & hash) : hash_m{ hash } { } -bool nano::vote_cache::entry::vote (const nano::account & representative, const uint64_t & timestamp, const nano::uint128_t & rep_weight, std::size_t max_voters) +bool nano::voting::cache::entry::vote (const nano::account & representative, const uint64_t & timestamp, const nano::uint128_t & rep_weight, std::size_t max_voters) { auto existing = std::find_if (voters_m.begin (), voters_m.end (), [&representative] (auto const & item) { return item.representative == representative; }); if (existing != voters_m.end ()) @@ -53,7 +53,7 @@ bool nano::vote_cache::entry::vote (const nano::account & representative, const } } -std::size_t nano::vote_cache::entry::fill (std::shared_ptr const & election) const +std::size_t nano::voting::cache::entry::fill (std::shared_ptr const & election) const { std::size_t inserted = 0; for (const auto & entry : voters_m) @@ -67,27 +67,27 @@ std::size_t nano::vote_cache::entry::fill (std::shared_ptr const return inserted; } -std::size_t nano::vote_cache::entry::size () const +std::size_t nano::voting::cache::entry::size () const { return voters_m.size (); } -nano::block_hash nano::vote_cache::entry::hash () const +nano::block_hash nano::voting::cache::entry::hash () const { return hash_m; } -nano::uint128_t nano::vote_cache::entry::tally () const +nano::uint128_t nano::voting::cache::entry::tally () const { return tally_m; } -nano::uint128_t nano::vote_cache::entry::final_tally () const +nano::uint128_t nano::voting::cache::entry::final_tally () const { return final_tally_m; } -std::vector nano::vote_cache::entry::voters () const +std::vector nano::voting::cache::entry::voters () const { return voters_m; } @@ -96,12 +96,12 @@ std::vector nano::vote_cache::entry::voter * vote_cache */ -nano::vote_cache::vote_cache (vote_cache_config const & config_a) : +nano::voting::cache::cache (cache_config const & config_a) : config{ config_a } { } -void nano::vote_cache::vote (const nano::block_hash & hash, const std::shared_ptr vote) +void nano::voting::cache::vote (const nano::block_hash & hash, const std::shared_ptr vote) { auto const representative = vote->account; auto const timestamp = vote->timestamp (); @@ -109,7 +109,7 @@ void nano::vote_cache::vote (const nano::block_hash & hash, const std::shared_pt nano::unique_lock lock{ mutex }; - auto & cache_by_hash = cache.get (); + auto & cache_by_hash = cache_m.get (); if (auto existing = cache_by_hash.find (hash); existing != cache_by_hash.end ()) { cache_by_hash.modify (existing, [this, &representative, ×tamp, &rep_weight] (entry & ent) { @@ -121,33 +121,33 @@ void nano::vote_cache::vote (const nano::block_hash & hash, const std::shared_pt entry cache_entry{ hash }; cache_entry.vote (representative, timestamp, rep_weight, config.max_voters); - cache.get ().insert (cache_entry); + cache_m.get ().insert (cache_entry); // When cache overflown remove the oldest entry - if (cache.size () > config.max_size) + if (cache_m.size () > config.max_size) { - cache.get ().pop_front (); + cache_m.get ().pop_front (); } } } -bool nano::vote_cache::empty () const +bool nano::voting::cache::empty () const { nano::lock_guard lock{ mutex }; - return cache.empty (); + return cache_m.empty (); } -std::size_t nano::vote_cache::size () const +std::size_t nano::voting::cache::size () const { nano::lock_guard lock{ mutex }; - return cache.size (); + return cache_m.size (); } -std::optional nano::vote_cache::find (const nano::block_hash & hash) const +std::optional nano::voting::cache::find (const nano::block_hash & hash) const { nano::lock_guard lock{ mutex }; - auto & cache_by_hash = cache.get (); + auto & cache_by_hash = cache_m.get (); if (auto existing = cache_by_hash.find (hash); existing != cache_by_hash.end ()) { return *existing; @@ -155,12 +155,12 @@ std::optional nano::vote_cache::find (const nano::block return {}; } -bool nano::vote_cache::erase (const nano::block_hash & hash) +bool nano::voting::cache::erase (const nano::block_hash & hash) { nano::lock_guard lock{ mutex }; bool result = false; - auto & cache_by_hash = cache.get (); + auto & cache_by_hash = cache_m.get (); if (auto existing = cache_by_hash.find (hash); existing != cache_by_hash.end ()) { cache_by_hash.erase (existing); @@ -169,13 +169,13 @@ bool nano::vote_cache::erase (const nano::block_hash & hash) return result; } -std::vector nano::vote_cache::top (const nano::uint128_t & min_tally) const +std::vector nano::voting::cache::top (const nano::uint128_t & min_tally) const { std::vector results; { nano::lock_guard lock{ mutex }; - for (auto & entry : cache.get ()) + for (auto & entry : cache_m.get ()) { if (entry.tally () < min_tally) { @@ -200,7 +200,7 @@ std::vector nano::vote_cache::top (const nano::uint return results; } -std::unique_ptr nano::vote_cache::collect_container_info (const std::string & name) +std::unique_ptr nano::voting::cache::collect_container_info (const std::string & name) { auto composite = std::make_unique (name); composite->add_component (std::make_unique (container_info{ "cache", size (), sizeof (ordered_cache::value_type) })); @@ -211,7 +211,7 @@ std::unique_ptr nano::vote_cache::collect_contai * vote_cache_config */ -nano::error nano::vote_cache_config::serialize (nano::tomlconfig & toml) const +nano::error nano::voting::cache_config::serialize (nano::tomlconfig & toml) const { toml.put ("max_size", max_size, "Maximum number of blocks to cache votes for. \ntype:uint64"); toml.put ("max_voters", max_voters, "Maximum number of voters to cache per block. \ntype:uint64"); @@ -219,10 +219,10 @@ nano::error nano::vote_cache_config::serialize (nano::tomlconfig & toml) const return toml.get_error (); } -nano::error nano::vote_cache_config::deserialize (nano::tomlconfig & toml) +nano::error nano::voting::cache_config::deserialize (nano::tomlconfig & toml) { toml.get ("max_size", max_size); toml.get ("max_voters", max_voters); return toml.get_error (); -} \ No newline at end of file +} diff --git a/nano/node/vote_cache.hpp b/nano/node/voting/cache.hpp similarity index 95% rename from nano/node/vote_cache.hpp rename to nano/node/voting/cache.hpp index a076981f7f..a4001c63fb 100644 --- a/nano/node/vote_cache.hpp +++ b/nano/node/voting/cache.hpp @@ -27,9 +27,9 @@ class election; class vote; } -namespace nano +namespace nano::voting { -class vote_cache_config final +class cache_config final { public: nano::error deserialize (nano::tomlconfig & toml); @@ -40,7 +40,7 @@ class vote_cache_config final std::size_t max_voters{ 128 }; }; -class vote_cache final +class cache final { public: /** @@ -83,7 +83,7 @@ class vote_cache final }; public: - explicit vote_cache (vote_cache_config const &); + explicit cache (cache_config const &); /** * Adds a new vote to cache @@ -127,7 +127,7 @@ class vote_cache final std::function rep_weight_query{ [] (nano::account const & rep) { debug_assert (false); return 0; } }; private: - vote_cache_config const & config; + cache_config const & config; // clang-format off class tag_sequenced {}; @@ -145,8 +145,8 @@ class vote_cache final mi::const_mem_fun, std::greater<>> // DESC >>; // clang-format on - ordered_cache cache; + ordered_cache cache_m; mutable nano::mutex mutex; }; -} \ No newline at end of file +} // namespace nano::voting diff --git a/nano/node/voting.cpp b/nano/node/voting/generator.cpp similarity index 52% rename from nano/node/voting.cpp rename to nano/node/voting/generator.cpp index b8f1dba707..d682463029 100644 --- a/nano/node/voting.cpp +++ b/nano/node/voting/generator.cpp @@ -4,165 +4,16 @@ #include #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 (); -} - -bool nano::local_vote_history::consistency_check (nano::root const & root_a) const -{ - auto & history_by_root (history.get ()); - auto const range (history_by_root.equal_range (root_a)); - // All cached votes for a root must be for the same hash, this is actively enforced in local_vote_history::add - auto consistent_same = std::all_of (range.first, range.second, [hash = range.first->hash] (auto const & info_a) { return info_a.hash == hash; }); - std::vector accounts; - std::transform (range.first, range.second, std::back_inserter (accounts), [] (auto const & info_a) { return info_a.vote->account; }); - std::sort (accounts.begin (), accounts.end ()); - // All cached votes must be unique by account, this is actively enforced in local_vote_history::add - auto consistent_unique = accounts.size () == std::unique (accounts.begin (), accounts.end ()) - accounts.begin (); - auto result = consistent_same && consistent_unique; - debug_assert (result); - return result; -} - -void nano::local_vote_history::add (nano::root const & root_a, nano::block_hash const & hash_a, std::shared_ptr const & vote_a) -{ - nano::lock_guard guard{ mutex }; - clean (); - auto add_vote (true); - auto & history_by_root (history.get ()); - // Erase any vote that is not for this hash, or duplicate by account, and if new timestamp is higher - auto range (history_by_root.equal_range (root_a)); - for (auto i (range.first); i != range.second;) - { - if (i->hash != hash_a || (vote_a->account == i->vote->account && i->vote->timestamp () <= vote_a->timestamp ())) - { - i = history_by_root.erase (i); - } - else if (vote_a->account == i->vote->account && i->vote->timestamp () > vote_a->timestamp ()) - { - add_vote = false; - ++i; - } - else - { - ++i; - } - } - // Do not add new vote to cache if representative account is same and timestamp is lower - if (add_vote) - { - auto result (history_by_root.emplace (root_a, hash_a, vote_a)); - (void)result; - debug_assert (result.second); - } - debug_assert (consistency_check (root_a)); -} - -void nano::local_vote_history::erase (nano::root const & root_a) -{ - nano::lock_guard guard{ mutex }; - auto & history_by_root (history.get ()); - auto range (history_by_root.equal_range (root_a)); - history_by_root.erase (range.first, range.second); -} - -std::vector> nano::local_vote_history::votes (nano::root const & root_a) const -{ - nano::lock_guard guard{ mutex }; - std::vector> result; - auto range (history.get ().equal_range (root_a)); - std::transform (range.first, range.second, std::back_inserter (result), [] (auto const & entry) { return entry.vote; }); - return result; -} - -std::vector> nano::local_vote_history::votes (nano::root const & root_a, nano::block_hash const & hash_a, bool const is_final_a) const -{ - nano::lock_guard guard{ mutex }; - std::vector> result; - auto range (history.get ().equal_range (root_a)); - // clang-format off - nano::transform_if (range.first, range.second, std::back_inserter (result), - [&hash_a, is_final_a](auto const & entry) { return entry.hash == hash_a && (!is_final_a || entry.vote->timestamp () == std::numeric_limits::max ()); }, - [](auto const & entry) { return entry.vote; }); - // clang-format on - return result; -} - -bool nano::local_vote_history::exists (nano::root const & root_a) const -{ - nano::lock_guard guard{ mutex }; - return history.get ().find (root_a) != history.get ().end (); -} - -void nano::local_vote_history::clean () -{ - debug_assert (constants.max_cache > 0); - auto & history_by_sequence (history.get ()); - while (history_by_sequence.size () > constants.max_cache) - { - history_by_sequence.erase (history_by_sequence.begin ()); - } -} - -std::size_t nano::local_vote_history::size () const -{ - nano::lock_guard guard{ mutex }; - return history.size (); -} - -std::unique_ptr nano::collect_container_info (nano::local_vote_history & history, std::string const & name) -{ - std::size_t history_count = history.size (); - auto sizeof_element = sizeof (decltype (history.history)::value_type); - auto composite = std::make_unique (name); - /* This does not currently loop over each element inside the cache to get the sizes of the votes inside history*/ - composite->add_component (std::make_unique (container_info{ "history", history_count, sizeof_element })); - return composite; -} - -nano::vote_generator::vote_generator (nano::node_config const & config_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, bool is_final_a) : +nano::voting::generator::generator (nano::node_config const & config_a, nano::ledger & ledger_a, nano::wallets & wallets_a, nano::voting::processor & vote_processor_a, nano::voting::history & history_a, nano::network & network_a, nano::stats & stats_a, bool is_final_a) : config (config_a), ledger (ledger_a), wallets (wallets_a), @@ -179,12 +30,12 @@ nano::vote_generator::vote_generator (nano::node_config const & config_a, nano:: }; } -nano::vote_generator::~vote_generator () +nano::voting::generator::~generator () { stop (); } -void nano::vote_generator::process (store::write_transaction const & transaction, nano::root const & root_a, nano::block_hash const & hash_a) +void nano::voting::generator::process (store::write_transaction const & transaction, nano::root const & root_a, nano::block_hash const & hash_a) { bool should_vote = false; if (is_final) @@ -210,7 +61,7 @@ void nano::vote_generator::process (store::write_transaction const & transaction } } -void nano::vote_generator::start () +void nano::voting::generator::start () { debug_assert (!thread.joinable ()); thread = std::thread ([this] () { run (); }); @@ -218,7 +69,7 @@ void nano::vote_generator::start () vote_generation_queue.start (); } -void nano::vote_generator::stop () +void nano::voting::generator::stop () { vote_generation_queue.stop (); @@ -234,12 +85,12 @@ void nano::vote_generator::stop () } } -void nano::vote_generator::add (const root & root, const block_hash & hash) +void nano::voting::generator::add (const root & root, const block_hash & hash) { vote_generation_queue.add (std::make_pair (root, hash)); } -void nano::vote_generator::process_batch (std::deque & batch) +void nano::voting::generator::process_batch (std::deque & batch) { auto transaction = ledger.store.tx_begin_write ({ tables::final_votes }); @@ -249,7 +100,7 @@ void nano::vote_generator::process_batch (std::deque & batch) } } -std::size_t nano::vote_generator::generate (std::vector> const & blocks_a, std::shared_ptr const & channel_a) +std::size_t nano::voting::generator::generate (std::vector> const & blocks_a, std::shared_ptr const & channel_a) { request_t::first_type req_candidates; { @@ -274,13 +125,13 @@ std::size_t nano::vote_generator::generate (std::vector const &, std::shared_ptr const &)> action_a) +void nano::voting::generator::set_reply_action (std::function const &, std::shared_ptr const &)> action_a) { release_assert (!reply_action); reply_action = action_a; } -void nano::vote_generator::broadcast (nano::unique_lock & lock_a) +void nano::voting::generator::broadcast (nano::unique_lock & lock_a) { debug_assert (lock_a.owns_lock ()); @@ -316,7 +167,7 @@ void nano::vote_generator::broadcast (nano::unique_lock & lock_a) } } -void nano::vote_generator::reply (nano::unique_lock & lock_a, request_t && request_a) +void nano::voting::generator::reply (nano::unique_lock & lock_a, request_t && request_a) { lock_a.unlock (); auto i (request_a.first.cbegin ()); @@ -356,7 +207,7 @@ void nano::vote_generator::reply (nano::unique_lock & lock_a, reque lock_a.lock (); } -void nano::vote_generator::vote (std::vector const & hashes_a, std::vector const & roots_a, std::function const &)> const & action_a) +void nano::voting::generator::vote (std::vector const & hashes_a, std::vector const & roots_a, std::function const &)> const & action_a) { debug_assert (hashes_a.size () == roots_a.size ()); std::vector> votes_l; @@ -376,14 +227,14 @@ void nano::vote_generator::vote (std::vector const & hashes_a, } } -void nano::vote_generator::broadcast_action (std::shared_ptr const & vote_a) const +void nano::voting::generator::broadcast_action (std::shared_ptr const & vote_a) const { network.flood_vote_pr (vote_a); network.flood_vote (vote_a, 2.0f); vote_processor.vote (vote_a, std::make_shared (network.node, network.node)); } -void nano::vote_generator::run () +void nano::voting::generator::run () { nano::thread_role::set (nano::thread_role::name::voting); nano::unique_lock lock{ mutex }; @@ -414,7 +265,7 @@ 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::voting::collect_container_info (nano::voting::generator & vote_generator, std::string const & name) { std::size_t candidates_count = 0; std::size_t requests_count = 0; diff --git a/nano/node/voting/generator.hpp b/nano/node/voting/generator.hpp new file mode 100644 index 0000000000..e5546d6a71 --- /dev/null +++ b/nano/node/voting/generator.hpp @@ -0,0 +1,100 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace mi = boost::multi_index; + +namespace nano::voting +{ +class history; +class processor; +} +namespace nano +{ +class ledger; +class network; +class node_config; +class stats; +class wallets; +namespace transport +{ + class channel; +} +} // namespace nano + +namespace nano::voting +{ +class generator final +{ +private: + using candidate_t = std::pair; + using request_t = std::pair, std::shared_ptr>; + using queue_entry_t = std::pair; + +public: + generator (nano::node_config const & config_a, nano::ledger & ledger_a, nano::wallets & wallets_a, nano::voting::processor & vote_processor_a, nano::voting::history & history_a, nano::network & network_a, nano::stats & stats_a, bool is_final_a); + ~generator (); + + /** Queue items for vote generation, or broadcast votes already in cache */ + void add (nano::root const &, nano::block_hash const &); + /** Queue blocks for vote generation, returning the number of successful candidates.*/ + std::size_t generate (std::vector> const & blocks_a, std::shared_ptr const & channel_a); + void set_reply_action (std::function const &, std::shared_ptr const &)>); + + void start (); + void stop (); + +private: + void run (); + void broadcast (nano::unique_lock &); + void reply (nano::unique_lock &, request_t &&); + void vote (std::vector const &, std::vector const &, std::function const &)> const &); + void broadcast_action (std::shared_ptr const &) const; + void process_batch (std::deque & batch); + /** + * Check if block is eligible for vote generation, then generates a vote or broadcasts votes already in cache + * @param transaction : needs `tables::final_votes` lock + */ + void process (store::write_transaction const &, nano::root const &, nano::block_hash const &); + +private: + std::function const &, std::shared_ptr &)> reply_action; // must be set only during initialization by using set_reply_action + +private: // Dependencies + nano::node_config const & config; + nano::ledger & ledger; + nano::wallets & wallets; + nano::voting::processor & vote_processor; + nano::voting::history & history; + nano::voting::spacing spacing; + nano::network & network; + nano::stats & stats; + +private: + processing_queue vote_generation_queue; + +private: + const bool is_final; + mutable nano::mutex mutex; + nano::condition_variable condition; + static std::size_t constexpr max_requests{ 2048 }; + std::deque requests; + std::deque candidates; + std::atomic stopped{ false }; + std::thread thread; + + friend std::unique_ptr collect_container_info (generator & vote_generator, std::string const & name); +}; + +std::unique_ptr collect_container_info (generator & generator, std::string const & name); +} // namespace nano::voting diff --git a/nano/node/voting/history.cpp b/nano/node/voting/history.cpp new file mode 100644 index 0000000000..4dd04c6a19 --- /dev/null +++ b/nano/node/voting/history.cpp @@ -0,0 +1,113 @@ +#include + +bool nano::voting::history::consistency_check (nano::root const & root_a) const +{ + auto & history_by_root (history_m.get ()); + auto const range (history_by_root.equal_range (root_a)); + // All cached votes for a root must be for the same hash, this is actively enforced in local_vote_history::add + auto consistent_same = std::all_of (range.first, range.second, [hash = range.first->hash] (auto const & info_a) { return info_a.hash == hash; }); + std::vector accounts; + std::transform (range.first, range.second, std::back_inserter (accounts), [] (auto const & info_a) { return info_a.vote->account; }); + std::sort (accounts.begin (), accounts.end ()); + // All cached votes must be unique by account, this is actively enforced in local_vote_history::add + auto consistent_unique = accounts.size () == std::unique (accounts.begin (), accounts.end ()) - accounts.begin (); + auto result = consistent_same && consistent_unique; + debug_assert (result); + return result; +} + +void nano::voting::history::add (nano::root const & root_a, nano::block_hash const & hash_a, std::shared_ptr const & vote_a) +{ + nano::lock_guard guard{ mutex }; + clean (); + auto add_vote (true); + auto & history_by_root (history_m.get ()); + // Erase any vote that is not for this hash, or duplicate by account, and if new timestamp is higher + auto range (history_by_root.equal_range (root_a)); + for (auto i (range.first); i != range.second;) + { + if (i->hash != hash_a || (vote_a->account == i->vote->account && i->vote->timestamp () <= vote_a->timestamp ())) + { + i = history_by_root.erase (i); + } + else if (vote_a->account == i->vote->account && i->vote->timestamp () > vote_a->timestamp ()) + { + add_vote = false; + ++i; + } + else + { + ++i; + } + } + // Do not add new vote to cache if representative account is same and timestamp is lower + if (add_vote) + { + auto result (history_by_root.emplace (root_a, hash_a, vote_a)); + (void)result; + debug_assert (result.second); + } + debug_assert (consistency_check (root_a)); +} + +void nano::voting::history::erase (nano::root const & root_a) +{ + nano::lock_guard guard{ mutex }; + auto & history_by_root (history_m.get ()); + auto range (history_by_root.equal_range (root_a)); + history_by_root.erase (range.first, range.second); +} + +std::vector> nano::voting::history::votes (nano::root const & root_a) const +{ + nano::lock_guard guard{ mutex }; + std::vector> result; + auto range (history_m.get ().equal_range (root_a)); + std::transform (range.first, range.second, std::back_inserter (result), [] (auto const & entry) { return entry.vote; }); + return result; +} + +std::vector> nano::voting::history::votes (nano::root const & root_a, nano::block_hash const & hash_a, bool const is_final_a) const +{ + nano::lock_guard guard{ mutex }; + std::vector> result; + auto range (history_m.get ().equal_range (root_a)); + // clang-format off + nano::transform_if (range.first, range.second, std::back_inserter (result), + [&hash_a, is_final_a](auto const & entry) { return entry.hash == hash_a && (!is_final_a || entry.vote->timestamp () == std::numeric_limits::max ()); }, + [](auto const & entry) { return entry.vote; }); + // clang-format on + return result; +} + +bool nano::voting::history::exists (nano::root const & root_a) const +{ + nano::lock_guard guard{ mutex }; + return history_m.get ().find (root_a) != history_m.get ().end (); +} + +void nano::voting::history::clean () +{ + debug_assert (constants.max_cache > 0); + auto & history_by_sequence (history_m.get ()); + while (history_by_sequence.size () > constants.max_cache) + { + history_by_sequence.erase (history_by_sequence.begin ()); + } +} + +std::size_t nano::voting::history::size () const +{ + nano::lock_guard guard{ mutex }; + return history_m.size (); +} + +std::unique_ptr nano::voting::collect_container_info (nano::voting::history & history, std::string const & name) +{ + std::size_t history_count = history.size (); + auto sizeof_element = sizeof (decltype (history.history_m)::value_type); + auto composite = std::make_unique (name); + /* This does not currently loop over each element inside the cache to get the sizes of the votes inside history*/ + composite->add_component (std::make_unique (container_info{ "history", history_count, sizeof_element })); + return composite; +} diff --git a/nano/node/voting/history.hpp b/nano/node/voting/history.hpp new file mode 100644 index 0000000000..8e503e8bc2 --- /dev/null +++ b/nano/node/voting/history.hpp @@ -0,0 +1,73 @@ +#pragma once + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace mi = boost::multi_index; + +namespace nano +{ +class local_vote_history_basic_Test; +class vote; +class voting_constants; +} +namespace nano::voting +{ +class history final +{ + class local_vote final + { + public: + local_vote (nano::root const & root_a, nano::block_hash const & hash_a, std::shared_ptr const & vote_a) : + root (root_a), + hash (hash_a), + vote (vote_a) + { + } + nano::root root; + nano::block_hash hash; + std::shared_ptr vote; + }; + +public: + history (nano::voting_constants const & constants) : + constants{ constants } + { + } + void add (nano::root const & root_a, nano::block_hash const & hash_a, std::shared_ptr const & vote_a); + void erase (nano::root const & root_a); + + std::vector> votes (nano::root const & root_a, nano::block_hash const & hash_a, bool const is_final_a = false) const; + bool exists (nano::root const &) const; + std::size_t size () const; + +private: + // clang-format off + boost::multi_index_container, + mi::member>, + mi::sequenced>>> + history_m; + // clang-format on + + nano::voting_constants const & constants; + void clean (); + std::vector> votes (nano::root const & root_a) const; + // Only used in Debug + bool consistency_check (nano::root const &) const; + mutable nano::mutex mutex; + + friend std::unique_ptr collect_container_info (history & history, std::string const & name); + friend class nano::local_vote_history_basic_Test; +}; + +std::unique_ptr collect_container_info (history & history, std::string const & name); +} // namespace nano::voting diff --git a/nano/node/vote_processor.cpp b/nano/node/voting/processor.cpp similarity index 86% rename from nano/node/vote_processor.cpp rename to nano/node/voting/processor.cpp index fd6d5696cb..cdad2d6e84 100644 --- a/nano/node/vote_processor.cpp +++ b/nano/node/voting/processor.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include @@ -17,7 +17,7 @@ #include using namespace std::chrono_literals; -nano::vote_processor::vote_processor (nano::signature_checker & checker_a, nano::active_transactions & active_a, nano::node_observers & observers_a, nano::stats & stats_a, nano::node_config & config_a, nano::node_flags & flags_a, nano::logger_mt & logger_a, nano::online_reps & online_reps_a, nano::rep_crawler & rep_crawler_a, nano::ledger & ledger_a, nano::network_params & network_params_a) : +nano::voting::processor::processor (nano::signature_checker & checker_a, nano::active_transactions & active_a, nano::node_observers & observers_a, nano::stats & stats_a, nano::node_config & config_a, nano::node_flags & flags_a, nano::logger_mt & logger_a, nano::online_reps & online_reps_a, nano::rep_crawler & rep_crawler_a, nano::ledger & ledger_a, nano::network_params & network_params_a) : checker (checker_a), active (active_a), observers (observers_a), @@ -43,7 +43,7 @@ nano::vote_processor::vote_processor (nano::signature_checker & checker_a, nano: condition.wait (lock, [&started = started] { return started; }); } -void nano::vote_processor::process_loop () +void nano::voting::processor::process_loop () { nano::timer elapsed; bool log_this_iteration; @@ -90,7 +90,7 @@ void nano::vote_processor::process_loop () } } -bool nano::vote_processor::vote (std::shared_ptr const & vote_a, std::shared_ptr const & channel_a) +bool nano::voting::processor::vote (std::shared_ptr const & vote_a, std::shared_ptr const & channel_a) { debug_assert (channel_a != nullptr); bool process (false); @@ -132,7 +132,7 @@ bool nano::vote_processor::vote (std::shared_ptr const & vote_a, std return !process; } -void nano::vote_processor::verify_votes (decltype (votes) const & votes_a) +void nano::voting::processor::verify_votes (decltype (votes) const & votes_a) { auto size (votes_a.size ()); std::vector messages; @@ -167,7 +167,7 @@ void nano::vote_processor::verify_votes (decltype (votes) const & votes_a) } } -nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr const & vote_a, std::shared_ptr const & channel_a, bool validated) +nano::vote_code nano::voting::processor::vote_blocking (std::shared_ptr const & vote_a, std::shared_ptr const & channel_a, bool validated) { auto result (nano::vote_code::invalid); if (validated || !vote_a->validate ()) @@ -202,7 +202,7 @@ nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr return result; } -void nano::vote_processor::stop () +void nano::voting::processor::stop () { { nano::lock_guard lock{ mutex }; @@ -215,7 +215,7 @@ void nano::vote_processor::stop () } } -void nano::vote_processor::flush () +void nano::voting::processor::flush () { nano::unique_lock lock{ mutex }; auto const cutoff = total_processed.load (std::memory_order_relaxed) + votes.size (); @@ -229,24 +229,24 @@ void nano::vote_processor::flush () } } -std::size_t nano::vote_processor::size () +std::size_t nano::voting::processor::size () { nano::lock_guard guard{ mutex }; return votes.size (); } -bool nano::vote_processor::empty () +bool nano::voting::processor::empty () { nano::lock_guard guard{ mutex }; return votes.empty (); } -bool nano::vote_processor::half_full () +bool nano::voting::processor::half_full () { return size () >= max_votes / 2; } -void nano::vote_processor::calculate_weights () +void nano::voting::processor::calculate_weights () { nano::unique_lock lock{ mutex }; if (!stopped) @@ -276,7 +276,7 @@ void nano::vote_processor::calculate_weights () } } -std::unique_ptr nano::collect_container_info (vote_processor & vote_processor, std::string const & name) +std::unique_ptr nano::voting::collect_container_info (processor & vote_processor, std::string const & name) { std::size_t votes_count; std::size_t representatives_1_count; diff --git a/nano/node/vote_processor.hpp b/nano/node/voting/processor.hpp similarity index 78% rename from nano/node/vote_processor.hpp rename to nano/node/voting/processor.hpp index 0ffb39dd00..9687e2ce34 100644 --- a/nano/node/vote_processor.hpp +++ b/nano/node/voting/processor.hpp @@ -13,6 +13,7 @@ namespace nano { class signature_checker; class active_transactions; +class vote_processor_weights_Test; namespace store { class component; @@ -32,11 +33,14 @@ namespace transport { class channel; } +} // namespace nano -class vote_processor final +namespace nano::voting +{ +class processor final { public: - vote_processor (nano::signature_checker & checker_a, nano::active_transactions & active_a, nano::node_observers & observers_a, nano::stats & stats_a, nano::node_config & config_a, nano::node_flags & flags_a, nano::logger_mt & logger_a, nano::online_reps & online_reps_a, nano::rep_crawler & rep_crawler_a, nano::ledger & ledger_a, nano::network_params & network_params_a); + processor (nano::signature_checker & checker_a, nano::active_transactions & active_a, nano::node_observers & observers_a, nano::stats & stats_a, nano::node_config & config_a, nano::node_flags & flags_a, nano::logger_mt & logger_a, nano::online_reps & online_reps_a, nano::rep_crawler & rep_crawler_a, nano::ledger & ledger_a, nano::network_params & network_params_a); /** Returns false if the vote was processed */ bool vote (std::shared_ptr const &, std::shared_ptr const &); @@ -78,9 +82,9 @@ class vote_processor final bool stopped; std::thread thread; - friend std::unique_ptr collect_container_info (vote_processor & vote_processor, std::string const & name); - friend class vote_processor_weights_Test; + friend std::unique_ptr collect_container_info (processor & vote_processor, std::string const & name); + friend class nano::vote_processor_weights_Test; }; -std::unique_ptr collect_container_info (vote_processor & vote_processor, std::string const & name); -} +std::unique_ptr collect_container_info (processor & vote_processor, std::string const & name); +} // nano::voting diff --git a/nano/node/voting/spacing.cpp b/nano/node/voting/spacing.cpp new file mode 100644 index 0000000000..c98d1de69e --- /dev/null +++ b/nano/node/voting/spacing.cpp @@ -0,0 +1,39 @@ +#include + +void nano::voting::spacing::trim () +{ + recent.get ().erase (recent.get ().begin (), recent.get ().upper_bound (std::chrono::steady_clock::now () - delay)); +} + +bool nano::voting::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::voting::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::voting::spacing::size () const +{ + return recent.size (); +} diff --git a/nano/node/voting/spacing.hpp b/nano/node/voting/spacing.hpp new file mode 100644 index 0000000000..86a44237e3 --- /dev/null +++ b/nano/node/voting/spacing.hpp @@ -0,0 +1,46 @@ +#pragma once + +#include +#include + +#include +#include +#include +#include + +#include + +namespace mi = boost::multi_index; + +namespace nano::voting +{ +class 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: + 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; +}; +} // namespace nano diff --git a/nano/node/wallet.hpp b/nano/node/wallet.hpp index 40409f5e35..4a75b2aed4 100644 --- a/nano/node/wallet.hpp +++ b/nano/node/wallet.hpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include diff --git a/nano/qt_test/qt.cpp b/nano/qt_test/qt.cpp index 8e3dea40b7..47b4bf07d1 100644 --- a/nano/qt_test/qt.cpp +++ b/nano/qt_test/qt.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #include #include diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 5416292cb0..35f17afcbf 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -5238,7 +5238,7 @@ TEST (rpc, confirmation_height_currently_processing) // Begin process for confirming the block (and setting confirmation height) { // Write guard prevents the confirmation height processor writing the blocks, so that we can inspect contents during the response - auto write_guard = node->write_database_queue.wait (nano::writer::testing); + auto write_guard = node->write_database_queue.wait (nano::store::writer::testing); nano::test::start_election (system, *node, frontier->hash ()); ASSERT_TIMELY (5s, node->confirmation_height_processor.current () == frontier->hash ()); @@ -6934,4 +6934,4 @@ TEST (rpc, confirmation_info) ASSERT_EQ (1, representatives.size ()); ASSERT_EQ (0, response.get ("total_tally")); } -} \ No newline at end of file +} diff --git a/nano/secure/ledger.cpp b/nano/secure/ledger.cpp index e7c7f1abf9..77e41be2ed 100644 --- a/nano/secure/ledger.cpp +++ b/nano/secure/ledger.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -11,6 +10,7 @@ #include #include #include +#include #include #include #include diff --git a/nano/slow_test/node.cpp b/nano/slow_test/node.cpp index 740383a83c..6f3d98c616 100644 --- a/nano/slow_test/node.cpp +++ b/nano/slow_test/node.cpp @@ -2,12 +2,12 @@ #include #include #include -#include #include #include #include #include #include +#include #include #include #include @@ -1043,7 +1043,7 @@ TEST (confirmation_height, dynamic_algorithm_no_transition_while_pending) } { - auto write_guard = node->write_database_queue.wait (nano::writer::testing); + auto write_guard = node->write_database_queue.wait (nano::store::writer::testing); // To limit any data races we are not calling node.block_confirm node->confirmation_height_processor.add (state_blocks.back ()); @@ -1231,7 +1231,7 @@ TEST (confirmation_height, many_accounts_send_receive_self_no_elections) ASSERT_TRUE (!store->init_error ()); nano::stats stats; nano::ledger ledger (*store, stats, nano::dev::constants); - nano::write_database_queue write_database_queue (false); + nano::store::write_database_queue write_database_queue (false); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; std::atomic stopped{ false }; boost::latch initialized_latch{ 0 }; diff --git a/nano/slow_test/vote_processor.cpp b/nano/slow_test/vote_processor.cpp index eaae35577d..320870e239 100644 --- a/nano/slow_test/vote_processor.cpp +++ b/nano/slow_test/vote_processor.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include diff --git a/nano/store/CMakeLists.txt b/nano/store/CMakeLists.txt index 427e038082..916a28e64a 100644 --- a/nano/store/CMakeLists.txt +++ b/nano/store/CMakeLists.txt @@ -11,12 +11,12 @@ add_library( frontier.hpp lmdb/account.hpp lmdb/block.hpp + lmdb/component.hpp lmdb/confirmation_height.hpp lmdb/db_val.hpp lmdb/final_vote.hpp lmdb/frontier.hpp lmdb/iterator.hpp - lmdb/lmdb.hpp lmdb/lmdb_env.hpp lmdb/iterator.hpp lmdb/transaction_impl.hpp @@ -27,12 +27,15 @@ add_library( lmdb/transaction_impl.hpp lmdb/version.hpp lmdb/wallet_value.hpp + make_store.hpp + make_store.cpp online_weight.hpp peer.hpp pending.hpp pruned.hpp rocksdb/account.hpp rocksdb/block.hpp + rocksdb/component.hpp rocksdb/confirmation_height.hpp rocksdb/db_val.hpp rocksdb/final_vote.hpp @@ -42,7 +45,6 @@ add_library( rocksdb/peer.hpp rocksdb/pending.hpp rocksdb/pruned.hpp - rocksdb/rocksdb.hpp rocksdb/iterator.hpp rocksdb/transaction_impl.hpp rocksdb/version.hpp @@ -61,11 +63,11 @@ add_library( frontier.cpp lmdb/account.cpp lmdb/block.cpp + lmdb/component.cpp lmdb/confirmation_height.cpp lmdb/db_val.cpp lmdb/final_vote.cpp lmdb/frontier.cpp - lmdb/lmdb.cpp lmdb/lmdb_env.cpp lmdb/transaction.cpp lmdb/online_weight.cpp @@ -80,6 +82,7 @@ add_library( pruned.cpp rocksdb/account.cpp rocksdb/block.cpp + rocksdb/component.cpp rocksdb/confirmation_height.cpp rocksdb/db_val.cpp rocksdb/final_vote.cpp @@ -88,12 +91,13 @@ add_library( rocksdb/peer.cpp rocksdb/pending.cpp rocksdb/pruned.cpp - rocksdb/rocksdb.cpp rocksdb/transaction.cpp rocksdb/version.cpp transaction.cpp version.cpp - versioning.cpp) + versioning.cpp + write_database_queue.hpp + write_database_queue.cpp) target_link_libraries( nano_store diff --git a/nano/store/lmdb/account.cpp b/nano/store/lmdb/account.cpp index 19a35c8e44..5591639419 100644 --- a/nano/store/lmdb/account.cpp +++ b/nano/store/lmdb/account.cpp @@ -1,7 +1,7 @@ #include #include +#include #include -#include nano::store::lmdb::account::account (nano::store::lmdb::component & store_a) : store (store_a){}; diff --git a/nano/store/lmdb/block.cpp b/nano/store/lmdb/block.cpp index e01d71fb21..cbd5ea4ebb 100644 --- a/nano/store/lmdb/block.cpp +++ b/nano/store/lmdb/block.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include namespace nano::store::lmdb { diff --git a/nano/store/lmdb/lmdb.cpp b/nano/store/lmdb/component.cpp similarity index 99% rename from nano/store/lmdb/lmdb.cpp rename to nano/store/lmdb/component.cpp index 8834c38d01..3eeb953521 100644 --- a/nano/store/lmdb/lmdb.cpp +++ b/nano/store/lmdb/component.cpp @@ -1,8 +1,8 @@ #include #include #include +#include #include -#include #include #include #include diff --git a/nano/store/lmdb/lmdb.hpp b/nano/store/lmdb/component.hpp similarity index 100% rename from nano/store/lmdb/lmdb.hpp rename to nano/store/lmdb/component.hpp diff --git a/nano/store/lmdb/confirmation_height.cpp b/nano/store/lmdb/confirmation_height.cpp index 1ce01edab4..1e8147dc6b 100644 --- a/nano/store/lmdb/confirmation_height.cpp +++ b/nano/store/lmdb/confirmation_height.cpp @@ -1,6 +1,6 @@ #include +#include #include -#include nano::store::lmdb::confirmation_height::confirmation_height (nano::store::lmdb::component & store) : store{ store } diff --git a/nano/store/lmdb/final_vote.cpp b/nano/store/lmdb/final_vote.cpp index 2ae93bd5a9..1e5c4513e9 100644 --- a/nano/store/lmdb/final_vote.cpp +++ b/nano/store/lmdb/final_vote.cpp @@ -1,6 +1,6 @@ #include +#include #include -#include nano::store::lmdb::final_vote::final_vote (nano::store::lmdb::component & store) : store{ store } {}; diff --git a/nano/store/lmdb/frontier.cpp b/nano/store/lmdb/frontier.cpp index 8b694a3ef4..78be3d840a 100644 --- a/nano/store/lmdb/frontier.cpp +++ b/nano/store/lmdb/frontier.cpp @@ -1,6 +1,6 @@ #include +#include #include -#include nano::store::lmdb::frontier::frontier (nano::store::lmdb::component & store) : store{ store } diff --git a/nano/store/lmdb/online_weight.cpp b/nano/store/lmdb/online_weight.cpp index 647aaf34da..7401063a3f 100644 --- a/nano/store/lmdb/online_weight.cpp +++ b/nano/store/lmdb/online_weight.cpp @@ -1,4 +1,4 @@ -#include +#include #include nano::store::lmdb::online_weight::online_weight (nano::store::lmdb::component & store_a) : diff --git a/nano/store/lmdb/peer.cpp b/nano/store/lmdb/peer.cpp index cb3de165c5..c3f9fb688a 100644 --- a/nano/store/lmdb/peer.cpp +++ b/nano/store/lmdb/peer.cpp @@ -1,4 +1,4 @@ -#include +#include #include nano::store::lmdb::peer::peer (nano::store::lmdb::component & store) : diff --git a/nano/store/lmdb/pending.cpp b/nano/store/lmdb/pending.cpp index 961b201829..48a178db7a 100644 --- a/nano/store/lmdb/pending.cpp +++ b/nano/store/lmdb/pending.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include nano::store::lmdb::pending::pending (nano::store::lmdb::component & store) : diff --git a/nano/store/lmdb/pruned.cpp b/nano/store/lmdb/pruned.cpp index 2be19c24f5..aebcf60b41 100644 --- a/nano/store/lmdb/pruned.cpp +++ b/nano/store/lmdb/pruned.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include nano::store::lmdb::pruned::pruned (nano::store::lmdb::component & store_a) : diff --git a/nano/store/lmdb/version.cpp b/nano/store/lmdb/version.cpp index f667dc4b9c..a871cb2568 100644 --- a/nano/store/lmdb/version.cpp +++ b/nano/store/lmdb/version.cpp @@ -1,4 +1,4 @@ -#include +#include #include nano::store::lmdb::version::version (nano::store::lmdb::component & store_a) : diff --git a/nano/node/make_store.cpp b/nano/store/make_store.cpp similarity index 87% rename from nano/node/make_store.cpp rename to nano/store/make_store.cpp index 7f24c649cf..13a7e60ce5 100644 --- a/nano/node/make_store.cpp +++ b/nano/store/make_store.cpp @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include std::unique_ptr nano::make_store (nano::logger_mt & logger, std::filesystem::path const & path, nano::ledger_constants & constants, bool read_only, bool add_db_postfix, nano::rocksdb_config const & rocksdb_config, nano::txn_tracking_config const & txn_tracking_config_a, std::chrono::milliseconds block_processor_batch_max_time_a, nano::lmdb_config const & lmdb_config_a, bool backup_before_upgrade) { diff --git a/nano/node/make_store.hpp b/nano/store/make_store.hpp similarity index 100% rename from nano/node/make_store.hpp rename to nano/store/make_store.hpp diff --git a/nano/store/rocksdb/account.cpp b/nano/store/rocksdb/account.cpp index 8ee01b98fe..4d1edb30c5 100644 --- a/nano/store/rocksdb/account.cpp +++ b/nano/store/rocksdb/account.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include nano::store::rocksdb::account::account (nano::store::rocksdb::component & store_a) : store (store_a){}; diff --git a/nano/store/rocksdb/block.cpp b/nano/store/rocksdb/block.cpp index fbf1ecf1f0..20b14f43f8 100644 --- a/nano/store/rocksdb/block.cpp +++ b/nano/store/rocksdb/block.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include namespace nano { diff --git a/nano/store/rocksdb/rocksdb.cpp b/nano/store/rocksdb/component.cpp similarity index 99% rename from nano/store/rocksdb/rocksdb.cpp rename to nano/store/rocksdb/component.cpp index 054fec0a82..d72b042705 100644 --- a/nano/store/rocksdb/rocksdb.cpp +++ b/nano/store/rocksdb/component.cpp @@ -1,6 +1,6 @@ #include +#include #include -#include #include #include diff --git a/nano/store/rocksdb/rocksdb.hpp b/nano/store/rocksdb/component.hpp similarity index 100% rename from nano/store/rocksdb/rocksdb.hpp rename to nano/store/rocksdb/component.hpp diff --git a/nano/store/rocksdb/confirmation_height.cpp b/nano/store/rocksdb/confirmation_height.cpp index 380cd80e32..8c5044aab5 100644 --- a/nano/store/rocksdb/confirmation_height.cpp +++ b/nano/store/rocksdb/confirmation_height.cpp @@ -1,6 +1,6 @@ #include +#include #include -#include nano::store::rocksdb::confirmation_height::confirmation_height (nano::store::rocksdb::component & store) : store{ store } diff --git a/nano/store/rocksdb/final_vote.cpp b/nano/store/rocksdb/final_vote.cpp index 121da33360..d440d2345f 100644 --- a/nano/store/rocksdb/final_vote.cpp +++ b/nano/store/rocksdb/final_vote.cpp @@ -1,6 +1,6 @@ #include +#include #include -#include nano::store::rocksdb::final_vote::final_vote (nano::store::rocksdb::component & store) : store{ store } {}; diff --git a/nano/store/rocksdb/frontier.cpp b/nano/store/rocksdb/frontier.cpp index 6724b93363..d3745c7ac8 100644 --- a/nano/store/rocksdb/frontier.cpp +++ b/nano/store/rocksdb/frontier.cpp @@ -1,6 +1,6 @@ #include +#include #include -#include nano::store::rocksdb::frontier::frontier (nano::store::rocksdb::component & store) : store{ store } diff --git a/nano/store/rocksdb/online_weight.cpp b/nano/store/rocksdb/online_weight.cpp index c6f7e1dacf..2029903b7b 100644 --- a/nano/store/rocksdb/online_weight.cpp +++ b/nano/store/rocksdb/online_weight.cpp @@ -1,5 +1,5 @@ +#include #include -#include nano::store::rocksdb::online_weight::online_weight (nano::store::rocksdb::component & store_a) : store{ store_a } diff --git a/nano/store/rocksdb/peer.cpp b/nano/store/rocksdb/peer.cpp index e837521605..53aafab5c1 100644 --- a/nano/store/rocksdb/peer.cpp +++ b/nano/store/rocksdb/peer.cpp @@ -1,5 +1,5 @@ +#include #include -#include nano::store::rocksdb::peer::peer (nano::store::rocksdb::component & store) : store{ store } {}; diff --git a/nano/store/rocksdb/pending.cpp b/nano/store/rocksdb/pending.cpp index f7e843f184..dcb38711c4 100644 --- a/nano/store/rocksdb/pending.cpp +++ b/nano/store/rocksdb/pending.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include nano::store::rocksdb::pending::pending (nano::store::rocksdb::component & store) : store{ store } {}; diff --git a/nano/store/rocksdb/pruned.cpp b/nano/store/rocksdb/pruned.cpp index 41d7fbb586..1414024ed1 100644 --- a/nano/store/rocksdb/pruned.cpp +++ b/nano/store/rocksdb/pruned.cpp @@ -1,6 +1,6 @@ #include +#include #include -#include nano::store::rocksdb::pruned::pruned (nano::store::rocksdb::component & store_a) : store{ store_a } {}; diff --git a/nano/store/rocksdb/version.cpp b/nano/store/rocksdb/version.cpp index c1df2e0232..1f5a6a20e9 100644 --- a/nano/store/rocksdb/version.cpp +++ b/nano/store/rocksdb/version.cpp @@ -1,4 +1,4 @@ -#include +#include #include nano::store::rocksdb::version::version (nano::store::rocksdb::component & store_a) : diff --git a/nano/node/write_database_queue.cpp b/nano/store/write_database_queue.cpp similarity index 69% rename from nano/node/write_database_queue.cpp rename to nano/store/write_database_queue.cpp index 8bb42fe322..8953a2391e 100644 --- a/nano/node/write_database_queue.cpp +++ b/nano/store/write_database_queue.cpp @@ -1,15 +1,15 @@ #include #include -#include +#include #include -nano::write_guard::write_guard (std::function guard_finish_callback_a) : +nano::store::write_guard::write_guard (std::function guard_finish_callback_a) : guard_finish_callback (guard_finish_callback_a) { } -nano::write_guard::write_guard (nano::write_guard && write_guard_a) noexcept : +nano::store::write_guard::write_guard (nano::store::write_guard && write_guard_a) noexcept : guard_finish_callback (std::move (write_guard_a.guard_finish_callback)), owns (write_guard_a.owns) { @@ -17,7 +17,7 @@ nano::write_guard::write_guard (nano::write_guard && write_guard_a) noexcept : write_guard_a.guard_finish_callback = nullptr; } -nano::write_guard & nano::write_guard::operator= (nano::write_guard && write_guard_a) noexcept +nano::store::write_guard & nano::store::write_guard::operator= (nano::store::write_guard && write_guard_a) noexcept { owns = write_guard_a.owns; guard_finish_callback = std::move (write_guard_a.guard_finish_callback); @@ -27,7 +27,7 @@ nano::write_guard & nano::write_guard::operator= (nano::write_guard && write_gua return *this; } -nano::write_guard::~write_guard () +nano::store::write_guard::~write_guard () { if (owns) { @@ -35,12 +35,12 @@ nano::write_guard::~write_guard () } } -bool nano::write_guard::is_owned () const +bool nano::store::write_guard::is_owned () const { return owns; } -void nano::write_guard::release () +void nano::store::write_guard::release () { debug_assert (owns); if (owns) @@ -50,7 +50,7 @@ void nano::write_guard::release () owns = false; } -nano::write_database_queue::write_database_queue (bool use_noops_a) : +nano::store::write_database_queue::write_database_queue (bool use_noops_a) : guard_finish_callback ([use_noops_a, &queue = queue, &mutex = mutex, &cv = cv] () { if (!use_noops_a) { @@ -65,7 +65,7 @@ nano::write_database_queue::write_database_queue (bool use_noops_a) : { } -nano::write_guard nano::write_database_queue::wait (nano::writer writer) +nano::store::write_guard nano::store::write_database_queue::wait (nano::store::writer writer) { if (use_noops) { @@ -88,14 +88,14 @@ nano::write_guard nano::write_database_queue::wait (nano::writer writer) return write_guard (guard_finish_callback); } -bool nano::write_database_queue::contains (nano::writer writer) +bool nano::store::write_database_queue::contains (nano::store::writer writer) { debug_assert (!use_noops); nano::lock_guard guard (mutex); return std::find (queue.cbegin (), queue.cend (), writer) != queue.cend (); } -bool nano::write_database_queue::process (nano::writer writer) +bool nano::store::write_database_queue::process (nano::store::writer writer) { if (use_noops) { @@ -123,7 +123,7 @@ bool nano::write_database_queue::process (nano::writer writer) return result; } -nano::write_guard nano::write_database_queue::pop () +nano::store::write_guard nano::store::write_database_queue::pop () { return write_guard (guard_finish_callback); } diff --git a/nano/node/write_database_queue.hpp b/nano/store/write_database_queue.hpp similarity index 84% rename from nano/node/write_database_queue.hpp rename to nano/store/write_database_queue.hpp index 4443eca6cc..e0d55a9c73 100644 --- a/nano/node/write_database_queue.hpp +++ b/nano/store/write_database_queue.hpp @@ -6,7 +6,7 @@ #include #include -namespace nano +namespace nano::store { /** Distinct areas write locking is done, order is irrelevant */ enum class writer @@ -39,22 +39,22 @@ class write_database_queue final public: write_database_queue (bool use_noops_a); /** Blocks until we are at the head of the queue */ - write_guard wait (nano::writer writer); + write_guard wait (nano::store::writer writer); /** Returns true if this writer is now at the front of the queue */ - bool process (nano::writer writer); + bool process (nano::store::writer writer); /** Returns true if this writer is anywhere in the queue. Currently only used in tests */ - bool contains (nano::writer writer); + bool contains (nano::store::writer writer); /** Doesn't actually pop anything until the returned write_guard is out of scope */ write_guard pop (); private: - std::deque queue; + std::deque queue; nano::mutex mutex; nano::condition_variable cv; std::function guard_finish_callback; bool use_noops; }; -} +} // namespace nano::store diff --git a/nano/test_common/ledger.cpp b/nano/test_common/ledger.cpp index a3337bc925..8bdc39d50f 100644 --- a/nano/test_common/ledger.cpp +++ b/nano/test_common/ledger.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include nano::test::context::ledger_context::ledger_context (std::deque> && blocks) :