Skip to content

Commit ebf449c

Browse files
committed
Cache eth.chain_id in simple_cache_middleware (ethereum#2425)
* 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
1 parent 2cd75fd commit ebf449c

File tree

6 files changed

+16
-57
lines changed

6 files changed

+16
-57
lines changed

docs/web3.eth.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ The following properties are available on the ``web3.eth`` namespace.
216216
>>> web3.eth.chain_id
217217
61
218218
219+
.. note::
220+
221+
This property gets called frequently in validation middleware,
222+
but `chain_id` is added to the ``simple_cache_middleware`` by default.
223+
Add the :meth:`simple_cache_middleware<web3.middleware.construct_simple_cache_middleware>`
224+
to the ``middleware_onion`` to increase performance:
225+
226+
.. code-block:: python
227+
228+
>>> from web3.middleware import simple_cache_middleware
229+
>>> w3.middleware_onion.add(simple_cache_middleare)
230+
219231
220232
.. py:attribute:: Eth.chainId
221233

newsfragments/2207.feature.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.

newsfragments/2425.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add sync chain_id to ``simple_middleware_cache``
Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
import pytest
22

3-
from web3 import Web3
4-
from web3.eth import (
5-
AsyncEth,
6-
)
7-
from web3.providers.eth_tester.main import (
8-
AsyncEthereumTesterProvider,
9-
)
10-
11-
12-
@pytest.fixture
13-
def async_w3():
14-
return Web3(
15-
AsyncEthereumTesterProvider(),
16-
middlewares=[],
17-
modules={
18-
'eth': (AsyncEth,),
19-
})
20-
213

224
def test_eth_protocol_version(w3):
235
with pytest.warns(DeprecationWarning):
@@ -36,24 +18,3 @@ def test_eth_chain_id(w3):
3618
def test_eth_chainId(w3):
3719
with pytest.warns(DeprecationWarning):
3820
assert w3.eth.chainId == 61
39-
40-
41-
def test_set_chain_id(w3):
42-
assert w3.eth.chain_id == 61
43-
44-
w3.eth.chain_id = 72
45-
assert w3.eth.chain_id == 72
46-
47-
w3.eth.chain_id = None
48-
assert w3.eth.chain_id == 61
49-
50-
51-
@pytest.mark.asyncio
52-
async def test_async_set_chain_id(async_w3):
53-
assert await async_w3.eth.chain_id == 61
54-
55-
async_w3.eth.chain_id = 72
56-
assert await async_w3.eth.chain_id == 72
57-
58-
async_w3.eth.chain_id = None
59-
assert await async_w3.eth.chain_id == 61

web3/eth.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114
class BaseEth(Module):
115115
_default_account: Union[ChecksumAddress, Empty] = empty
116116
_default_block: BlockIdentifier = "latest"
117-
_default_chain_id: Optional[int] = None
118117
gasPriceStrategy = None
119118

120119
_gas_price: Method[Callable[[], Wei]] = Method(
@@ -358,14 +357,7 @@ async def block_number(self) -> BlockNumber:
358357

359358
@property
360359
async def chain_id(self) -> int:
361-
if self._default_chain_id is None:
362-
return await self._chain_id() # type: ignore
363-
else:
364-
return self._default_chain_id
365-
366-
@chain_id.setter
367-
def chain_id(self, value: int) -> None:
368-
self._default_chain_id = value
360+
return await self._chain_id() # type: ignore
369361

370362
@property
371363
async def coinbase(self) -> ChecksumAddress:
@@ -637,14 +629,7 @@ def blockNumber(self) -> BlockNumber:
637629

638630
@property
639631
def chain_id(self) -> int:
640-
if self._default_chain_id is None:
641-
return self._chain_id()
642-
else:
643-
return self._default_chain_id
644-
645-
@chain_id.setter
646-
def chain_id(self, value: int) -> None:
647-
self._default_chain_id = value
632+
return self._chain_id()
648633

649634
@property
650635
def chainId(self) -> int:

web3/middleware/cache.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
# 'eth_getWork',
8383
# 'eth_submitWork',
8484
# 'eth_submitHashrate',
85+
'eth_chainId',
8586
})
8687

8788

0 commit comments

Comments
 (0)