Moving threshold classes from free-functions with static network_params initialization on to nano::work_thresholds class where they belong.

This commit is contained in:
clemahieu 2021-08-08 19:14:42 +01:00
commit 0e8b7cdd73
No known key found for this signature in database
GPG key ID: 43708520C8DFB938
6 changed files with 45 additions and 52 deletions

View file

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

View file

@ -1,3 +1,4 @@
#include <nano/lib/blocks.hpp>
#include <nano/lib/config.hpp>
#include <boost/filesystem/path.hpp>
@ -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<uint64_t>::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.";

View file

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

View file

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

View file

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

View file

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