diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index 60160955..a82a2609 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -2295,7 +2295,7 @@ TEST (node, block_arrival_size) nano::block_hash hash (0); for (auto i (0); i < nano::block_arrival::arrival_size_min * 2; ++i) { - node.block_arrival.arrival.insert (nano::block_arrival_info{ time, hash }); + node.block_arrival.arrival.push_back (nano::block_arrival_info{ time, hash }); ++hash.qwords[0]; } ASSERT_EQ (nano::block_arrival::arrival_size_min * 2, node.block_arrival.arrival.size ()); @@ -2311,7 +2311,7 @@ TEST (node, block_arrival_time) nano::block_hash hash (0); for (auto i (0); i < nano::block_arrival::arrival_size_min * 2; ++i) { - node.block_arrival.arrival.insert (nano::block_arrival_info{ time, hash }); + node.block_arrival.arrival.push_back (nano::block_arrival_info{ time, hash }); ++hash.qwords[0]; } ASSERT_EQ (nano::block_arrival::arrival_size_min * 2, node.block_arrival.arrival.size ()); diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index ccdd3203..834ae90b 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -71,9 +71,9 @@ void nano::active_transactions::search_frontiers (nano::transaction const & tran auto start_elections_for_prioritized_frontiers = [&transaction_a, &elections_count, max_elections, &lk, &representative, this](prioritize_num_uncemented & cementable_frontiers) { while (!cementable_frontiers.empty () && !this->stopped && elections_count < max_elections) { - auto cementable_account_front_it = cementable_frontiers.get<1> ().begin (); + auto cementable_account_front_it = cementable_frontiers.get ().begin (); auto cementable_account = *cementable_account_front_it; - cementable_frontiers.get<1> ().erase (cementable_account_front_it); + cementable_frontiers.get ().erase (cementable_account_front_it); lk.unlock (); nano::account_info info; auto error = node.store.account_get (transaction_a, cementable_account.account, info); @@ -331,7 +331,7 @@ void nano::active_transactions::request_confirm (nano::unique_lock & auto const representatives_l (node.rep_crawler.representatives (std::numeric_limits::max ())); auto roots_size_l (roots.size ()); - auto & sorted_roots_l = roots.get<1> (); + auto & sorted_roots_l = roots.get (); size_t count_l{ 0 }; /* @@ -434,12 +434,12 @@ void nano::active_transactions::request_confirm (nano::unique_lock & // Erase inactive elections for (auto i (inactive_l.begin ()), n (inactive_l.end ()); i != n; ++i) { - auto root_it (roots.find (*i)); - if (root_it != roots.end ()) + auto root_it (roots.get ().find (*i)); + if (root_it != roots.get ().end ()) { root_it->election->clear_blocks (); root_it->election->clear_dependent (); - roots.erase (root_it); + roots.get ().erase (root_it); } } } @@ -485,13 +485,13 @@ void nano::active_transactions::prioritize_account_for_confirmation (nano::activ { auto num_uncemented = info_a.block_count - confirmation_height; nano::lock_guard guard (mutex); - auto it = cementable_frontiers_a.find (account_a); - if (it != cementable_frontiers_a.end ()) + auto it = cementable_frontiers_a.get ().find (account_a); + if (it != cementable_frontiers_a.get ().end ()) { if (it->blocks_uncemented != num_uncemented) { // Account already exists and there is now a different uncemented block count so update it in the container - cementable_frontiers_a.modify (it, [num_uncemented](nano::cementable_account & info) { + cementable_frontiers_a.get ().modify (it, [num_uncemented](nano::cementable_account & info) { info.blocks_uncemented = num_uncemented; }); } @@ -503,17 +503,17 @@ void nano::active_transactions::prioritize_account_for_confirmation (nano::activ { // The maximum amount of frontiers stored has been reached. Check if the current frontier // has more uncemented blocks than the lowest uncemented frontier in the collection if so replace it. - auto least_uncemented_frontier_it = cementable_frontiers_a.get<1> ().end (); + auto least_uncemented_frontier_it = cementable_frontiers_a.get ().end (); --least_uncemented_frontier_it; if (num_uncemented > least_uncemented_frontier_it->blocks_uncemented) { - cementable_frontiers_a.get<1> ().erase (least_uncemented_frontier_it); - cementable_frontiers_a.emplace (account_a, num_uncemented); + cementable_frontiers_a.get ().erase (least_uncemented_frontier_it); + cementable_frontiers_a.get ().emplace (account_a, num_uncemented); } } else { - cementable_frontiers_a.emplace (account_a, num_uncemented); + cementable_frontiers_a.get ().emplace (account_a, num_uncemented); } } cementable_frontiers_size_a = cementable_frontiers_a.size (); @@ -666,15 +666,15 @@ bool nano::active_transactions::add (std::shared_ptr block_a, bool if (!stopped) { auto root (block_a->qualified_root ()); - auto existing (roots.find (root)); - if (existing == roots.end () && confirmed_set.get<1> ().find (root) == confirmed_set.get<1> ().end ()) + auto existing (roots.get ().find (root)); + if (existing == roots.get ().end () && confirmed_set.get ().find (root) == confirmed_set.get ().end ()) { auto hash (block_a->hash ()); auto election (nano::make_shared (node, block_a, skip_delay_a, confirmation_action_a)); uint64_t difficulty (0); error = nano::work_validate (*block_a, &difficulty); release_assert (!error); - roots.insert (nano::conflict_info{ root, difficulty, difficulty, election }); + roots.get ().emplace (nano::conflict_info{ root, difficulty, difficulty, election }); blocks.insert (std::make_pair (hash, election)); adjust_difficulty (hash); election->insert_inactive_votes_cache (); @@ -714,8 +714,8 @@ bool nano::active_transactions::vote (std::shared_ptr vote_a, bool s else { auto block (boost::get> (vote_block)); - auto existing (roots.find (block->qualified_root ())); - if (existing != roots.end ()) + auto existing (roots.get ().find (block->qualified_root ())); + if (existing != roots.get ().end ()) { result = existing->election->vote (vote_a->account, vote_a->sequence, block->hash ()); } @@ -738,7 +738,7 @@ bool nano::active_transactions::vote (std::shared_ptr vote_a, bool s bool nano::active_transactions::active (nano::qualified_root const & root_a) { nano::lock_guard lock (mutex); - return roots.find (root_a) != roots.end (); + return roots.get ().find (root_a) != roots.get ().end (); } bool nano::active_transactions::active (nano::block const & block_a) @@ -749,8 +749,8 @@ bool nano::active_transactions::active (nano::block const & block_a) void nano::active_transactions::update_difficulty (std::shared_ptr block_a, boost::optional opt_transaction_a) { nano::unique_lock lock (mutex); - auto existing_election (roots.find (block_a->qualified_root ())); - if (existing_election != roots.end ()) + auto existing_election (roots.get ().find (block_a->qualified_root ())); + if (existing_election != roots.get ().end ()) { uint64_t difficulty; auto error (nano::work_validate (*block_a, &difficulty)); @@ -762,7 +762,7 @@ void nano::active_transactions::update_difficulty (std::shared_ptr { node.logger.try_log (boost::str (boost::format ("Block %1% was updated from difficulty %2% to %3%") % block_a->hash ().to_string () % nano::to_string_hex (existing_election->difficulty) % nano::to_string_hex (difficulty))); } - roots.modify (existing_election, [difficulty](nano::conflict_info & info_a) { + roots.get ().modify (existing_election, [difficulty](nano::conflict_info & info_a) { info_a.difficulty = difficulty; }); adjust_difficulty (block_a->hash ()); @@ -846,8 +846,8 @@ void nano::active_transactions::adjust_difficulty (nano::block_hash const & hash } processed_blocks.insert (hash); nano::qualified_root root (previous, existing->second->status.winner->root ()); - auto existing_root (roots.find (root)); - if (existing_root != roots.end ()) + auto existing_root (roots.get ().find (root)); + if (existing_root != roots.get ().end ()) { sum += nano::difficulty::to_multiplier (existing_root->difficulty, node.network_params.network.publish_threshold); elections_list.emplace_back (root, level); @@ -886,9 +886,9 @@ void nano::active_transactions::adjust_difficulty (nano::block_hash const & hash // Set adjusted difficulty for (auto & item : elections_list) { - auto existing_root (roots.find (item.first)); + auto existing_root (roots.get ().find (item.first)); uint64_t difficulty_a = average + item.second - limiter; - roots.modify (existing_root, [difficulty_a](nano::conflict_info & info_a) { + roots.get ().modify (existing_root, [difficulty_a](nano::conflict_info & info_a) { info_a.adjusted_difficulty = difficulty_a; }); } @@ -901,9 +901,9 @@ void nano::active_transactions::update_active_difficulty (nano::unique_lock (); + auto & sorted_roots = roots.get (); std::vector active_root_difficulties; - active_root_difficulties.reserve (std::min (roots.size (), node.config.active_elections_size)); + active_root_difficulties.reserve (std::min (sorted_roots.size (), node.config.active_elections_size)); size_t count (0); auto cutoff (std::chrono::steady_clock::now () - election_request_delay - 1s); for (auto it (sorted_roots.begin ()), end (sorted_roots.end ()); it != end && count++ < node.config.active_elections_size; ++it) @@ -948,9 +948,9 @@ std::deque> nano::active_transactions::list_blocks { lock = nano::unique_lock (mutex); } - for (auto i (roots.begin ()), n (roots.end ()); i != n; ++i) + for (auto & root : roots) { - result.push_back (i->election->status.winner); + result.push_back (root.election->status.winner); } return result; } @@ -964,13 +964,13 @@ std::deque nano::active_transactions::list_confirmed () void nano::active_transactions::add_confirmed (nano::election_status const & status_a, nano::qualified_root const & root_a) { confirmed.push_back (status_a); - auto inserted (confirmed_set.insert (nano::election_timepoint{ std::chrono::steady_clock::now (), root_a })); + auto inserted (confirmed_set.get ().push_back (root_a)); if (confirmed.size () > node.config.confirmation_history_size) { confirmed.pop_front (); if (inserted.second) { - confirmed_set.erase (confirmed_set.begin ()); + confirmed_set.get ().pop_front (); } } } @@ -978,13 +978,13 @@ void nano::active_transactions::add_confirmed (nano::election_status const & sta void nano::active_transactions::erase (nano::block const & block_a) { nano::lock_guard lock (mutex); - auto root_it (roots.find (block_a.qualified_root ())); - if (root_it != roots.end ()) + auto root_it (roots.get ().find (block_a.qualified_root ())); + if (root_it != roots.get ().end ()) { root_it->election->stop (); root_it->election->clear_blocks (); root_it->election->clear_dependent (); - roots.erase (root_it); + roots.get ().erase (root_it); node.logger.try_log (boost::str (boost::format ("Election erased for block block %1% root %2%") % block_a.hash ().to_string () % block_a.root ().to_string ())); } } @@ -1004,9 +1004,9 @@ size_t nano::active_transactions::size () bool nano::active_transactions::publish (std::shared_ptr block_a) { nano::lock_guard lock (mutex); - auto existing (roots.find (block_a->qualified_root ())); + auto existing (roots.get ().find (block_a->qualified_root ())); auto result (true); - if (existing != roots.end ()) + if (existing != roots.get ().end ()) { auto election (existing->election); result = election->publish (block_a); @@ -1077,11 +1077,11 @@ void nano::active_transactions::add_inactive_votes_cache (nano::block_hash const // Check principal representative status if (node.ledger.weight (representative_a) > node.minimum_principal_weight ()) { - auto existing (inactive_votes_cache.get<1> ().find (hash_a)); - if (existing != inactive_votes_cache.get<1> ().end () && !existing->confirmed) + auto existing (inactive_votes_cache.get ().find (hash_a)); + if (existing != inactive_votes_cache.get ().end () && !existing->confirmed) { auto is_new (false); - inactive_votes_cache.get<1> ().modify (existing, [representative_a, &is_new](nano::gap_information & info) { + inactive_votes_cache.get ().modify (existing, [representative_a, &is_new](nano::gap_information & info) { auto it = std::find (info.voters.begin (), info.voters.end (), representative_a); is_new = (it == info.voters.end ()); if (is_new) @@ -1095,7 +1095,7 @@ void nano::active_transactions::add_inactive_votes_cache (nano::block_hash const { if (node.gap_cache.bootstrap_check (existing->voters, hash_a)) { - inactive_votes_cache.get<1> ().modify (existing, [](nano::gap_information & info) { + inactive_votes_cache.get ().modify (existing, [](nano::gap_information & info) { info.confirmed = true; }); } @@ -1103,10 +1103,10 @@ void nano::active_transactions::add_inactive_votes_cache (nano::block_hash const } else { - inactive_votes_cache.insert ({ std::chrono::steady_clock::now (), hash_a, std::vector (1, representative_a) }); + inactive_votes_cache.get ().emplace (nano::gap_information{ std::chrono::steady_clock::now (), hash_a, std::vector (1, representative_a) }); if (inactive_votes_cache.size () > inactive_votes_cache_max) { - inactive_votes_cache.get<0> ().erase (inactive_votes_cache.get<0> ().begin ()); + inactive_votes_cache.get ().erase (inactive_votes_cache.get ().begin ()); } } } @@ -1114,8 +1114,8 @@ void nano::active_transactions::add_inactive_votes_cache (nano::block_hash const nano::gap_information nano::active_transactions::find_inactive_votes_cache (nano::block_hash const & hash_a) { - auto existing (inactive_votes_cache.get<1> ().find (hash_a)); - if (existing != inactive_votes_cache.get<1> ().end ()) + auto existing (inactive_votes_cache.get ().find (hash_a)); + if (existing != inactive_votes_cache.get ().end ()) { return *existing; } @@ -1134,18 +1134,18 @@ size_t nano::active_transactions::dropped_elections_cache_size () void nano::active_transactions::add_dropped_elections_cache (nano::qualified_root const & root_a) { assert (!mutex.try_lock ()); - dropped_elections_cache.insert (nano::election_timepoint{ std::chrono::steady_clock::now (), root_a }); + dropped_elections_cache.get ().emplace_back (nano::election_timepoint{ std::chrono::steady_clock::now (), root_a }); if (dropped_elections_cache.size () > dropped_elections_cache_max) { - dropped_elections_cache.get<0> ().erase (dropped_elections_cache.get<0> ().begin ()); + dropped_elections_cache.get ().pop_front (); } } std::chrono::steady_clock::time_point nano::active_transactions::find_dropped_elections_cache (nano::qualified_root const & root_a) { assert (!mutex.try_lock ()); - auto existing (dropped_elections_cache.get<1> ().find (root_a)); - if (existing != dropped_elections_cache.get<1> ().end ()) + auto existing (dropped_elections_cache.get ().find (root_a)); + if (existing != dropped_elections_cache.get ().end ()) { return existing->time; } diff --git a/nano/node/active_transactions.hpp b/nano/node/active_transactions.hpp index e39e68d2..41300293 100644 --- a/nano/node/active_transactions.hpp +++ b/nano/node/active_transactions.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -21,6 +22,8 @@ #include #include +namespace mi = boost::multi_index; + namespace nano { class node; @@ -80,6 +83,14 @@ public: // Holds all active blocks i.e. recently added blocks that need confirmation class active_transactions final { + // clang-format off + class tag_account {}; + class tag_difficulty {}; + class tag_root {}; + class tag_sequence {}; + class tag_uncemented {}; + // clang-format on + public: explicit active_transactions (nano::node &); ~active_transactions (); @@ -107,15 +118,16 @@ public: bool publish (std::shared_ptr block_a); boost::optional confirm_block (nano::transaction const &, std::shared_ptr); void post_confirmation_height_set (nano::transaction const & transaction_a, std::shared_ptr block_a, nano::block_sideband const & sideband_a, nano::election_status_type election_status_type_a); - boost::multi_index_container< - nano::conflict_info, - boost::multi_index::indexed_by< - boost::multi_index::hashed_unique< - boost::multi_index::member>, - boost::multi_index::ordered_non_unique< - boost::multi_index::member, - std::greater>>> + // clang-format off + boost::multi_index_container, + mi::member>, + mi::ordered_non_unique, + mi::member, + std::greater>>> roots; + // clang-format on std::unordered_map> blocks; std::deque list_confirmed (); std::deque confirmed; @@ -164,37 +176,40 @@ private: bool started{ false }; std::atomic stopped{ false }; unsigned ongoing_broadcasts{ 0 }; - using ordered_elections_timepoint = boost::multi_index_container< - nano::election_timepoint, - boost::multi_index::indexed_by< - boost::multi_index::ordered_non_unique>, - boost::multi_index::hashed_unique>>>; - ordered_elections_timepoint confirmed_set; - void prioritize_frontiers_for_confirmation (nano::transaction const &, std::chrono::milliseconds, std::chrono::milliseconds); - using prioritize_num_uncemented = boost::multi_index_container< - nano::cementable_account, - boost::multi_index::indexed_by< - boost::multi_index::hashed_unique< - boost::multi_index::member>, - boost::multi_index::ordered_non_unique< - boost::multi_index::member, - std::greater>>>; + // clang-format off + boost::multi_index_container>, + mi::hashed_unique, + mi::identity>>> + confirmed_set; + using prioritize_num_uncemented = boost::multi_index_container, + mi::member>, + mi::ordered_non_unique, + mi::member, + std::greater>>>; + // clang-format on prioritize_num_uncemented priority_wallet_cementable_frontiers; prioritize_num_uncemented priority_cementable_frontiers; + void prioritize_frontiers_for_confirmation (nano::transaction const &, std::chrono::milliseconds, std::chrono::milliseconds); std::unordered_set wallet_ids_already_iterated; std::unordered_map next_wallet_id_accounts; bool skip_wallets{ false }; void prioritize_account_for_confirmation (prioritize_num_uncemented &, size_t &, nano::account const &, nano::account_info const &, uint64_t); static size_t constexpr max_priority_cementable_frontiers{ 100000 }; static size_t constexpr confirmed_frontiers_max_pending_cut_off{ 1000 }; - boost::multi_index_container< - nano::gap_information, - boost::multi_index::indexed_by< - boost::multi_index::ordered_non_unique>, - boost::multi_index::hashed_unique>>> - inactive_votes_cache; + nano::gap_cache::ordered_gaps inactive_votes_cache; static size_t constexpr inactive_votes_cache_max{ 16 * 1024 }; - ordered_elections_timepoint dropped_elections_cache; + // clang-format off + boost::multi_index_container>, + mi::hashed_unique, + mi::member>>> + dropped_elections_cache; + // clang-format on static size_t constexpr dropped_elections_cache_max{ 32 * 1024 }; boost::thread thread; diff --git a/nano/node/blockprocessor.cpp b/nano/node/blockprocessor.cpp index 25ee74ed..3da55904 100644 --- a/nano/node/blockprocessor.cpp +++ b/nano/node/blockprocessor.cpp @@ -75,7 +75,7 @@ void nano::block_processor::add (nano::unchecked_info const & info_a) auto hash (info_a.block->hash ()); auto filter_hash (filter_item (hash, info_a.block->block_signature ())); nano::lock_guard lock (mutex); - if (blocks_filter.find (filter_hash) == blocks_filter.end () && rolled_back.get<1> ().find (hash) == rolled_back.get<1> ().end ()) + if (blocks_filter.find (filter_hash) == blocks_filter.end () && rolled_back.get ().find (hash) == rolled_back.get ().end ()) { if (info_a.verified == nano::signature_verification::unknown && (info_a.block->type () == nano::block_type::state || info_a.block->type () == nano::block_type::open || !info_a.account.is_zero ())) { @@ -328,15 +328,15 @@ void nano::block_processor::process_batch (nano::unique_lock & lock_ } lock_a.lock (); // Prevent rolled back blocks second insertion - auto inserted (rolled_back.insert (nano::rolled_hash{ std::chrono::steady_clock::now (), successor->hash () })); + auto inserted (rolled_back.get ().push_back (successor->hash ())); if (inserted.second) { // Possible election winner change - rolled_back.get<1> ().erase (hash); + rolled_back.get ().erase (hash); // Prevent overflow if (rolled_back.size () > rolled_back_max) { - rolled_back.erase (rolled_back.begin ()); + rolled_back.get ().pop_front (); } } lock_a.unlock (); diff --git a/nano/node/blockprocessor.hpp b/nano/node/blockprocessor.hpp index 1c2b0fe1..ecb3d773 100644 --- a/nano/node/blockprocessor.hpp +++ b/nano/node/blockprocessor.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -20,12 +21,6 @@ class transaction; class write_transaction; class write_database_queue; -class rolled_hash -{ -public: - std::chrono::steady_clock::time_point time; - nano::block_hash hash; -}; /** * Processing blocks is a potentially long IO operation. * This class isolates block insertion from other operations like servicing network operations @@ -68,12 +63,16 @@ private: std::deque> forced; nano::block_hash filter_item (nano::block_hash const &, nano::signature const &); std::unordered_set blocks_filter; - boost::multi_index_container< - nano::rolled_hash, + // clang-format off + class tag_sequence {}; + class tag_hash {}; + boost::multi_index_container>, - boost::multi_index::hashed_unique>>> + boost::multi_index::sequenced>, + boost::multi_index::hashed_unique, + boost::multi_index::identity>>> rolled_back; + // clang-format on static size_t const rolled_back_max = 1024; nano::condition_variable condition; nano::node & node; diff --git a/nano/node/bootstrap/bootstrap.cpp b/nano/node/bootstrap/bootstrap.cpp index eb77b9e3..4307eadb 100644 --- a/nano/node/bootstrap/bootstrap.cpp +++ b/nano/node/bootstrap/bootstrap.cpp @@ -1209,7 +1209,7 @@ void nano::bootstrap_attempt::lazy_destinations_increment (nano::account const & } else { - lazy_destinations.insert (nano::lazy_destinations_item{ destination_a, 1 }); + lazy_destinations.emplace (nano::lazy_destinations_item{ destination_a, 1 }); } } } @@ -1547,7 +1547,7 @@ void nano::pulls_cache::add (nano::pull_info const & pull_a) if (existing == cache.get ().end ()) { // Insert new pull - auto inserted (cache.insert (nano::cached_pulls{ std::chrono::steady_clock::now (), head_512, pull_a.head })); + auto inserted (cache.emplace (nano::cached_pulls{ std::chrono::steady_clock::now (), head_512, pull_a.head })); (void)inserted; assert (inserted.second); } @@ -1594,7 +1594,7 @@ uint64_t nano::bootstrap_excluded_peers::add (nano::tcp_endpoint const & endpoin if (existing == peers.get ().end ()) { // Insert new endpoint - auto inserted (peers.insert (nano::excluded_peers_item{ std::chrono::steady_clock::steady_clock::now () + exclude_time_hours, endpoint_a, 1 })); + auto inserted (peers.emplace (nano::excluded_peers_item{ std::chrono::steady_clock::steady_clock::now () + exclude_time_hours, endpoint_a, 1 })); (void)inserted; assert (inserted.second); result = 1; diff --git a/nano/node/bootstrap/bootstrap.hpp b/nano/node/bootstrap/bootstrap.hpp index 2af597ee..089826cf 100644 --- a/nano/node/bootstrap/bootstrap.hpp +++ b/nano/node/bootstrap/bootstrap.hpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include @@ -18,6 +17,8 @@ #include #include +namespace mi = boost::multi_index; + namespace nano { class bootstrap_attempt; @@ -146,12 +147,16 @@ public: class count_tag { }; - boost::multi_index_container< - lazy_destinations_item, - boost::multi_index::indexed_by< - boost::multi_index::ordered_non_unique, boost::multi_index::member, std::greater>, - boost::multi_index::hashed_unique, boost::multi_index::member>>> + // clang-format off + boost::multi_index_container, + mi::member, + std::greater>, + mi::hashed_unique, + mi::member>>> lazy_destinations; + // clang-format on std::atomic lazy_blocks_count{ 0 }; std::atomic lazy_destinations_flushed{ false }; std::mutex lazy_mutex; @@ -194,12 +199,15 @@ public: class account_head_tag { }; - boost::multi_index_container< - nano::cached_pulls, - boost::multi_index::indexed_by< - boost::multi_index::ordered_non_unique>, - boost::multi_index::hashed_unique, boost::multi_index::member>>> + // clang-format off + boost::multi_index_container>, + mi::hashed_unique, + mi::member>>> cache; + // clang-format on constexpr static size_t cache_size_max = 10000; }; class excluded_peers_item final @@ -219,12 +227,15 @@ public: class endpoint_tag { }; - boost::multi_index_container< - nano::excluded_peers_item, - boost::multi_index::indexed_by< - boost::multi_index::ordered_non_unique>, - boost::multi_index::hashed_unique, boost::multi_index::member>>> + // clang-format off + boost::multi_index_container>, + mi::hashed_unique, + mi::member>>> peers; + // clang-format on constexpr static size_t excluded_peers_size_max = 5000; constexpr static double excluded_peers_percentage_limit = 0.5; constexpr static uint64_t score_limit = 2; diff --git a/nano/node/gap_cache.cpp b/nano/node/gap_cache.cpp index 55352bf3..25565968 100644 --- a/nano/node/gap_cache.cpp +++ b/nano/node/gap_cache.cpp @@ -12,19 +12,19 @@ node (node_a) void nano::gap_cache::add (nano::block_hash const & hash_a, std::chrono::steady_clock::time_point time_point_a) { nano::lock_guard lock (mutex); - auto existing (blocks.get<1> ().find (hash_a)); - if (existing != blocks.get<1> ().end ()) + auto existing (blocks.get ().find (hash_a)); + if (existing != blocks.get ().end ()) { - blocks.get<1> ().modify (existing, [time_point_a](nano::gap_information & info) { + blocks.get ().modify (existing, [time_point_a](nano::gap_information & info) { info.arrival = time_point_a; }); } else { - blocks.insert ({ time_point_a, hash_a, std::vector () }); - if (blocks.size () > max) + blocks.get ().emplace (nano::gap_information{ time_point_a, hash_a, std::vector () }); + if (blocks.get ().size () > max) { - blocks.get<0> ().erase (blocks.get<0> ().begin ()); + blocks.get ().erase (blocks.get ().begin ()); } } } @@ -32,7 +32,7 @@ void nano::gap_cache::add (nano::block_hash const & hash_a, std::chrono::steady_ void nano::gap_cache::erase (nano::block_hash const & hash_a) { nano::lock_guard lock (mutex); - blocks.get<1> ().erase (hash_a); + blocks.get ().erase (hash_a); } void nano::gap_cache::vote (std::shared_ptr vote_a) @@ -40,11 +40,11 @@ void nano::gap_cache::vote (std::shared_ptr vote_a) nano::lock_guard lock (mutex); for (auto hash : *vote_a) { - auto existing (blocks.get<1> ().find (hash)); - if (existing != blocks.get<1> ().end () && !existing->confirmed) + auto existing (blocks.get ().find (hash)); + if (existing != blocks.get ().end () && !existing->confirmed) { auto is_new (false); - blocks.get<1> ().modify (existing, [&is_new, &vote_a](nano::gap_information & info) { + blocks.get ().modify (existing, [&is_new, &vote_a](nano::gap_information & info) { auto it = std::find (info.voters.begin (), info.voters.end (), vote_a->account); is_new = (it == info.voters.end ()); if (is_new) @@ -57,7 +57,7 @@ void nano::gap_cache::vote (std::shared_ptr vote_a) { if (bootstrap_check (existing->voters, hash)) { - blocks.get<1> ().modify (existing, [](nano::gap_information & info) { + blocks.get ().modify (existing, [](nano::gap_information & info) { info.confirmed = true; }); } diff --git a/nano/node/gap_cache.hpp b/nano/node/gap_cache.hpp index b5d068e4..9599aa67 100644 --- a/nano/node/gap_cache.hpp +++ b/nano/node/gap_cache.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -40,12 +41,17 @@ public: bool bootstrap_check (std::vector const &, nano::block_hash const &); nano::uint128_t bootstrap_threshold (); size_t size (); - boost::multi_index_container< - nano::gap_information, + // clang-format off + class tag_arrival {}; + class tag_hash {}; + using ordered_gaps = boost::multi_index_container>, - boost::multi_index::hashed_unique>>> - blocks; + boost::multi_index::ordered_non_unique, + boost::multi_index::member>, + boost::multi_index::hashed_unique, + boost::multi_index::member>>>; + ordered_gaps blocks; + // clang-format on size_t const max = 256; std::mutex mutex; nano::node & node; diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index e1ff088b..527b2abb 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -2063,12 +2063,16 @@ void epoch_upgrader (std::shared_ptr node_a, nano::private_key const class modified_tag { }; - boost::multi_index_container< - account_upgrade_item, + // clang-format off + boost::multi_index_container, boost::multi_index::member, std::greater>, - boost::multi_index::hashed_unique, boost::multi_index::member>>> + boost::multi_index::ordered_non_unique, + boost::multi_index::member, + std::greater>, + boost::multi_index::hashed_unique, + boost::multi_index::member>>> accounts_list; + // clang-format on bool finished_upgrade (false); @@ -2088,7 +2092,7 @@ void epoch_upgrader (std::shared_ptr node_a, nano::private_key const if (info.epoch () < epoch_a) { release_assert (nano::epochs::is_sequential (info.epoch (), epoch_a)); - accounts_list.insert (account_upgrade_item{ account, info.modified }); + accounts_list.emplace (account_upgrade_item{ account, info.modified }); } } } diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 762e9d8f..4a3c39c4 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -1241,7 +1241,7 @@ bool nano::block_arrival::add (nano::block_hash const & hash_a) { nano::lock_guard lock (mutex); auto now (std::chrono::steady_clock::now ()); - auto inserted (arrival.insert (nano::block_arrival_info{ now, hash_a })); + auto inserted (arrival.get ().emplace_back (nano::block_arrival_info{ now, hash_a })); auto result (!inserted.second); return result; } @@ -1250,11 +1250,11 @@ bool nano::block_arrival::recent (nano::block_hash const & hash_a) { nano::lock_guard lock (mutex); auto now (std::chrono::steady_clock::now ()); - while (arrival.size () > arrival_size_min && arrival.begin ()->arrival + arrival_time_min < now) + while (arrival.size () > arrival_size_min && arrival.get ().front ().arrival + arrival_time_min < now) { - arrival.erase (arrival.begin ()); + arrival.get ().pop_front (); } - return arrival.get<1> ().find (hash_a) != arrival.get<1> ().end (); + return arrival.get ().find (hash_a) != arrival.get ().end (); } namespace nano diff --git a/nano/node/node.hpp b/nano/node/node.hpp index a6bb9cf2..64510760 100644 --- a/nano/node/node.hpp +++ b/nano/node/node.hpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -61,12 +62,16 @@ public: // Return `true' to indicated an error if the block has already been inserted bool add (nano::block_hash const &); bool recent (nano::block_hash const &); - boost::multi_index_container< - nano::block_arrival_info, - boost::multi_index::indexed_by< - boost::multi_index::ordered_non_unique>, - boost::multi_index::hashed_unique>>> + // clang-format off + class tag_sequence {}; + class tag_hash {}; + boost::multi_index_container>, + boost::multi_index::hashed_unique, + boost::multi_index::member>>> arrival; + // clang-format on std::mutex mutex; static size_t constexpr arrival_size_min = 8 * 1024; static std::chrono::seconds constexpr arrival_time_min = std::chrono::seconds (300); diff --git a/nano/node/repcrawler.cpp b/nano/node/repcrawler.cpp index 0e34ebda..da1a2285 100644 --- a/nano/node/repcrawler.cpp +++ b/nano/node/repcrawler.cpp @@ -141,7 +141,7 @@ bool nano::rep_crawler::response (std::shared_ptr chan } else { - probable_reps.insert (nano::representative (rep_account_a, weight_a, channel_a)); + probable_reps.emplace (nano::representative (rep_account_a, weight_a, channel_a)); updated_or_inserted = true; } return updated_or_inserted; diff --git a/nano/node/transport/tcp.hpp b/nano/node/transport/tcp.hpp index 3b42ce9f..db5ec655 100644 --- a/nano/node/transport/tcp.hpp +++ b/nano/node/transport/tcp.hpp @@ -9,9 +9,10 @@ #include #include #include - #include +namespace mi = boost::multi_index; + namespace nano { class bootstrap_server; @@ -165,22 +166,29 @@ namespace transport std::chrono::steady_clock::time_point last_attempt; }; mutable std::mutex mutex; - boost::multi_index_container< - channel_tcp_wrapper, - boost::multi_index::indexed_by< - boost::multi_index::random_access>, - boost::multi_index::ordered_non_unique, boost::multi_index::const_mem_fun>, - boost::multi_index::hashed_unique, boost::multi_index::const_mem_fun>, - boost::multi_index::hashed_non_unique, boost::multi_index::const_mem_fun>, - boost::multi_index::ordered_non_unique, boost::multi_index::const_mem_fun>, - boost::multi_index::ordered_non_unique, boost::multi_index::const_mem_fun>>> + // clang-format off + boost::multi_index_container>, + mi::ordered_non_unique, + mi::const_mem_fun>, + mi::hashed_unique, + mi::const_mem_fun>, + mi::hashed_non_unique, + mi::const_mem_fun>, + mi::ordered_non_unique, + mi::const_mem_fun>, + mi::hashed_non_unique, + mi::const_mem_fun>>> channels; - boost::multi_index_container< - tcp_endpoint_attempt, - boost::multi_index::indexed_by< - boost::multi_index::hashed_unique>, - boost::multi_index::ordered_non_unique>>> + boost::multi_index_container>, + mi::ordered_non_unique< + mi::member>>> attempts; + // clang-format on // This owns the sockets until the node_id_handshake has been completed. Needed to prevent self referencing callbacks, they are periodically removed if any are dangling. std::vector> node_id_handshake_sockets; std::atomic stopped{ false }; diff --git a/nano/node/transport/udp.hpp b/nano/node/transport/udp.hpp index d399a791..90cba168 100644 --- a/nano/node/transport/udp.hpp +++ b/nano/node/transport/udp.hpp @@ -13,6 +13,8 @@ #include #include +namespace mi = boost::multi_index; + namespace nano { class message_buffer; @@ -144,22 +146,31 @@ namespace transport std::chrono::steady_clock::time_point last_attempt; }; mutable std::mutex mutex; + // clang-format off boost::multi_index_container< channel_udp_wrapper, - boost::multi_index::indexed_by< - boost::multi_index::random_access>, - boost::multi_index::ordered_non_unique, boost::multi_index::const_mem_fun>, - boost::multi_index::hashed_unique, boost::multi_index::const_mem_fun>, - boost::multi_index::hashed_non_unique, boost::multi_index::const_mem_fun>, - boost::multi_index::ordered_non_unique, boost::multi_index::const_mem_fun>, - boost::multi_index::ordered_non_unique, boost::multi_index::const_mem_fun>>> + mi::indexed_by< + mi::random_access>, + mi::ordered_non_unique, + mi::const_mem_fun>, + mi::hashed_unique, + mi::const_mem_fun>, + mi::hashed_non_unique, + mi::const_mem_fun>, + mi::ordered_non_unique, + mi::const_mem_fun>, + mi::hashed_non_unique, + mi::const_mem_fun>>> channels; boost::multi_index_container< endpoint_attempt, - boost::multi_index::indexed_by< - boost::multi_index::hashed_unique>, - boost::multi_index::ordered_non_unique>>> + mi::indexed_by< + mi::hashed_unique< + mi::member>, + mi::ordered_non_unique< + mi::member>>> attempts; + // clang-format on boost::asio::strand strand; boost::asio::ip::udp::socket socket; nano::endpoint local_endpoint; diff --git a/nano/node/voting.cpp b/nano/node/voting.cpp index 8465f49d..6b989795 100644 --- a/nano/node/voting.cpp +++ b/nano/node/voting.cpp @@ -106,23 +106,23 @@ void nano::votes_cache::add (std::shared_ptr const & vote_a) for (auto & block : vote_a->blocks) { auto hash (boost::get (block)); - auto existing (cache.get<1> ().find (hash)); - if (existing == cache.get<1> ().end ()) + auto existing (cache.get ().find (hash)); + if (existing == cache.get ().end ()) { // Clean old votes if (cache.size () >= network_params.voting.max_cache) { - cache.erase (cache.begin ()); + cache.get ().pop_front (); } // Insert new votes (new hash) - auto inserted (cache.insert (nano::cached_votes{ std::chrono::steady_clock::now (), hash, std::vector> (1, vote_a) })); + auto inserted (cache.get ().emplace_back (nano::cached_votes{ hash, std::vector> (1, vote_a) })); (void)inserted; assert (inserted.second); } else { // Insert new votes (old hash) - cache.get<1> ().modify (existing, [vote_a](nano::cached_votes & cache_a) { + cache.get ().modify (existing, [vote_a](nano::cached_votes & cache_a) { // Replace old vote for same representative & hash bool replaced (false); for (auto i (cache_a.votes.begin ()), n (cache_a.votes.end ()); i != n && !replaced; ++i) @@ -147,8 +147,8 @@ std::vector> nano::votes_cache::find (nano::block_ha { std::vector> result; nano::lock_guard lock (cache_mutex); - auto existing (cache.get<1> ().find (hash_a)); - if (existing != cache.get<1> ().end ()) + auto existing (cache.get ().find (hash_a)); + if (existing != cache.get ().end ()) { result = existing->votes; } @@ -158,7 +158,7 @@ std::vector> nano::votes_cache::find (nano::block_ha void nano::votes_cache::remove (nano::block_hash const & hash_a) { nano::lock_guard lock (cache_mutex); - cache.get<1> ().erase (hash_a); + cache.get ().erase (hash_a); } namespace nano diff --git a/nano/node/voting.hpp b/nano/node/voting.hpp index 66edd70b..70aad2d3 100644 --- a/nano/node/voting.hpp +++ b/nano/node/voting.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -55,7 +56,6 @@ std::unique_ptr collect_seq_con_info (vote_generator & v class cached_votes final { public: - std::chrono::steady_clock::time_point time; nano::block_hash hash; std::vector> votes; }; @@ -68,12 +68,16 @@ public: private: std::mutex cache_mutex; - boost::multi_index_container< - nano::cached_votes, + // clang-format off + class tag_sequence {}; + class tag_hash {}; + boost::multi_index_container>, - boost::multi_index::hashed_unique>>> + boost::multi_index::sequenced>, + boost::multi_index::hashed_unique, + boost::multi_index::member>>> cache; + // clang-format on nano::network_params network_params; friend std::unique_ptr collect_seq_con_info (votes_cache & votes_cache, const std::string & name); };