From 220c31bbc5ee20ce0c5a59d8a158ad5615375f18 Mon Sep 17 00:00:00 2001 From: doggie <3859395+fubuloubu@users.noreply.github.com> Date: Mon, 21 Aug 2023 01:07:30 -0400 Subject: [PATCH 01/10] feat: allow configuring the runner class --- silverback/_cli.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/silverback/_cli.py b/silverback/_cli.py index 1fac3231..13e702f4 100644 --- a/silverback/_cli.py +++ b/silverback/_cli.py @@ -18,9 +18,10 @@ def cli(): @verbosity_option() @network_option(default=None) @click.option("--account", type=AccountAliasPromptChoice(), default=None) +@click.option("--runner", default=None) @click.option("-x", "--max-exceptions", type=int, default=3) @click.argument("path") -def run(cli_ctx, network, account, max_exceptions, path): +def run(cli_ctx, network, account, runner, max_exceptions, path): if network: os.environ["SILVERBACK_NETWORK_CHOICE"] = network else: @@ -29,7 +30,10 @@ def run(cli_ctx, network, account, max_exceptions, path): if account: os.environ["SILVERBACK_SIGNER_ALIAS"] = account.alias.replace("dev_", "TEST::") + if not (runner := runner and import_from_string(runner)): + runner = LiveRunner + with cli_ctx.network_manager.parse_network_choice(network): app = import_from_string(path) - runner = LiveRunner(app, max_exceptions=max_exceptions) + runner = runner(app, max_exceptions=max_exceptions) asyncio.run(runner.run()) From 1cd1e2839fd658dfc6a5bd97560755da38f9171b Mon Sep 17 00:00:00 2001 From: doggie <3859395+fubuloubu@users.noreply.github.com> Date: Mon, 21 Aug 2023 14:01:25 -0400 Subject: [PATCH 02/10] chore: (WIP) trying to allow running additional tasks in `BaseRunner.run` --- silverback/runner.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/silverback/runner.py b/silverback/runner.py index 82e060cb..60eafe23 100644 --- a/silverback/runner.py +++ b/silverback/runner.py @@ -1,5 +1,6 @@ import asyncio from abc import ABC, abstractmethod +from typing import Coroutine, Iterable from ape import chain from ape.contracts import ContractEvent, ContractInstance @@ -42,7 +43,7 @@ async def _event_task( handle an event handler task for the given contract event """ - async def run(self): + async def run(self, *other_tasks: Coroutine): await self.app.broker.startup() if block_handler := self.app.get_block_handler(): @@ -55,16 +56,14 @@ async def run(self): if event_handler := self.app.get_event_handler(contract_address, event_name): tasks.append(self._event_task(contract_event, event_handler)) + tasks.extend(other_tasks) + if len(tasks) == 0: raise SilverBackException("No tasks to execute") - try: - await asyncio.gather(*tasks) - except Exception as e: - logger.error(f"Critical exception, {type(e).__name__}: {e}") + await asyncio.gather(*tasks) - finally: - await self.app.broker.shutdown() + await self.app.broker.shutdown() class LiveRunner(BaseRunner): From 265778e2446183f79cef829b069813170bfed925 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Mon, 21 Aug 2023 15:06:02 -0500 Subject: [PATCH 03/10] chore: connection fixes --- silverback/_cli.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/silverback/_cli.py b/silverback/_cli.py index 13e702f4..05fb434c 100644 --- a/silverback/_cli.py +++ b/silverback/_cli.py @@ -13,12 +13,27 @@ def cli(): """Work with SilverBack applications in local context (using Ape).""" +def _load_runner(ctx, param, val): + if not val: + return LiveRunner + + elif runner := import_from_string(val): + return runner + + raise ValueError(f"Failed to import runner '{val}'.") + + @cli.command() @ape_cli_context() @verbosity_option() @network_option(default=None) -@click.option("--account", type=AccountAliasPromptChoice(), default=None) -@click.option("--runner", default=None) +@click.option("--account", type=AccountAliasPromptChoice()) +@click.option( + "--runner", + help="An import str in format ':'", + default=LiveRunner, + callback=_load_runner, +) @click.option("-x", "--max-exceptions", type=int, default=3) @click.argument("path") def run(cli_ctx, network, account, runner, max_exceptions, path): @@ -30,9 +45,6 @@ def run(cli_ctx, network, account, runner, max_exceptions, path): if account: os.environ["SILVERBACK_SIGNER_ALIAS"] = account.alias.replace("dev_", "TEST::") - if not (runner := runner and import_from_string(runner)): - runner = LiveRunner - with cli_ctx.network_manager.parse_network_choice(network): app = import_from_string(path) runner = runner(app, max_exceptions=max_exceptions) From 6f7b1e82cfd8786542c2dbf94ea451d39f2b85f8 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Mon, 21 Aug 2023 15:20:01 -0500 Subject: [PATCH 04/10] chore: add log stmt --- silverback/_cli.py | 2 ++ silverback/application.py | 1 + 2 files changed, 3 insertions(+) diff --git a/silverback/_cli.py b/silverback/_cli.py index 05fb434c..c4e3b8c4 100644 --- a/silverback/_cli.py +++ b/silverback/_cli.py @@ -3,6 +3,7 @@ import click from ape.cli import AccountAliasPromptChoice, ape_cli_context, network_option, verbosity_option +from ape.logging import logger from silverback._importer import import_from_string from silverback.runner import LiveRunner @@ -18,6 +19,7 @@ def _load_runner(ctx, param, val): return LiveRunner elif runner := import_from_string(val): + logger.info(f"Using custom runner '{runner.__name__}'.") return runner raise ValueError(f"Failed to import runner '{val}'.") diff --git a/silverback/application.py b/silverback/application.py index 06fdeec0..ca627c0d 100644 --- a/silverback/application.py +++ b/silverback/application.py @@ -52,6 +52,7 @@ def __init__(self, settings: Optional[Settings] = None): new_bock_timeout_str = ( f"\n NEW_BLOCK_TIMEOUT={self.new_block_timeout}" if self.new_block_timeout else "" ) + logger.info( f"Loaded Silverback App:{network_str}" f"{signer_str}{start_block_str}{new_bock_timeout_str}" From 6b98c3d3b73a4166cbbb67c027b23188bfafae55 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Mon, 21 Aug 2023 15:32:32 -0500 Subject: [PATCH 05/10] fix: default --- silverback/_cli.py | 1 - 1 file changed, 1 deletion(-) diff --git a/silverback/_cli.py b/silverback/_cli.py index c4e3b8c4..a652b210 100644 --- a/silverback/_cli.py +++ b/silverback/_cli.py @@ -33,7 +33,6 @@ def _load_runner(ctx, param, val): @click.option( "--runner", help="An import str in format ':'", - default=LiveRunner, callback=_load_runner, ) @click.option("-x", "--max-exceptions", type=int, default=3) From dbebb1c3c63d3c3ac2a56e8eaa9dc9d0245877f8 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Mon, 21 Aug 2023 15:33:56 -0500 Subject: [PATCH 06/10] chore: undo log add --- silverback/_cli.py | 2 -- silverback/runner.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/silverback/_cli.py b/silverback/_cli.py index a652b210..f4cf9473 100644 --- a/silverback/_cli.py +++ b/silverback/_cli.py @@ -3,7 +3,6 @@ import click from ape.cli import AccountAliasPromptChoice, ape_cli_context, network_option, verbosity_option -from ape.logging import logger from silverback._importer import import_from_string from silverback.runner import LiveRunner @@ -19,7 +18,6 @@ def _load_runner(ctx, param, val): return LiveRunner elif runner := import_from_string(val): - logger.info(f"Using custom runner '{runner.__name__}'.") return runner raise ValueError(f"Failed to import runner '{val}'.") diff --git a/silverback/runner.py b/silverback/runner.py index 60eafe23..5428094d 100644 --- a/silverback/runner.py +++ b/silverback/runner.py @@ -1,6 +1,6 @@ import asyncio from abc import ABC, abstractmethod -from typing import Coroutine, Iterable +from typing import Coroutine from ape import chain from ape.contracts import ContractEvent, ContractInstance From 0f994a991c44ace41d0846567d6cef530a7ad72b Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Tue, 22 Aug 2023 07:46:59 -0500 Subject: [PATCH 07/10] feat: cli cbs --- silverback/_cli.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/silverback/_cli.py b/silverback/_cli.py index f4cf9473..c3c9592a 100644 --- a/silverback/_cli.py +++ b/silverback/_cli.py @@ -13,7 +13,7 @@ def cli(): """Work with SilverBack applications in local context (using Ape).""" -def _load_runner(ctx, param, val): +def _runner_callback(ctx, param, val): if not val: return LiveRunner @@ -23,27 +23,36 @@ def _load_runner(ctx, param, val): raise ValueError(f"Failed to import runner '{val}'.") +def _account_callback(ctx, param, val): + if val: + val = val.alias.replace("dev_", "TEST::") + os.environ["SILVERBACK_SIGNER_ALIAS"] = val + + return val + + +def _network_callback(ctx, param, val): + if val: + os.environ["SILVERBACK_NETWORK_CHOICE"] = val + else: + val = os.environ.get("SILVERBACK_NETWORK_CHOICE", "") + + return val + + @cli.command() @ape_cli_context() @verbosity_option() -@network_option(default=None) -@click.option("--account", type=AccountAliasPromptChoice()) +@network_option(default=None, callback=_network_callback) +@click.option("--account", type=AccountAliasPromptChoice(), callback=_account_callback) @click.option( "--runner", help="An import str in format ':'", - callback=_load_runner, + callback=_runner_callback, ) @click.option("-x", "--max-exceptions", type=int, default=3) @click.argument("path") def run(cli_ctx, network, account, runner, max_exceptions, path): - if network: - os.environ["SILVERBACK_NETWORK_CHOICE"] = network - else: - network = os.environ.get("SILVERBACK_NETWORK_CHOICE", "") - - if account: - os.environ["SILVERBACK_SIGNER_ALIAS"] = account.alias.replace("dev_", "TEST::") - with cli_ctx.network_manager.parse_network_choice(network): app = import_from_string(path) runner = runner(app, max_exceptions=max_exceptions) From bb13b4a6cd1837cb40bcbdeffb8638e1037ab89a Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Tue, 22 Aug 2023 08:41:12 -0500 Subject: [PATCH 08/10] chore: lint deps --- .pre-commit-config.yaml | 4 ++-- setup.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b402841f..f7b6ea95 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,7 +10,7 @@ repos: - id: isort - repo: https://github.com/psf/black - rev: 22.12.0 + rev: 23.7.0 hooks: - id: black name: black @@ -21,7 +21,7 @@ repos: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.991 + rev: v1.4.1 hooks: - id: mypy additional_dependencies: [types-setuptools, pydantic] diff --git a/setup.py b/setup.py index 8e61c244..dd0779dc 100644 --- a/setup.py +++ b/setup.py @@ -10,8 +10,8 @@ "hypothesis>=6.2.0,<7.0", # Strategy-based fuzzer ], "lint": [ - "black>=22.12.0", # auto-formatter and linter - "mypy>=0.991", # Static type analyzer + "black>=23.7.0", # auto-formatter and linter + "mypy>=1.4.1,<2", # Static type analyzer "types-setuptools", # Needed for mypy type shed "flake8>=5.0.4", # Style linter "isort>=5.10.1", # Import sorting linter From 36ef127a7bc3e6fc1fc53e4592e0898530dd7dd5 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Tue, 22 Aug 2023 10:17:35 -0500 Subject: [PATCH 09/10] refactor: rm other tasks feat --- silverback/application.py | 1 - silverback/runner.py | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/silverback/application.py b/silverback/application.py index ca627c0d..06fdeec0 100644 --- a/silverback/application.py +++ b/silverback/application.py @@ -52,7 +52,6 @@ def __init__(self, settings: Optional[Settings] = None): new_bock_timeout_str = ( f"\n NEW_BLOCK_TIMEOUT={self.new_block_timeout}" if self.new_block_timeout else "" ) - logger.info( f"Loaded Silverback App:{network_str}" f"{signer_str}{start_block_str}{new_bock_timeout_str}" diff --git a/silverback/runner.py b/silverback/runner.py index 5428094d..1fb056c1 100644 --- a/silverback/runner.py +++ b/silverback/runner.py @@ -43,7 +43,7 @@ async def _event_task( handle an event handler task for the given contract event """ - async def run(self, *other_tasks: Coroutine): + async def run(self): await self.app.broker.startup() if block_handler := self.app.get_block_handler(): @@ -56,8 +56,6 @@ async def run(self, *other_tasks: Coroutine): if event_handler := self.app.get_event_handler(contract_address, event_name): tasks.append(self._event_task(contract_event, event_handler)) - tasks.extend(other_tasks) - if len(tasks) == 0: raise SilverBackException("No tasks to execute") From 05e315e68840b072d594751303fe6f4340061bc5 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Tue, 22 Aug 2023 10:19:39 -0500 Subject: [PATCH 10/10] chore: lint --- silverback/runner.py | 1 - 1 file changed, 1 deletion(-) diff --git a/silverback/runner.py b/silverback/runner.py index 1fb056c1..25778e99 100644 --- a/silverback/runner.py +++ b/silverback/runner.py @@ -1,6 +1,5 @@ import asyncio from abc import ABC, abstractmethod -from typing import Coroutine from ape import chain from ape.contracts import ContractEvent, ContractInstance