Skip to content

Commit 0b98ee1

Browse files
kclowespacrob
authored and
pacrob
committed
Add sync chain_id to simple cache (#2426)
* Cache eth.chain_id in simple_cache_middleware (#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 16a45c3 commit 0b98ee1

File tree

7 files changed

+16
-58
lines changed

7 files changed

+16
-58
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/2376.bugfix.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 == 131277322940537
39-
40-
41-
def test_set_chain_id(w3):
42-
assert w3.eth.chain_id == 131277322940537
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 == 131277322940537
49-
50-
51-
@pytest.mark.asyncio
52-
async def test_async_set_chain_id(async_w3):
53-
assert await async_w3.eth.chain_id == 131277322940537
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 == 131277322940537

web3/eth.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@
116116
class BaseEth(Module):
117117
_default_account: Union[ChecksumAddress, Empty] = empty
118118
_default_block: BlockIdentifier = "latest"
119-
_default_chain_id: Optional[int] = None
120119
gasPriceStrategy = None
121120
defaultContractFactory: Any = None
122121

@@ -386,14 +385,7 @@ async def block_number(self) -> BlockNumber:
386385

387386
@property
388387
async def chain_id(self) -> int:
389-
if self._default_chain_id is None:
390-
return await self._chain_id() # type: ignore
391-
else:
392-
return self._default_chain_id
393-
394-
@chain_id.setter
395-
def chain_id(self, value: int) -> None:
396-
self._default_chain_id = value
388+
return await self._chain_id() # type: ignore
397389

398390
@property
399391
async def coinbase(self) -> ChecksumAddress:
@@ -665,14 +657,7 @@ def blockNumber(self) -> BlockNumber:
665657

666658
@property
667659
def chain_id(self) -> int:
668-
if self._default_chain_id is None:
669-
return self._chain_id()
670-
else:
671-
return self._default_chain_id
672-
673-
@chain_id.setter
674-
def chain_id(self, value: int) -> None:
675-
self._default_chain_id = value
660+
return self._chain_id()
676661

677662
@property
678663
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)