Skip to content

Commit 5903c06

Browse files
committed
Demo async/blocking module definition with web3.Version
1 parent de9635b commit 5903c06

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pytest
2+
3+
from web3 import (
4+
EthereumTesterProvider,
5+
Web3,
6+
)
7+
from web3.providers.eth_tester.main import (
8+
AsyncEthereumTesterProvider,
9+
)
10+
from web3.version import (
11+
AsyncVersion,
12+
BlockingVersion,
13+
Version,
14+
)
15+
16+
17+
@pytest.fixture
18+
def blocking_w3():
19+
return Web3(
20+
EthereumTesterProvider(),
21+
modules={
22+
'blocking_version': BlockingVersion,
23+
'legacy_version': Version
24+
})
25+
26+
27+
@pytest.fixture
28+
def async_w3():
29+
return Web3(
30+
AsyncEthereumTesterProvider(),
31+
middlewares=[],
32+
modules={
33+
'async_version': AsyncVersion,
34+
})
35+
36+
37+
def test_blocking_version(blocking_w3):
38+
assert blocking_w3.blocking_version.api == blocking_w3.legacy_version.api
39+
assert blocking_w3.blocking_version.node == blocking_w3.legacy_version.node
40+
assert blocking_w3.blocking_version.ethereum == blocking_w3.legacy_version.ethereum
41+
42+
43+
@pytest.mark.asyncio
44+
async def test_async_blocking_version(async_w3, blocking_w3):
45+
assert async_w3.async_version.api == blocking_w3.legacy_version.api
46+
47+
assert await async_w3.async_version.node == blocking_w3.legacy_version.node
48+
with pytest.raises(
49+
ValueError,
50+
message="RPC Endpoint has not been implemented: eth_protocolVersion"
51+
):
52+
assert await async_w3.async_version.ethereum == blocking_w3.legacy_version.ethereum

web3/version.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
1+
from web3.method import (
2+
Method,
3+
)
14
from web3.module import (
25
Module,
6+
ModuleV2,
37
)
48

59

10+
class BaseVersion(ModuleV2):
11+
retrieve_caller_fn = None
12+
13+
_get_node_version = Method('web3_clientVersion')
14+
_get_protocol_version = Method('eth_protocolVersion')
15+
16+
@property
17+
def api(self):
18+
from web3 import __version__
19+
return __version__
20+
21+
22+
class AsyncVersion(BaseVersion):
23+
is_async = True
24+
25+
@property
26+
async def node(self):
27+
return await self._get_node_version()
28+
29+
@property
30+
async def ethereum(self):
31+
return await self._get_protocol_version()
32+
33+
34+
class BlockingVersion(BaseVersion):
35+
@property
36+
def node(self):
37+
return self._get_node_version()
38+
39+
@property
40+
def ethereum(self):
41+
return self._get_protocol_version()
42+
43+
644
class Version(Module):
745
@property
846
def api(self):

0 commit comments

Comments
 (0)