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

Deprecate RPC active_difficulty instead of removing it #3267

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
22 changes: 22 additions & 0 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,27 @@ void nano::json_handler::accounts_pending ()
response_errors ();
}

void nano::json_handler::active_difficulty ()
{
auto include_trend (request.get<bool> ("include_trend", false));
auto const multiplier_active = 1.0;
auto const default_difficulty (node.default_difficulty (nano::work_version::work_1));
auto const default_receive_difficulty (node.default_receive_difficulty (nano::work_version::work_1));
auto const receive_current_denormalized (nano::denormalized_multiplier (multiplier_active, node.network_params.network.publish_thresholds.epoch_2_receive));
response_l.put ("deprecated", "1");
response_l.put ("network_minimum", nano::to_string_hex (default_difficulty));
response_l.put ("network_receive_minimum", nano::to_string_hex (default_receive_difficulty));
response_l.put ("network_current", nano::to_string_hex (nano::difficulty::from_multiplier (multiplier_active, default_difficulty)));
response_l.put ("network_receive_current", nano::to_string_hex (nano::difficulty::from_multiplier (receive_current_denormalized, default_receive_difficulty)));
response_l.put ("multiplier", 1.0);
if (include_trend)
{
boost::property_tree::ptree trend_entry_l;
response_l.add_child ("difficulty_trend", trend_entry_l);
}
response_errors ();
}

void nano::json_handler::available_supply ()
{
auto genesis_balance (node.balance (node.network_params.ledger.genesis_account)); // Cold storage genesis
Expand Down Expand Up @@ -5054,6 +5075,7 @@ ipc_json_handler_no_arg_func_map create_ipc_json_handler_no_arg_func_map ()
no_arg_funcs.emplace ("accounts_create", &nano::json_handler::accounts_create);
no_arg_funcs.emplace ("accounts_frontiers", &nano::json_handler::accounts_frontiers);
no_arg_funcs.emplace ("accounts_pending", &nano::json_handler::accounts_pending);
no_arg_funcs.emplace ("active_difficulty", &nano::json_handler::active_difficulty);
no_arg_funcs.emplace ("available_supply", &nano::json_handler::available_supply);
no_arg_funcs.emplace ("block_info", &nano::json_handler::block_info);
no_arg_funcs.emplace ("block", &nano::json_handler::block_info);
Expand Down
1 change: 1 addition & 0 deletions nano/node/json_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class json_handler : public std::enable_shared_from_this<nano::json_handler>
void accounts_create ();
void accounts_frontiers ();
void accounts_pending ();
void active_difficulty ();
void available_supply ();
void block_info ();
void block_confirm ();
Expand Down
54 changes: 54 additions & 0 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6924,6 +6924,60 @@ TEST (rpc, database_txn_tracker)
thread.join ();
}

TEST (rpc, active_difficulty)
{
nano::system system;
auto node = add_ipc_enabled_node (system);
ASSERT_EQ (node->default_difficulty (nano::work_version::work_1), node->network_params.network.publish_thresholds.epoch_2);
scoped_io_thread_name_change scoped_thread_name_io;
nano::node_rpc_config node_rpc_config;
nano::ipc::ipc_server ipc_server (*node, node_rpc_config);
nano::rpc_config rpc_config (nano::get_available_port (), true);
rpc_config.rpc_process.ipc_port = node->config.ipc_config.transport_tcp.port;
nano::ipc_rpc_processor ipc_rpc_processor (system.io_ctx, rpc_config);
nano::rpc rpc (system.io_ctx, rpc_config, ipc_rpc_processor);
rpc.start ();
boost::property_tree::ptree request;
request.put ("action", "active_difficulty");
auto expected_multiplier{ 1.0 };
{
test_response response (request, rpc.config.port, system.io_ctx);
ASSERT_TIMELY (5s, response.status != 0);
ASSERT_EQ (200, response.status);
auto network_minimum_text (response.json.get<std::string> ("network_minimum"));
uint64_t network_minimum;
ASSERT_FALSE (nano::from_string_hex (network_minimum_text, network_minimum));
ASSERT_EQ (node->default_difficulty (nano::work_version::work_1), network_minimum);
auto network_receive_minimum_text (response.json.get<std::string> ("network_receive_minimum"));
uint64_t network_receive_minimum;
ASSERT_FALSE (nano::from_string_hex (network_receive_minimum_text, network_receive_minimum));
ASSERT_EQ (node->default_receive_difficulty (nano::work_version::work_1), network_receive_minimum);
auto multiplier (response.json.get<double> ("multiplier"));
ASSERT_NEAR (expected_multiplier, multiplier, 1e-6);
auto network_current_text (response.json.get<std::string> ("network_current"));
uint64_t network_current;
ASSERT_FALSE (nano::from_string_hex (network_current_text, network_current));
ASSERT_EQ (nano::difficulty::from_multiplier (expected_multiplier, node->default_difficulty (nano::work_version::work_1)), network_current);
auto network_receive_current_text (response.json.get<std::string> ("network_receive_current"));
uint64_t network_receive_current;
ASSERT_FALSE (nano::from_string_hex (network_receive_current_text, network_receive_current));
auto network_receive_current_multiplier (nano::difficulty::to_multiplier (network_receive_current, network_receive_minimum));
auto network_receive_current_normalized_multiplier (nano::normalized_multiplier (network_receive_current_multiplier, network_receive_minimum));
ASSERT_NEAR (network_receive_current_normalized_multiplier, multiplier, 1e-6);
ASSERT_EQ (response.json.not_found (), response.json.find ("difficulty_trend"));
}
// Test include_trend optional
request.put ("include_trend", true);
{
test_response response (request, rpc.config.port, system.io_ctx);
ASSERT_TIMELY (5s, response.status != 0);
auto trend_opt (response.json.get_child_optional ("difficulty_trend"));
ASSERT_TRUE (trend_opt.is_initialized ());
auto & trend (trend_opt.get ());
ASSERT_EQ (0, trend.size ());
}
}

// This is mainly to check for threading issues with TSAN
TEST (rpc, simultaneous_calls)
{
Expand Down