Skip to content

Commit

Permalink
Work version parameter in default_difficulty and use it in more places (
Browse files Browse the repository at this point in the history
#2690)

* Receive work version in default_difficulty and use it in more places

* Empty commit to trigger actions

* Update test
  • Loading branch information
guilhermelawless authored Mar 31, 2020
1 parent 9790605 commit 8d4a447
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 43 deletions.
6 changes: 3 additions & 3 deletions nano/core_test/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ TEST (wallet, work)
uint64_t work (0);
if (!wallet->store.work_get (transaction, nano::test_genesis_key.pub, work))
{
done = nano::work_difficulty (genesis.open->work_version (), genesis.hash (), work) >= system.nodes[0]->default_difficulty ();
done = nano::work_difficulty (genesis.open->work_version (), genesis.hash (), work) >= system.nodes[0]->default_difficulty (genesis.open->work_version ());
}
ASSERT_NO_ERROR (system.poll ());
}
Expand Down Expand Up @@ -683,7 +683,7 @@ TEST (wallet, work_generate)
ASSERT_NO_ERROR (system.poll ());
auto block_transaction (node1.store.tx_begin_read ());
auto transaction (system.wallet (0)->wallets.tx_begin_read ());
again = wallet->store.work_get (transaction, account1, work1) || nano::work_difficulty (block->work_version (), node1.ledger.latest_root (block_transaction, account1), work1) < node1.default_difficulty ();
again = wallet->store.work_get (transaction, account1, work1) || nano::work_difficulty (block->work_version (), node1.ledger.latest_root (block_transaction, account1), work1) < node1.default_difficulty (block->work_version ());
}
}

Expand All @@ -706,7 +706,7 @@ TEST (wallet, work_cache_delayed)
auto block2 (wallet->send_action (nano::test_genesis_key.pub, key.pub, 100));
ASSERT_EQ (block2->hash (), node1.latest (nano::test_genesis_key.pub));
ASSERT_EQ (block2->hash (), node1.wallets.delayed_work->operator[] (nano::test_genesis_key.pub));
auto threshold (node1.default_difficulty ());
auto threshold (node1.default_difficulty (nano::work_version::work_1));
auto again (true);
system.deadline_set (10s);
while (again)
Expand Down
6 changes: 3 additions & 3 deletions nano/core_test/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,15 +680,15 @@ TEST (websocket, work)
auto & request = contents.get_child ("request");
ASSERT_EQ (request.get<std::string> ("version"), nano::to_string (nano::work_version::work_1));
ASSERT_EQ (request.get<std::string> ("hash"), hash.to_string ());
ASSERT_EQ (request.get<std::string> ("difficulty"), nano::to_string_hex (node1->default_difficulty ()));
ASSERT_EQ (request.get<std::string> ("difficulty"), nano::to_string_hex (node1->default_difficulty (nano::work_version::work_1)));
ASSERT_EQ (request.get<double> ("multiplier"), 1.0);

ASSERT_EQ (1, contents.count ("result"));
auto & result = contents.get_child ("result");
uint64_t result_difficulty;
nano::from_string_hex (result.get<std::string> ("difficulty"), result_difficulty);
ASSERT_GE (result_difficulty, node1->default_difficulty ());
ASSERT_NEAR (result.get<double> ("multiplier"), nano::difficulty::to_multiplier (result_difficulty, node1->default_difficulty ()), 1e-6);
ASSERT_GE (result_difficulty, node1->default_difficulty (nano::work_version::work_1));
ASSERT_NEAR (result.get<double> ("multiplier"), nano::difficulty::to_multiplier (result_difficulty, node1->default_difficulty (nano::work_version::work_1)), 1e-6);
ASSERT_EQ (result.get<std::string> ("work"), nano::to_string_hex (work.get ()));

