diff --git a/nano/lib/config.cpp b/nano/lib/config.cpp index 5fac9d43..53e65fd6 100644 --- a/nano/lib/config.cpp +++ b/nano/lib/config.cpp @@ -70,6 +70,24 @@ uint64_t nano::work_thresholds::threshold_entry (nano::work_version const versio return result; } +#ifndef NANO_FUZZER_TEST +uint64_t nano::work_thresholds::value (nano::root const & root_a, uint64_t work_a) +{ + uint64_t result; + blake2b_state hash; + blake2b_init (&hash, sizeof (result)); + blake2b_update (&hash, reinterpret_cast (&work_a), sizeof (work_a)); + blake2b_update (&hash, root_a.bytes.data (), root_a.bytes.size ()); + blake2b_final (&hash, reinterpret_cast (&result), sizeof (result)); + return result; +} +#else +uint64_t nano::work_thresholds::value (nano::root const & root_a, uint64_t work_a) +{ + return base + 1; +} +#endif + 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 0a88fa7d..e53aa5f6 100644 --- a/nano/lib/config.hpp +++ b/nano/lib/config.hpp @@ -82,6 +82,7 @@ enum class work_version work_1 }; enum class block_type : uint8_t; +class root; class work_thresholds { @@ -109,6 +110,7 @@ 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); /** 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 01db1322..17bc32e5 100644 --- a/nano/lib/work.cpp +++ b/nano/lib/work.cpp @@ -36,11 +36,12 @@ bool nano::work_validate_entry (nano::work_version const version_a, nano::root c uint64_t nano::work_difficulty (nano::work_version const version_a, nano::root const & root_a, uint64_t const work_a) { + static nano::network_constants network_constants; uint64_t result{ 0 }; switch (version_a) { case nano::work_version::work_1: - result = nano::work_v1::value (root_a, work_a); + result = network_constants.publish_thresholds.value (root_a, work_a); break; default: debug_assert (false && "Invalid version specified to work_difficulty"); @@ -98,30 +99,6 @@ uint64_t nano::work_v1::threshold (nano::block_details const details_a) return result; } -#ifndef NANO_FUZZER_TEST -uint64_t nano::work_v1::value (nano::root const & root_a, uint64_t work_a) -{ - uint64_t result; - blake2b_state hash; - blake2b_init (&hash, sizeof (result)); - blake2b_update (&hash, reinterpret_cast (&work_a), sizeof (work_a)); - blake2b_update (&hash, root_a.bytes.data (), root_a.bytes.size ()); - blake2b_final (&hash, reinterpret_cast (&result), sizeof (result)); - return result; -} -#else -uint64_t nano::work_v1::value (nano::root const & root_a, uint64_t work_a) -{ - static nano::network_constants network_constants; - if (!network_constants.is_dev_network ()) - { - debug_assert (false); - std::exit (1); - } - return network_constants.publish_thresholds.base + 1; -} -#endif - double nano::normalized_multiplier (double const multiplier_a, uint64_t const threshold_a) { static nano::network_constants network_constants; @@ -235,7 +212,7 @@ void nano::work_pool::loop (uint64_t thread) if (opt_work.is_initialized ()) { work = *opt_work; - output = nano::work_v1::value (current_l.item, work); + output = network_constants.publish_thresholds.value (current_l.item, work); } else { @@ -268,7 +245,7 @@ void nano::work_pool::loop (uint64_t thread) { // If the ticket matches what we started with, we're the ones that found the solution debug_assert (output >= current_l.difficulty); - debug_assert (current_l.difficulty == 0 || nano::work_v1::value (current_l.item, work) == output); + debug_assert (current_l.difficulty == 0 || network_constants.publish_thresholds.value (current_l.item, work) == output); // Signal other threads to stop their work next time they check ticket ++ticket; pending.pop_front (); diff --git a/nano/lib/work.hpp b/nano/lib/work.hpp index 4efad035..7cf9ae2e 100644 --- a/nano/lib/work.hpp +++ b/nano/lib/work.hpp @@ -29,7 +29,6 @@ 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 (nano::block_details const); } diff --git a/nano/nano_node/entry.cpp b/nano/nano_node/entry.cpp index ea9f20cd..674e129b 100644 --- a/nano/nano_node/entry.cpp +++ b/nano/nano_node/entry.cpp @@ -508,6 +508,7 @@ int main (int argc, char * const * argv) } else if (vm.count ("debug_profile_validate")) { + nano::network_constants network_constants; uint64_t difficulty{ nano::work_thresholds::publish_full.base }; std::cerr << "Starting validation profile" << std::endl; auto start (std::chrono::steady_clock::now ()); @@ -516,7 +517,7 @@ int main (int argc, char * const * argv) uint64_t count{ 10000000U }; // 10M for (uint64_t i (0); i < count; ++i) { - valid = nano::work_v1::value (hash, i) > difficulty; + valid = network_constants.publish_thresholds.value (hash, i) > difficulty; } std::ostringstream oss (valid ? "true" : "false"); // IO forces compiler to not dismiss the variable auto total_time (std::chrono::duration_cast (std::chrono::steady_clock::now () - start).count ());