diff --git a/docs/providers.rst b/docs/providers.rst index 9cdcd19039..599a01f823 100644 --- a/docs/providers.rst +++ b/docs/providers.rst @@ -386,9 +386,14 @@ AsyncHTTPProvider >>> from web3.eth import AsyncEth >>> from web3.net import AsyncNet - >>> w3 = Web3(AsyncHTTPProvider("http://127.0.0.1:8545"), - ... modules={'eth': AsyncEth, 'net': AsyncNet}, - ... middlewares=[]) # See supported middleware section below for middleware options + >>> w3 = Web3( + ... AsyncHTTPProvider(endpoint_uri), + ... modules={'eth': (AsyncEth,), + ... 'net': (AsyncNet,), + ... 'geth': (Geth, + ... {'txpool': (AsyncGethTxPool,)}) + ... }, + ... middlewares=[]) # See supported middleware section below for middleware options Under the hood, the ``AsyncHTTPProvider`` uses the python `aiohttp `_ library for making requests. @@ -427,7 +432,11 @@ Net - :meth:`web3.net.peer_count() ` - :meth:`web3.net.version() ` - +Geth +**** +- :meth:`web3.geth.txpool.inspect() ` +- :meth:`web3.geth.txpool.content() ` +- :meth:`web3.geth.txpool.status() ` Supported Middleware ^^^^^^^^^^^^^^^^^^^^ diff --git a/newsfragments/1413.feature.rst b/newsfragments/1413.feature.rst new file mode 100644 index 0000000000..52835c3d0f --- /dev/null +++ b/newsfragments/1413.feature.rst @@ -0,0 +1 @@ +Added Async functions for Geth TxPool \ No newline at end of file diff --git a/tests/integration/go_ethereum/common.py b/tests/integration/go_ethereum/common.py index b80de2b173..86448bff46 100644 --- a/tests/integration/go_ethereum/common.py +++ b/tests/integration/go_ethereum/common.py @@ -5,7 +5,9 @@ AsyncNetModuleTest, EthModuleTest, GoEthereumAdminModuleTest, + GoEthereumAsyncTxPoolModuleTest, GoEthereumPersonalModuleTest, + GoEthereumTxPoolModuleTest, NetModuleTest, VersionModuleTest, Web3ModuleTest, diff --git a/tests/integration/go_ethereum/test_goethereum_http.py b/tests/integration/go_ethereum/test_goethereum_http.py index 793daffd6c..6c14cf5787 100644 --- a/tests/integration/go_ethereum/test_goethereum_http.py +++ b/tests/integration/go_ethereum/test_goethereum_http.py @@ -7,6 +7,10 @@ from web3.eth import ( AsyncEth, ) +from web3.geth import ( + AsyncGethTxPool, + Geth, +) from web3.middleware import ( async_buffered_gas_estimate_middleware, async_gas_price_strategy_middleware, @@ -22,10 +26,12 @@ GoEthereumAdminModuleTest, GoEthereumAsyncEthModuleTest, GoEthereumAsyncNetModuleTest, + GoEthereumAsyncTxPoolModuleTest, GoEthereumEthModuleTest, GoEthereumNetModuleTest, GoEthereumPersonalModuleTest, GoEthereumTest, + GoEthereumTxPoolModuleTest, GoEthereumVersionModuleTest, ) from .utils import ( @@ -52,7 +58,7 @@ def _geth_command_arguments(rpc_port, yield from ( '--http', '--http.port', rpc_port, - '--http.api', 'admin,eth,net,web3,personal,miner', + '--http.api', 'admin,eth,net,web3,personal,miner,txpool', '--ipcdisable', '--allow-insecure-unlock' ) @@ -88,7 +94,13 @@ async def async_w3(geth_process, endpoint_uri): async_gas_price_strategy_middleware, async_buffered_gas_estimate_middleware ], - modules={'eth': (AsyncEth,), 'async_net': (AsyncNet,)}) + modules={'eth': (AsyncEth,), + 'async_net': (AsyncNet,), + 'geth': (Geth, + {'txpool': (AsyncGethTxPool,)} + ) + } + ) return _web3 @@ -134,3 +146,11 @@ class TestGoEthereumPersonalModuleTest(GoEthereumPersonalModuleTest): class TestGoEthereumAsyncEthModuleTest(GoEthereumAsyncEthModuleTest): pass + + +class TestGoEthereumTxPoolModuleTest(GoEthereumTxPoolModuleTest): + pass + + +class TestGoEthereumAsyncTxPoolModuleTest(GoEthereumAsyncTxPoolModuleTest): + pass diff --git a/web3/_utils/module_testing/__init__.py b/web3/_utils/module_testing/__init__.py index 2bf4cdf60b..57b52501a1 100644 --- a/web3/_utils/module_testing/__init__.py +++ b/web3/_utils/module_testing/__init__.py @@ -5,6 +5,10 @@ from .go_ethereum_admin_module import ( # noqa: F401 GoEthereumAdminModuleTest, ) +from .go_ethereum_txpool_module import ( # noqa: F401 + GoEthereumAsyncTxPoolModuleTest, + GoEthereumTxPoolModuleTest +) from .net_module import ( # noqa: F401 AsyncNetModuleTest, NetModuleTest, diff --git a/web3/_utils/module_testing/go_ethereum_txpool_module.py b/web3/_utils/module_testing/go_ethereum_txpool_module.py new file mode 100644 index 0000000000..98117b7882 --- /dev/null +++ b/web3/_utils/module_testing/go_ethereum_txpool_module.py @@ -0,0 +1,36 @@ +import pytest + +from web3 import Web3 + + +class GoEthereumAsyncTxPoolModuleTest: + + @pytest.mark.asyncio + async def test_async_geth_txpool_inspect(self, async_w3: "Web3") -> None: + test_data = await async_w3.geth.txpool.inspect() # type: ignore + assert "pending" in test_data + + @pytest.mark.asyncio + async def test_async_geth_txpool_content(self, async_w3: "Web3") -> None: + test_data = await async_w3.geth.txpool.content() # type: ignore + assert "pending" in test_data + + @pytest.mark.asyncio + async def test_async_geth_txpool_status(self, async_w3: "Web3") -> None: + test_data = await async_w3.geth.txpool.status() # type: ignore + assert "pending" in test_data + + +class GoEthereumTxPoolModuleTest: + + def test_geth_txpool_inspect(self, web3: "Web3") -> None: + test_data = web3.geth.txpool.inspect() # type: ignore + assert "pending" in test_data + + def test_geth_txpool_content(self, web3: "Web3") -> None: + test_data = web3.geth.txpool.content() # type: ignore + assert "pending" in test_data + + def test_geth_txpool_status(self, web3: "Web3") -> None: + test_data = web3.geth.txpool.status() # type: ignore + assert "pending" in test_data diff --git a/web3/geth.py b/web3/geth.py index db479acccb..478e0a141b 100644 --- a/web3/geth.py +++ b/web3/geth.py @@ -1,3 +1,8 @@ +from typing import ( + Any, + Awaitable, +) + from web3._utils.admin import ( add_peer, addPeer, @@ -58,6 +63,11 @@ from web3.module import ( Module, ) +from web3.types import ( + TxPoolContent, + TxPoolInspect, + TxPoolStatus, +) class GethPersonal(Module): @@ -85,13 +95,39 @@ class GethPersonal(Module): unlockAccount = unlockAccount -class GethTxPool(Module): +class BaseTxPool(Module): """ https://github.com/ethereum/go-ethereum/wiki/Management-APIs#txpool """ - content = content - inspect = inspect - status = status + _content = content + _inspect = inspect + _status = status + + +class GethTxPool(BaseTxPool): + is_async = False + + def content(self) -> TxPoolContent: + return self._content() + + def inspect(self) -> TxPoolInspect: + return self._inspect() + + def status(self) -> TxPoolStatus: + return self._status() + + +class AsyncGethTxPool(BaseTxPool): + is_async = True + + async def content(self) -> Awaitable[Any]: + return await self._content() # type: ignore + + async def inspect(self) -> Awaitable[Any]: + return await self._inspect() # type: ignore + + async def status(self) -> Awaitable[Any]: + return await self._status() # type: ignore class GethAdmin(Module):