diff --git a/nano/core_test/distributed_work.cpp b/nano/core_test/distributed_work.cpp index 9c8a8eedca..acd14bcdf5 100644 --- a/nano/core_test/distributed_work.cpp +++ b/nano/core_test/distributed_work.cpp @@ -55,7 +55,6 @@ TEST (distributed_work, no_peers_cancel) nano::system system; nano::node_config node_config (nano::get_available_port (), system.logging); node_config.max_work_generate_multiplier = 1e6; - node_config.max_work_generate_difficulty = nano::difficulty::from_multiplier (node_config.max_work_generate_multiplier, nano::network_constants ().publish_thresholds.base); auto & node = *system.add_node (node_config); nano::block_hash hash{ 1 }; bool done{ false }; diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index ae8e0c8809..ae64cc4ddd 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -3727,6 +3727,22 @@ TEST (node, aggressive_flooding) ASSERT_EQ (1 + 2 * nodes_wallets.size () + 2, node1.ledger.cache.block_count); } +// Tests that upon changing the default difficulty, max generation difficulty changes proportionally +TEST (node, max_work_generate_difficulty) +{ + nano::system system; + nano::node_config node_config (nano::get_available_port (), system.logging); + node_config.max_work_generate_multiplier = 2.0; + auto & node = *system.add_node (node_config); + auto initial_difficulty = node.default_difficulty (nano::work_version::work_1); + ASSERT_EQ (node.max_work_generate_difficulty (nano::work_version::work_1), nano::difficulty::from_multiplier (node.config.max_work_generate_multiplier, initial_difficulty)); + ASSERT_NE (nullptr, system.upgrade_genesis_epoch (node, nano::epoch::epoch_1)); + ASSERT_NE (nullptr, system.upgrade_genesis_epoch (node, nano::epoch::epoch_2)); + auto final_difficulty = node.default_difficulty (nano::work_version::work_1); + ASSERT_NE (final_difficulty, initial_difficulty); + ASSERT_EQ (node.max_work_generate_difficulty (nano::work_version::work_1), nano::difficulty::from_multiplier (node.config.max_work_generate_multiplier, final_difficulty)); +} + TEST (active_difficulty, recalculate_work) { nano::system system; diff --git a/nano/core_test/wallet.cpp b/nano/core_test/wallet.cpp index 3cad3d5888..c8a148314a 100644 --- a/nano/core_test/wallet.cpp +++ b/nano/core_test/wallet.cpp @@ -1164,7 +1164,7 @@ TEST (wallet, work_watcher_update) nano::node_config node_config (nano::get_available_port (), system.logging); node_config.enable_voting = false; node_config.work_watcher_period = 1s; - node_config.max_work_generate_difficulty = std::numeric_limits::max (); + node_config.max_work_generate_multiplier = 1e6; nano::node_flags node_flags; node_flags.disable_request_loop = true; auto & node = *system.add_node (node_config, node_flags); @@ -1273,7 +1273,6 @@ TEST (wallet, work_watcher_cancel) nano::node_config node_config (nano::get_available_port (), system.logging); node_config.work_watcher_period = 1s; node_config.max_work_generate_multiplier = 1e6; - node_config.max_work_generate_difficulty = nano::difficulty::from_multiplier (node_config.max_work_generate_multiplier, nano::network_constants ().publish_thresholds.base); node_config.enable_voting = false; auto & node = *system.add_node (node_config); auto & wallet (*system.wallet (0)); @@ -1323,7 +1322,7 @@ TEST (wallet, limited_difficulty) { nano::system system; nano::node_config node_config (nano::get_available_port (), system.logging); - node_config.max_work_generate_difficulty = nano::network_constants ().publish_thresholds.base; + node_config.max_work_generate_multiplier = 1; nano::node_flags node_flags; node_flags.disable_request_loop = true; auto & node = *system.add_node (node_config, node_flags); @@ -1338,7 +1337,7 @@ TEST (wallet, limited_difficulty) nano::lock_guard guard (node.active.mutex); node.active.trended_active_difficulty = std::numeric_limits::max (); } - ASSERT_EQ (node_config.max_work_generate_difficulty, node.active.limited_active_difficulty ()); + ASSERT_EQ (node.max_work_generate_difficulty (nano::work_version::work_1), node.active.limited_active_difficulty ()); auto send = wallet.send_action (nano::test_genesis_key.pub, nano::keypair ().pub, 1, 1); ASSERT_NE (nullptr, send); } diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index 8245225030..ee4cb95e25 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -795,7 +795,7 @@ uint64_t nano::active_transactions::active_difficulty () uint64_t nano::active_transactions::limited_active_difficulty () { - return std::min (active_difficulty (), node.config.max_work_generate_difficulty); + return std::min (active_difficulty (), node.max_work_generate_difficulty (nano::work_version::work_1)); } // List of active blocks in elections diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index 4b89e6666b..9350d72973 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -4953,7 +4953,7 @@ void nano::json_handler::work_generate () auto hash (hash_impl ()); auto difficulty (difficulty_optional_impl (work_version)); multiplier_optional_impl (work_version, difficulty); - if (!ec && (difficulty > node.config.max_work_generate_difficulty || difficulty < nano::work_threshold_entry (work_version))) + if (!ec && (difficulty > node.max_work_generate_difficulty (work_version) || difficulty < nano::work_threshold_entry (work_version))) { ec = nano::error_rpc::difficulty_limit; } diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 7854032c97..41e1662c33 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -1018,6 +1018,11 @@ uint64_t nano::node::default_difficulty (nano::work_version const version_a) con return result; } +uint64_t nano::node::max_work_generate_difficulty (nano::work_version const version_a) const +{ + return nano::difficulty::from_multiplier (config.max_work_generate_multiplier, default_difficulty (version_a)); +} + bool nano::node::local_work_generation_enabled () const { return config.work_threads > 0 || work.opencl; diff --git a/nano/node/node.hpp b/nano/node/node.hpp index b2cefff6ca..a4accf9fc5 100644 --- a/nano/node/node.hpp +++ b/nano/node/node.hpp @@ -128,6 +128,7 @@ class node final : public std::enable_shared_from_this int price (nano::uint128_t const &, int); // The default difficulty updates to base only when the first epoch_2 block is processed uint64_t default_difficulty (nano::work_version const) const; + uint64_t max_work_generate_difficulty (nano::work_version const) const; bool local_work_generation_enabled () const; bool work_generation_enabled () const; bool work_generation_enabled (std::vector> const &) const; diff --git a/nano/node/nodeconfig.cpp b/nano/node/nodeconfig.cpp index acf598c79c..b563f1cb11 100644 --- a/nano/node/nodeconfig.cpp +++ b/nano/node/nodeconfig.cpp @@ -35,7 +35,6 @@ external_address (boost::asio::ip::address_v6{}.to_string ()) { peering_port = network_params.network.default_node_port; } - max_work_generate_difficulty = nano::difficulty::from_multiplier (max_work_generate_multiplier, network_params.network.publish_thresholds.base); switch (network_params.network.network ()) { case nano::nano_networks::nano_test_network: @@ -361,7 +360,6 @@ nano::error nano::node_config::deserialize_toml (nano::tomlconfig & toml) nano::network_constants network; toml.get ("max_work_generate_multiplier", max_work_generate_multiplier); - max_work_generate_difficulty = nano::difficulty::from_multiplier (max_work_generate_multiplier, network.publish_thresholds.base); toml.get ("max_queued_requests", max_queued_requests); diff --git a/nano/node/nodeconfig.hpp b/nano/node/nodeconfig.hpp index e5a6bd7322..c9f5bb17b9 100644 --- a/nano/node/nodeconfig.hpp +++ b/nano/node/nodeconfig.hpp @@ -98,7 +98,6 @@ class node_config bool backup_before_upgrade{ false }; std::chrono::seconds work_watcher_period{ std::chrono::seconds (5) }; double max_work_generate_multiplier{ 64. }; - uint64_t max_work_generate_difficulty{ nano::network_constants ().publish_full.base }; uint32_t max_queued_requests{ 512 }; nano::rocksdb_config rocksdb_config; nano::lmdb_config lmdb_config; diff --git a/nano/node/wallet.cpp b/nano/node/wallet.cpp index 8cc6c4c578..be2e6e20e8 100644 --- a/nano/node/wallet.cpp +++ b/nano/node/wallet.cpp @@ -1152,7 +1152,7 @@ bool nano::wallet::action_complete (std::shared_ptr const & block_a if (block_a->difficulty () < required_difficulty) { wallets.node.logger.try_log (boost::str (boost::format ("Cached or provided work for block %1% account %2% is invalid, regenerating") % block_a->hash ().to_string () % account_a.to_account ())); - debug_assert (required_difficulty <= wallets.node.config.max_work_generate_difficulty); + debug_assert (required_difficulty <= wallets.node.max_work_generate_difficulty (block_a->work_version ())); auto target_difficulty = std::max (required_difficulty, wallets.node.active.limited_active_difficulty ()); error = !wallets.node.work_generate_blocking (*block_a, target_difficulty).is_initialized (); } diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 7e92190ec8..68dbb21b01 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -2953,9 +2953,7 @@ TEST (rpc, work_generate) TEST (rpc, work_generate_difficulty) { nano::system system; - nano::node_config node_config (nano::get_available_port (), system.logging); - node_config.max_work_generate_difficulty = 0xffff000000000000; - auto node = add_ipc_enabled_node (system, node_config); + auto node = add_ipc_enabled_node (system); scoped_io_thread_name_change scoped_thread_name_io; nano::node_rpc_config node_rpc_config; nano::ipc::ipc_server ipc_server (*node, node_rpc_config); @@ -3008,7 +3006,7 @@ TEST (rpc, work_generate_difficulty) ASSERT_GE (result_difficulty, difficulty); } { - uint64_t difficulty (node->config.max_work_generate_difficulty + 1); + uint64_t difficulty (node->max_work_generate_difficulty (nano::work_version::work_1) + 1); request.put ("difficulty", nano::to_string_hex (difficulty)); test_response response (request, rpc.config.port, system.io_ctx); system.deadline_set (5s); @@ -3026,7 +3024,6 @@ TEST (rpc, work_generate_multiplier) { nano::system system; nano::node_config node_config (nano::get_available_port (), system.logging); - node_config.max_work_generate_difficulty = 0xfffff00000000000; auto node = add_ipc_enabled_node (system, node_config); scoped_io_thread_name_change scoped_thread_name_io; nano::node_rpc_config node_rpc_config; @@ -3082,7 +3079,7 @@ TEST (rpc, work_generate_multiplier) ASSERT_EQ (response.json.get ("error"), ec.message ()); } { - double max_multiplier (nano::difficulty::to_multiplier (node->config.max_work_generate_difficulty, node->default_difficulty (nano::work_version::work_1))); + double max_multiplier (nano::difficulty::to_multiplier (node->max_work_generate_difficulty (nano::work_version::work_1), node->default_difficulty (nano::work_version::work_1))); request.put ("multiplier", max_multiplier + 1); test_response response (request, rpc.config.port, system.io_ctx); system.deadline_set (5s);