Skip to content

Commit

Permalink
test: auto mine functionality to eth-tester (#1972)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Mar 28, 2024
1 parent a4a6a3d commit a4753da
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/ape_test/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ast import literal_eval
from functools import cached_property
from re import Pattern
from typing import Dict, Optional, cast
from typing import Any, Dict, Optional, cast

from eth.exceptions import HeaderNotFound
from eth_pydantic_types import HexBytes
Expand Down Expand Up @@ -32,6 +32,7 @@

class EthTesterProviderConfig(PluginConfig):
chain_id: int = DEFAULT_TEST_CHAIN_ID
auto_mine: bool = True


class LocalProvider(TestProviderAPI, Web3Provider):
Expand All @@ -50,7 +51,8 @@ def evm_backend(self) -> PyEVMBackend:

return self._evm_backend

def connect(self):
@cached_property
def tester(self):
chain_id = self.settings.chain_id
if self._web3 is not None:
connected_chain_id = self._make_request("eth_chainId")
Expand All @@ -66,8 +68,29 @@ def connect(self):
)
endpoints = {**API_ENDPOINTS}
endpoints["eth"] = merge(endpoints["eth"], {"chainId": static_return(chain_id)})
tester = EthereumTesterProvider(ethereum_tester=self._evm_backend, api_endpoints=endpoints)
self._web3 = Web3(tester)
return EthereumTesterProvider(ethereum_tester=self._evm_backend, api_endpoints=endpoints)

@property
def auto_mine(self) -> bool:
return self.tester.ethereum_tester.auto_mine_transactions

@auto_mine.setter
def auto_mine(self, value: Any) -> None:
if value is True:
self.tester.ethereum_tester.enable_auto_mine_transactions()
elif value is False:
self.tester.ethereum_tester.disable_auto_mine_transactions()
else:
raise TypeError("Expecting bool-value for auto_mine setter.")

def connect(self):
if "tester" in self.__dict__:
del self.__dict__["tester"]

self._web3 = Web3(self.tester)
# Handle disabling auto-mine if the user requested via config.
if self.config.provider.auto_mine is False:
self.auto_mine = False # type: ignore[misc]

def disconnect(self):
# NOTE: This type ignore seems like a bug in pydantic.
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,17 @@ def test_create_access_list(eth_tester_provider, vyper_contract_instance, owner)
tx = vyper_contract_instance.setNumber.as_transaction(123, sender=owner)
with pytest.raises(APINotImplementedError):
eth_tester_provider.create_access_list(tx)


def test_auto_mine(eth_tester_provider):
eth_tester_provider.auto_mine = False
assert not eth_tester_provider.auto_mine

# Ensure can still manually mine.
block = eth_tester_provider.get_block("latest").number
eth_tester_provider.mine()
next_block = eth_tester_provider.get_block("latest").number
assert next_block > block

eth_tester_provider.auto_mine = True
assert eth_tester_provider.auto_mine

0 comments on commit a4753da

Please sign in to comment.