From 651e23aa0a081b69c86bff6e262449b501bbad46 Mon Sep 17 00:00:00 2001 From: kclowes Date: Mon, 21 Jun 2021 16:03:20 -0600 Subject: [PATCH 1/2] Async net module --- newsfragments/2044.feature.rst | 1 + tests/integration/go_ethereum/common.py | 5 ++ .../go_ethereum/test_goethereum_http.py | 10 ++- tests/integration/test_ethereum_tester.py | 12 +++ web3/_utils/module_testing/__init__.py | 1 + web3/_utils/module_testing/net_module.py | 21 +++++ web3/_utils/net.py | 33 -------- web3/main.py | 2 + web3/net.py | 82 ++++++++++++++----- 9 files changed, 112 insertions(+), 55 deletions(-) create mode 100644 newsfragments/2044.feature.rst delete mode 100644 web3/_utils/net.py 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..805df0a9d6 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,), 'net': (AsyncNet,)}) return _web3 @@ -120,6 +124,10 @@ class TestGoEthereumNetModuleTest(GoEthereumNetModuleTest): pass +class TestGoEthereumAsyncNetModuleTest(GoEthereumAsyncNetModuleTest): + pass + + class TestGoEthereumPersonalModuleTest(GoEthereumPersonalModuleTest): pass diff --git a/tests/integration/test_ethereum_tester.py b/tests/integration/test_ethereum_tester.py index 80d80eb37a..6d6d7688b6 100644 --- a/tests/integration/test_ethereum_tester.py +++ b/tests/integration/test_ethereum_tester.py @@ -25,6 +25,7 @@ EMITTER_ENUM, ) from web3.providers.eth_tester import ( + AsyncEthereumTesterProvider, EthereumTesterProvider, ) from web3.types import ( # noqa: F401 @@ -44,12 +45,23 @@ def eth_tester_provider(eth_tester): return provider +@pytest.fixture(scope="module") +def async_eth_tester_provider(eth_tester): + provider = AsyncEthereumTesterProvider(eth_tester) + return provider + + @pytest.fixture(scope="module") def web3(eth_tester_provider): _web3 = Web3(eth_tester_provider) return _web3 +@pytest.fixture(scope="module") +def async_w3(eth_tester_provider): + _web3 = Web3(async_eth_tester_provider) + return _web3 + # # Math Contract Setup # 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..515f445a23 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.net.version # type: ignore + + assert is_string(version) + assert version.isdigit() + + @pytest.mark.asyncio + async def test_net_listening(self, async_w3: "Web3") -> None: + listening = await async_w3.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.net.peer_count # type: ignore + + 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..6d90859e86 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..61b5d0dace 100644 --- a/web3/net.py +++ b/web3/net.py @@ -1,44 +1,84 @@ from typing import ( + 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, ) -class Net(Module): - """ - https://github.com/ethereum/wiki/wiki/JSON-RPC - """ +class BaseNet(Module): + _listening: Method[Callable[[], bool]] = Method( + RPC.net_listening, + mungers=[default_root_munger], + ) - _listening = listening - _peer_count = peer_count - _peerCount = peerCount - _version = version + _peer_count: Method[Callable[[], int]] = Method( + RPC.net_peerCount, + mungers=[default_root_munger], + ) - @property - def listening(self) -> bool: - return self._listening() + _version: Method[Callable[[], str]] = Method( + RPC.net_version, + mungers=[default_root_munger], + ) @property def peer_count(self) -> int: return self._peer_count() @property - def peerCount(self) -> int: - return self._peerCount() + def version(self) -> str: + return self._version() + + @property + def listening(self) -> bool: + return self._listening() + +class Net(BaseNet): @property def chainId(self) -> NoReturn: raise DeprecationWarning("This method has been deprecated in EIP 1474.") - @property - def version(self) -> str: - return self._version() + # @property + # def listening(self) -> bool: + # return self._listening() + + # @property + # def peer_count(self) -> int: + # return self._peer_count() + + # @property + # def version(self) -> str: + # return self._version() + + # + # Deprecated Methods + # + peerCount = DeprecatedMethod(BaseNet.peer_count, 'peerCount', 'peer_count') # type: ignore + + +class AsyncNet(BaseNet): + is_async = True + + # @property + # def listening(self) -> bool: + # return self._listening() + + # @property + # def peer_count(self) -> int: + # return self._peer_count() + + # @property + # def version(self) -> str: + # return self._version() From 314706a2a18c0055c9153a49a9c72a4b9e4fa84a Mon Sep 17 00:00:00 2001 From: kclowes Date: Wed, 14 Jul 2021 15:11:11 -0600 Subject: [PATCH 2/2] Remove BaseNet inheritance for typing --- .../go_ethereum/test_goethereum_http.py | 2 +- tests/integration/test_ethereum_tester.py | 12 ---- web3/_utils/module_testing/net_module.py | 6 +- web3/main.py | 2 +- web3/net.py | 68 ++++++++++--------- 5 files changed, 40 insertions(+), 50 deletions(-) diff --git a/tests/integration/go_ethereum/test_goethereum_http.py b/tests/integration/go_ethereum/test_goethereum_http.py index 805df0a9d6..793daffd6c 100644 --- a/tests/integration/go_ethereum/test_goethereum_http.py +++ b/tests/integration/go_ethereum/test_goethereum_http.py @@ -88,7 +88,7 @@ async def async_w3(geth_process, endpoint_uri): async_gas_price_strategy_middleware, async_buffered_gas_estimate_middleware ], - modules={'eth': (AsyncEth,), 'net': (AsyncNet,)}) + modules={'eth': (AsyncEth,), 'async_net': (AsyncNet,)}) return _web3 diff --git a/tests/integration/test_ethereum_tester.py b/tests/integration/test_ethereum_tester.py index 6d6d7688b6..80d80eb37a 100644 --- a/tests/integration/test_ethereum_tester.py +++ b/tests/integration/test_ethereum_tester.py @@ -25,7 +25,6 @@ EMITTER_ENUM, ) from web3.providers.eth_tester import ( - AsyncEthereumTesterProvider, EthereumTesterProvider, ) from web3.types import ( # noqa: F401 @@ -45,23 +44,12 @@ def eth_tester_provider(eth_tester): return provider -@pytest.fixture(scope="module") -def async_eth_tester_provider(eth_tester): - provider = AsyncEthereumTesterProvider(eth_tester) - return provider - - @pytest.fixture(scope="module") def web3(eth_tester_provider): _web3 = Web3(eth_tester_provider) return _web3 -@pytest.fixture(scope="module") -def async_w3(eth_tester_provider): - _web3 = Web3(async_eth_tester_provider) - return _web3 - # # Math Contract Setup # diff --git a/web3/_utils/module_testing/net_module.py b/web3/_utils/module_testing/net_module.py index 515f445a23..57deed74cc 100644 --- a/web3/_utils/module_testing/net_module.py +++ b/web3/_utils/module_testing/net_module.py @@ -44,19 +44,19 @@ def test_net_chainId_deprecation(self, web3: "Web3") -> None: class AsyncNetModuleTest: @pytest.mark.asyncio async def test_net_version(self, async_w3: "Web3") -> None: - version = await async_w3.net.version # type: ignore + 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.net.listening + 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.net.peer_count # type: ignore + peer_count = await async_w3.async_net.peer_count assert is_integer(peer_count) diff --git a/web3/main.py b/web3/main.py index 6d90859e86..bc2a182c23 100644 --- a/web3/main.py +++ b/web3/main.py @@ -226,7 +226,7 @@ def toChecksumAddress(value: Union[AnyAddress, str, bytes]) -> ChecksumAddress: parity: Parity geth: Geth net: Net - # async_net: AsyncNet + async_net: AsyncNet def __init__( self, diff --git a/web3/net.py b/web3/net.py index 61b5d0dace..fc5e3fc6d1 100644 --- a/web3/net.py +++ b/web3/net.py @@ -1,4 +1,5 @@ from typing import ( + Awaitable, Callable, NoReturn, ) @@ -16,7 +17,7 @@ ) -class BaseNet(Module): +class Net(Module): _listening: Method[Callable[[], bool]] = Method( RPC.net_listening, mungers=[default_root_munger], @@ -33,52 +34,53 @@ class BaseNet(Module): ) @property - def peer_count(self) -> int: - return self._peer_count() - - @property - def version(self) -> str: - return self._version() + def chainId(self) -> NoReturn: + raise DeprecationWarning("This method has been deprecated in EIP 1474.") @property def listening(self) -> bool: return self._listening() - -class Net(BaseNet): @property - def chainId(self) -> NoReturn: - raise DeprecationWarning("This method has been deprecated in EIP 1474.") - - # @property - # def listening(self) -> bool: - # return self._listening() - - # @property - # def peer_count(self) -> int: - # return self._peer_count() + def peer_count(self) -> int: + return self._peer_count() - # @property - # def version(self) -> str: - # return self._version() + @property + def version(self) -> str: + return self._version() # # Deprecated Methods # - peerCount = DeprecatedMethod(BaseNet.peer_count, 'peerCount', 'peer_count') # type: ignore + peerCount = DeprecatedMethod(peer_count, 'peerCount', 'peer_count') # type: ignore -class AsyncNet(BaseNet): +class AsyncNet(Module): is_async = True - # @property - # def listening(self) -> bool: - # return self._listening() + _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 peer_count(self) -> int: - # return self._peer_count() + @property + async def peer_count(self) -> int: + return await self._peer_count() - # @property - # def version(self) -> str: - # return self._version() + @property + async def version(self) -> str: + return await self._version()