Skip to content

Commit

Permalink
feat!: add automine abstract property to TestProviderAPI (#1974)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed May 21, 2024
1 parent 6670ed7 commit 15ebf35
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
30 changes: 30 additions & 0 deletions docs/userguides/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
14 changes: 14 additions & 0 deletions src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/ape_geth/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ def data_dir(self) -> Path:
def __repr__(self) -> str:
return f"<geth chain_id={self.chain_id}>"

@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:
Expand Down
3 changes: 1 addition & 2 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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:
Expand Down

0 comments on commit 15ebf35

Please sign in to comment.