Moving multiplier normalization from being a free function in to work_thresholds whre it belongs and to remove static instantiation of network_constants.

This commit is contained in:
clemahieu 2021-08-08 22:13:26 +01:00
commit 8e53b142d6
No known key found for this signature in database
GPG key ID: 43708520C8DFB938
6 changed files with 47 additions and 50 deletions

View file

@ -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.";

View file

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

View file

@ -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<boost::optional<uint64_t> (nano::work_version const, nano::root const &, uint64_t, std::atomic<int> &)> opencl_a) :
network_constants{ network_constants },
ticket (0),

View file

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

View file

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

View file

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