From 290d7edf541579ac940bc134a468cfb410771657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Wed, 8 Feb 2023 14:16:54 +0200 Subject: [PATCH 1/3] Adjust log level config. Add some extra logs for contract verification. --- multiversx_sdk_cli/cli.py | 4 +- multiversx_sdk_cli/contract_verification.py | 52 ++++++++++++++++----- multiversx_sdk_cli/docker.py | 7 ++- multiversx_sdk_cli/errors.py | 5 -- 4 files changed, 45 insertions(+), 23 deletions(-) diff --git a/multiversx_sdk_cli/cli.py b/multiversx_sdk_cli/cli.py index 5fdc80e0..bad11893 100644 --- a/multiversx_sdk_cli/cli.py +++ b/multiversx_sdk_cli/cli.py @@ -45,9 +45,9 @@ def _do_main(cli_args: List[str]): args = parser.parse_args(argv_with_config_args) if args.verbose: - logging.basicConfig(level=logging.DEBUG) + logging.basicConfig(level=logging.DEBUG, force=True) else: - logging.basicConfig(level=logging.WARN) + logging.basicConfig(level=logging.INFO, force=True) if not hasattr(args, "func"): parser.print_help() diff --git a/multiversx_sdk_cli/contract_verification.py b/multiversx_sdk_cli/contract_verification.py index 55984414..9eb443a1 100644 --- a/multiversx_sdk_cli/contract_verification.py +++ b/multiversx_sdk_cli/contract_verification.py @@ -3,13 +3,14 @@ import logging import time from pathlib import Path -from typing import Any, Dict +from typing import Any, Dict, Tuple import requests from multiversx_sdk_core import MessageV1 from nacl.signing import SigningKey from multiversx_sdk_cli.accounts import Account, Address +from multiversx_sdk_cli.errors import KnownError from multiversx_sdk_cli.utils import dump_out_json, read_json_file HTTP_REQUEST_TIMEOUT = 408 @@ -74,24 +75,25 @@ def trigger_contract_verification( request_dictionary = contract_verification.to_dictionary() url = f"{verifier_url}/verifier" - response = requests.post(url, json=request_dictionary) + status_code, message, data = _do_post(url, request_dictionary) - if response.status_code == HTTP_REQUEST_TIMEOUT: - task_id = response.json().get("taskId", "") + if status_code == HTTP_REQUEST_TIMEOUT: + task_id = data.get("taskId", "") if task_id: query_status_with_task_id(verifier_url, task_id) else: - dump_out_json(response.json()) - elif response.status_code != HTTP_SUCCESS: - dump_out_json(response.json()) - elif response.status_code == HTTP_SUCCESS: - status = response.json().get("status", "") + dump_out_json(data) + elif status_code != HTTP_SUCCESS: + dump_out_json(data) + raise KnownError(f"Cannot verify contract: {message}") + else: + status = data.get("status", "") if status: logger.info(f"Task status: {status}") - dump_out_json(response.json()) + dump_out_json(data) else: - task_id = response.json().get("taskId", "") + task_id = data.get("taskId", "") query_status_with_task_id(verifier_url, task_id) @@ -116,7 +118,7 @@ def query_status_with_task_id(url: str, task_id: str, interval: int = 10): old_status = "" while True: - response = requests.get(f"{url}/tasks/{task_id}").json() + _, _, response = _do_get(f"{url}/tasks/{task_id}") status = response.get("status", "") if status == "finished": @@ -129,3 +131,29 @@ def query_status_with_task_id(url: str, task_id: str, interval: int = 10): old_status = status time.sleep(interval) + + +def _do_post(url: str, payload: Any) -> Tuple[int, str, Dict[str, Any]]: + logger.debug(f"_do_post() to {url}") + response = requests.post(url, json=payload) + + try: + data = response.json() + message = data.get("message", "") + return response.status_code, message, data + except Exception as error: + logger.error(f"Erroneous response from {url}: {response.text}") + raise KnownError(f"Cannot parse response from {url}", error) + + +def _do_get(url: str) -> Tuple[int, str, Dict[str, Any]]: + logger.debug(f"_do_get() from {url}") + response = requests.get(url) + + try: + data = response.json() + message = data.get("message", "") + return response.status_code, message, data + except Exception as error: + logger.error(f"Erroneous response from {url}: {response.text}") + raise KnownError(f"Cannot parse response from {url}", error) diff --git a/multiversx_sdk_cli/docker.py b/multiversx_sdk_cli/docker.py index 4062ca55..84ee7083 100644 --- a/multiversx_sdk_cli/docker.py +++ b/multiversx_sdk_cli/docker.py @@ -4,10 +4,9 @@ from pathlib import Path from typing import List -from multiversx_sdk_cli import myprocess -from multiversx_sdk_cli.errors import ExternalProcessError, KnownError +from multiversx_sdk_cli.errors import KnownError -logger = logging.getLogger("build-with-docker") +logger = logging.getLogger("docker") def is_docker_installed(): @@ -49,7 +48,7 @@ def run_docker( docker_args += ["--user", f"{str(os.getuid())}:{str(os.getgid())}"] docker_args += ["--rm", image] - if not(no_default_platform): + if not (no_default_platform): docker_args += ["--platform", "linux/amd64"] entrypoint_args: List[str] = [] diff --git a/multiversx_sdk_cli/errors.py b/multiversx_sdk_cli/errors.py index 673109d1..ecae7e4e 100644 --- a/multiversx_sdk_cli/errors.py +++ b/multiversx_sdk_cli/errors.py @@ -79,11 +79,6 @@ def __init__(self, message, inner=None): super().__init__(f"Build error: {message}.", inner) -class UnknownArgumentFormat(KnownError): - def __init__(self, argument: Any): - super().__init__(f"Cannot handle non-hex, non-number arguments yet: {argument}.") - - class ProxyRequestError(KnownError): def __init__(self, url: str, data: Any): super().__init__(f"Proxy request error for url [{url}]: {data}") From da8c5385bf0bd4f7555b718df05106f3a1b5c384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Wed, 8 Feb 2023 14:17:42 +0200 Subject: [PATCH 2/3] Bump version. --- multiversx_sdk_cli/_version.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/multiversx_sdk_cli/_version.py b/multiversx_sdk_cli/_version.py index fd4cdef3..f5752882 100644 --- a/multiversx_sdk_cli/_version.py +++ b/multiversx_sdk_cli/_version.py @@ -1 +1 @@ -__version__ = "5.2.3" +__version__ = "5.3.0" diff --git a/setup.py b/setup.py index a6197d48..3f9cbc45 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ import setuptools -VERSION = "5.2.3" +VERSION = "5.3.0" try: with open('./multiversx_sdk_cli/_version.py', 'wt') as versionfile: From 5b99ef2678843f25ced1c25edb7fb8e071a2d4ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Wed, 8 Feb 2023 14:22:47 +0200 Subject: [PATCH 3/3] Fix error removal. --- multiversx_sdk_cli/errors.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/multiversx_sdk_cli/errors.py b/multiversx_sdk_cli/errors.py index ecae7e4e..b187ed1a 100644 --- a/multiversx_sdk_cli/errors.py +++ b/multiversx_sdk_cli/errors.py @@ -79,9 +79,9 @@ def __init__(self, message, inner=None): super().__init__(f"Build error: {message}.", inner) -class ProxyRequestError(KnownError): - def __init__(self, url: str, data: Any): - super().__init__(f"Proxy request error for url [{url}]: {data}") +class UnknownArgumentFormat(KnownError): + def __init__(self, argument: Any): + super().__init__(f"Cannot handle non-hex, non-number arguments yet: {argument}.") class BadInputError(KnownError):