ASSERT_EQ (1, contents.count ("bad_peers"));
Expand Down
8 changes: 4 additions & 4 deletions nano/node/distributed_work.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ nano::distributed_work::~distributed_work ()
nano::websocket::message_builder builder;
if (status == work_generation_status::success)
{
node_l->websocket_server->broadcast (builder.work_generation (request.version, request.root, work_result, request.difficulty, node_l->default_difficulty (), elapsed.value (), winner, bad_peers));
node_l->websocket_server->broadcast (builder.work_generation (request.version, request.root, work_result, request.difficulty, node_l->default_difficulty (request.version), elapsed.value (), winner, bad_peers));
}
else if (status == work_generation_status::cancelled)
{
node_l->websocket_server->broadcast (builder.work_cancelled (request.version, request.root, request.difficulty, node_l->default_difficulty (), elapsed.value (), bad_peers));
node_l->websocket_server->broadcast (builder.work_cancelled (request.version, request.root, request.difficulty, node_l->default_difficulty (request.version), elapsed.value (), bad_peers));
}
else if (status == work_generation_status::failure_local || status == work_generation_status::failure_peers)
{
node_l->websocket_server->broadcast (builder.work_failed (request.version, request.root, request.difficulty, node_l->default_difficulty (), elapsed.value (), bad_peers));
node_l->websocket_server->broadcast (builder.work_failed (request.version, request.root, request.difficulty, node_l->default_difficulty (request.version), elapsed.value (), bad_peers));
}
}
stop_once (true);
Expand Down Expand Up @@ -321,7 +321,7 @@ void nano::distributed_work::set_once (uint64_t const work_a, std::string const
if (node.config.logging.work_generation_time ())
{
boost::format unformatted_l ("Work generation for %1%, with a threshold difficulty of %2% (multiplier %3%x) complete: %4% ms");
auto multiplier_text_l (nano::to_string (nano::difficulty::to_multiplier (request.difficulty, node.network_params.network.publish_thresholds.base), 2));
auto multiplier_text_l (nano::to_string (nano::difficulty::to_multiplier (request.difficulty, node.default_difficulty (request.version)), 2));
node.logger.try_log (boost::str (unformatted_l % request.root.to_string () % nano::to_string_hex (request.difficulty) % multiplier_text_l % elapsed.value ().count ()));
}
}
Expand Down
28 changes: 14 additions & 14 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ uint64_t nano::json_handler::work_optional_impl ()
return result;
}

