Skip to content

Commit

Permalink
Async net module (#2044)
Browse files Browse the repository at this point in the history
* Async net module

* Remove BaseNet inheritance for typing
  • Loading branch information
kclowes authored Jul 15, 2021
1 parent 3184c50 commit 8ce56bd
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 52 deletions.
1 change: 1 addition & 0 deletions newsfragments/2044.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add AsyncNet module
5 changes: 5 additions & 0 deletions tests/integration/go_ethereum/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from web3._utils.module_testing import ( # noqa: F401
AsyncEthModuleTest,
AsyncNetModuleTest,
EthModuleTest,
GoEthereumAdminModuleTest,
GoEthereumPersonalModuleTest,
Expand Down Expand Up @@ -52,6 +53,10 @@ class GoEthereumNetModuleTest(NetModuleTest):
pass


class GoEthereumAsyncNetModuleTest(AsyncNetModuleTest):
pass


class GoEthereumAdminModuleTest(GoEthereumAdminModuleTest):
pass

Expand Down
10 changes: 9 additions & 1 deletion tests/integration/go_ethereum/test_goethereum_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
async_buffered_gas_estimate_middleware,
async_gas_price_strategy_middleware,
)
from web3.net import (
AsyncNet,
)
from web3.providers.async_rpc import (
AsyncHTTPProvider,
)

from .common import (
GoEthereumAdminModuleTest,
GoEthereumAsyncEthModuleTest,
GoEthereumAsyncNetModuleTest,
GoEthereumEthModuleTest,
GoEthereumNetModuleTest,
GoEthereumPersonalModuleTest,
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -120,6 +124,10 @@ class TestGoEthereumNetModuleTest(GoEthereumNetModuleTest):
pass


class TestGoEthereumAsyncNetModuleTest(GoEthereumAsyncNetModuleTest):
pass


class TestGoEthereumPersonalModuleTest(GoEthereumPersonalModuleTest):
pass

Expand Down
1 change: 1 addition & 0 deletions web3/_utils/module_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
GoEthereumAdminModuleTest,
)
from .net_module import ( # noqa: F401
AsyncNetModuleTest,
NetModuleTest,
)
from .parity_module import ( # noqa: F401
Expand Down
21 changes: 21 additions & 0 deletions web3/_utils/module_testing/net_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
33 changes: 0 additions & 33 deletions web3/_utils/net.py

This file was deleted.

2 changes: 2 additions & 0 deletions web3/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
RequestManager as DefaultRequestManager,
)
from web3.net import (
AsyncNet,
Net,
)
from web3.parity import (
Expand Down Expand Up @@ -225,6 +226,7 @@ def toChecksumAddress(value: Union[AnyAddress, str, bytes]) -> ChecksumAddress:
parity: Parity
geth: Geth
net: Net
async_net: AsyncNet

def __init__(
self,
Expand Down
78 changes: 60 additions & 18 deletions web3/net.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
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,
)


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:
Expand All @@ -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()

0 comments on commit 8ce56bd

Please sign in to comment.