diff --git a/docs/web3.eth.rst b/docs/web3.eth.rst index eaa1653782..7d5a473772 100644 --- a/docs/web3.eth.rst +++ b/docs/web3.eth.rst @@ -216,6 +216,18 @@ The following properties are available on the ``web3.eth`` namespace. >>> web3.eth.chain_id 61 + .. note:: + + This property gets called frequently in validation middleware, + but `chain_id` is added to the ``simple_cache_middleware`` by default. + Add the :meth:`simple_cache_middleware` + to the ``middleware_onion`` to increase performance: + + .. code-block:: python + + >>> from web3.middleware import simple_cache_middleware + >>> w3.middleware_onion.add(simple_cache_middleare) + .. py:attribute:: Eth.chainId diff --git a/newsfragments/2207.feature.rst b/newsfragments/2207.feature.rst deleted file mode 100644 index 78a6f8bbd5..0000000000 --- a/newsfragments/2207.feature.rst +++ /dev/null @@ -1 +0,0 @@ -Add setter for the Eth.chain_id property diff --git a/newsfragments/2425.feature.rst b/newsfragments/2425.feature.rst new file mode 100644 index 0000000000..fc01c0dcdc --- /dev/null +++ b/newsfragments/2425.feature.rst @@ -0,0 +1 @@ +Add sync chain_id to ``simple_middleware_cache`` diff --git a/tests/core/eth-module/test_eth_properties.py b/tests/core/eth-module/test_eth_properties.py index 813af404e8..f782061f65 100644 --- a/tests/core/eth-module/test_eth_properties.py +++ b/tests/core/eth-module/test_eth_properties.py @@ -1,23 +1,5 @@ import pytest -from web3 import Web3 -from web3.eth import ( - AsyncEth, -) -from web3.providers.eth_tester.main import ( - AsyncEthereumTesterProvider, -) - - -@pytest.fixture -def async_w3(): - return Web3( - AsyncEthereumTesterProvider(), - middlewares=[], - modules={ - 'eth': (AsyncEth,), - }) - def test_eth_protocol_version(web3): with pytest.warns(DeprecationWarning): @@ -36,24 +18,3 @@ def test_eth_chain_id(web3): def test_eth_chainId(web3): with pytest.warns(DeprecationWarning): assert web3.eth.chainId == 61 - - -def test_set_chain_id(web3): - assert web3.eth.chain_id == 61 - - web3.eth.chain_id = 72 - assert web3.eth.chain_id == 72 - - web3.eth.chain_id = None - assert web3.eth.chain_id == 61 - - -@pytest.mark.asyncio -async def test_async_set_chain_id(async_w3): - assert await async_w3.eth.chain_id == 61 - - async_w3.eth.chain_id = 72 - assert await async_w3.eth.chain_id == 72 - - async_w3.eth.chain_id = None - assert await async_w3.eth.chain_id == 61 diff --git a/web3/eth.py b/web3/eth.py index 81cc271c6f..d3b23b5f04 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -114,7 +114,6 @@ class BaseEth(Module): _default_account: Union[ChecksumAddress, Empty] = empty _default_block: BlockIdentifier = "latest" - _default_chain_id: Optional[int] = None gasPriceStrategy = None _gas_price: Method[Callable[[], Wei]] = Method( @@ -358,14 +357,7 @@ async def block_number(self) -> BlockNumber: @property async def chain_id(self) -> int: - if self._default_chain_id is None: - return await self._chain_id() # type: ignore - else: - return self._default_chain_id - - @chain_id.setter - def chain_id(self, value: int) -> None: - self._default_chain_id = value + return await self._chain_id() # type: ignore @property async def coinbase(self) -> ChecksumAddress: @@ -637,14 +629,7 @@ def blockNumber(self) -> BlockNumber: @property def chain_id(self) -> int: - if self._default_chain_id is None: - return self._chain_id() - else: - return self._default_chain_id - - @chain_id.setter - def chain_id(self, value: int) -> None: - self._default_chain_id = value + return self._chain_id() @property def chainId(self) -> int: diff --git a/web3/middleware/cache.py b/web3/middleware/cache.py index e81761b0a3..a47f4eb585 100644 --- a/web3/middleware/cache.py +++ b/web3/middleware/cache.py @@ -82,6 +82,7 @@ # 'eth_getWork', # 'eth_submitWork', # 'eth_submitHashrate', + 'eth_chainId', })