From 1ecff3a9f617558e8ba727686ec6feff5fb06e2c Mon Sep 17 00:00:00 2001 From: F4ever <1590415904a@gmail.com> Date: Sat, 1 Apr 2023 03:11:24 +0200 Subject: [PATCH 1/2] hotfixes --- src/modules/submodules/oracle_module.py | 5 +++-- src/providers/http_provider.py | 22 +++++++++++----------- src/utils/slot.py | 2 +- src/utils/web3converter.py | 4 +++- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/modules/submodules/oracle_module.py b/src/modules/submodules/oracle_module.py index b9a2b3b87..9f1697bdf 100644 --- a/src/modules/submodules/oracle_module.py +++ b/src/modules/submodules/oracle_module.py @@ -4,7 +4,8 @@ from dataclasses import asdict from enum import Enum -from timeout_decorator import timeout +from requests.exceptions import ConnectionError +from timeout_decorator import timeout, TimeoutError from src.metrics.prometheus.basic import ORACLE_BLOCK_NUMBER, ORACLE_SLOT_NUMBER from src.modules.submodules.exceptions import IsNotMemberException, IncompatibleContractVersion @@ -92,7 +93,7 @@ def run_cycle(self, blockstamp: BlockStamp) -> ModuleExecuteDelay: except NoActiveProviderError as exception: logger.error({'msg': 'No active node available.', 'error': str(exception)}) except ConnectionError as error: - logger.error({'msg': error.args, 'error': str(error)}) + logger.error({'msg': 'Connection error.', 'error': str(error)}) except NotOkResponse as error: logger.error({'msg': 'Received non-ok response.', 'error': str(error)}) except (NoSlotsAvailable, SlotNotFinalized, InconsistentData) as error: diff --git a/src/providers/http_provider.py b/src/providers/http_provider.py index 6d3f5ab7f..b1bb4c61b 100644 --- a/src/providers/http_provider.py +++ b/src/providers/http_provider.py @@ -5,8 +5,9 @@ from urllib.parse import urljoin, urlparse from prometheus_client import Histogram -from requests import Session, JSONDecodeError, Timeout +from requests import Session, JSONDecodeError from requests.adapters import HTTPAdapter +from requests.exceptions import ConnectionError from urllib3 import Retry from src.variables import HTTP_REQUEST_RETRY_COUNT, HTTP_REQUEST_SLEEP_BEFORE_RETRY_IN_SECONDS, HTTP_REQUEST_TIMEOUT @@ -114,15 +115,20 @@ def _get_without_fallbacks( params=query_params, timeout=HTTP_REQUEST_TIMEOUT, ) - except Timeout as error: - msg = f'Timeout error from {complete_endpoint}.' - logger.debug({'msg': msg}) + except ConnectionError as error: + logger.debug({'msg': str(error)}) t.labels( endpoint=endpoint, code=0, domain=urlparse(host).netloc, ) - raise TimeoutError(msg) from error + raise error + else: + t.labels( + endpoint=endpoint, + code=response.status_code, + domain=urlparse(host).netloc, + ) response_fail_msg = f'Response from {complete_endpoint} [{response.status_code}] with text: "{str(response.text)}" returned.' @@ -135,12 +141,6 @@ def _get_without_fallbacks( except JSONDecodeError as error: logger.debug({'msg': response_fail_msg}) raise error - finally: - t.labels( - endpoint=endpoint, - code=response.status_code, - domain=urlparse(host).netloc, - ) if 'data' in json_response: data = json_response['data'] diff --git a/src/utils/slot.py b/src/utils/slot.py index adfdb48ac..620e21a9b 100644 --- a/src/utils/slot.py +++ b/src/utils/slot.py @@ -54,7 +54,7 @@ def get_first_non_missed_slot( # 2nd tick - 20 slot is missed. Check next slot. # 3rd tick - 21 slot is missed. Check next slot. # 4th tick - 22 slot is missed. Check next slot. - # 5th tick - 23 slot is existed! + # 5th tick - 23 slot exists! # Get `parent_root` of 23 slot and get its parent slot by this root # In our case it is 18 slot because it's first non-missed slot before 23 slot. # diff --git a/src/utils/web3converter.py b/src/utils/web3converter.py index 043df356a..176e5fa4e 100644 --- a/src/utils/web3converter.py +++ b/src/utils/web3converter.py @@ -24,7 +24,9 @@ def get_frame_last_slot(self, frame: FrameNumber) -> SlotNumber: return SlotNumber(self.get_frame_first_slot(FrameNumber(frame + 1)) - 1) def get_frame_first_slot(self, frame: FrameNumber) -> SlotNumber: - return SlotNumber(frame * self.frame_config.epochs_per_frame * self.chain_config.slots_per_epoch) + return SlotNumber( + (self.frame_config.initial_epoch + frame * self.frame_config.epochs_per_frame) * self.chain_config.slots_per_epoch + ) def get_epoch_by_slot(self, ref_slot: SlotNumber) -> EpochNumber: return EpochNumber(ref_slot // self.chain_config.slots_per_epoch) From cb6d45441f512e139eb1f317a6505a030fa9207e Mon Sep 17 00:00:00 2001 From: F4ever <1590415904a@gmail.com> Date: Sat, 1 Apr 2023 03:28:19 +0200 Subject: [PATCH 2/2] fix tests and linters --- src/modules/submodules/oracle_module.py | 8 ++++---- src/providers/http_provider.py | 16 ++++++++-------- tests/modules/submodules/test_oracle_module.py | 7 +++++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/modules/submodules/oracle_module.py b/src/modules/submodules/oracle_module.py index 9f1697bdf..9adb94dc1 100644 --- a/src/modules/submodules/oracle_module.py +++ b/src/modules/submodules/oracle_module.py @@ -4,8 +4,8 @@ from dataclasses import asdict from enum import Enum -from requests.exceptions import ConnectionError -from timeout_decorator import timeout, TimeoutError +from requests.exceptions import ConnectionError as RequestsConnectionError +from timeout_decorator import timeout, TimeoutError as DecoratorTimeoutError from src.metrics.prometheus.basic import ORACLE_BLOCK_NUMBER, ORACLE_SLOT_NUMBER from src.modules.submodules.exceptions import IsNotMemberException, IncompatibleContractVersion @@ -88,11 +88,11 @@ def run_cycle(self, blockstamp: BlockStamp) -> ModuleExecuteDelay: except IncompatibleContractVersion as exception: logger.error({'msg': 'Incompatible Contract version. Please update Oracle Daemon.'}) raise exception - except TimeoutError as exception: + except DecoratorTimeoutError as exception: logger.error({'msg': 'Oracle module do not respond.', 'error': str(exception)}) except NoActiveProviderError as exception: logger.error({'msg': 'No active node available.', 'error': str(exception)}) - except ConnectionError as error: + except RequestsConnectionError as error: logger.error({'msg': 'Connection error.', 'error': str(error)}) except NotOkResponse as error: logger.error({'msg': 'Received non-ok response.', 'error': str(error)}) diff --git a/src/providers/http_provider.py b/src/providers/http_provider.py index b1bb4c61b..786fd5849 100644 --- a/src/providers/http_provider.py +++ b/src/providers/http_provider.py @@ -7,7 +7,7 @@ from prometheus_client import Histogram from requests import Session, JSONDecodeError from requests.adapters import HTTPAdapter -from requests.exceptions import ConnectionError +from requests.exceptions import ConnectionError as RequestsConnectionError from urllib3 import Retry from src.variables import HTTP_REQUEST_RETRY_COUNT, HTTP_REQUEST_SLEEP_BEFORE_RETRY_IN_SECONDS, HTTP_REQUEST_TIMEOUT @@ -115,7 +115,7 @@ def _get_without_fallbacks( params=query_params, timeout=HTTP_REQUEST_TIMEOUT, ) - except ConnectionError as error: + except RequestsConnectionError as error: logger.debug({'msg': str(error)}) t.labels( endpoint=endpoint, @@ -123,12 +123,12 @@ def _get_without_fallbacks( domain=urlparse(host).netloc, ) raise error - else: - t.labels( - endpoint=endpoint, - code=response.status_code, - domain=urlparse(host).netloc, - ) + + t.labels( + endpoint=endpoint, + code=response.status_code, + domain=urlparse(host).netloc, + ) response_fail_msg = f'Response from {complete_endpoint} [{response.status_code}] with text: "{str(response.text)}" returned.' diff --git a/tests/modules/submodules/test_oracle_module.py b/tests/modules/submodules/test_oracle_module.py index f12a38cf4..847a7a878 100644 --- a/tests/modules/submodules/test_oracle_module.py +++ b/tests/modules/submodules/test_oracle_module.py @@ -2,6 +2,9 @@ from typing import Type import pytest +from requests.exceptions import ConnectionError as RequestsConnectionError +from timeout_decorator import TimeoutError as DecoratorTimeoutError + from web3_multi_provider.multi_http_provider import NoActiveProviderError from src.modules.submodules.exceptions import IsNotMemberException, IncompatibleContractVersion @@ -92,9 +95,9 @@ def _throw_on_third_call(): @pytest.mark.parametrize( "ex", [ - TimeoutError, + DecoratorTimeoutError, NoActiveProviderError, - ConnectionError, + RequestsConnectionError, NotOkResponse, NoSlotsAvailable, SlotNotFinalized,