diff --git a/newsfragments/2044.feature.rst b/newsfragments/2044.feature.rst new file mode 100644 index 0000000000..8baff1011f --- /dev/null +++ b/newsfragments/2044.feature.rst @@ -0,0 +1 @@ +Add AsyncNet module diff --git a/tests/integration/go_ethereum/common.py b/tests/integration/go_ethereum/common.py index a014effbbb..b80de2b173 100644 --- a/tests/integration/go_ethereum/common.py +++ b/tests/integration/go_ethereum/common.py @@ -2,6 +2,7 @@ from web3._utils.module_testing import ( # noqa: F401 AsyncEthModuleTest, + AsyncNetModuleTest, EthModuleTest, GoEthereumAdminModuleTest, GoEthereumPersonalModuleTest, @@ -52,6 +53,10 @@ class GoEthereumNetModuleTest(NetModuleTest): pass +class GoEthereumAsyncNetModuleTest(AsyncNetModuleTest): + pass + + class GoEthereumAdminModuleTest(GoEthereumAdminModuleTest): pass diff --git a/tests/integration/go_ethereum/test_goethereum_http.py b/tests/integration/go_ethereum/test_goethereum_http.py index bff466eef2..793daffd6c 100644 --- a/tests/integration/go_ethereum/test_goethereum_http.py +++ b/tests/integration/go_ethereum/test_goethereum_http.py @@ -11,6 +11,9 @@ async_buffered_gas_estimate_middleware, async_gas_price_strategy_middleware, ) +from web3.net import ( + AsyncNet, +) from web3.providers.async_rpc import ( AsyncHTTPProvider, ) @@ -18,6 +21,7 @@ from .common import ( GoEthereumAdminModuleTest, GoEthereumAsyncEthModuleTest, + GoEthereumAsyncNetModuleTest, GoEthereumEthModuleTest, GoEthereumNetModuleTest, GoEthereumPersonalModuleTest, @@ -84,7 +88,7 @@ async def async_w3(geth_process, endpoint_uri): async_gas_price_strategy_middleware, async_buffered_gas_estimate_middleware ], - modules={'eth': (AsyncEth,)}) + modules={'eth': (AsyncEth,), 'async_net': (AsyncNet,)}) return _web3 @@ -120,6 +124,10 @@ class TestGoEthereumNetModuleTest(GoEthereumNetModuleTest): pass +class TestGoEthereumAsyncNetModuleTest(GoEthereumAsyncNetModuleTest): + pass + + class TestGoEthereumPersonalModuleTest(GoEthereumPersonalModuleTest): pass diff --git a/web3/_utils/module_testing/__init__.py b/web3/_utils/module_testing/__init__.py index 94228bebad..cf19fcc141 100644 --- a/web3/_utils/module_testing/__init__.py +++ b/web3/_utils/module_testing/__init__.py @@ -6,6 +6,7 @@ GoEthereumAdminModuleTest, ) from .net_module import ( # noqa: F401 + AsyncNetModuleTest, NetModuleTest, ) from .parity_module import ( # noqa: F401 diff --git a/web3/_utils/module_testing/net_module.py b/web3/_utils/module_testing/net_module.py index 2330489175..57deed74cc 100644 --- a/web3/_utils/module_testing/net_module.py +++ b/web3/_utils/module_testing/net_module.py @@ -39,3 +39,24 @@ def test_net_peerCount(self, web3: "Web3") -> None: def test_net_chainId_deprecation(self, web3: "Web3") -> None: with pytest.raises(DeprecationWarning): web3.net.chainId + + +class AsyncNetModuleTest: + @pytest.mark.asyncio + async def test_net_version(self, async_w3: "Web3") -> None: + version = await async_w3.async_net.version + + assert is_string(version) + assert version.isdigit() + + @pytest.mark.asyncio + async def test_net_listening(self, async_w3: "Web3") -> None: + listening = await async_w3.async_net.listening + + assert is_boolean(listening) + + @pytest.mark.asyncio + async def test_net_peer_count(self, async_w3: "Web3") -> None: + peer_count = await async_w3.async_net.peer_count + + assert is_integer(peer_count) diff --git a/web3/_utils/net.py b/web3/_utils/net.py deleted file mode 100644 index 6cf1e50299..0000000000 --- a/web3/_utils/net.py +++ /dev/null @@ -1,33 +0,0 @@ -from typing import ( - Callable, -) - -from web3._utils.rpc_abi import ( - RPC, -) -from web3.method import ( - DeprecatedMethod, - Method, - default_root_munger, -) - -listening: Method[Callable[[], bool]] = Method( - RPC.net_listening, - mungers=[default_root_munger], -) - -peer_count: Method[Callable[[], int]] = Method( - RPC.net_peerCount, - mungers=[default_root_munger], -) - -version: Method[Callable[[], str]] = Method( - RPC.net_version, - mungers=[default_root_munger], -) - - -# -# Deprecated Methods -# -peerCount = DeprecatedMethod(peer_count, 'peerCount', 'peer_count') diff --git a/web3/main.py b/web3/main.py index f2db97361e..bc2a182c23 100644 --- a/web3/main.py +++ b/web3/main.py @@ -87,6 +87,7 @@ RequestManager as DefaultRequestManager, ) from web3.net import ( + AsyncNet, Net, ) from web3.parity import ( @@ -225,6 +226,7 @@ def toChecksumAddress(value: Union[AnyAddress, str, bytes]) -> ChecksumAddress: parity: Parity geth: Geth net: Net + async_net: AsyncNet def __init__( self, diff --git a/web3/net.py b/web3/net.py index 68e0193d66..fc5e3fc6d1 100644 --- a/web3/net.py +++ b/web3/net.py @@ -1,12 +1,16 @@ from typing import ( + Awaitable, + Callable, NoReturn, ) -from web3._utils.net import ( - listening, - peer_count, - peerCount, - version, +from web3._utils.rpc_abi import ( + RPC, +) +from web3.method import ( + DeprecatedMethod, + Method, + default_root_munger, ) from web3.module import ( Module, @@ -14,14 +18,24 @@ class Net(Module): - """ - https://github.com/ethereum/wiki/wiki/JSON-RPC - """ + _listening: Method[Callable[[], bool]] = Method( + RPC.net_listening, + mungers=[default_root_munger], + ) + + _peer_count: Method[Callable[[], int]] = Method( + RPC.net_peerCount, + mungers=[default_root_munger], + ) + + _version: Method[Callable[[], str]] = Method( + RPC.net_version, + mungers=[default_root_munger], + ) - _listening = listening - _peer_count = peer_count - _peerCount = peerCount - _version = version + @property + def chainId(self) -> NoReturn: + raise DeprecationWarning("This method has been deprecated in EIP 1474.") @property def listening(self) -> bool: @@ -32,13 +46,41 @@ def peer_count(self) -> int: return self._peer_count() @property - def peerCount(self) -> int: - return self._peerCount() + def version(self) -> str: + return self._version() + + # + # Deprecated Methods + # + peerCount = DeprecatedMethod(peer_count, 'peerCount', 'peer_count') # type: ignore + + +class AsyncNet(Module): + is_async = True + + _listening: Method[Callable[[], Awaitable[bool]]] = Method( + RPC.net_listening, + mungers=[default_root_munger], + ) + + _peer_count: Method[Callable[[], Awaitable[int]]] = Method( + RPC.net_peerCount, + mungers=[default_root_munger], + ) + + _version: Method[Callable[[], Awaitable[str]]] = Method( + RPC.net_version, + mungers=[default_root_munger], + ) + + @property + async def listening(self) -> bool: + return await self._listening() @property - def chainId(self) -> NoReturn: - raise DeprecationWarning("This method has been deprecated in EIP 1474.") + async def peer_count(self) -> int: + return await self._peer_count() @property - def version(self) -> str: - return self._version() + async def version(self) -> str: + return await self._version()