Skip to content

Commit

Permalink
Cache eth.chain_id in simple_cache_middleware (ethereum#2425)
Browse files Browse the repository at this point in the history
* Add caching for sync chain_id

* Remove async chainId setter

Will add async chainId to the cache once the simple_cache_middleware is available on async

* Add docs

* Add newsfragment for simple_cache_middleware, take out old, confusing newsfragment
  • Loading branch information
kclowes committed Apr 13, 2022
1 parent 2cd75fd commit ebf449c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 57 deletions.
12 changes: 12 additions & 0 deletions docs/web3.eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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<web3.middleware.construct_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
Expand Down
1 change: 0 additions & 1 deletion newsfragments/2207.feature.rst

This file was deleted.

1 change: 1 addition & 0 deletions newsfragments/2425.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add sync chain_id to ``simple_middleware_cache``
39 changes: 0 additions & 39 deletions tests/core/eth-module/test_eth_properties.py
Original file line number Diff line number Diff line change
@@ -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(w3):
with pytest.warns(DeprecationWarning):
Expand All @@ -36,24 +18,3 @@ def test_eth_chain_id(w3):
def test_eth_chainId(w3):
with pytest.warns(DeprecationWarning):
assert w3.eth.chainId == 61


def test_set_chain_id(w3):
assert w3.eth.chain_id == 61

w3.eth.chain_id = 72
assert w3.eth.chain_id == 72

w3.eth.chain_id = None
assert w3.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
19 changes: 2 additions & 17 deletions web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions web3/middleware/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
# 'eth_getWork',
# 'eth_submitWork',
# 'eth_submitHashrate',
'eth_chainId',
})


Expand Down

0 comments on commit ebf449c

Please sign in to comment.