Skip to content

Commit 6a90a26

Browse files
authored
Add async geth txpool (ethereum#2273)
1 parent 2e6bcb0 commit 6a90a26

File tree

7 files changed

+118
-10
lines changed

7 files changed

+118
-10
lines changed

docs/providers.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,14 @@ AsyncHTTPProvider
386386
>>> from web3.eth import AsyncEth
387387
>>> from web3.net import AsyncNet
388388
389-
>>> w3 = Web3(AsyncHTTPProvider("http://127.0.0.1:8545"),
390-
... modules={'eth': AsyncEth, 'net': AsyncNet},
391-
... middlewares=[]) # See supported middleware section below for middleware options
389+
>>> w3 = Web3(
390+
... AsyncHTTPProvider(endpoint_uri),
391+
... modules={'eth': (AsyncEth,),
392+
... 'net': (AsyncNet,),
393+
... 'geth': (Geth,
394+
... {'txpool': (AsyncGethTxPool,)})
395+
... },
396+
... middlewares=[]) # See supported middleware section below for middleware options
392397
393398
Under the hood, the ``AsyncHTTPProvider`` uses the python
394399
`aiohttp <https://docs.aiohttp.org/en/stable/>`_ library for making requests.
@@ -427,7 +432,11 @@ Net
427432
- :meth:`web3.net.peer_count() <web3.net.peer_count>`
428433
- :meth:`web3.net.version() <web3.net.version>`
429434

430-
435+
Geth
436+
****
437+
- :meth:`web3.geth.txpool.inspect() <web3.geth.txpool.TxPool.inspect()>`
438+
- :meth:`web3.geth.txpool.content() <web3.geth.txpool.TxPool.content()>`
439+
- :meth:`web3.geth.txpool.status() <web3.geth.txpool.TxPool.status()>`
431440

432441
Supported Middleware
433442
^^^^^^^^^^^^^^^^^^^^

newsfragments/1413.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added Async functions for Geth TxPool

tests/integration/go_ethereum/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
AsyncNetModuleTest,
66
EthModuleTest,
77
GoEthereumAdminModuleTest,
8+
GoEthereumAsyncTxPoolModuleTest,
89
GoEthereumPersonalModuleTest,
10+
GoEthereumTxPoolModuleTest,
911
NetModuleTest,
1012
VersionModuleTest,
1113
Web3ModuleTest,

tests/integration/go_ethereum/test_goethereum_http.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
from web3.eth import (
88
AsyncEth,
99
)
10+
from web3.geth import (
11+
AsyncGethTxPool,
12+
Geth,
13+
)
1014
from web3.middleware import (
1115
async_buffered_gas_estimate_middleware,
1216
async_gas_price_strategy_middleware,
@@ -22,10 +26,12 @@
2226
GoEthereumAdminModuleTest,
2327
GoEthereumAsyncEthModuleTest,
2428
GoEthereumAsyncNetModuleTest,
29+
GoEthereumAsyncTxPoolModuleTest,
2530
GoEthereumEthModuleTest,
2631
GoEthereumNetModuleTest,
2732
GoEthereumPersonalModuleTest,
2833
GoEthereumTest,
34+
GoEthereumTxPoolModuleTest,
2935
GoEthereumVersionModuleTest,
3036
)
3137
from .utils import (
@@ -52,7 +58,7 @@ def _geth_command_arguments(rpc_port,
5258
yield from (
5359
'--http',
5460
'--http.port', rpc_port,
55-
'--http.api', 'admin,eth,net,web3,personal,miner',
61+
'--http.api', 'admin,eth,net,web3,personal,miner,txpool',
5662
'--ipcdisable',
5763
'--allow-insecure-unlock'
5864
)
@@ -88,7 +94,13 @@ async def async_w3(geth_process, endpoint_uri):
8894
async_gas_price_strategy_middleware,
8995
async_buffered_gas_estimate_middleware
9096
],
91-
modules={'eth': (AsyncEth,), 'async_net': (AsyncNet,)})
97+
modules={'eth': (AsyncEth,),
98+
'async_net': (AsyncNet,),
99+
'geth': (Geth,
100+
{'txpool': (AsyncGethTxPool,)}
101+
)
102+
}
103+
)
92104
return _web3
93105

94106

