diff --git a/docs/userguides/testing.md b/docs/userguides/testing.md index 5ce2e31c6b..71eb42f7f4 100644 --- a/docs/userguides/testing.md +++ b/docs/userguides/testing.md @@ -249,6 +249,36 @@ Another option for testing providers is the [ape-hardhat](https://github.com/Ape ape plugins install hardhat ``` +### Mining + +Test providers allow you to control mining. +For example, mine an empty block using the [mine](../methoddocs/api.html#ape.api.providers.TestProviderAPI.mine) method: + +```python +from ape import chain + +chain.provider.mine() +``` + +You can also pass it a number of blocks to mine: + +```python +from ape import chain + +chain.provider.mine(5) +``` + +By default, testing providers automatically mine after sending transactions. +However, you can disable this feature by setting the property. + +```python +from ape import chain + +chain.provider.auto_mine = False +# You can also re-enable +chain.provider.auto_mine = True +``` + ## Advanced Testing Tips If you want to use sample projects, follow this link to [Ape Academy](https://github.com/ApeAcademy). diff --git a/src/ape/api/providers.py b/src/ape/api/providers.py index 59fd7329c9..d5efe9166d 100644 --- a/src/ape/api/providers.py +++ b/src/ape/api/providers.py @@ -874,6 +874,20 @@ def mine(self, num_blocks: int = 1): num_blocks (int): The number of blocks allotted to mine. Defaults to ``1``. """ + @property + @abstractmethod + def auto_mine(self) -> bool: + """ + Whether automine is enabled. + """ + + @auto_mine.setter + @abstractmethod + def auto_mine(self) -> bool: + """ + Enable or disbale automine. + """ + def _increment_call_func_coverage_hit_count(self, txn: TransactionAPI): """ A helper method for incrementing a method call function hit count in a diff --git a/src/ape_geth/provider.py b/src/ape_geth/provider.py index e87a60a7c3..8018e5246b 100644 --- a/src/ape_geth/provider.py +++ b/src/ape_geth/provider.py @@ -249,6 +249,14 @@ def data_dir(self) -> Path: def __repr__(self) -> str: return f"" + @property + def auto_mine(self) -> bool: + return self._make_request("eth_mining", []) + + @auto_mine.setter + def auto_mine(self, value): + raise NotImplementedError("'auto_mine' setter not implemented.") + def connect(self): self._set_web3() if self.is_connected: diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index 8a8f3d8918..749040c51c 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -737,7 +737,7 @@ def disable_fork_providers(ethereum): @pytest.fixture -def mock_fork_provider(mocker, ethereum): +def mock_fork_provider(mocker, ethereum, mock_sepolia): """ A fake provider representing something like ape-foundry that can fork networks (only uses sepolia-fork). @@ -755,7 +755,6 @@ def fake_partial(*args, **kwargs): ethereum.sepolia_fork._default_provider = "mock" ethereum.sepolia_fork.__dict__["providers"] = {"mock": fake_partial} - yield mock_provider if initial_providers: