From 6b560fccaa3567223fef5eef36454383cbd546eb Mon Sep 17 00:00:00 2001 From: Oliver Schwarz <46133123+DollyVolley@users.noreply.github.com> Date: Fri, 26 Mar 2021 08:28:49 +0100 Subject: [PATCH] Fix/network security change detection (#155) * Hotfix add missing requirement * Fix change detection * Add another testcase --- bot/constants/globals.py | 2 -- bot/handlers/handlers.py | 2 +- bot/handlers/network_info_handlers.py | 2 +- bot/jobs/thorchain_network_jobs.py | 2 +- test/unit_tests/_trial_temp.lock | 2 +- test/unit_tests/test_jobs.py | 16 ++++++++++++++-- 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/bot/constants/globals.py b/bot/constants/globals.py index 8a205c1..66a7626 100644 --- a/bot/constants/globals.py +++ b/bot/constants/globals.py @@ -29,7 +29,6 @@ KEYBOARD_PAGE_SIZE = 30 # Emojis - # Be aware that keys of STATUS_EMOJIS are displayed to the user STATUS_EMOJIS = { "unknown": "❓", @@ -64,4 +63,3 @@ # Thorchain SLASH_POINTS_NOTIFICATION_THRESHOLD_DEFAULT = 3 THORCHAIN_ONCHAIN_API_URL = "https://thorchain-service.b42.tech/v1/" - diff --git a/bot/handlers/handlers.py b/bot/handlers/handlers.py index ca0c018..23d4b6f 100644 --- a/bot/handlers/handlers.py +++ b/bot/handlers/handlers.py @@ -21,7 +21,7 @@ def on_start_command(update, context): if 'job_started' not in context.chat_data: start_user_job(context, update.message.chat.id) - text = 'Heil ok sæll! I am your THORNode Bot runaning on ' + NETWORK_TYPE + '. 🤖\n\n' + text = f'Heil ok sæll! I am your THORNode Bot running on {NETWORK_TYPE}. 🤖\n\n' text += 'I will notify you about changes of your THORNode\'s\n' \ '- *Status*\n' \ '- *Bond*\n' \ diff --git a/bot/handlers/network_info_handlers.py b/bot/handlers/network_info_handlers.py index 6572bd2..04664ae 100644 --- a/bot/handlers/network_info_handlers.py +++ b/bot/handlers/network_info_handlers.py @@ -70,7 +70,7 @@ async def show_network_stats(update, context): '{:.2f}'.format((int(network['blockRewards']['stakeReward']) / int( network['blockRewards']['blockReward']) * 100)) + " %* (staker share)\n" - text += f"\n🔓 Network Security: *{network_security_ratio_to_string(get_network_security_ratio(network))}*\n" + text += f"\n🔓 Network Security: *{network_security_ratio_to_string(get_network_security_ratio(network)).value}*\n" text += "\n↩️ Node ROI: *" + \ '{:.2f}'.format(float(network['bondingAPY']) * 100) \ diff --git a/bot/jobs/thorchain_network_jobs.py b/bot/jobs/thorchain_network_jobs.py index d3cd455..0504349 100644 --- a/bot/jobs/thorchain_network_jobs.py +++ b/bot/jobs/thorchain_network_jobs.py @@ -20,7 +20,7 @@ def check_network_security(context): return None if network_health_status != context.bot_data["network_health_status"]: - context.bot_data["network_health_status"] = network_health_status.value + context.bot_data["network_health_status"] = network_health_status if network_health_status is NetworkHealthStatus.OPTIMAL: logger.info(f"Network is healthy again: {network_health_status.value}") diff --git a/test/unit_tests/_trial_temp.lock b/test/unit_tests/_trial_temp.lock index 34af5a3..0b5cabc 120000 --- a/test/unit_tests/_trial_temp.lock +++ b/test/unit_tests/_trial_temp.lock @@ -1 +1 @@ -66986 \ No newline at end of file +21565 \ No newline at end of file diff --git a/test/unit_tests/test_jobs.py b/test/unit_tests/test_jobs.py index ed7369d..ae86804 100644 --- a/test/unit_tests/test_jobs.py +++ b/test/unit_tests/test_jobs.py @@ -128,22 +128,34 @@ def test_solvency_check_success(self, mock_asgard_solvency_check, mock_yggdrasil def test_check_network_security(self, mock_get_network_security_ratio): mock_get_network_security_ratio.return_value = 0.66 network_security_message = check_network_security(self.context) - self.assertIs(network_security_message, None, "Can not trigger comparison difference. No previous value") + self.assertIs(network_security_message, None, + "Can not trigger comparison difference. No previous value") mock_get_network_security_ratio.return_value = 0.66 network_security_message = check_network_security(self.context) - self.assertIs(network_security_message, None, "Network is optimal, but an warning is raised") + self.assertIs(network_security_message, None, + "Network is optimal, but an warning is raised") mock_get_network_security_ratio.return_value = 0.8 network_security_message = check_network_security(self.context) self.assertIn(get_network_health_warning(NetworkHealthStatus.OVERBONDED), network_security_message, "Network state should have changed to OVERBONDED") + mock_get_network_security_ratio.return_value = 0.8 + network_security_message = check_network_security(self.context) + self.assertIs(network_security_message, None, + "Network health state did not change but user was notified") + mock_get_network_security_ratio.return_value = 0.7 network_security_message = check_network_security(self.context) self.assertIn(network_security_message, NETWORK_HEALTHY_AGAIN, "Network state should have changed back to OPTIMAL again") + mock_get_network_security_ratio.return_value = 0.66 + network_security_message = check_network_security(self.context) + self.assertIs(network_security_message, None, + "Network health state did not change but user was notified") + mock_get_network_security_ratio.return_value = 0.91 network_security_message = check_network_security(self.context) self.assertIn(network_security_message, get_network_health_warning(NetworkHealthStatus.INEFFICIENT),