diff --git a/libraries/cli/include/cli/config.hpp b/libraries/cli/include/cli/config.hpp index d2c91d75bf..99ed1cc28c 100644 --- a/libraries/cli/include/cli/config.hpp +++ b/libraries/cli/include/cli/config.hpp @@ -42,6 +42,7 @@ class Config { static constexpr const char* HELP = "help"; static constexpr const char* VERSION = "version"; static constexpr const char* WALLET = "wallet"; + static constexpr const char* PRUNE_STATE_DB = "prune-state-db"; static constexpr const char* NODE_COMMAND = "node"; static constexpr const char* ACCOUNT_COMMAND = "account"; diff --git a/libraries/cli/src/config.cpp b/libraries/cli/src/config.cpp index 2465a368f3..2a4c8fdf20 100644 --- a/libraries/cli/src/config.cpp +++ b/libraries/cli/src/config.cpp @@ -38,6 +38,8 @@ Config::Config(int argc, const char* argv[]) { bool destroy_db = false; bool rebuild_network = false; bool rebuild_db = false; + bool prune_state_db = false; + bool light_node = false; bool version = false; uint64_t rebuild_db_period = 0; @@ -129,6 +131,7 @@ Config::Config(int argc, const char* argv[]) { "Enables Test JsonRPC. Disabled by default"); node_command_options.add_options()(ENABLE_DEBUG, bpo::bool_switch(&enable_debug), "Enables Debug RPC interface. Disabled by default"); + node_command_options.add_options()(PRUNE_STATE_DB, bpo::bool_switch(&prune_state_db), "Prune state_db"); allowed_options.add(main_options); @@ -261,6 +264,7 @@ Config::Config(int argc, const char* argv[]) { } node_config_.db_config.db_revert_to_period = revert_to_period; node_config_.db_config.rebuild_db = rebuild_db; + node_config_.db_config.prune_state_db = prune_state_db; node_config_.db_config.rebuild_db_period = rebuild_db_period; node_config_.enable_test_rpc = enable_test_rpc; diff --git a/libraries/config/include/config/config.hpp b/libraries/config/include/config/config.hpp index c4c577e22d..9a4388d0cb 100644 --- a/libraries/config/include/config/config.hpp +++ b/libraries/config/include/config/config.hpp @@ -15,6 +15,7 @@ struct DBConfig { uint32_t db_max_open_files = 0; PbftPeriod db_revert_to_period = 0; bool rebuild_db = false; + bool prune_state_db = false; PbftPeriod rebuild_db_period = 0; }; diff --git a/libraries/config/src/config.cpp b/libraries/config/src/config.cpp index ddf67d0da4..91746e521d 100644 --- a/libraries/config/src/config.cpp +++ b/libraries/config/src/config.cpp @@ -98,13 +98,11 @@ FullNodeConfig::FullNodeConfig(const Json::Value &string_or_object, const Json:: } is_light_node = getConfigDataAsBoolean(root, {"is_light_node"}, true, is_light_node); - if (is_light_node) { - const auto min_light_node_history = (genesis.state.dpos.blocks_per_year * kDefaultLightNodeHistoryDays) / 365; - light_node_history = getConfigDataAsUInt(root, {"light_node_history"}, true, min_light_node_history); - if (light_node_history < min_light_node_history) { - throw ConfigException("Min. required light node history is " + std::to_string(min_light_node_history) + - " blocks (" + std::to_string(kDefaultLightNodeHistoryDays) + " days)"); - } + const auto min_light_node_history = (genesis.state.dpos.blocks_per_year * kDefaultLightNodeHistoryDays) / 365; + light_node_history = getConfigDataAsUInt(root, {"light_node_history"}, true, min_light_node_history); + if (light_node_history < min_light_node_history) { + throw ConfigException("Min. required light node history is " + std::to_string(min_light_node_history) + + " blocks (" + std::to_string(kDefaultLightNodeHistoryDays) + " days)"); } try { diff --git a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp index c5ee4bbeaf..212a2a2127 100644 --- a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp +++ b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp @@ -20,11 +20,9 @@ class FinalChainImpl final : public FinalChain { const bool kLightNode = false; const uint64_t kLightNodeHistory = 0; const uint32_t kMaxLevelsPerPeriod; - const uint64_t kLightNodePruneOffset = 0; // It is not prepared to use more then 1 thread. Examine it if you want to change threads count boost::asio::thread_pool executor_thread_{1}; - boost::asio::thread_pool prune_thread_{1}; std::atomic num_executed_dag_blk_ = 0; std::atomic num_executed_trx_ = 0; @@ -61,10 +59,6 @@ class FinalChainImpl final : public FinalChain { kLightNode(config.is_light_node), kLightNodeHistory(config.light_node_history), kMaxLevelsPerPeriod(config.max_levels_per_period), - // This will provide a speific random offset based on node address for each node to prevent all light nodes - // performing prune at the same block height - kLightNodePruneOffset((*reinterpret_cast(node_addr.asBytes().data())) % - std::max(config.light_node_history, (uint64_t)1)), block_headers_cache_(config.final_chain_cache_in_blocks, [this](uint64_t blk) { return get_block_header(blk); }), block_hashes_cache_(config.final_chain_cache_in_blocks, [this](uint64_t blk) { return get_block_hash(blk); }), @@ -125,12 +119,14 @@ class FinalChainImpl final : public FinalChain { } delegation_delay_ = config.genesis.state.dpos.delegation_delay; + if (config.db_config.prune_state_db && last_blk_num.has_value() && *last_blk_num > kLightNodeHistory) { + LOG(log_si_) << "Pruning state db, this might take several minutes"; + prune(*last_blk_num - kLightNodeHistory); + LOG(log_si_) << "Pruning state db complete"; + } } - void stop() override { - executor_thread_.join(); - prune_thread_.join(); - } + void stop() override { executor_thread_.join(); } std::future> finalize( PeriodData&& new_blk, std::vector&& finalized_dag_blk_hashes, @@ -246,13 +242,6 @@ class FinalChainImpl final : public FinalChain { state_api_.create_snapshot(blk_header->number); } - if (kLightNode) { - // Actual history size will be between 100% and 105% of light_node_history_ to avoid deleting on every period - if ((((blk_header->number + kLightNodePruneOffset) % (std::max(kLightNodeHistory / 20, (uint64_t)1)) == 0)) && - blk_header->number > kLightNodeHistory) { - prune(blk_header->number - kLightNodeHistory); - } - } return result; } @@ -274,10 +263,7 @@ class FinalChainImpl final : public FinalChain { db_->compactColumn(DB::Columns::final_chain_blk_hash_by_number); db_->compactColumn(DB::Columns::final_chain_blk_number_by_hash); - boost::asio::post( - prune_thread_, - [this, to_keep = std::move(last_block_to_keep->state_root), to_prune = std::move(state_root_to_prune), - number = last_block_to_keep->number]() { state_api_.prune(to_keep, to_prune, number); }); + state_api_.prune(last_block_to_keep->state_root, state_root_to_prune, last_block_to_keep->number); } }