From 774625907d0befdcfca062ad6098f246d0cc5c18 Mon Sep 17 00:00:00 2001 From: Guilherme Lawless Date: Sat, 25 Apr 2020 00:11:29 +0100 Subject: [PATCH] Remove invalid uses of epoch_1 work threshold (#2733) Was affecting websocket active_difficulty. Tests for it and RPC active_difficulty now pre-upgrade the node to epoch2 to ensure the output is correct. --- nano/core_test/websocket.cpp | 7 +++++-- nano/node/active_transactions.cpp | 2 +- nano/node/node.cpp | 2 +- nano/rpc_test/rpc.cpp | 7 +++++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/nano/core_test/websocket.cpp b/nano/core_test/websocket.cpp index d04c66878..5d19c4f0d 100644 --- a/nano/core_test/websocket.cpp +++ b/nano/core_test/websocket.cpp @@ -60,6 +60,9 @@ TEST (websocket, active_difficulty) config.websocket_config.enabled = true; config.websocket_config.port = nano::get_available_port (); auto node1 (system.add_node (config)); + // "Start" epoch 2 + node1->ledger.cache.epoch_2_started = true; + ASSERT_EQ (node1->default_difficulty (nano::work_version::work_1), node1->network_params.network.publish_thresholds.epoch_2); ASSERT_EQ (0, node1->websocket_server->subscriber_count (nano::websocket::topic::active_difficulty)); @@ -104,14 +107,14 @@ TEST (websocket, active_difficulty) auto message_contents = event.get_child ("message"); uint64_t network_minimum; nano::from_string_hex (message_contents.get ("network_minimum"), network_minimum); - ASSERT_EQ (network_minimum, node1->network_params.network.publish_thresholds.epoch_1); + ASSERT_EQ (network_minimum, node1->default_difficulty (nano::work_version::work_1)); uint64_t network_current; nano::from_string_hex (message_contents.get ("network_current"), network_current); ASSERT_EQ (network_current, node1->active.active_difficulty ()); double multiplier = message_contents.get ("multiplier"); - ASSERT_NEAR (multiplier, nano::difficulty::to_multiplier (node1->active.active_difficulty (), node1->network_params.network.publish_thresholds.epoch_1), 1e-6); + ASSERT_NEAR (multiplier, nano::difficulty::to_multiplier (node1->active.active_difficulty (), node1->default_difficulty (nano::work_version::work_1)), 1e-6); } // Subscribes to block confirmations, confirms a block and then awaits websocket notification diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index 27edcd878..f7833ed68 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -863,7 +863,7 @@ void nano::active_transactions::update_active_multiplier (nano::unique_lock= nano::difficulty::to_multiplier (node.network_params.network.publish_thresholds.entry, node.network_params.network.publish_thresholds.epoch_1)); + debug_assert (multiplier >= nano::difficulty::to_multiplier (node.network_params.network.publish_thresholds.entry, node.network_params.network.publish_thresholds.base)); multipliers_cb.push_front (multiplier); auto sum (std::accumulate (multipliers_cb.begin (), multipliers_cb.end (), double(0))); double avg_multiplier (sum / multipliers_cb.size ()); diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 966084e79..48ccb5ad8 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -260,7 +260,7 @@ node_seq (seq) if (this->websocket_server->any_subscriber (nano::websocket::topic::active_difficulty)) { nano::websocket::message_builder builder; - auto msg (builder.difficulty_changed (network_params.network.publish_thresholds.epoch_1, active_difficulty)); + auto msg (builder.difficulty_changed (this->default_difficulty (nano::work_version::work_1), active_difficulty)); this->websocket_server->broadcast (msg); } }); diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index d66b48b75..852f60551 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -7675,6 +7675,9 @@ TEST (rpc, active_difficulty) { nano::system system; auto node = add_ipc_enabled_node (system); + // "Start" epoch 2 + node->ledger.cache.epoch_2_started = true; + 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); @@ -7705,13 +7708,13 @@ TEST (rpc, active_difficulty) auto network_minimum_text (response.json.get ("network_minimum")); uint64_t network_minimum; ASSERT_FALSE (nano::from_string_hex (network_minimum_text, network_minimum)); - ASSERT_EQ (node->network_params.network.publish_thresholds.epoch_1, network_minimum); + ASSERT_EQ (node->default_difficulty (nano::work_version::work_1), network_minimum); auto multiplier (response.json.get ("multiplier")); ASSERT_NEAR (expected_multiplier, multiplier, 1e-6); auto network_current_text (response.json.get ("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->network_params.network.publish_thresholds.epoch_1), network_current); + ASSERT_EQ (nano::difficulty::from_multiplier (expected_multiplier, node->default_difficulty (nano::work_version::work_1)), network_current); ASSERT_EQ (response.json.not_found (), response.json.find ("difficulty_trend")); } // Test include_trend optional