Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Add custom block time [SBK-262] #34

Open
PatrickAlphaC opened this issue Aug 30, 2023 · 2 comments
Open

[FEAT] Add custom block time [SBK-262] #34

PatrickAlphaC opened this issue Aug 30, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@PatrickAlphaC
Copy link

Overview

For testing my setup, I'd like to run my environment on a local chain like anvil, hardhat, or even ape networks run. However, due to how block time is calculated, it's quite difficult.

Exact issue

I have an anvil local chain running that I have run with :

anvil -b 2

The -b 2 gives it a block time of 2.

When I run the following command:

silverback run "monitoring:app" --network http://127.0.0.1:8545

I get the following stack trace

WARNING: Connecting Geth plugin to non-Geth client 'anvil'.
INFO: Loading Silverback App with settings:
  BROKER_CLASS="taskiq:InMemoryBroker"
  NETWORK_CHOICE="http://127.0.0.1:8545"
WARNING: Connecting Geth plugin to non-Geth client 'anvil'.
INFO: Loaded Silverback App:
  NETWORK="ethereum:adhoc"
  SIGNER=None
INFO: Using LiveRunner: max_exceptions=3
INFO: `ape-cache` database has not been initialized
INFO: block[block=0x45a75375d3e5e314597b98c678d592d35512fe26e4da0f6cd6c5f13aa6a92a9b] - Started
INFO: block[block=0x45a75375d3e5e314597b98c678d592d35512fe26e4da0f6cd6c5f13aa6a92a9b] - 0.010s (0.5%)
Traceback (most recent call last):
  File "/PATH/venv/bin/silverback", line 8, in <module>
    sys.exit(cli())
  File "/PATH/venv/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/PATH/venv/lib/python3.10/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/PATH/venv/lib/python3.10/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/PATH/venv/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/PATH/venv/lib/python3.10/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/PATH/venv/lib/python3.10/site-packages/click/decorators.py", line 92, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/PATH/venv/lib/python3.10/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/PATH/venv/lib/python3.10/site-packages/silverback/_cli.py", line 59, in run
    asyncio.run(runner.run())
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/PATH/venv/lib/python3.10/site-packages/silverback/runner.py", line 61, in run
    await asyncio.gather(*tasks)
  File "/PATH/venv/lib/python3.10/site-packages/silverback/runner.py", line 87, in _block_task
    async for block in async_wrap_iter(
  File "/PATH/venv/lib/python3.10/site-packages/silverback/utils.py", line 21, in yield_queue_items
    raise exception
  File "/PATH/venv/lib/python3.10/site-packages/silverback/utils.py", line 26, in iter_to_queue
    for item in it:
  File "/PATH/venv/lib/python3.10/site-packages/ape/managers/chain.py", line 346, in poll_blocks
    _try_timeout()
  File "/PATH/venv/lib/python3.10/site-packages/ape/managers/chain.py", line 326, in _try_timeout
    raise ChainError(message)
ape.exceptions.ChainError: Timed out waiting for new block (time_waited=0.0144).

This is because the timeout variable in ape/managers/chain.py::poll_blocks is set as such:

timeout = (
            (
                10.0
                if network_name == LOCAL_NETWORK_NAME or network_name.endswith("-fork")
                else 50 * block_time
            )
            if new_block_timeout is None
            else new_block_timeout
        )

I'm not sure how to tell silverback that the anvil chain block time is 2.

Request / Ask

Is there a way to tell silverback/ape that the ad-hoc's block time is X?

@PatrickAlphaC PatrickAlphaC added the enhancement New feature or request label Aug 30, 2023
@vany365 vany365 changed the title [FEAT] Add custom block time [FEAT] Add custom block time [SBK-262] Aug 30, 2023
@fubuloubu
Copy link
Member

You can add new_block_timeout kwargs to app.on_ decorators, but in general make sure when you run anvil you are running it in a mode where it continuously mines blocks and not based on transactions occuring (e.g. "automine" functionality)

@fubuloubu
Copy link
Member

Some further information: Silverback uses Ape's config system to detect timing, falling back to automatic detection if not available

def compute_block_time() -> int:
genesis = self.chain_manager.blocks[0]
head = self.chain_manager.blocks.head
if not head.number or head.number == 0:
return 10
return int((head.timestamp - genesis.timestamp) / head.number)
self.block_time = self.chain_manager.provider.network.block_time or compute_block_time()

So make sure you've configured Ape correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants