Skip to content

Commit

Permalink
Ensure max_work_generate_difficulty is updated when changing the defa…
Browse files Browse the repository at this point in the history
…ult difficulty (#2705)

* Ensure max_work_generate_difficulty is updated when changing the default difficulty

* Fix broken RPC tests
  • Loading branch information
guilhermelawless authored Apr 8, 2020
1 parent 9046201 commit 7f6fd1a
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 20 deletions.
1 change: 0 additions & 1 deletion nano/core_test/distributed_work.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ TEST (distributed_work, no_peers_cancel)
nano::system system;
nano::node_config node_config (nano::get_available_port (), system.logging);
node_config.max_work_generate_multiplier = 1e6;
node_config.max_work_generate_difficulty = nano::difficulty::from_multiplier (node_config.max_work_generate_multiplier, nano::network_constants ().publish_thresholds.base);
auto & node = *system.add_node (node_config);
nano::block_hash hash{ 1 };
bool done{ false };
Expand Down
18 changes: 17 additions & 1 deletion nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3727,7 +3727,23 @@ TEST (node, aggressive_flooding)
ASSERT_EQ (1 + 2 * nodes_wallets.size () + 2, node1.ledger.cache.block_count);
}

TEST (active_multiplier, recalculate_work)
// Tests that upon changing the default difficulty, max generation difficulty changes proportionally
TEST (node, max_work_generate_difficulty)
{
nano::system system;
nano::node_config node_config (nano::get_available_port (), system.logging);
node_config.max_work_generate_multiplier = 2.0;
auto & node = *system.add_node (node_config);
auto initial_difficulty = node.default_difficulty (nano::work_version::work_1);
ASSERT_EQ (node.max_work_generate_difficulty (nano::work_version::work_1), nano::difficulty::from_multiplier (node.config.max_work_generate_multiplier, initial_difficulty));
ASSERT_NE (nullptr, system.upgrade_genesis_epoch (node, nano::epoch::epoch_1));
ASSERT_NE (nullptr, system.upgrade_genesis_epoch (node, nano::epoch::epoch_2));
auto final_difficulty = node.default_difficulty (nano::work_version::work_1);
ASSERT_NE (final_difficulty, initial_difficulty);
ASSERT_EQ (node.max_work_generate_difficulty (nano::work_version::work_1), nano::difficulty::from_multiplier (node.config.max_work_generate_multiplier, final_difficulty));
}

TEST (active_difficulty, recalculate_work)
{
nano::system system;
nano::node_config node_config (nano::get_available_port (), system.logging);
Expand Down
7 changes: 3 additions & 4 deletions nano/core_test/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ TEST (wallet, work_watcher_update)
nano::node_config node_config (nano::get_available_port (), system.logging);
node_config.enable_voting = false;
node_config.work_watcher_period = 1s;
node_config.max_work_generate_difficulty = std::numeric_limits<uint64_t>::max ();
node_config.max_work_generate_multiplier = 1e6;
nano::node_flags node_flags;
node_flags.disable_request_loop = true;
auto & node = *system.add_node (node_config, node_flags);
Expand Down Expand Up @@ -1275,7 +1275,6 @@ TEST (wallet, work_watcher_cancel)
nano::node_config node_config (nano::get_available_port (), system.logging);
node_config.work_watcher_period = 1s;
node_config.max_work_generate_multiplier = 1e6;
node_config.max_work_generate_difficulty = nano::difficulty::from_multiplier (node_config.max_work_generate_multiplier, nano::network_constants ().publish_thresholds.base);
node_config.enable_voting = false;
auto & node = *system.add_node (node_config);
auto & wallet (*system.wallet (0));
Expand Down Expand Up @@ -1326,7 +1325,7 @@ TEST (wallet, limited_difficulty)
nano::system system;
nano::genesis genesis;
nano::node_config node_config (nano::get_available_port (), system.logging);
node_config.max_work_generate_difficulty = nano::network_constants ().publish_thresholds.base;
node_config.max_work_generate_multiplier = 1;
nano::node_flags node_flags;
node_flags.disable_request_loop = true;
auto & node = *system.add_node (node_config, node_flags);
Expand All @@ -1341,7 +1340,7 @@ TEST (wallet, limited_difficulty)
nano::lock_guard<std::mutex> guard (node.active.mutex);
node.active.trended_active_multiplier = 1024 * 1024 * 1024;
}
ASSERT_EQ (node_config.max_work_generate_difficulty, node.active.limited_active_difficulty (*genesis.open));
ASSERT_EQ (node.max_work_generate_difficulty (nano::work_version::work_1), node.active.limited_active_difficulty (*genesis.open));
auto send = wallet.send_action (nano::test_genesis_key.pub, nano::keypair ().pub, 1, 1);
ASSERT_NE (nullptr, send);
}
Expand Down
6 changes: 3 additions & 3 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,13 +823,13 @@ uint64_t nano::active_transactions::limited_active_difficulty (nano::block const
{
threshold = node.default_difficulty (block_a.work_version ());
}
return limited_active_difficulty (threshold);
return limited_active_difficulty (block_a.work_version (), threshold);
}

