From da086952a10d30c7fbc46b3d1a6878fb6df7719e Mon Sep 17 00:00:00 2001 From: kclowes Date: Wed, 13 Apr 2022 10:26:18 -0600 Subject: [PATCH 1/4] Add caching for sync chain_id --- web3/eth.py | 19 ++----------------- web3/middleware/cache.py | 1 + 2 files changed, 3 insertions(+), 17 deletions(-) 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', }) From 70ece1c67efcaa48e547a5ded49f1e1508af9966 Mon Sep 17 00:00:00 2001 From: kclowes Date: Wed, 13 Apr 2022 11:48:06 -0600 Subject: [PATCH 2/4] Remove async chainId setter Will add async chainId to the cache once the simple_cache_middleware is available on async --- tests/core/eth-module/test_eth_properties.py | 39 -------------------- 1 file changed, 39 deletions(-) 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 From e1f41981193cd8feb3f3a75ee14fc5e117b751a4 Mon Sep 17 00:00:00 2001 From: kclowes Date: Wed, 13 Apr 2022 12:05:48 -0600 Subject: [PATCH 3/4] Add docs --- docs/web3.eth.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 From dc22af8cde3e8edf1974b669dfd66f24a899d191 Mon Sep 17 00:00:00 2001 From: kclowes Date: Wed, 13 Apr 2022 12:15:25 -0600 Subject: [PATCH 4/4] Add newsfragment for simple_cache_middleware, take out old, confusing newsfragment --- newsfragments/2207.feature.rst | 1 - newsfragments/2425.feature.rst | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 newsfragments/2207.feature.rst create mode 100644 newsfragments/2425.feature.rst 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``