From 0e8b7cdd734b2be1848c6bc5fedcf8facde572fc Mon Sep 17 00:00:00 2001 From: clemahieu Date: Sun, 8 Aug 2021 19:14:42 +0100 Subject: [PATCH] Moving threshold classes from free-functions with static network_params initialization on to nano::work_thresholds class where they belong. --- nano/core_test/difficulty.cpp | 10 ++++---- nano/lib/config.cpp | 22 ++++++++++++++++++ nano/lib/config.hpp | 12 +++++++++- nano/lib/work.cpp | 43 +++++------------------------------ nano/lib/work.hpp | 8 ------- nano/node/json_handler.cpp | 2 +- 6 files changed, 45 insertions(+), 52 deletions(-) diff --git a/nano/core_test/difficulty.cpp b/nano/core_test/difficulty.cpp index 5034e16b..499b6462 100644 --- a/nano/core_test/difficulty.cpp +++ b/nano/core_test/difficulty.cpp @@ -130,11 +130,11 @@ 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.entry, nano::work_threshold_entry (version, nano::block_type::state)); - ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.epoch_1, nano::work_threshold_entry (version, nano::block_type::send)); - ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.epoch_1, nano::work_threshold_entry (version, nano::block_type::receive)); - ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.epoch_1, nano::work_threshold_entry (version, nano::block_type::open)); - ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.epoch_1, nano::work_threshold_entry (version, nano::block_type::change)); + 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)); + ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.epoch_1, nano::dev::network_params.network.publish_thresholds.threshold_entry (version, nano::block_type::open)); + ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.epoch_1, nano::dev::network_params.network.publish_thresholds.threshold_entry (version, nano::block_type::change)); ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.epoch_1, nano::work_threshold (version, nano::block_details (nano::epoch::epoch_0, false, false, false))); ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.epoch_1, nano::work_threshold (version, nano::block_details (nano::epoch::epoch_1, false, false, false))); ASSERT_EQ (nano::dev::network_params.network.publish_thresholds.epoch_1, nano::work_threshold (version, nano::block_details (nano::epoch::epoch_1, false, false, false))); diff --git a/nano/lib/config.cpp b/nano/lib/config.cpp index 72c0af2d..5fac9d43 100644 --- a/nano/lib/config.cpp +++ b/nano/lib/config.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -48,6 +49,27 @@ get_env_threshold_or_default ("NANO_TEST_EPOCH_2", 0xfffffff800000000), // 8x hi get_env_threshold_or_default ("NANO_TEST_EPOCH_2_RECV", 0xfffffe0000000000) // 8x lower than epoch_1 ); +uint64_t nano::work_thresholds::threshold_entry (nano::work_version const version_a, nano::block_type const type_a) +{ + uint64_t result{ std::numeric_limits::max () }; + if (type_a == nano::block_type::state) + { + switch (version_a) + { + case nano::work_version::work_1: + result = entry; + break; + default: + debug_assert (false && "Invalid version specified to work_threshold_entry"); + } + } + else + { + result = epoch_1; + } + 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 6bcc9879..0a88fa7d 100644 --- a/nano/lib/config.hpp +++ b/nano/lib/config.hpp @@ -76,8 +76,16 @@ enum class networks : uint16_t nano_test_network = 0x5258, // 'R', 'X' }; -struct work_thresholds +enum class work_version { + unspecified, + work_1 +}; +enum class block_type : uint8_t; + +class work_thresholds +{ +public: uint64_t const epoch_1; uint64_t const epoch_2; uint64_t const epoch_2_receive; @@ -100,6 +108,8 @@ struct work_thresholds return other_a; } + uint64_t threshold_entry (nano::work_version const, nano::block_type const); + /** Network work thresholds. Define these inline as constexpr when moving to cpp17. */ static const nano::work_thresholds publish_full; static const nano::work_thresholds publish_beta; diff --git a/nano/lib/work.cpp b/nano/lib/work.cpp index bd7309ba..01db1322 100644 --- a/nano/lib/work.cpp +++ b/nano/lib/work.cpp @@ -24,12 +24,14 @@ std::string nano::to_string (nano::work_version const version_a) bool nano::work_validate_entry (nano::block const & block_a) { - return block_a.difficulty () < nano::work_threshold_entry (block_a.work_version (), block_a.type ()); + static nano::network_constants network_constants; + return block_a.difficulty () < network_constants.publish_thresholds.threshold_entry (block_a.work_version (), block_a.type ()); } bool nano::work_validate_entry (nano::work_version const version_a, nano::root const & root_a, uint64_t const work_a) { - return nano::work_difficulty (version_a, root_a, work_a) < nano::work_threshold_entry (version_a, nano::block_type::state); + static nano::network_constants network_constants; + return nano::work_difficulty (version_a, root_a, work_a) < network_constants.publish_thresholds.threshold_entry (version_a, nano::block_type::state); } uint64_t nano::work_difficulty (nano::work_version const version_a, nano::root const & root_a, uint64_t const work_a) @@ -52,7 +54,8 @@ uint64_t nano::work_threshold_base (nano::work_version const version_a) switch (version_a) { case nano::work_version::work_1: - result = nano::work_v1::threshold_base (); + static nano::network_constants network_constants; + result = network_constants.publish_thresholds.base; break; default: debug_assert (false && "Invalid version specified to work_threshold_base"); @@ -60,28 +63,6 @@ uint64_t nano::work_threshold_base (nano::work_version const version_a) return result; } -uint64_t nano::work_threshold_entry (nano::work_version const version_a, nano::block_type const type_a) -{ - uint64_t result{ std::numeric_limits::max () }; - if (type_a == nano::block_type::state) - { - switch (version_a) - { - case nano::work_version::work_1: - result = nano::work_v1::threshold_entry (); - break; - default: - debug_assert (false && "Invalid version specified to work_threshold_entry"); - } - } - else - { - static nano::network_constants network_constants; - result = network_constants.publish_thresholds.epoch_1; - } - return result; -} - uint64_t nano::work_threshold (nano::work_version const version_a, nano::block_details const details_a) { uint64_t result{ std::numeric_limits::max () }; @@ -96,18 +77,6 @@ uint64_t nano::work_threshold (nano::work_version const version_a, nano::block_d return result; } -uint64_t nano::work_v1::threshold_base () -{ - static nano::network_constants network_constants; - return network_constants.publish_thresholds.base; -} - -uint64_t nano::work_v1::threshold_entry () -{ - static nano::network_constants network_constants; - return network_constants.publish_thresholds.entry; -} - uint64_t nano::work_v1::threshold (nano::block_details const details_a) { static_assert (nano::epoch::max == nano::epoch::epoch_2, "work_v1::threshold is ill-defined"); diff --git a/nano/lib/work.hpp b/nano/lib/work.hpp index 26fb6db6..4efad035 100644 --- a/nano/lib/work.hpp +++ b/nano/lib/work.hpp @@ -13,11 +13,6 @@ namespace nano { -enum class work_version -{ - unspecified, - work_1 -}; std::string to_string (nano::work_version const version_a); class block; @@ -29,15 +24,12 @@ 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); -uint64_t work_threshold_entry (nano::work_version const, nano::block_type const); // Ledger threshold uint64_t work_threshold (nano::work_version const, nano::block_details const); namespace work_v1 { uint64_t value (nano::root const & root_a, uint64_t work_a); - uint64_t threshold_base (); - uint64_t threshold_entry (); uint64_t threshold (nano::block_details const); } diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index 47fc1c1f..9c0d6976 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -4872,7 +4872,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.max_work_generate_difficulty (work_version) || difficulty < nano::work_threshold_entry (work_version, nano::block_type::state))) + if (!ec && (difficulty > node.max_work_generate_difficulty (work_version) || difficulty < node.network_params.network.publish_thresholds.threshold_entry (work_version, nano::block_type::state))) { ec = nano::error_rpc::difficulty_limit; }