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

Cache eth.chain_id in simple_cache_middleware #2425

Merged
merged 4 commits into from
Apr 13, 2022
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
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(web3):
with pytest.warns(DeprecationWarning):
Expand All @@ -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
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