Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support difficulty for RPC "work_generate" #1798

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions nano/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,49 @@ TEST (rpc, work_generate)
ASSERT_FALSE (nano::work_validate (hash1, work2));
}

TEST (rpc, work_generate_difficulty)
{
nano::system system (24000, 1);
nano::node_init init1;
auto node1 (system.nodes[0]);
nano::rpc rpc (system.io_ctx, *system.nodes[0], nano::rpc_config (true));
rpc.start ();
nano::block_hash hash1 (1);
uint64_t difficulty1 (0xfff0000000000000);
boost::property_tree::ptree request1;
request1.put ("action", "work_generate");
request1.put ("hash", hash1.to_string ());
request1.put ("difficulty", nano::to_string_hex (difficulty1));
test_response response1 (request1, rpc, system.io_ctx);
system.deadline_set (5s);
while (response1.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response1.status);
auto work_text1 (response1.json.get<std::string> ("work"));
uint64_t work1;
ASSERT_FALSE (nano::from_string_hex (work_text1, work1));
uint64_t result_difficulty1;
ASSERT_FALSE (nano::work_validate (hash1, work1, &result_difficulty1));
ASSERT_GE (result_difficulty1, difficulty1);
uint64_t difficulty2 (0xffff000000000000);
request1.put ("difficulty", nano::to_string_hex (difficulty2));
test_response response2 (request1, rpc, system.io_ctx);
system.deadline_set (10s);
while (response2.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response2.status);
auto work_text2 (response2.json.get<std::string> ("work"));
uint64_t work2;
ASSERT_FALSE (nano::from_string_hex (work_text2, work2));
uint64_t result_difficulty2;
ASSERT_FALSE (nano::work_validate (hash1, work2, &result_difficulty2));
ASSERT_GE (result_difficulty2, difficulty2);
}

TEST (rpc, work_cancel)
{
nano::system system (24000, 1);
Expand Down
2 changes: 2 additions & 0 deletions nano/lib/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ std::string nano::error_rpc_messages::message (int ev) const
return "Unknown error";
case nano::error_rpc::bad_destination:
return "Bad destination account";
case nano::error_rpc::bad_difficulty_format:
return "Bad difficulty";
case nano::error_rpc::bad_key:
return "Bad key";
case nano::error_rpc::bad_link:
Expand Down
1 change: 1 addition & 0 deletions nano/lib/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ enum class error_rpc
{
generic = 1,
bad_destination,
bad_difficulty_format,
bad_key,
bad_link,
bad_previous,
Expand Down
6 changes: 4 additions & 2 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,7 @@ class distributed_work : public std::enable_shared_from_this<distributed_work>
boost::property_tree::ptree request;
request.put ("action", "work_generate");
request.put ("hash", this_l->root.to_string ());
request.put ("difficulty", nano::to_string_hex (this_l->difficulty));
std::stringstream ostream;
boost::property_tree::write_json (ostream, request);
request_string = ostream.str ();
Expand Down Expand Up @@ -2430,14 +2431,15 @@ class distributed_work : public std::enable_shared_from_this<distributed_work>
uint64_t work;
if (!nano::from_string_hex (work_text, work))
{
if (!nano::work_validate (root, work))
uint64_t result_difficulty (0);
if (!nano::work_validate (root, work, &result_difficulty) && result_difficulty >= difficulty)
{
set_once (work);
stop ();
}
else
{
node->logger.try_log (boost::str (boost::format ("Incorrect work response from %1% for root %2%: %3%") % address % root.to_string () % work_text));
node->logger.try_log (boost::str (boost::format ("Incorrect work response from %1% for root %2% with diffuculty %3%: %4%") % address % root.to_string () % nano::to_string_hex (difficulty) % work_text));
handle_failure (last);
}
}
Expand Down
13 changes: 11 additions & 2 deletions nano/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4132,6 +4132,15 @@ void nano::rpc_handler::work_generate ()
{
rpc_control_impl ();
auto hash (hash_impl ());
uint64_t difficulty (nano::work_pool::publish_threshold);
boost::optional<std::string> difficulty_text (request.get_optional<std::string> ("difficulty"));
if (!ec && difficulty_text.is_initialized ())
{
if (nano::from_string_hex (difficulty_text.get (), difficulty))
{
ec = nano::error_rpc::bad_difficulty_format;
}
}
if (!ec)
{
bool use_peers (request.get_optional<bool> ("use_peers") == true);
Expand All @@ -4150,11 +4159,11 @@ void nano::rpc_handler::work_generate ()
};
if (!use_peers)
{
node.work.generate (hash, callback);
node.work.generate (hash, callback, difficulty);
}
else
{
node.work_generate (hash, callback);
node.work_generate (hash, callback, difficulty);
}
}
// Because of callback
Expand Down