uint64_t nano::active_transactions::limited_active_difficulty (uint64_t const threshold_a)
uint64_t nano::active_transactions::limited_active_difficulty (nano::work_version const version_a, uint64_t const threshold_a)
{
auto difficulty (nano::difficulty::from_multiplier (nano::denormalized_multiplier (active_multiplier (), threshold_a), threshold_a));
return std::min (difficulty, node.config.max_work_generate_difficulty);
return std::min (difficulty, node.max_work_generate_difficulty (version_a));
}

double nano::active_transactions::active_multiplier ()
Expand Down
2 changes: 1 addition & 1 deletion nano/node/active_transactions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class active_transactions final
void update_active_multiplier (nano::unique_lock<std::mutex> &);
uint64_t active_difficulty ();
uint64_t limited_active_difficulty (nano::block const &);
uint64_t limited_active_difficulty (uint64_t const);
uint64_t limited_active_difficulty (nano::work_version const, uint64_t const);
double active_multiplier ();
std::deque<std::shared_ptr<nano::block>> list_blocks ();
void erase (nano::block const &);
Expand Down
2 changes: 1 addition & 1 deletion nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4988,7 +4988,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.config.max_work_generate_difficulty || difficulty < nano::work_threshold_entry (work_version)))
if (!ec && (difficulty > node.max_work_generate_difficulty (work_version) || difficulty < nano::work_threshold_entry (work_version)))
{
ec = nano::error_rpc::difficulty_limit;
}
Expand Down
5 changes: 5 additions & 0 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,11 @@ uint64_t nano::node::default_difficulty (nano::work_version const version_a) con
return result;
}

uint64_t nano::node::max_work_generate_difficulty (nano::work_version const version_a) const
{
return nano::difficulty::from_multiplier (config.max_work_generate_multiplier, default_difficulty (version_a));
}