@@ -134,3 +146,11 @@ class TestGoEthereumPersonalModuleTest(GoEthereumPersonalModuleTest):
134146

135147
class TestGoEthereumAsyncEthModuleTest(GoEthereumAsyncEthModuleTest):
136148
pass
149+
150+
151+
class TestGoEthereumTxPoolModuleTest(GoEthereumTxPoolModuleTest):
152+
pass
153+
154+
155+
class TestGoEthereumAsyncTxPoolModuleTest(GoEthereumAsyncTxPoolModuleTest):
156+
pass

web3/_utils/module_testing/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
from .go_ethereum_admin_module import ( # noqa: F401
66
GoEthereumAdminModuleTest,
77
)
8+
from .go_ethereum_txpool_module import ( # noqa: F401
9+
GoEthereumAsyncTxPoolModuleTest,
10+
GoEthereumTxPoolModuleTest
11+
)
812
from .net_module import ( # noqa: F401
913
AsyncNetModuleTest,
1014
NetModuleTest,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
3+
from web3 import Web3
4+
5+
6+
class GoEthereumAsyncTxPoolModuleTest:
7+
8+
@pytest.mark.asyncio
9+
async def test_async_geth_txpool_inspect(self, async_w3: "Web3") -> None:
10+
test_data = await async_w3.geth.txpool.inspect() # type: ignore
11+
assert "pending" in test_data
12+
13+
@pytest.mark.asyncio
14+
async def test_async_geth_txpool_content(self, async_w3: "Web3") -> None:
15+
test_data = await async_w3.geth.txpool.content() # type: ignore
16+
assert "pending" in test_data
17+
18+
@pytest.mark.asyncio
19+
async def test_async_geth_txpool_status(self, async_w3: "Web3") -> None:
20+
test_data = await async_w3.geth.txpool.status() # type: ignore
21+
assert "pending" in test_data
22+
23+
24+
class GoEthereumTxPoolModuleTest:
25+
26+
def test_geth_txpool_inspect(self, web3: "Web3") -> None:
27+
test_data = web3.geth.txpool.inspect() # type: ignore
28+
assert "pending" in test_data
29+
30+
def test_geth_txpool_content(self, web3: "Web3") -> None:
31+
test_data = web3.geth.txpool.content() # type: ignore
32+
assert "pending" in test_data
33+
34+
def test_geth_txpool_status(self, web3: "Web3") -> None:
35+
test_data = web3.geth.txpool.status() # type: ignore
36+
assert "pending" in test_data

web3/geth.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from typing import (
2+
Any,
3+
Awaitable,
4+
)
5+
16
from web3._utils.admin import (
27
add_peer,
38
addPeer,
@@ -58,6 +63,11 @@
5863
from web3.module import (
5964
Module,
6065
)
66+
from web3.types import (
67+
TxPoolContent,
68+
TxPoolInspect,
69+
TxPoolStatus,
70+
)
6171

6272

6373
class GethPersonal(Module):
@@ -85,13 +95,39 @@ class GethPersonal(Module):
8595
unlockAccount = unlockAccount
8696

8797

88-
class GethTxPool(Module):
98+
class BaseTxPool(Module):
8999
"""
90100
https://github.com/ethereum/go-ethereum/wiki/Management-APIs#txpool
91101
"""
92-
content = content
93-
inspect = inspect
94-
status = status
102+
_content = content
103+
_inspect = inspect
104+
_status = status
105+
106+
107+
class GethTxPool(BaseTxPool):
108+
is_async = False
109+
110+
def content(self) -> TxPoolContent:
111+
return self._content()
112+
113+
def inspect(self) -> TxPoolInspect:
114+
return self._inspect()
115+
116+
def status(self) -> TxPoolStatus:
117+
return self._status()
118+
119+
120+
class AsyncGethTxPool(BaseTxPool):
121+
is_async = True
122+
123+
async def content(self) -> Awaitable[Any]:
124+
return await self._content() # type: ignore
125+
126+
async def inspect(self) -> Awaitable[Any]:
127+
return await self._inspect() # type: ignore
128+
129+
async def status(self) -> Awaitable[Any]:
130+
return await self._status() # type: ignore
95131

96132

97133
class GethAdmin(Module):

0 commit comments

Comments
 (0)