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

Add difficulty limit for RPC "work_generate" in config.json #1830

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions nano/core_test/rpc.cpp
Expand Up @@ -2103,6 +2103,17 @@ TEST (rpc, work_generate_difficulty)
uint64_t result_difficulty2;
ASSERT_FALSE (nano::work_validate (hash1, work2, &result_difficulty2));
ASSERT_GE (result_difficulty2, difficulty2);
uint64_t difficulty3 (rpc.config.max_work_generate_difficulty + 1);
request1.put ("difficulty", nano::to_string_hex (difficulty3));
test_response response3 (request1, rpc, system.io_ctx);
system.deadline_set (5s);
while (response3.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response3.status);
std::error_code ec (nano::error_rpc::difficulty_limit);
ASSERT_EQ (response3.json.get<std::string> ("error"), ec.message ());
}

TEST (rpc, work_cancel)
Expand Down
2 changes: 2 additions & 0 deletions nano/lib/errors.cpp
Expand Up @@ -148,6 +148,8 @@ std::string nano::error_rpc_messages::message (int ev) const
return "Destination account, previous hash, current balance and amount required";
case nano::error_rpc::confirmation_not_found:
return "Active confirmation not found";
case nano::error_rpc::difficulty_limit:
return "Difficulty above config limit";
case nano::error_rpc::invalid_balance:
return "Invalid balance number";
case nano::error_rpc::invalid_destinations:
Expand Down
1 change: 1 addition & 0 deletions nano/lib/errors.hpp
Expand Up @@ -90,6 +90,7 @@ enum class error_rpc
block_create_requirements_change,
block_create_requirements_send,
confirmation_not_found,
difficulty_limit,
invalid_balance,
invalid_destinations,
invalid_offset,
Expand Down
4 changes: 4 additions & 0 deletions nano/node/rpc.cpp
Expand Up @@ -4227,6 +4227,10 @@ void nano::rpc_handler::work_generate ()
ec = nano::error_rpc::bad_difficulty_format;
}
}
if (!ec && difficulty > rpc.config.max_work_generate_difficulty)
{
ec = nano::error_rpc::difficulty_limit;
}
if (!ec)
{
bool use_peers (request.get_optional<bool> ("use_peers") == true);
Expand Down
11 changes: 10 additions & 1 deletion nano/node/rpcconfig.cpp
Expand Up @@ -38,7 +38,8 @@ port (network_params.default_rpc_port),
enable_control (enable_control_a),
max_json_depth (20),
enable_sign_hash (false),
max_request_size (32 * 1024 * 1024)
max_request_size (32 * 1024 * 1024),
max_work_generate_difficulty (0xffffffffc0000000)
{
}

Expand All @@ -51,6 +52,7 @@ nano::error nano::rpc_config::serialize_json (nano::jsonconfig & json) const
json.put ("max_json_depth", max_json_depth);
json.put ("enable_sign_hash", enable_sign_hash);
json.put ("max_request_size", max_request_size);
json.put ("max_work_generate_difficulty", nano::to_string_hex (max_work_generate_difficulty));
return json.get_error ();
}

Expand All @@ -62,6 +64,7 @@ nano::error nano::rpc_config::deserialize_json (bool & upgraded_a, nano::jsoncon
version_l = 1;
json.put ("version", *version_l);
json.put ("max_request_size", max_request_size);
json.put ("max_work_generate_difficulty", nano::to_string_hex (max_work_generate_difficulty));
json.erase ("frontier_request_limit");
json.erase ("chain_request_limit");

Expand All @@ -80,5 +83,11 @@ nano::error nano::rpc_config::deserialize_json (bool & upgraded_a, nano::jsoncon
json.get_optional<uint8_t> ("max_json_depth", max_json_depth);
json.get_optional<bool> ("enable_sign_hash", enable_sign_hash);
json.get_optional<uint64_t> ("max_request_size", max_request_size);
std::string max_work_generate_difficulty_text;
json.get_optional<std::string> ("max_work_generate_difficulty", max_work_generate_difficulty_text);
if (!max_work_generate_difficulty_text.empty ())
{
nano::from_string_hex (max_work_generate_difficulty_text, max_work_generate_difficulty);
}
return json.get_error ();
}
1 change: 1 addition & 0 deletions nano/node/rpcconfig.hpp
Expand Up @@ -46,6 +46,7 @@ class rpc_config
uint8_t max_json_depth;
bool enable_sign_hash;
uint64_t max_request_size;
uint64_t max_work_generate_difficulty;
static int json_version ()
{
return 1;
Expand Down