uint64_t nano::json_handler::difficulty_optional_impl ()
uint64_t nano::json_handler::difficulty_optional_impl (nano::work_version const version_a)
{
auto difficulty (node.default_difficulty ());
auto difficulty (node.default_difficulty (version_a));
boost::optional<std::string> difficulty_text (request.get_optional<std::string> ("difficulty"));
if (!ec && difficulty_text.is_initialized ())
{
Expand All @@ -357,7 +357,7 @@ uint64_t nano::json_handler::difficulty_optional_impl ()
return difficulty;
}

double nano::json_handler::multiplier_optional_impl (uint64_t & difficulty)
double nano::json_handler::multiplier_optional_impl (nano::work_version const version_a, uint64_t & difficulty)
{
double multiplier (1.);
boost::optional<std::string> multiplier_text (request.get_optional<std::string> ("multiplier"));
Expand All @@ -366,7 +366,7 @@ double nano::json_handler::multiplier_optional_impl (uint64_t & difficulty)
auto success = boost::conversion::try_lexical_convert<double> (multiplier_text.get (), multiplier);
if (success && multiplier > 0.)
{
difficulty = nano::difficulty::from_multiplier (multiplier, node.network_params.network.publish_thresholds.base);
difficulty = nano::difficulty::from_multiplier (multiplier, node.default_difficulty (version_a));
}
else
{
Expand Down Expand Up @@ -1248,7 +1248,9 @@ void nano::json_handler::block_create ()
{
std::string type (request.get<std::string> ("type"));
nano::wallet_id wallet (0);
auto difficulty_l (difficulty_optional_impl ());
// Default to work_1 if not specified
auto work_version (work_version_optional_impl (nano::work_version::work_1));
auto difficulty_l (difficulty_optional_impl (work_version));
boost::optional<std::string> wallet_text (request.get_optional<std::string> ("wallet"));
if (!ec && wallet_text.is_initialized ())
{
Expand Down Expand Up @@ -1294,8 +1296,6 @@ void nano::json_handler::block_create ()
}
}
auto work (work_optional_impl ());
// Default to work_1 if not specified
auto work_version (work_version_optional_impl (nano::work_version::work_1));
nano::raw_key prv;
prv.data.clear ();
nano::block_hash previous (0);
Expand Down Expand Up @@ -4950,9 +4950,9 @@ void nano::json_handler::work_generate ()
if (!ec)
{
auto hash (hash_impl ());
auto difficulty (difficulty_optional_impl ());
multiplier_optional_impl (difficulty);
if (!ec && (difficulty > node.config.max_work_generate_difficulty || difficulty < node.network_params.network.publish_thresholds.entry))
auto difficulty (difficulty_optional_impl (work_version));
multiplier_optional_impl (work_version, difficulty);
if (!ec && (difficulty > node.config.max_work_generate_difficulty || difficulty < nano::work_threshold_entry (work_version)))
{
ec = nano::error_rpc::difficulty_limit;
}
Expand All @@ -4970,7 +4970,7 @@ void nano::json_handler::work_generate ()
std::stringstream ostream;
auto result_difficulty (nano::work_difficulty (work_version, hash, work));
response_l.put ("difficulty", nano::to_string_hex (result_difficulty));
auto result_multiplier = nano::difficulty::to_multiplier (result_difficulty, node.network_params.network.publish_thresholds.base);
auto result_multiplier = nano::difficulty::to_multiplier (result_difficulty, node.default_difficulty (work_version));
response_l.put ("multiplier", nano::to_string (result_multiplier));
boost::property_tree::write_json (ostream, response_l);
rpc_l->response (ostream.str ());
Expand Down Expand Up @@ -5076,16 +5076,16 @@ void nano::json_handler::work_validate ()
{
auto hash (hash_impl ());
auto work (work_optional_impl ());
auto difficulty (difficulty_optional_impl ());
multiplier_optional_impl (difficulty);
// Default to work_1 if not specified
auto work_version (work_version_optional_impl (nano::work_version::work_1));
auto difficulty (difficulty_optional_impl (work_version));
multiplier_optional_impl (work_version, difficulty);
if (!ec)
{
auto result_difficulty (nano::work_difficulty (work_version, hash, work));
response_l.put ("valid", (result_difficulty >= difficulty) ? "1" : "0");
response_l.put ("difficulty", nano::to_string_hex (result_difficulty));
auto result_multiplier = nano::difficulty::to_multiplier (result_difficulty, node.default_difficulty ());
auto result_multiplier = nano::difficulty::to_multiplier (result_difficulty, node.default_difficulty (work_version));
response_l.put ("multiplier", nano::to_string (result_multiplier));
}
response_errors ();
Expand Down
4 changes: 2 additions & 2 deletions nano/node/json_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ class json_handler : public std::enable_shared_from_this<nano::json_handler>
uint64_t count_impl ();
uint64_t count_optional_impl (uint64_t = std::numeric_limits<uint64_t>::max ());
uint64_t offset_optional_impl (uint64_t = 0);
uint64_t difficulty_optional_impl ();
double multiplier_optional_impl (uint64_t &);
uint64_t difficulty_optional_impl (nano::work_version const);
double multiplier_optional_impl (nano::work_version const, uint64_t &);
nano::work_version work_version_optional_impl (nano::work_version const default_a);
bool enable_sign_hash{ false };
std::function<void()> stop_callback;
Expand Down
17 changes: 13 additions & 4 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1004,9 +1004,18 @@ int nano::node::price (nano::uint128_t const & balance_a, int amount_a)
return static_cast<int> (result * 100.0);
}

uint64_t nano::node::default_difficulty () const
uint64_t nano::node::default_difficulty (nano::work_version const version_a) const
{
return ledger.cache.epoch_2_started ? network_params.network.publish_thresholds.base : network_params.network.publish_thresholds.epoch_1;
uint64_t result{ std::numeric_limits<uint64_t>::max () };
switch (version_a)
{
case nano::work_version::work_1:
result = ledger.cache.epoch_2_started ? nano::work_threshold_base (version_a) : network_params.network.publish_thresholds.epoch_1;
break;
default:
debug_assert (false && "Invalid version specified to default_difficulty");
}
return result;
}

bool nano::node::local_work_generation_enabled () const
Expand Down Expand Up @@ -1058,13 +1067,13 @@ boost::optional<uint64_t> nano::node::work_generate_blocking (nano::work_version
boost::optional<uint64_t> nano::node::work_generate_blocking (nano::block & block_a)
{
debug_assert (network_params.network.is_test_network ());
return work_generate_blocking (block_a, default_difficulty ());
return work_generate_blocking (block_a, default_difficulty (nano::work_version::work_1));
}

boost::optional<uint64_t> nano::node::work_generate_blocking (nano::root const & root_a)
{
debug_assert (network_params.network.is_test_network ());
return work_generate_blocking (root_a, default_difficulty ());
return work_generate_blocking (root_a, default_difficulty (nano::work_version::work_1));
}

boost::optional<uint64_t> nano::node::work_generate_blocking (nano::root const & root_a, uint64_t difficulty_a)
Expand Down
2 changes: 1 addition & 1 deletion nano/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class node final : public std::enable_shared_from_this<nano::node>
void unchecked_cleanup ();
int price (nano::uint128_t const &, int);
// The default difficulty updates to base only when the first epoch_2 block is processed
uint64_t default_difficulty () const;
uint64_t default_difficulty (nano::work_version const) const;
bool local_work_generation_enabled () const;
bool work_generation_enabled () const;
bool work_generation_enabled (std::vector<std::pair<std::string, uint16_t>> const &) const;
Expand Down
2 changes: 1 addition & 1 deletion nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ void nano::wallet::work_cache_blocking (nano::account const & account_a, nano::r
{
if (wallets.node.work_generation_enabled ())
{
auto difficulty (wallets.node.default_difficulty ());
auto difficulty (wallets.node.default_difficulty (nano::work_version::work_1));
auto opt_work_l (wallets.node.work_generate_blocking (nano::work_version::work_1, root_a, difficulty, account_a));
if (opt_work_l.is_initialized ())
{
Expand Down
22 changes: 11 additions & 11 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2310,7 +2310,7 @@ TEST (rpc, payment_begin_end)
ASSERT_LT (work, 50);
}
system.deadline_set (10s);
while (nano::work_difficulty (nano::work_version::work_1, root1, work) < node1->default_difficulty ())
while (nano::work_difficulty (nano::work_version::work_1, root1, work) < node1->default_difficulty (nano::work_version::work_1))
{
auto ec = system.poll ();
auto transaction (wallet->wallets.tx_begin_read ());
Expand Down Expand Up @@ -2920,7 +2920,7 @@ TEST (rpc, work_generate)
ASSERT_FALSE (nano::from_string_hex (response_difficulty_text, response_difficulty));
ASSERT_EQ (result_difficulty, response_difficulty);
auto multiplier = response.json.get<double> ("multiplier");
ASSERT_NEAR (nano::difficulty::to_multiplier (result_difficulty, node->network_params.network.publish_thresholds.base), multiplier, 1e-6);
ASSERT_NEAR (nano::difficulty::to_multiplier (result_difficulty, node->default_difficulty (nano::work_version::work_1)), multiplier, 1e-6);
};
verify_response (request, hash);
request.put ("use_peers", "true");
Expand Down Expand Up @@ -2965,7 +2965,7 @@ TEST (rpc, work_generate_difficulty)
ASSERT_EQ (result_difficulty, response_difficulty);
auto multiplier = response.json.get<double> ("multiplier");
// Expected multiplier from base threshold, not from the given difficulty
ASSERT_NEAR (nano::difficulty::to_multiplier (result_difficulty, node->network_params.network.publish_thresholds.base), multiplier, 1e-10);
ASSERT_NEAR (nano::difficulty::to_multiplier (result_difficulty, node->default_difficulty (nano::work_version::work_1)), multiplier, 1e-10);
ASSERT_GE (result_difficulty, difficulty);
}
{
Expand Down Expand Up @@ -3059,7 +3059,7 @@ TEST (rpc, work_generate_multiplier)
ASSERT_EQ (response.json.get<std::string> ("error"), ec.message ());
}
{
double max_multiplier (nano::difficulty::to_multiplier (node->config.max_work_generate_difficulty, node->network_params.network.publish_thresholds.base));
double max_multiplier (nano::difficulty::to_multiplier (node->config.max_work_generate_difficulty, node->default_difficulty (nano::work_version::work_1)));
request.put ("multiplier", max_multiplier + 1);
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
Expand Down Expand Up @@ -4010,9 +4010,9 @@ TEST (rpc, work_validate)
std::string difficulty_text (response.json.get<std::string> ("difficulty"));
uint64_t difficulty;
ASSERT_FALSE (nano::from_string_hex (difficulty_text, difficulty));
ASSERT_GE (difficulty, node1.default_difficulty ());
ASSERT_GE (difficulty, node1.default_difficulty (nano::work_version::work_1));
double multiplier (response.json.get<double> ("multiplier"));
ASSERT_NEAR (multiplier, nano::difficulty::to_multiplier (difficulty, node1.default_difficulty ()), 1e-6);
ASSERT_NEAR (multiplier, nano::difficulty::to_multiplier (difficulty, node1.default_difficulty (nano::work_version::work_1)), 1e-6);
}
uint64_t work2 (0);
request.put ("work", nano::to_string_hex (work2));
Expand All @@ -4029,12 +4029,12 @@ TEST (rpc, work_validate)
std::string difficulty_text (response.json.get<std::string> ("difficulty"));
uint64_t difficulty;
ASSERT_FALSE (nano::from_string_hex (difficulty_text, difficulty));
ASSERT_GE (node1.default_difficulty (), difficulty);
ASSERT_GE (node1.default_difficulty (nano::work_version::work_1), difficulty);
double multiplier (response.json.get<double> ("multiplier"));
ASSERT_NEAR (multiplier, nano::difficulty::to_multiplier (difficulty, node1.default_difficulty ()), 1e-6);
ASSERT_NEAR (multiplier, nano::difficulty::to_multiplier (difficulty, node1.default_difficulty (nano::work_version::work_1)), 1e-6);
}
auto result_difficulty (nano::work_difficulty (nano::work_version::work_1, hash, work1));
ASSERT_GE (result_difficulty, node1.default_difficulty ());
ASSERT_GE (result_difficulty, node1.default_difficulty (nano::work_version::work_1));
request.put ("work", nano::to_string_hex (work1));
request.put ("difficulty", nano::to_string_hex (result_difficulty));
{
Expand Down Expand Up @@ -4129,7 +4129,7 @@ TEST (rpc, work_validate_epoch_2)
uint64_t difficulty{ 0 };
ASSERT_FALSE (nano::from_string_hex (difficulty_text, difficulty));
double multiplier (response.json.get<double> ("multiplier"));
ASSERT_NEAR (multiplier, nano::difficulty::to_multiplier (difficulty, node->network_params.network.publish_thresholds.base), 1e-6);
ASSERT_NEAR (multiplier, nano::difficulty::to_multiplier (difficulty, node->default_difficulty (nano::work_version::work_1)), 1e-6);
};
}

Expand Down Expand Up @@ -6027,7 +6027,7 @@ TEST (rpc, block_create_state_request_work)
boost::property_tree::read_json (block_stream, block_l);
auto block (nano::deserialize_block_json (block_l));
ASSERT_NE (nullptr, block);
ASSERT_GE (block->difficulty (), node->default_difficulty ());
ASSERT_GE (block->difficulty (), node->default_difficulty (nano::work_version::work_1));
}
}

Expand Down

0 comments on commit 8d4a447

Please sign in to comment.