bool nano::node::local_work_generation_enabled () const
{
return config.work_threads > 0 || work.opencl;
Expand Down
1 change: 1 addition & 0 deletions nano/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class node final : public std::enable_shared_from_this<nano::node>
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 (nano::work_version const) const;
uint64_t max_work_generate_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: 0 additions & 2 deletions nano/node/nodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ external_address (boost::asio::ip::address_v6{}.to_string ())
{
peering_port = network_params.network.default_node_port;
}
max_work_generate_difficulty = nano::difficulty::from_multiplier (max_work_generate_multiplier, network_params.network.publish_thresholds.base);
switch (network_params.network.network ())
{
case nano::nano_networks::nano_test_network:
Expand Down Expand Up @@ -361,7 +360,6 @@ nano::error nano::node_config::deserialize_toml (nano::tomlconfig & toml)

nano::network_constants network;
toml.get<double> ("max_work_generate_multiplier", max_work_generate_multiplier);
max_work_generate_difficulty = nano::difficulty::from_multiplier (max_work_generate_multiplier, network.publish_thresholds.base);

toml.get<uint32_t> ("max_queued_requests", max_queued_requests);

Expand Down
1 change: 0 additions & 1 deletion nano/node/nodeconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ class node_config
bool backup_before_upgrade{ false };
std::chrono::seconds work_watcher_period{ std::chrono::seconds (5) };
double max_work_generate_multiplier{ 64. };
uint64_t max_work_generate_difficulty{ nano::network_constants ().publish_full.base };
uint32_t max_queued_requests{ 512 };
nano::rocksdb_config rocksdb_config;
nano::lmdb_config lmdb_config;
Expand Down
4 changes: 2 additions & 2 deletions nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1152,8 +1152,8 @@ bool nano::wallet::action_complete (std::shared_ptr<nano::block> const & block_a
if (block_a->difficulty () < required_difficulty)
{
wallets.node.logger.try_log (boost::str (boost::format ("Cached or provided work for block %1% account %2% is invalid, regenerating") % block_a->hash ().to_string () % account_a.to_account ()));
debug_assert (required_difficulty <= wallets.node.config.max_work_generate_difficulty);
auto target_difficulty = std::max (required_difficulty, wallets.node.active.limited_active_difficulty (required_difficulty));
debug_assert (required_difficulty <= wallets.node.max_work_generate_difficulty (block_a->work_version ()));
auto target_difficulty = std::max (required_difficulty, wallets.node.active.limited_active_difficulty (block_a->work_version (), required_difficulty));
error = !wallets.node.work_generate_blocking (*block_a, target_difficulty).is_initialized ();
}
if (!error)
Expand Down
9 changes: 5 additions & 4 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,7 @@ TEST (rpc, process_block_with_work_watcher)
nano::node_config node_config (nano::get_available_port (), system.logging);
node_config.enable_voting = false;
node_config.work_watcher_period = 1s;
node_config.max_work_generate_multiplier = 1e6;
auto & node1 = *add_ipc_enabled_node (system, node_config);
nano::keypair key;
auto latest (node1.latest (nano::test_genesis_key.pub));
Expand Down Expand Up @@ -2954,7 +2955,7 @@ TEST (rpc, work_generate_difficulty)
{
nano::system system;
nano::node_config node_config (nano::get_available_port (), system.logging);
node_config.max_work_generate_difficulty = 0xffff000000000000;
node_config.max_work_generate_multiplier = 1000;
auto node = add_ipc_enabled_node (system, node_config);
scoped_io_thread_name_change scoped_thread_name_io;
nano::node_rpc_config node_rpc_config;
Expand Down Expand Up @@ -3008,7 +3009,7 @@ TEST (rpc, work_generate_difficulty)
ASSERT_GE (result_difficulty, difficulty);
}
{
uint64_t difficulty (node->config.max_work_generate_difficulty + 1);
uint64_t difficulty (node->max_work_generate_difficulty (nano::work_version::work_1) + 1);
request.put ("difficulty", nano::to_string_hex (difficulty));
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
Expand All @@ -3026,7 +3027,7 @@ TEST (rpc, work_generate_multiplier)
{
nano::system system;
nano::node_config node_config (nano::get_available_port (), system.logging);
node_config.max_work_generate_difficulty = 0xfffff00000000000;
node_config.max_work_generate_multiplier = 100;
auto node = add_ipc_enabled_node (system, node_config);
scoped_io_thread_name_change scoped_thread_name_io;
nano::node_rpc_config node_rpc_config;
Expand Down Expand Up @@ -3082,7 +3083,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->default_difficulty (nano::work_version::work_1)));
double max_multiplier (nano::difficulty::to_multiplier (node->max_work_generate_difficulty (nano::work_version::work_1), 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

0 comments on commit 7f6fd1a

Please sign in to comment.