Skip to content

Commit

Permalink
Add async geth txpool (#2273)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbfreem committed Jan 7, 2022
1 parent 2e6bcb0 commit 6a90a26
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 10 deletions.
17 changes: 13 additions & 4 deletions docs/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://docs.aiohttp.org/en/stable/>`_ library for making requests.
Expand Down Expand Up @@ -427,7 +432,11 @@ Net
- :meth:`web3.net.peer_count() <web3.net.peer_count>`
- :meth:`web3.net.version() <web3.net.version>`


Geth
****
- :meth:`web3.geth.txpool.inspect() <web3.geth.txpool.TxPool.inspect()>`
- :meth:`web3.geth.txpool.content() <web3.geth.txpool.TxPool.content()>`
- :meth:`web3.geth.txpool.status() <web3.geth.txpool.TxPool.status()>`

Supported Middleware
^^^^^^^^^^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions newsfragments/1413.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added Async functions for Geth TxPool
2 changes: 2 additions & 0 deletions tests/integration/go_ethereum/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
AsyncNetModuleTest,
EthModuleTest,
GoEthereumAdminModuleTest,
GoEthereumAsyncTxPoolModuleTest,
GoEthereumPersonalModuleTest,
GoEthereumTxPoolModuleTest,
NetModuleTest,
VersionModuleTest,
Web3ModuleTest,
Expand Down
24 changes: 22 additions & 2 deletions tests/integration/go_ethereum/test_goethereum_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,10 +26,12 @@
GoEthereumAdminModuleTest,
GoEthereumAsyncEthModuleTest,
GoEthereumAsyncNetModuleTest,
GoEthereumAsyncTxPoolModuleTest,
GoEthereumEthModuleTest,
GoEthereumNetModuleTest,
GoEthereumPersonalModuleTest,
GoEthereumTest,
GoEthereumTxPoolModuleTest,
GoEthereumVersionModuleTest,
)
from .utils import (
Expand All @@ -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'
)
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -134,3 +146,11 @@ class TestGoEthereumPersonalModuleTest(GoEthereumPersonalModuleTest):

class TestGoEthereumAsyncEthModuleTest(GoEthereumAsyncEthModuleTest):
pass


class TestGoEthereumTxPoolModuleTest(GoEthereumTxPoolModuleTest):
pass


class TestGoEthereumAsyncTxPoolModuleTest(GoEthereumAsyncTxPoolModuleTest):
pass
4 changes: 4 additions & 0 deletions web3/_utils/module_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 36 additions & 0 deletions web3/_utils/module_testing/go_ethereum_txpool_module.py
Original file line number Diff line number Diff line change
@@ -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
44 changes: 40 additions & 4 deletions web3/geth.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from typing import (
Any,
Awaitable,
)

from web3._utils.admin import (
add_peer,
addPeer,
Expand Down Expand Up @@ -58,6 +63,11 @@
from web3.module import (
Module,
)
from web3.types import (
TxPoolContent,
TxPoolInspect,
TxPoolStatus,
)


class GethPersonal(Module):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 6a90a26

Please sign in to comment.