diff --git a/nano/lib/config.cpp b/nano/lib/config.cpp index 84f8f39e..05c6370e 100644 --- a/nano/lib/config.cpp +++ b/nano/lib/config.cpp @@ -123,6 +123,49 @@ uint64_t nano::work_thresholds::threshold (nano::work_version const version_a, n return result; } +double nano::work_thresholds::normalized_multiplier (double const multiplier_a, uint64_t const threshold_a) +{ + debug_assert (multiplier_a >= 1); + auto multiplier (multiplier_a); + /* Normalization rules + ratio = multiplier of max work threshold (send epoch 2) from given threshold + i.e. max = 0xfe00000000000000, given = 0xf000000000000000, ratio = 8.0 + normalized = (multiplier + (ratio - 1)) / ratio; + Epoch 1 + multiplier | normalized + 1.0 | 1.0 + 9.0 | 2.0 + 25.0 | 4.0 + Epoch 2 (receive / epoch subtypes) + multiplier | normalized + 1.0 | 1.0 + 65.0 | 2.0 + 241.0 | 4.0 + */ + if (threshold_a == epoch_1 || threshold_a == epoch_2_receive) + { + auto ratio (nano::difficulty::to_multiplier (epoch_2, threshold_a)); + debug_assert (ratio >= 1); + multiplier = (multiplier + (ratio - 1.0)) / ratio; + debug_assert (multiplier >= 1); + } + return multiplier; +} + +double nano::work_thresholds::denormalized_multiplier (double const multiplier_a, uint64_t const threshold_a) +{ + debug_assert (multiplier_a >= 1); + auto multiplier (multiplier_a); + if (threshold_a == epoch_1 || threshold_a == epoch_2_receive) + { + auto ratio (nano::difficulty::to_multiplier (epoch_2, threshold_a)); + debug_assert (ratio >= 1); + multiplier = multiplier * ratio + 1.0 - ratio; + debug_assert (multiplier >= 1); + } + return multiplier; +} + namespace nano { const char * network_constants::active_network_err_msg = "Invalid network. Valid values are live, test, beta and dev."; diff --git a/nano/lib/config.hpp b/nano/lib/config.hpp index 69c96d01..53c6a592 100644 --- a/nano/lib/config.hpp +++ b/nano/lib/config.hpp @@ -115,6 +115,8 @@ public: uint64_t threshold (nano::block_details const &); // Ledger threshold uint64_t threshold (nano::work_version const, nano::block_details const); + double normalized_multiplier (double const, uint64_t const); + double denormalized_multiplier (double const, uint64_t const); /** Network work thresholds. Define these inline as constexpr when moving to cpp17. */ static const nano::work_thresholds publish_full; diff --git a/nano/lib/work.cpp b/nano/lib/work.cpp index 0873fc4c..7c290dd3 100644 --- a/nano/lib/work.cpp +++ b/nano/lib/work.cpp @@ -64,51 +64,6 @@ uint64_t nano::work_threshold_base (nano::work_version const version_a) return result; } -double nano::normalized_multiplier (double const multiplier_a, uint64_t const threshold_a) -{ - static nano::network_constants network_constants; - debug_assert (multiplier_a >= 1); - auto multiplier (multiplier_a); - /* Normalization rules - ratio = multiplier of max work threshold (send epoch 2) from given threshold - i.e. max = 0xfe00000000000000, given = 0xf000000000000000, ratio = 8.0 - normalized = (multiplier + (ratio - 1)) / ratio; - Epoch 1 - multiplier | normalized - 1.0 | 1.0 - 9.0 | 2.0 - 25.0 | 4.0 - Epoch 2 (receive / epoch subtypes) - multiplier | normalized - 1.0 | 1.0 - 65.0 | 2.0 - 241.0 | 4.0 - */ - if (threshold_a == network_constants.publish_thresholds.epoch_1 || threshold_a == network_constants.publish_thresholds.epoch_2_receive) - { - auto ratio (nano::difficulty::to_multiplier (network_constants.publish_thresholds.epoch_2, threshold_a)); - debug_assert (ratio >= 1); - multiplier = (multiplier + (ratio - 1.0)) / ratio; - debug_assert (multiplier >= 1); - } - return multiplier; -} - -double nano::denormalized_multiplier (double const multiplier_a, uint64_t const threshold_a) -{ - static nano::network_constants network_constants; - debug_assert (multiplier_a >= 1); - auto multiplier (multiplier_a); - if (threshold_a == network_constants.publish_thresholds.epoch_1 || threshold_a == network_constants.publish_thresholds.epoch_2_receive) - { - auto ratio (nano::difficulty::to_multiplier (network_constants.publish_thresholds.epoch_2, threshold_a)); - debug_assert (ratio >= 1); - multiplier = multiplier * ratio + 1.0 - ratio; - debug_assert (multiplier >= 1); - } - return multiplier; -} - nano::work_pool::work_pool (nano::network_constants & network_constants, unsigned max_threads_a, std::chrono::nanoseconds pow_rate_limiter_a, std::function (nano::work_version const, nano::root const &, uint64_t, std::atomic &)> opencl_a) : network_constants{ network_constants }, ticket (0), diff --git a/nano/lib/work.hpp b/nano/lib/work.hpp index 0d878363..aca28c68 100644 --- a/nano/lib/work.hpp +++ b/nano/lib/work.hpp @@ -24,9 +24,6 @@ bool work_validate_entry (nano::work_version const, nano::root const &, uint64_t uint64_t work_difficulty (nano::work_version const, nano::root const &, uint64_t const); uint64_t work_threshold_base (nano::work_version const); - -double normalized_multiplier (double const, uint64_t const); -double denormalized_multiplier (double const, uint64_t const); class opencl_work; class work_item final { diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index 6dbf81a0..25ce7901 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -1027,7 +1027,7 @@ void nano::json_handler::active_difficulty () auto const multiplier_active = 1.0; auto const default_difficulty (node.default_difficulty (nano::work_version::work_1)); auto const default_receive_difficulty (node.default_receive_difficulty (nano::work_version::work_1)); - auto const receive_current_denormalized (nano::denormalized_multiplier (multiplier_active, node.network_params.network.publish_thresholds.epoch_2_receive)); + auto const receive_current_denormalized (node.network_params.network.publish_thresholds.denormalized_multiplier (multiplier_active, node.network_params.network.publish_thresholds.epoch_2_receive)); response_l.put ("deprecated", "1"); response_l.put ("network_minimum", nano::to_string_hex (default_difficulty)); response_l.put ("network_receive_minimum", nano::to_string_hex (default_receive_difficulty)); diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 948d0c11..0580191f 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -5474,7 +5474,7 @@ TEST (rpc, active_difficulty) uint64_t network_receive_current; ASSERT_FALSE (nano::from_string_hex (network_receive_current_text, network_receive_current)); auto network_receive_current_multiplier (nano::difficulty::to_multiplier (network_receive_current, network_receive_minimum)); - auto network_receive_current_normalized_multiplier (nano::normalized_multiplier (network_receive_current_multiplier, network_receive_minimum)); + auto network_receive_current_normalized_multiplier (nano::dev::network_params.network.publish_thresholds.normalized_multiplier (network_receive_current_multiplier, network_receive_minimum)); ASSERT_NEAR (network_receive_current_normalized_multiplier, multiplier, 1e-6); ASSERT_EQ (response.not_found (), response.find ("difficulty_trend")); }