From adc0458fd2ad27426dacb00bcda898b4c6115d4d Mon Sep 17 00:00:00 2001 From: 0xthedance <150128373+0xthedance@users.noreply.github.com> Date: Fri, 31 Jan 2025 16:14:02 +0000 Subject: [PATCH] feat: Include additional flags to ape networks run (#2482) --- docs/userguides/networks.md | 2 ++ src/ape/api/networks.py | 1 - src/ape_networks/_cli.py | 9 ++++++++- tests/integration/cli/test_networks.py | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/docs/userguides/networks.md b/docs/userguides/networks.md index 9e91b89db6..6f18ce8f48 100644 --- a/docs/userguides/networks.md +++ b/docs/userguides/networks.md @@ -499,6 +499,8 @@ To use a different network, such as `hardhat` or Anvil nodes, use the `--network ape networks run --network ethereum:local:foundry ``` +To configure the network's block time, use the `--block-time` option. + ## Provider Interaction Once you are connected to a network, you now have access to a `.provider`. diff --git a/src/ape/api/networks.py b/src/ape/api/networks.py index 952a0f19e6..86b5457247 100644 --- a/src/ape/api/networks.py +++ b/src/ape/api/networks.py @@ -1038,7 +1038,6 @@ def block_time(self) -> int: mainnet: block_time: 15 """ - return self.config.get("block_time", 0) @property diff --git a/src/ape_networks/_cli.py b/src/ape_networks/_cli.py index a8b2095f46..785cf25745 100644 --- a/src/ape_networks/_cli.py +++ b/src/ape_networks/_cli.py @@ -112,7 +112,8 @@ def make_sub_tree(data: dict, create_tree: Callable) -> Tree: @cli.command(short_help="Start a node process") @ape_cli_context() @network_option(default="ethereum:local:node") -def run(cli_ctx, provider): +@click.option("--block-time", default=None, type=int, help="Block time in seconds") +def run(cli_ctx, provider, block_time): """ Start a subprocess node as if running independently and stream stdout and stderr. @@ -128,6 +129,10 @@ def run(cli_ctx, provider): elif provider.is_connected: cli_ctx.abort("Process already running.") + # Set block time if provided + if block_time is not None: + provider.provider_settings.update({"block_time": block_time}) + # Start showing process logs. original_level = cli_ctx.logger.level original_format = cli_ctx.logger.fmt @@ -135,6 +140,7 @@ def run(cli_ctx, provider): # Change format to exclude log level (since it is always just DEBUG) cli_ctx.logger.format(fmt="%(message)s") + try: _run(cli_ctx, provider) finally: @@ -143,6 +149,7 @@ def run(cli_ctx, provider): def _run(cli_ctx, provider: "SubprocessProvider"): + provider.connect() if process := provider.process: try: diff --git a/tests/integration/cli/test_networks.py b/tests/integration/cli/test_networks.py index c300c80307..45d33bc58f 100644 --- a/tests/integration/cli/test_networks.py +++ b/tests/integration/cli/test_networks.py @@ -201,6 +201,22 @@ def test_run_custom_network(ape_cli, runner): assert expected in result.output +@run_once +def test_run_block_time(ape_cli, runner): + cmd = ( + "networks", + "run", + "--network", + "ethereum:local:test", + "--block-time", + "10", + ) + result = runner.invoke(ape_cli, cmd) + expected = "`ape networks run` requires a provider that manages a process, not 'test'" + assert result.exit_code != 0 + assert expected in result.output + + @geth_process_test @skip_projects_except("geth") def test_run_already_running(networks_runner, integ_project, geth_provider):