Keep rep_weights container directly in ledger class

This commit is contained in:
Piotr Wójcik 2025-05-29 20:17:31 +02:00
commit 5cce839f5b
22 changed files with 103 additions and 154 deletions

View file

@ -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<unsigned>::max () };
auto send = builder
.send ()

View file

@ -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<unsigned>::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<unsigned>::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<unsigned>::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<unsigned>::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<unsigned>::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<unsigned>::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<unsigned>::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<unsigned>::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<unsigned>::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

View file

@ -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 ()), "");
}
}

View file

@ -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

View file

@ -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<unsigned>::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<unsigned>::max () };
auto info1 = ledger.any.account_get (transaction, nano::dev::genesis_key.pub);
ASSERT_TRUE (info1);

View file

@ -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 ();

View file

@ -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<nano::account, nano::uint128_t> ordered_reps (rep_amounts.begin (), rep_amounts.end ());
for (auto const & rep : ordered_reps)
{

View file

@ -837,7 +837,7 @@ std::vector<nano::vote_with_weight_info> 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);
}

View file

@ -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<uint64_t>::max ())
{
@ -3538,7 +3538,7 @@ void nano::json_handler::representatives ()
{
bool const sorting = request.get<bool> ("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<nano::account, nano::uint128_t> ordered (rep_amounts.begin (), rep_amounts.end ());

View file

@ -330,20 +330,6 @@ nano::node::node (std::shared_ptr<boost::asio::io_context> 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.");

View file

@ -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<nano::send_block> (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<nano::send_block> (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));

View file

@ -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

View file

@ -1,6 +1,7 @@
#pragma once
#include <nano/lib/fwd.hpp>
#include <nano/store/fwd.hpp>
namespace nano
{

View file

@ -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<ledger_set_any> (*this) },
confirmed_impl{ std::make_unique<ledger_set_confirmed> (*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::account, nano::uint128_t> 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;
}

View file

@ -1,35 +1,43 @@
#pragma once
#include <nano/lib/config.hpp>
#include <nano/lib/numbers.hpp>
#include <nano/lib/timer.hpp>
#include <nano/secure/account_info.hpp>
#include <nano/secure/fwd.hpp>
#include <nano/secure/generate_cache_flags.hpp>
#include <nano/secure/ledger_cache.hpp>
#include <nano/secure/pending_info.hpp>
#include <nano/secure/rep_weights.hpp>
#include <nano/secure/transaction.hpp>
#include <deque>
#include <map>
#include <memory>
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<uint64_t> cemented_count{ 0 };
std::atomic<uint64_t> block_count{ 0 };
std::atomic<uint64_t> pruned_count{ 0 };
std::atomic<uint64_t> account_count{ 0 };
};
class ledger final
{
template <typename T>
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<nano::account, nano::uint128_t> bootstrap_weights;
uint64_t bootstrap_weight_max_blocks{ 1 };

View file

@ -1,6 +0,0 @@
#include <nano/secure/ledger_cache.hpp>
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 }
{
}

View file

@ -1,36 +0,0 @@
#pragma once
#include <nano/lib/numbers.hpp>
#include <nano/secure/rep_weights.hpp>
#include <nano/store/rep_weight.hpp>
#include <atomic>
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<uint64_t> cemented_count{ 0 };
std::atomic<uint64_t> block_count{ 0 };
std::atomic<uint64_t> pruned_count{ 0 };
std::atomic<uint64_t> account_count{ 0 };
};
}

View file

@ -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<unsigned>::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)

View file

@ -1,11 +1,13 @@
#include <nano/lib/blocks.hpp>
#include <nano/lib/enum_util.hpp>
#include <nano/lib/timer.hpp>
#include <nano/secure/ledger_cache.hpp>
#include <nano/store/account.hpp>
#include <nano/store/block.hpp>
#include <nano/store/component.hpp>
#include <nano/store/confirmation_height.hpp>
#include <nano/store/final_vote.hpp>
#include <nano/store/pending.hpp>
#include <nano/store/pruned.hpp>
#include <nano/store/rep_weight.hpp>
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<nano::uint128_t>::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<nano::uint128_t>::max ());
ledger_cache_a.rep_weights.representation_put (constants.genesis->account (), std::numeric_limits<nano::uint128_t>::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<nano::uint128_t>::max (), nano::seconds_since_epoch (), 1, nano::epoch::epoch_0 });
rep_weight.put (transaction, constants.genesis->account (), std::numeric_limits<nano::uint128_t>::max ());
}
/*

View file

@ -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 };

View file

@ -5,14 +5,12 @@
nano::test::ledger_context::ledger_context (std::deque<std::shared_ptr<nano::block>> && 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);

View file

@ -4,6 +4,7 @@
#include <nano/lib/stats.hpp>
#include <nano/lib/work.hpp>
#include <nano/secure/ledger.hpp>
#include <nano/store/component.hpp>
#include <nano/store/fwd.hpp>
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<std::shared_ptr<nano::block>> && blocks = std::deque<std::shared_ptr<nano::block>>{});
nano::ledger & ledger ();
nano::store::component & store ();
std::deque<std::shared_ptr<nano::block>> const & blocks () const;
@ -23,8 +25,8 @@ public:
private:
nano::logger logger_m;
nano::stats stats_m{ logger_m };
std::unique_ptr<nano::store::component> store_m;
nano::stats stats_m;
nano::ledger ledger_m;
std::deque<std::shared_ptr<nano::block>> blocks_m;
nano::work_pool pool_m;