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 automine abstract property to TestProviderAPI #1974

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 2 additions & 5 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,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 @@ -750,11 +750,8 @@ def fake_partial(*args, **kwargs):
return mock_provider

ethereum.sepolia_fork.__dict__["providers"] = {"mock": fake_partial}

yield mock_provider

if actual:
ethereum.sepolia_fork.__dict__["providers"] = actual
ethereum.sepolia_fork.__dict__["providers"] = actual or {}


@pytest.fixture
Expand Down
Loading