From 5cce839f5bbb849881705fcfd5cc0538aedf7339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Thu, 29 May 2025 20:17:31 +0200 Subject: [PATCH] Keep `rep_weights` container directly in ledger class --- nano/core_test/block_store.cpp | 14 ++------ nano/core_test/ledger.cpp | 25 +++++-------- nano/core_test/ledger_confirm.cpp | 2 -- nano/core_test/node.cpp | 3 +- nano/core_test/processor_service.cpp | 2 -- nano/core_test/vote_processor.cpp | 2 +- nano/nano_node/entry.cpp | 4 +-- nano/node/election.cpp | 2 +- nano/node/json_handler.cpp | 4 +-- nano/node/node.cpp | 14 -------- nano/qt_test/qt.cpp | 2 -- nano/secure/CMakeLists.txt | 2 -- nano/secure/fwd.hpp | 1 + nano/secure/ledger.cpp | 54 +++++++++++++++++----------- nano/secure/ledger.hpp | 29 ++++++++++----- nano/secure/ledger_cache.cpp | 6 ---- nano/secure/ledger_cache.hpp | 36 ------------------- nano/slow_test/node.cpp | 2 -- nano/store/component.cpp | 30 +++++++++------- nano/store/component.hpp | 17 ++++----- nano/test_common/ledger_context.cpp | 2 -- nano/test_common/ledger_context.hpp | 4 ++- 22 files changed, 103 insertions(+), 154 deletions(-) delete mode 100644 nano/secure/ledger_cache.cpp delete mode 100644 nano/secure/ledger_cache.hpp diff --git a/nano/core_test/block_store.cpp b/nano/core_test/block_store.cpp index 5bc4f8a50..32e65351e 100644 --- a/nano/core_test/block_store.cpp +++ b/nano/core_test/block_store.cpp @@ -383,9 +383,8 @@ TEST (block_store, genesis) nano::logger logger; auto store = nano::make_store (logger, nano::unique_path (), nano::dev::constants); ASSERT_TRUE (!store->init_error ()); - nano::ledger_cache ledger_cache{ store->rep_weight }; auto transaction (store->tx_begin_write ()); - store->initialize (transaction, ledger_cache, nano::dev::constants); + store->initialize (transaction, nano::dev::constants); nano::account_info info; ASSERT_FALSE (store->account.get (transaction, nano::dev::genesis_key.pub, info)); ASSERT_EQ (nano::dev::genesis->hash (), info.head); @@ -635,7 +634,6 @@ TEST (mdb_block_store, supported_version_upgrades) nano::stats stats{ logger }; nano::ledger ledger (store, nano::dev::constants, stats, logger); auto transaction (store.tx_begin_write ()); - store.initialize (transaction, ledger.cache, nano::dev::constants); // Lower the database to the max version unsupported for upgrades store.version.put (transaction, store.version_minimum - 1); } @@ -653,7 +651,6 @@ TEST (mdb_block_store, supported_version_upgrades) nano::stats stats{ logger }; nano::ledger ledger (store, nano::dev::constants, stats, logger); auto transaction (store.tx_begin_write ()); - store.initialize (transaction, ledger.cache, nano::dev::constants); // Lower the database version to the minimum version supported for upgrade. store.version.put (transaction, store.version_minimum); } @@ -897,10 +894,8 @@ TEST (block_store, cemented_count_cache) nano::logger logger; auto store = nano::make_store (logger, nano::unique_path (), nano::dev::constants); ASSERT_TRUE (!store->init_error ()); - auto transaction (store->tx_begin_write ()); nano::stats stats{ logger }; nano::ledger ledger (*store, nano::dev::constants, stats, logger); - store->initialize (transaction, ledger.cache, nano::dev::constants); ASSERT_EQ (1, ledger.cemented_count ()); } @@ -921,9 +916,8 @@ TEST (block_store, pruned_random) block->sideband_set ({}); auto hash1 (block->hash ()); { - nano::ledger_cache ledger_cache{ store->rep_weight }; auto transaction (store->tx_begin_write ()); - store->initialize (transaction, ledger_cache, nano::dev::constants); + store->initialize (transaction, nano::dev::constants); store->pruned.put (transaction, hash1); } auto transaction (store->tx_begin_read ()); @@ -951,9 +945,8 @@ TEST (block_store, state_block) block1->sideband_set ({}); { - nano::ledger_cache ledger_cache{ store->rep_weight }; auto transaction (store->tx_begin_write ()); - store->initialize (transaction, ledger_cache, nano::dev::constants); + store->initialize (transaction, nano::dev::constants); ASSERT_EQ (nano::block_type::state, block1->type ()); store->block.put (transaction, block1->hash (), *block1); ASSERT_TRUE (store->block.exists (transaction, block1->hash ())); @@ -990,7 +983,6 @@ TEST (mdb_block_store, sideband_height) nano::ledger ledger (store, nano::dev::constants, stats, logger); nano::block_builder builder; auto transaction = ledger.tx_begin_write (); - store.initialize (transaction, ledger.cache, nano::dev::constants); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; auto send = builder .send () diff --git a/nano/core_test/ledger.cpp b/nano/core_test/ledger.cpp index c932e27c5..7c515f57b 100644 --- a/nano/core_test/ledger.cpp +++ b/nano/core_test/ledger.cpp @@ -738,7 +738,7 @@ TEST (ledger, representation) auto ctx = nano::test::ledger_empty (); auto & ledger = ctx.ledger (); auto & store = ctx.store (); - auto & rep_weights = ledger.cache.rep_weights; + auto & rep_weights = ledger.rep_weights; auto transaction = ledger.tx_begin_write (); auto & pool = ctx.pool (); ASSERT_EQ (nano::dev::constants.genesis_amount, rep_weights.representation_get (nano::dev::genesis_key.pub)); @@ -873,7 +873,6 @@ TEST (ledger, double_open) nano::stats stats{ logger }; nano::ledger ledger (*store, nano::dev::constants, stats, logger); auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::keypair key2; nano::block_builder builder; @@ -4634,8 +4633,8 @@ TEST (ledger, zero_rep) .build (); auto transaction = node1.ledger.tx_begin_write (); ASSERT_EQ (nano::block_status::progress, node1.ledger.process (transaction, block1)); - ASSERT_EQ (0, node1.ledger.cache.rep_weights.representation_get (nano::dev::genesis_key.pub)); - ASSERT_EQ (nano::dev::constants.genesis_amount, node1.ledger.cache.rep_weights.representation_get (0)); + ASSERT_EQ (0, node1.ledger.rep_weights.representation_get (nano::dev::genesis_key.pub)); + ASSERT_EQ (nano::dev::constants.genesis_amount, node1.ledger.rep_weights.representation_get (0)); auto block2 = builder.state () .account (nano::dev::genesis_key.pub) .previous (block1->hash ()) @@ -4646,8 +4645,8 @@ TEST (ledger, zero_rep) .work (*system.work.generate (block1->hash ())) .build (); ASSERT_EQ (nano::block_status::progress, node1.ledger.process (transaction, block2)); - ASSERT_EQ (nano::dev::constants.genesis_amount, node1.ledger.cache.rep_weights.representation_get (nano::dev::genesis_key.pub)); - ASSERT_EQ (0, node1.ledger.cache.rep_weights.representation_get (0)); + ASSERT_EQ (nano::dev::constants.genesis_amount, node1.ledger.rep_weights.representation_get (nano::dev::genesis_key.pub)); + ASSERT_EQ (0, node1.ledger.rep_weights.representation_get (0)); } TEST (ledger, work_validation) @@ -4810,7 +4809,6 @@ TEST (ledger, dependents_confirmed_pruning) nano::ledger ledger (*store, nano::dev::constants, stats, logger); ledger.pruning = true; auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); nano::block_builder builder; nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::keypair key1; @@ -4901,10 +4899,13 @@ TEST (ledger, cache) ASSERT_EQ (account_count, ledger.account_count ()); ASSERT_EQ (block_count, ledger.block_count ()); ASSERT_EQ (cemented_count, ledger.cemented_count ()); - ASSERT_EQ (genesis_weight, ledger.cache.rep_weights.representation_get (nano::dev::genesis_key.pub)); + ASSERT_EQ (genesis_weight, ledger.rep_weights.representation_get (nano::dev::genesis_key.pub)); ASSERT_EQ (pruned_count, ledger.pruned_count ()); }; + cache_check (ledger); + cache_check (nano::ledger (store, nano::dev::constants, stats, logger)); + nano::keypair key; auto const latest = ledger.any.account_head (ledger.tx_begin_read (), nano::dev::genesis_key.pub); auto send = builder.state () @@ -4984,7 +4985,6 @@ TEST (ledger, pruning_action) nano::ledger ledger (*store, nano::dev::constants, stats, logger); ledger.pruning = true; auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::block_builder builder; auto send1 = builder @@ -5070,7 +5070,6 @@ TEST (ledger, pruning_large_chain) nano::ledger ledger (*store, nano::dev::constants, stats, logger); ledger.pruning = true; auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; size_t send_receive_pairs (20); auto last_hash (nano::dev::genesis->hash ()); @@ -5126,7 +5125,6 @@ TEST (ledger, pruning_source_rollback) nano::ledger ledger (*store, nano::dev::constants, stats, logger); ledger.pruning = true; auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::block_builder builder; auto epoch1 = builder @@ -5215,7 +5213,6 @@ TEST (ledger, pruning_source_rollback_legacy) nano::ledger ledger (*store, nano::dev::constants, stats, logger); ledger.pruning = true; auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::block_builder builder; auto send1 = builder @@ -5330,7 +5327,6 @@ TEST (ledger, pruning_legacy_blocks) ledger.pruning = true; nano::keypair key1; auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::block_builder builder; auto send1 = builder @@ -5416,7 +5412,6 @@ TEST (ledger, pruning_safe_functions) nano::ledger ledger (*store, nano::dev::constants, stats, logger); ledger.pruning = true; auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::block_builder builder; auto send1 = builder @@ -5468,7 +5463,6 @@ TEST (ledger, random_blocks) nano::ledger ledger (*store, nano::dev::constants, stats, logger); ledger.pruning = true; auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::block_builder builder; auto send1 = builder @@ -5571,7 +5565,6 @@ TEST (ledger, migrate_lmdb_to_rocksdb) { auto transaction = ledger.tx_begin_write (); - store.initialize (transaction, ledger.cache, ledger.constants); ASSERT_FALSE (store.init_error ()); // Lower the database to the max version unsupported for upgrades diff --git a/nano/core_test/ledger_confirm.cpp b/nano/core_test/ledger_confirm.cpp index 7cb489250..0397c4e14 100644 --- a/nano/core_test/ledger_confirm.cpp +++ b/nano/core_test/ledger_confirm.cpp @@ -817,7 +817,6 @@ TEST (ledger_confirm, pruned_source) .work (*pool.generate (key2.pub)) .build (); auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, nano::dev::constants); ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send1)); ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, open1)); ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send2)); @@ -859,7 +858,6 @@ TEST (ledger_confirmDeathTest, rollback_added_block) .work (*pool.generate (nano::dev::genesis->hash ())) .build (); auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); ASSERT_DEATH_IF_SUPPORTED (ledger.confirm (transaction, send->hash ()), ""); } } diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index 6dad6dda6..82efcb26c 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -2572,9 +2572,8 @@ TEST (node, dont_write_lock_node) nano::logger logger; auto store = nano::make_store (logger, path, nano::dev::constants, false, true); { - nano::ledger_cache ledger_cache{ store->rep_weight }; auto transaction (store->tx_begin_write ()); - store->initialize (transaction, ledger_cache, nano::dev::constants); + store->initialize (transaction, nano::dev::constants); } // Hold write lock open until main thread is done needing it diff --git a/nano/core_test/processor_service.cpp b/nano/core_test/processor_service.cpp index 44796a949..c788e6048 100644 --- a/nano/core_test/processor_service.cpp +++ b/nano/core_test/processor_service.cpp @@ -20,7 +20,6 @@ TEST (processor_service, bad_send_signature) ASSERT_FALSE (store->init_error ()); nano::ledger ledger (*store, nano::dev::constants, system.stats, system.logger); auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; auto info1 = ledger.any.account_get (transaction, nano::dev::genesis_key.pub); ASSERT_TRUE (info1); @@ -46,7 +45,6 @@ TEST (processor_service, bad_receive_signature) ASSERT_FALSE (store->init_error ()); nano::ledger ledger (*store, nano::dev::constants, system.stats, system.logger); auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; auto info1 = ledger.any.account_get (transaction, nano::dev::genesis_key.pub); ASSERT_TRUE (info1); diff --git a/nano/core_test/vote_processor.cpp b/nano/core_test/vote_processor.cpp index 144a965d2..55e9ce043 100644 --- a/nano/core_test/vote_processor.cpp +++ b/nano/core_test/vote_processor.cpp @@ -135,7 +135,7 @@ TEST (vote_processor, weights) system.wallet (0)->send_sync (nano::dev::genesis_key.pub, key2.pub, level2); // Wait for representatives - ASSERT_TIMELY_EQ (10s, node.ledger.cache.rep_weights.get_rep_amounts ().size (), 4); + ASSERT_TIMELY_EQ (10s, node.ledger.rep_weights.get_rep_amounts ().size (), 4); // Wait for rep tiers to be updated node.stats.clear (); diff --git a/nano/nano_node/entry.cpp b/nano/nano_node/entry.cpp index c7b69df61..3378009c2 100644 --- a/nano/nano_node/entry.cpp +++ b/nano/nano_node/entry.cpp @@ -217,7 +217,7 @@ int main (int argc, char * const * argv) auto const bootstrap_weights = node->get_bootstrap_weights (); auto const & hardcoded = bootstrap_weights.second; auto const hardcoded_height = bootstrap_weights.first; - auto const ledger_unfiltered = node->ledger.cache.rep_weights.get_rep_amounts (); + auto const ledger_unfiltered = node->ledger.rep_weights.get_rep_amounts (); auto const ledger_height = node->ledger.block_count (); auto get_total = [] (decltype (bootstrap_weights.second) const & reps) -> nano::uint128_union { @@ -459,7 +459,7 @@ int main (int argc, char * const * argv) auto node = inactive_node.node; auto transaction (node->store.tx_begin_read ()); nano::uint128_t total; - auto rep_amounts = node->ledger.cache.rep_weights.get_rep_amounts (); + auto rep_amounts = node->ledger.rep_weights.get_rep_amounts (); std::map ordered_reps (rep_amounts.begin (), rep_amounts.end ()); for (auto const & rep : ordered_reps) { diff --git a/nano/node/election.cpp b/nano/node/election.cpp index b6857fbf3..c20739c75 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -837,7 +837,7 @@ std::vector nano::election::votes_with_weight () co { if (vote_l.first != nullptr) { - auto amount (node.ledger.cache.rep_weights.representation_get (vote_l.first)); + auto amount (node.ledger.weight (vote_l.first)); nano::vote_with_weight_info vote_info{ vote_l.first, vote_l.second.time, vote_l.second.timestamp, vote_l.second.hash, amount }; sorted_votes.emplace (std::move (amount), vote_info); } diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index 2b2729698..575a8c546 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -2136,7 +2136,7 @@ void nano::json_handler::confirmation_info () { if (block->hash () == vote.hash) { - auto amount (node.ledger.cache.rep_weights.representation_get (representative)); + auto amount (node.ledger.weight (representative)); representatives.emplace (amount, representative); if (vote.timestamp == std::numeric_limits::max ()) { @@ -3538,7 +3538,7 @@ void nano::json_handler::representatives () { bool const sorting = request.get ("sorting", false); boost::property_tree::ptree representatives; - auto rep_amounts = node.ledger.cache.rep_weights.get_rep_amounts (); + auto rep_amounts = node.ledger.rep_weights.get_rep_amounts (); if (!sorting) // Simple { std::map ordered (rep_amounts.begin (), rep_amounts.end ()); diff --git a/nano/node/node.cpp b/nano/node/node.cpp index da87ba4ba..05b4c0d72 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -330,20 +330,6 @@ nano::node::node (std::shared_ptr io_ctx_a, std::filesy config.bandwidth_limit, config.bandwidth_limit_burst_ratio); - // First do a pass with a read to see if any writing needs doing, this saves needing to open a write lock (and potentially blocking) - auto is_initialized (false); - { - auto const transaction (store.tx_begin_read ()); - is_initialized = (store.account.begin (transaction) != store.account.end (transaction)); - } - - if (!is_initialized && !flags.read_only) - { - auto const transaction = store.tx_begin_write (); - // Store was empty meaning we just created it, add the genesis block - store.initialize (transaction, ledger.cache, ledger.constants); - } - if (!block_or_pruned_exists (config.network_params.ledger.genesis->hash ())) { logger.critical (nano::log::type::node, "Genesis block not found. This commonly indicates a configuration issue, check that the --network or --data_path command line arguments are correct, and also the ledger backend node config option. If using a read-only CLI command a ledger must already exist, start the node with --daemon first."); diff --git a/nano/qt_test/qt.cpp b/nano/qt_test/qt.cpp index d02524f30..59b091de7 100644 --- a/nano/qt_test/qt.cpp +++ b/nano/qt_test/qt.cpp @@ -524,7 +524,6 @@ TEST (history, short_text) nano::ledger ledger (*store, nano::dev::constants, stats, logger); { auto transaction (ledger.tx_begin_write ()); - store->initialize (transaction, ledger.cache, ledger.constants); nano::keypair key; auto latest (ledger.any.account_head (transaction, nano::dev::genesis_key.pub)); auto send = std::make_shared (latest, nano::dev::genesis_key.pub, 0, nano::dev::genesis_key.prv, nano::dev::genesis_key.pub, *system.work.generate (latest)); @@ -566,7 +565,6 @@ TEST (history, pruned_source) // Basic pruning for legacy blocks. Previous block is pruned, source is pruned { auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, nano::dev::constants); auto latest (ledger.any.account_head (transaction, nano::dev::genesis_key.pub)); auto send1 = std::make_shared (latest, nano::dev::genesis_key.pub, nano::dev::constants.genesis_amount - 100, nano::dev::genesis_key.prv, nano::dev::genesis_key.pub, *system.work.generate (latest)); ASSERT_EQ (nano::block_status::progress, ledger.process (transaction, send1)); diff --git a/nano/secure/CMakeLists.txt b/nano/secure/CMakeLists.txt index a0cbea2a4..795d7002b 100644 --- a/nano/secure/CMakeLists.txt +++ b/nano/secure/CMakeLists.txt @@ -25,8 +25,6 @@ add_library( generate_cache_flags.cpp ledger.hpp ledger.cpp - ledger_cache.hpp - ledger_cache.cpp ledger_set_any.hpp ledger_set_any.cpp ledger_set_confirmed.hpp diff --git a/nano/secure/fwd.hpp b/nano/secure/fwd.hpp index d33606c11..50315f2d1 100644 --- a/nano/secure/fwd.hpp +++ b/nano/secure/fwd.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include namespace nano { diff --git a/nano/secure/ledger.cpp b/nano/secure/ledger.cpp index ec0566c56..c239f47af 100644 --- a/nano/secure/ledger.cpp +++ b/nano/secure/ledger.cpp @@ -60,7 +60,7 @@ public: auto info = ledger.any.account_get (transaction, pending.value ().source); release_assert (info); ledger.store.pending.del (transaction, key); - ledger.cache.rep_weights.representation_add (transaction, info->representative, pending.value ().amount.number ()); + ledger.rep_weights.representation_add (transaction, info->representative, pending.value ().amount.number ()); nano::account_info new_info (block_a.hashables.previous, info->representative, info->open_block, ledger.any.block_balance (transaction, block_a.hashables.previous).value (), nano::seconds_since_epoch (), info->block_count - 1, nano::epoch::epoch_0); ledger.update_account (transaction, pending.value ().source, *info, new_info); ledger.store.block.del (transaction, hash); @@ -77,7 +77,7 @@ public: auto source_account = ledger.any.block_account (transaction, block_a.hashables.source); auto info = ledger.any.account_get (transaction, destination_account); release_assert (info); - ledger.cache.rep_weights.representation_add (transaction, info->representative, 0 - amount); + ledger.rep_weights.representation_add (transaction, info->representative, 0 - amount); nano::account_info new_info (block_a.hashables.previous, info->representative, info->open_block, ledger.any.block_balance (transaction, block_a.hashables.previous).value (), nano::seconds_since_epoch (), info->block_count - 1, nano::epoch::epoch_0); ledger.update_account (transaction, destination_account, *info, new_info); ledger.store.block.del (transaction, hash); @@ -91,7 +91,7 @@ public: auto amount = ledger.any.block_amount (transaction, hash).value ().number (); auto destination_account = block_a.account (); auto source_account = ledger.any.block_account (transaction, block_a.hashables.source); - ledger.cache.rep_weights.representation_add (transaction, block_a.representative_field ().value (), 0 - amount); + ledger.rep_weights.representation_add (transaction, block_a.representative_field ().value (), 0 - amount); nano::account_info new_info; ledger.update_account (transaction, destination_account, new_info, new_info); ledger.store.block.del (transaction, hash); @@ -109,7 +109,7 @@ public: auto block = ledger.store.block.get (transaction, rep_block); release_assert (block != nullptr); auto representative = block->representative_field ().value (); - ledger.cache.rep_weights.representation_add_dual (transaction, block_a.hashables.representative, 0 - balance.number (), representative, balance.number ()); + ledger.rep_weights.representation_add_dual (transaction, block_a.hashables.representative, 0 - balance.number (), representative, balance.number ()); ledger.store.block.del (transaction, hash); nano::account_info new_info (block_a.hashables.previous, representative, info->open_block, info->balance, nano::seconds_since_epoch (), info->block_count - 1, nano::epoch::epoch_0); ledger.update_account (transaction, account, *info, new_info); @@ -163,12 +163,12 @@ public: auto rep_block (ledger.store.block.get (transaction, rep_block_hash)); release_assert (rep_block != nullptr); representative = rep_block->representative_field ().value (); - ledger.cache.rep_weights.representation_add_dual (transaction, representative, previous_balance, block_a.hashables.representative, 0 - block_a.hashables.balance.number ()); + ledger.rep_weights.representation_add_dual (transaction, representative, previous_balance, block_a.hashables.representative, 0 - block_a.hashables.balance.number ()); } else { // Add in amount delta only - ledger.cache.rep_weights.representation_add (transaction, block_a.hashables.representative, 0 - block_a.hashables.balance.number ()); + ledger.rep_weights.representation_add (transaction, block_a.hashables.representative, 0 - block_a.hashables.balance.number ()); } auto previous_version (ledger.version (transaction, block_a.hashables.previous)); @@ -353,12 +353,12 @@ void ledger_processor::state_block_impl (nano::state_block & block_a) if (!info.head.is_zero ()) { // Move existing representation & add in amount delta - ledger.cache.rep_weights.representation_add_dual (transaction, info.representative, 0 - info.balance.number (), block_a.hashables.representative, block_a.hashables.balance.number ()); + ledger.rep_weights.representation_add_dual (transaction, info.representative, 0 - info.balance.number (), block_a.hashables.representative, block_a.hashables.balance.number ()); } else { // Add in amount delta only - ledger.cache.rep_weights.representation_add (transaction, block_a.hashables.representative, block_a.hashables.balance.number ()); + ledger.rep_weights.representation_add (transaction, block_a.hashables.representative, block_a.hashables.balance.number ()); } if (is_send) @@ -481,7 +481,7 @@ void ledger_processor::change_block (nano::change_block & block_a) block_a.sideband_set (nano::block_sideband (account, 0, info->balance, info->block_count + 1, nano::seconds_since_epoch (), block_details, nano::epoch::epoch_0 /* unused */)); ledger.store.block.put (transaction, hash, block_a); auto balance = previous->balance (); - ledger.cache.rep_weights.representation_add_dual (transaction, block_a.hashables.representative, balance.number (), info->representative, 0 - balance.number ()); + ledger.rep_weights.representation_add_dual (transaction, block_a.hashables.representative, balance.number (), info->representative, 0 - balance.number ()); nano::account_info new_info (hash, block_a.hashables.representative, info->open_block, info->balance, nano::seconds_since_epoch (), info->block_count + 1, nano::epoch::epoch_0); ledger.update_account (transaction, account, *info, new_info); ledger.stats.inc (nano::stat::type::ledger, nano::stat::detail::change); @@ -526,7 +526,7 @@ void ledger_processor::send_block (nano::send_block & block_a) if (result == nano::block_status::progress) { auto amount (info->balance.number () - block_a.hashables.balance.number ()); - ledger.cache.rep_weights.representation_add (transaction, info->representative, 0 - amount); + ledger.rep_weights.representation_add (transaction, info->representative, 0 - amount); block_a.sideband_set (nano::block_sideband (account, 0, block_a.hashables.balance /* unused */, info->block_count + 1, nano::seconds_since_epoch (), block_details, nano::epoch::epoch_0 /* unused */)); ledger.store.block.put (transaction, hash, block_a); nano::account_info new_info (hash, info->representative, info->open_block, block_a.hashables.balance, nano::seconds_since_epoch (), info->block_count + 1, nano::epoch::epoch_0); @@ -590,7 +590,7 @@ void ledger_processor::receive_block (nano::receive_block & block_a) ledger.store.block.put (transaction, hash, block_a); nano::account_info new_info (hash, info->representative, info->open_block, new_balance, nano::seconds_since_epoch (), info->block_count + 1, nano::epoch::epoch_0); ledger.update_account (transaction, account, *info, new_info); - ledger.cache.rep_weights.representation_add (transaction, info->representative, pending.value ().amount.number ()); + ledger.rep_weights.representation_add (transaction, info->representative, pending.value ().amount.number ()); ledger.stats.inc (nano::stat::type::ledger, nano::stat::detail::receive); } } @@ -642,7 +642,7 @@ void ledger_processor::open_block (nano::open_block & block_a) ledger.store.block.put (transaction, hash, block_a); nano::account_info new_info (hash, block_a.representative_field ().value (), hash, pending.value ().amount.number (), nano::seconds_since_epoch (), 1, nano::epoch::epoch_0); ledger.update_account (transaction, block_a.hashables.account, info, new_info); - ledger.cache.rep_weights.representation_add (transaction, block_a.representative_field ().value (), pending.value ().amount.number ()); + ledger.rep_weights.representation_add (transaction, block_a.representative_field ().value (), pending.value ().amount.number ()); ledger.stats.inc (nano::stat::type::ledger, nano::stat::detail::open); } } @@ -728,17 +728,18 @@ void representative_visitor::state_block (nano::state_block const & block_a) * ledger */ -nano::ledger::ledger (nano::store::component & store_a, nano::ledger_constants & constants_a, nano::stats & stats_a, nano::logger & logger_a, nano::generate_cache_flags const & generate_cache_flags_a, nano::uint128_t min_rep_weight_a) : - constants{ constants_a }, +nano::ledger::ledger (nano::store::component & store_a, nano::ledger_constants & constants_a, nano::stats & stats_a, nano::logger & logger_a, nano::generate_cache_flags generate_cache_flags_a, nano::uint128_t min_rep_weight_a) : store{ store_a }, - cache{ store_a.rep_weight, min_rep_weight_a }, + constants{ constants_a }, stats{ stats_a }, logger{ logger_a }, + rep_weights{ store_a.rep_weight, min_rep_weight_a }, any_impl{ std::make_unique (*this) }, confirmed_impl{ std::make_unique (*this) }, any{ *any_impl }, confirmed{ *confirmed_impl } { + // TODO: Throw on error if (!store.init_error ()) { initialize (generate_cache_flags_a); @@ -770,6 +771,19 @@ void nano::ledger::initialize (nano::generate_cache_flags const & generate_cache { logger.info (nano::log::type::ledger, "Loading ledger, this may take a while..."); + bool is_initialized = false; + { + auto const transaction = store.tx_begin_read (); + is_initialized = (store.account.begin (transaction) != store.account.end (transaction)); + } + if (!is_initialized && store.get_mode () != nano::store::open_mode::read_only) + { + // Store was empty meaning we just created it, add the genesis block + auto const transaction = store.tx_begin_write (); + logger.info (nano::log::type::ledger, "Initializing ledger with genesis block"); + store.initialize (transaction, constants); + } + if (generate_cache_flags_a.reps || generate_cache_flags_a.account_count || generate_cache_flags_a.block_count) { store.account.for_each_par ( @@ -793,7 +807,7 @@ void nano::ledger::initialize (nano::generate_cache_flags const & generate_cache { rep_weights_l.representation_put (i->first, i->second.number ()); } - this->cache.rep_weights.copy_from (rep_weights_l); + this->rep_weights.copy_from (rep_weights_l); }); } @@ -819,7 +833,7 @@ void nano::ledger::initialize (nano::generate_cache_flags const & generate_cache logger.info (nano::log::type::ledger, "Cemented count: {:>11}", cache.cemented_count.load ()); logger.info (nano::log::type::ledger, "Account count: {:>11}", cache.account_count.load ()); logger.info (nano::log::type::ledger, "Pruned count: {:>11}", cache.pruned_count.load ()); - logger.info (nano::log::type::ledger, "Representative count: {:>5}", cache.rep_weights.size ()); + logger.info (nano::log::type::ledger, "Representative count: {:>5}", rep_weights.size ()); } bool nano::ledger::unconfirmed_exists (secure::transaction const & transaction, nano::block_hash const & hash) @@ -1005,7 +1019,7 @@ std::unordered_map nano::ledger::rep_weights_sna } else { - return cache.rep_weights.get_rep_amounts (); + return rep_weights.get_rep_amounts (); } } @@ -1022,7 +1036,7 @@ nano::uint128_t nano::ledger::weight (nano::account const & account_a) const } else { - return cache.rep_weights.representation_get (account_a); + return rep_weights.representation_get (account_a); } } @@ -1611,6 +1625,6 @@ nano::container_info nano::ledger::container_info () const { nano::container_info info; info.put ("bootstrap_weights", bootstrap_weights); - info.add ("rep_weights", cache.rep_weights.container_info ()); + info.add ("rep_weights", rep_weights.container_info ()); return info; } diff --git a/nano/secure/ledger.hpp b/nano/secure/ledger.hpp index d9fc78fbc..c48b1d67f 100644 --- a/nano/secure/ledger.hpp +++ b/nano/secure/ledger.hpp @@ -1,35 +1,43 @@ #pragma once +#include #include #include #include #include #include -#include #include +#include #include #include #include #include -namespace nano::store -{ -class component; -} - namespace nano { +class ledger; class ledger_set_any; class ledger_set_confirmed; +class ledger_cache +{ + friend class ledger; + +private: + std::atomic cemented_count{ 0 }; + std::atomic block_count{ 0 }; + std::atomic pruned_count{ 0 }; + std::atomic account_count{ 0 }; +}; + class ledger final { template friend class receivable_iterator; public: - ledger (nano::store::component &, nano::ledger_constants &, nano::stats &, nano::logger &, nano::generate_cache_flags const & = nano::generate_cache_flags{}, nano::uint128_t min_rep_weight_a = 0); + ledger (nano::store::component &, nano::ledger_constants &, nano::stats &, nano::logger &, nano::generate_cache_flags = {}, nano::uint128_t min_rep_weight = 0); ~ledger (); /** Start read-write transaction */ @@ -92,12 +100,15 @@ public: public: static nano::uint128_t const unit; - nano::ledger_constants & constants; nano::store::component & store; - nano::ledger_cache cache; + nano::ledger_constants & constants; nano::stats & stats; nano::logger & logger; + nano::ledger_cache cache; + nano::rep_weights rep_weights; + +public: std::unordered_map bootstrap_weights; uint64_t bootstrap_weight_max_blocks{ 1 }; diff --git a/nano/secure/ledger_cache.cpp b/nano/secure/ledger_cache.cpp deleted file mode 100644 index 16c46c0f5..000000000 --- a/nano/secure/ledger_cache.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -nano::ledger_cache::ledger_cache (nano::store::rep_weight & rep_weight_store_a, nano::uint128_t min_rep_weight_a) : - rep_weights{ rep_weight_store_a, min_rep_weight_a } -{ -} diff --git a/nano/secure/ledger_cache.hpp b/nano/secure/ledger_cache.hpp deleted file mode 100644 index 81503b6df..000000000 --- a/nano/secure/ledger_cache.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include -#include -#include - -#include - -namespace nano -{ -class ledger; -} -namespace nano::store -{ -class component; -} - -namespace nano -{ -/* Holds an in-memory cache of various counts */ -class ledger_cache -{ - friend class store::component; - friend class ledger; - -public: - explicit ledger_cache (nano::store::rep_weight & rep_weight_store_a, nano::uint128_t min_rep_weight_a = 0); - nano::rep_weights rep_weights; - -private: - std::atomic cemented_count{ 0 }; - std::atomic block_count{ 0 }; - std::atomic pruned_count{ 0 }; - std::atomic account_count{ 0 }; -}; -} diff --git a/nano/slow_test/node.cpp b/nano/slow_test/node.cpp index b95c69656..0d5177b05 100644 --- a/nano/slow_test/node.cpp +++ b/nano/slow_test/node.cpp @@ -134,7 +134,6 @@ TEST (ledger, deep_account_compute) nano::stats stats{ logger }; nano::ledger ledger (*store, nano::dev::constants, stats, logger); auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::keypair key; auto balance (nano::dev::constants.genesis_amount - 1); @@ -1159,7 +1158,6 @@ TEST (confirmation_height, many_accounts_send_receive_self_no_elections) { auto transaction = ledger.tx_begin_write (); - store->initialize (transaction, ledger.cache, ledger.constants); // Send from genesis account to all other accounts and create open block for them for (auto i = 0; i < num_accounts; ++i) diff --git a/nano/store/component.cpp b/nano/store/component.cpp index 1af3aad7a..0dda015fd 100644 --- a/nano/store/component.cpp +++ b/nano/store/component.cpp @@ -1,11 +1,13 @@ #include #include #include -#include #include #include #include #include +#include +#include +#include #include nano::store::component::component (nano::store::block & block_store_a, nano::store::account & account_store_a, nano::store::pending & pending_store_a, nano::store::online_weight & online_weight_store_a, nano::store::pruned & pruned_store_a, nano::store::peer & peer_store_a, nano::store::confirmation_height & confirmation_height_store_a, nano::store::final_vote & final_vote_store_a, nano::store::version & version_store_a, nano::store::rep_weight & rep_weight_a) : @@ -26,19 +28,21 @@ nano::store::component::component (nano::store::block & block_store_a, nano::sto * If using a different store version than the latest then you may need * to modify some of the objects in the store to be appropriate for the version before an upgrade. */ -void nano::store::component::initialize (store::write_transaction const & transaction_a, nano::ledger_cache & ledger_cache_a, nano::ledger_constants & constants) +void nano::store::component::initialize (store::write_transaction const & transaction, nano::ledger_constants & constants) { - debug_assert (constants.genesis->has_sideband ()); - debug_assert (account.begin (transaction_a) == account.end (transaction_a)); - auto hash_l (constants.genesis->hash ()); - block.put (transaction_a, hash_l, *constants.genesis); - ++ledger_cache_a.block_count; - confirmation_height.put (transaction_a, constants.genesis->account (), nano::confirmation_height_info{ 1, constants.genesis->hash () }); - ++ledger_cache_a.cemented_count; - account.put (transaction_a, constants.genesis->account (), { hash_l, constants.genesis->account (), constants.genesis->hash (), std::numeric_limits::max (), nano::seconds_since_epoch (), 1, nano::epoch::epoch_0 }); - ++ledger_cache_a.account_count; - rep_weight.put (transaction_a, constants.genesis->account (), std::numeric_limits::max ()); - ledger_cache_a.rep_weights.representation_put (constants.genesis->account (), std::numeric_limits::max ()); + release_assert (constants.genesis->has_sideband ()); + release_assert (account.begin (transaction) == account.end (transaction)); + release_assert (block.begin (transaction) == block.end (transaction)); + release_assert (pending.begin (transaction) == pending.end (transaction)); + release_assert (confirmation_height.begin (transaction) == confirmation_height.end (transaction)); + release_assert (final_vote.begin (transaction) == final_vote.end (transaction)); + release_assert (rep_weight.begin (transaction) == rep_weight.end (transaction)); + release_assert (pruned.begin (transaction) == pruned.end (transaction)); + + block.put (transaction, constants.genesis->hash (), *constants.genesis); + confirmation_height.put (transaction, constants.genesis->account (), nano::confirmation_height_info{ 1, constants.genesis->hash () }); + account.put (transaction, constants.genesis->account (), { constants.genesis->hash (), constants.genesis->account (), constants.genesis->hash (), std::numeric_limits::max (), nano::seconds_since_epoch (), 1, nano::epoch::epoch_0 }); + rep_weight.put (transaction, constants.genesis->account (), std::numeric_limits::max ()); } /* diff --git a/nano/store/component.hpp b/nano/store/component.hpp index 1de32ea52..b4a0de0fb 100644 --- a/nano/store/component.hpp +++ b/nano/store/component.hpp @@ -37,24 +37,24 @@ namespace store friend class mdb_block_store_upgrade_v21_v22_Test; public: - // clang-format off - explicit component ( + explicit component ( nano::store::block &, nano::store::account &, nano::store::pending &, - nano::store::online_weight&, + nano::store::online_weight &, nano::store::pruned &, nano::store::peer &, nano::store::confirmation_height &, nano::store::final_vote &, nano::store::version &, - nano::store::rep_weight & - ); - // clang-format on + nano::store::rep_weight &); + virtual ~component () = default; - void initialize (write_transaction const & transaction_a, nano::ledger_cache & ledger_cache_a, nano::ledger_constants & constants); + + void initialize (store::write_transaction const &, nano::ledger_constants &); + virtual uint64_t count (store::transaction const & transaction_a, tables table_a) const = 0; - virtual int drop (write_transaction const & transaction_a, tables table_a) = 0; + virtual int drop (store::write_transaction const & transaction_a, tables table_a) = 0; virtual bool not_found (int status) const = 0; virtual bool success (int status) const = 0; virtual std::string error_string (int status) const = 0; @@ -63,6 +63,7 @@ namespace store store::account & account; store::pending & pending; store::rep_weight & rep_weight; + static int constexpr version_minimum{ 21 }; static int constexpr version_current{ 24 }; diff --git a/nano/test_common/ledger_context.cpp b/nano/test_common/ledger_context.cpp index 6ae4b0fa4..0d5725823 100644 --- a/nano/test_common/ledger_context.cpp +++ b/nano/test_common/ledger_context.cpp @@ -5,14 +5,12 @@ nano::test::ledger_context::ledger_context (std::deque> && blocks) : store_m{ nano::make_store (logger_m, nano::unique_path (), nano::dev::constants) }, - stats_m{ logger_m }, ledger_m{ *store_m, nano::dev::constants, stats_m, logger_m }, blocks_m{ blocks }, pool_m{ nano::dev::network_params.network, 1 } { debug_assert (!store_m->init_error ()); auto tx = ledger_m.tx_begin_write (); - store_m->initialize (tx, ledger_m.cache, ledger_m.constants); for (auto const & i : blocks_m) { auto process_result = ledger_m.process (tx, i); diff --git a/nano/test_common/ledger_context.hpp b/nano/test_common/ledger_context.hpp index 62b376a4f..f7bb1274c 100644 --- a/nano/test_common/ledger_context.hpp +++ b/nano/test_common/ledger_context.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace nano::test @@ -14,6 +15,7 @@ public: /** 'blocks' initialises the ledger with each block in-order Blocks must all return process_result::progress when processed */ ledger_context (std::deque> && blocks = std::deque>{}); + nano::ledger & ledger (); nano::store::component & store (); std::deque> const & blocks () const; @@ -23,8 +25,8 @@ public: private: nano::logger logger_m; + nano::stats stats_m{ logger_m }; std::unique_ptr store_m; - nano::stats stats_m; nano::ledger ledger_m; std::deque> blocks_m; nano::work_pool pool_m;