From 3d71605cfdb627977c4bdf6d38b8cd35c30d9fb8 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Sun, 8 Aug 2021 23:20:17 +0100 Subject: [PATCH] Moving work_threshold_base from a free function to a member of work_thresholds. --- nano/core_test/difficulty.cpp | 2 +- nano/core_test/fakes/work_peer.hpp | 5 +++-- nano/core_test/work_pool.cpp | 6 +++--- nano/lib/config.cpp | 14 ++++++++++++++ nano/lib/config.hpp | 3 ++- nano/lib/work.cpp | 15 --------------- nano/lib/work.hpp | 1 - nano/node/node.cpp | 3 ++- nano/rpc_test/rpc.cpp | 6 +++--- 9 files changed, 28 insertions(+), 27 deletions(-) diff --git a/nano/core_test/difficulty.cpp b/nano/core_test/difficulty.cpp index ab44a59d..9f2fe0d9 100644 --- a/nano/core_test/difficulty.cpp +++ b/nano/core_test/difficulty.cpp @@ -129,7 +129,7 @@ TEST (difficulty, network_constants) nano::work_version version{ nano::work_version::work_1 }; ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.base, nano::dev::network_params.network.publish_thresholds.epoch_2); - ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.base, nano::work_threshold_base (version)); + ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.base, nano::dev::network_params.network.publish_thresholds.threshold_base (version)); ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.entry, nano::dev::network_params.network.publish_thresholds.threshold_entry (version, nano::block_type::state)); ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.epoch_1, nano::dev::network_params.network.publish_thresholds.threshold_entry (version, nano::block_type::send)); ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.epoch_1, nano::dev::network_params.network.publish_thresholds.threshold_entry (version, nano::block_type::receive)); diff --git a/nano/core_test/fakes/work_peer.hpp b/nano/core_test/fakes/work_peer.hpp index 03452669..4e03504e 100644 --- a/nano/core_test/fakes/work_peer.hpp +++ b/nano/core_test/fakes/work_peer.hpp @@ -138,10 +138,11 @@ private: void handle_generate (nano::block_hash const & hash_a) { + static nano::network_constants constants; if (type == work_peer_type::good) { auto hash = hash_a; - auto request_difficulty = nano::work_threshold_base (version); + auto request_difficulty = constants.publish_thresholds.threshold_base (version); auto this_l (shared_from_this ()); work_pool.generate (version, hash, request_difficulty, [this_l, hash] (boost::optional work_a) { auto result = work_a.value_or (0); @@ -149,7 +150,7 @@ private: ptree::ptree message_l; message_l.put ("work", nano::to_string_hex (result)); message_l.put ("difficulty", nano::to_string_hex (result_difficulty)); - message_l.put ("multiplier", nano::to_string (nano::difficulty::to_multiplier (result_difficulty, nano::work_threshold_base (this_l->version)))); + message_l.put ("multiplier", nano::to_string (nano::difficulty::to_multiplier (result_difficulty, constants.publish_thresholds.threshold_base (this_l->version)))); message_l.put ("hash", hash.to_string ()); std::stringstream ostream; ptree::write_json (ostream, message_l); diff --git a/nano/core_test/work_pool.cpp b/nano/core_test/work_pool.cpp index c7f7ed22..22328873 100644 --- a/nano/core_test/work_pool.cpp +++ b/nano/core_test/work_pool.cpp @@ -19,7 +19,7 @@ TEST (work, one) nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::change_block block (1, 1, nano::keypair ().prv, 3, 4); block.block_work_set (*pool.generate (block.root ())); - ASSERT_LT (nano::work_threshold_base (block.work_version ()), block.difficulty ()); + ASSERT_LT (nano::dev::network_params.network.publish_thresholds.threshold_base (block.work_version ()), block.difficulty ()); } TEST (work, disabled) @@ -33,9 +33,9 @@ TEST (work, validate) { nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits::max () }; nano::send_block send_block (1, 1, 2, nano::keypair ().prv, 4, 6); - ASSERT_LT (send_block.difficulty (), nano::work_threshold_base (send_block.work_version ())); + ASSERT_LT (send_block.difficulty (), nano::dev::network_params.network.publish_thresholds.threshold_base (send_block.work_version ())); send_block.block_work_set (*pool.generate (send_block.root ())); - ASSERT_LT (nano::work_threshold_base (send_block.work_version ()), send_block.difficulty ()); + ASSERT_LT (nano::dev::network_params.network.publish_thresholds.threshold_base (send_block.work_version ()), send_block.difficulty ()); } TEST (work, cancel) diff --git a/nano/lib/config.cpp b/nano/lib/config.cpp index 05c6370e..ca7e95a7 100644 --- a/nano/lib/config.cpp +++ b/nano/lib/config.cpp @@ -166,6 +166,20 @@ double nano::work_thresholds::denormalized_multiplier (double const multiplier_a return multiplier; } +uint64_t nano::work_thresholds::threshold_base (nano::work_version const version_a) +{ + uint64_t result{ std::numeric_limits::max () }; + switch (version_a) + { + case nano::work_version::work_1: + result = base; + break; + default: + debug_assert (false && "Invalid version specified to work_threshold_base"); + } + return result; +} + 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 53c6a592..50cea18c 100644 --- a/nano/lib/config.hpp +++ b/nano/lib/config.hpp @@ -111,10 +111,11 @@ public: } uint64_t threshold_entry (nano::work_version const, nano::block_type const); - uint64_t value (nano::root const & root_a, uint64_t work_a); uint64_t threshold (nano::block_details const &); // Ledger threshold uint64_t threshold (nano::work_version const, nano::block_details const); + uint64_t threshold_base (nano::work_version const); + uint64_t value (nano::root const & root_a, uint64_t work_a); double normalized_multiplier (double const, uint64_t const); double denormalized_multiplier (double const, uint64_t const); diff --git a/nano/lib/work.cpp b/nano/lib/work.cpp index 7c290dd3..0fcd7524 100644 --- a/nano/lib/work.cpp +++ b/nano/lib/work.cpp @@ -49,21 +49,6 @@ uint64_t nano::work_difficulty (nano::work_version const version_a, nano::root c return result; } -uint64_t nano::work_threshold_base (nano::work_version const version_a) -{ - uint64_t result{ std::numeric_limits::max () }; - switch (version_a) - { - case nano::work_version::work_1: - static nano::network_constants network_constants; - result = network_constants.publish_thresholds.base; - break; - default: - debug_assert (false && "Invalid version specified to work_threshold_base"); - } - return result; -} - 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 aca28c68..7b3599de 100644 --- a/nano/lib/work.hpp +++ b/nano/lib/work.hpp @@ -23,7 +23,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); class opencl_work; class work_item final { diff --git a/nano/node/node.cpp b/nano/node/node.cpp index f50e1e98..9e6a75be 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -1110,11 +1110,12 @@ int nano::node::price (nano::uint128_t const & balance_a, int amount_a) uint64_t nano::node::default_difficulty (nano::work_version const version_a) const { + nano::network_constants constants; uint64_t result{ std::numeric_limits::max () }; switch (version_a) { case nano::work_version::work_1: - result = nano::work_threshold_base (version_a); + result = constants.publish_thresholds.threshold_base (version_a); break; default: debug_assert (false && "Invalid version specified to default_difficulty"); diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 0580191f..985ecdbe 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -2215,7 +2215,7 @@ TEST (rpc, work_peer_bad) ASSERT_TRUE (work_a.is_initialized ()); work = *work_a; }); - ASSERT_TIMELY (5s, nano::work_difficulty (nano::work_version::work_1, hash1, work) >= nano::work_threshold_base (nano::work_version::work_1)); + ASSERT_TIMELY (5s, nano::work_difficulty (nano::work_version::work_1, hash1, work) >= nano::dev::network_params.network.publish_thresholds.threshold_base (nano::work_version::work_1)); } TEST (rpc, work_peer_one) @@ -2231,7 +2231,7 @@ TEST (rpc, work_peer_one) ASSERT_TRUE (work_a.is_initialized ()); work = *work_a; }); - ASSERT_TIMELY (5s, nano::work_difficulty (nano::work_version::work_1, key1.pub, work) >= nano::work_threshold_base (nano::work_version::work_1)); + ASSERT_TIMELY (5s, nano::work_difficulty (nano::work_version::work_1, key1.pub, work) >= nano::dev::network_params.network.publish_thresholds.threshold_base (nano::work_version::work_1)); } TEST (rpc, work_peer_many) @@ -2258,7 +2258,7 @@ TEST (rpc, work_peer_many) node1.work_generate (nano::work_version::work_1, key1.pub, node1.network_params.network.publish_thresholds.base, [&work = works[i]] (boost::optional work_a) { work = *work_a; }); - while (nano::work_difficulty (nano::work_version::work_1, key1.pub, works[i]) < nano::work_threshold_base (nano::work_version::work_1)) + while (nano::work_difficulty (nano::work_version::work_1, key1.pub, works[i]) < nano::dev::network_params.network.publish_thresholds.threshold_base (nano::work_version::work_1)) { system1.poll (); system2.poll ();