Skip to content

Commit 0a2cc6b

Browse files
authored
Feature/async geth admin (#2329)
* Added BaseGethPersonal to geth.py * Added AsyncGethPersonal and test * Added Docs * lint fixes * news fragment update * removed import_raw_key test for now * mypy typing issues * typo * Added AsyncGethAdmin and BaseGethAdmin. Also, fixed test due to typing * made suggested changes * made suggested changes * fixed spelling errors * added test and docs * newsfragment * merge conflict * remove setSolc * copy in suggested test * forgot to check liniting before commit * linting * linting
1 parent e316a19 commit 0a2cc6b

File tree

7 files changed

+195
-23
lines changed

7 files changed

+195
-23
lines changed

docs/providers.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ AsyncHTTPProvider
393393
... 'net': (AsyncNet,),
394394
... 'geth': (Geth,
395395
... {'txpool': (AsyncGethTxPool,),
396-
... 'personal': (AsyncGethPersonal,)})
396+
... 'personal': (AsyncGethPersonal,),
397+
... 'admin' : (AsyncGethAdmin,)})
397398
... },
398399
... middlewares=[]) # See supported middleware section below for middleware options
399400
@@ -440,6 +441,14 @@ Net
440441

441442
Geth
442443
****
444+
- :meth:`web3.geth.admin.add_peer() <web3.geth.admin.add_peer>`
445+
- :meth:`web3.geth.admin.datadir() <web3.geth.admin.datadir>`
446+
- :meth:`web3.geth.admin.node_info() <web3.geth.admin.node_info>`
447+
- :meth:`web3.geth.admin.peers() <web3.geth.admin.peers>`
448+
- :meth:`web3.geth.admin.start_rpc() <web3.geth.admin.start_rpc>`
449+
- :meth:`web3.geth.admin.start_ws() <web3.geth.admin.start_ws>`
450+
- :meth:`web3.geth.admin.stop_rpc() <web3.geth.admin.stop_rpc>`
451+
- :meth:`web3.geth.admin.stop_ws() <web3.geth.admin.stop_ws>`
443452
- :meth:`web3.geth.personal.ec_recover()`
444453
- :meth:`web3.geth.personal.import_raw_key() <web3.geth.personal.import_raw_key>`
445454
- :meth:`web3.geth.personal.list_accounts() <web3.geth.personal.list_accounts>`

docs/web3.geth.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ The ``web3.geth.admin`` object exposes methods to interact with the RPC APIs und
122122
123123
.. warning:: Deprecated: This method is deprecated in favor of :meth:`~web3.geth.admin.add_peer()`
124124

125-
.. py:method:: setSolc(solc_path)
126-
127-
.. Warning:: This method has been removed from Geth
128-
129125
.. py:method:: start_rpc(host='localhost', port=8545, cors="", apis="eth,net,web3")
130126
131127
* Delegates to ``admin_startRPC`` RPC Method

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 Personal and Admin modules

newsfragments/2299.feature.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/integration/go_ethereum/test_goethereum_http.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
get_open_port,
55
)
66
from web3 import Web3
7+
from web3._utils.module_testing.go_ethereum_admin_module import (
8+
GoEthereumAsyncAdminModuleTest,
9+
)
710
from web3._utils.module_testing.go_ethereum_personal_module import (
811
GoEthereumAsyncPersonalModuleTest,
912
)
1013
from web3.eth import (
1114
AsyncEth,
1215
)
1316
from web3.geth import (
17+
AsyncGethAdmin,
1418
AsyncGethPersonal,
1519
AsyncGethTxPool,
1620
Geth,
@@ -104,7 +108,8 @@ async def async_w3(geth_process, endpoint_uri):
104108
'async_net': AsyncNet,
105109
'geth': (Geth,
106110
{'txpool': (AsyncGethTxPool,),
107-
'personal': (AsyncGethPersonal,)}
111+
'personal': (AsyncGethPersonal,),
112+
'admin': (AsyncGethAdmin,)}
108113
)
109114
}
110115
)
@@ -131,6 +136,25 @@ def test_admin_start_stop_ws(self, web3: "Web3") -> None:
131136
super().test_admin_start_stop_ws(web3)
132137

133138

139+
class TestGoEthereumAsyncAdminModuleTest(GoEthereumAsyncAdminModuleTest):
140+
@pytest.mark.asyncio
141+
@pytest.mark.xfail(reason="running geth with the --nodiscover flag doesn't allow peer addition")
142+
async def test_admin_peers(self, web3: "Web3") -> None:
143+
await super().test_admin_peers(web3)
144+
145+
@pytest.mark.asyncio
146+
async def test_admin_start_stop_rpc(self, web3: "Web3") -> None:
147+
# This test causes all tests after it to fail on CI if it's allowed to run
148+
pytest.xfail(reason='Only one RPC endpoint is allowed to be active at any time')
149+
await super().test_admin_start_stop_rpc(web3)
150+
151+
@pytest.mark.asyncio
152+
async def test_admin_start_stop_ws(self, web3: "Web3") -> None:
153+
# This test causes all tests after it to fail on CI if it's allowed to run
154+
pytest.xfail(reason='Only one WS endpoint is allowed to be active at any time')
155+
await super().test_admin_start_stop_ws(web3)
156+
157+
134158
class TestGoEthereumEthModuleTest(GoEthereumEthModuleTest):
135159
pass
136160

web3/_utils/module_testing/go_ethereum_admin_module.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from typing import (
33
TYPE_CHECKING,
4+
List,
45
)
56

67
from web3.datastructures import (
@@ -80,7 +81,7 @@ def test_admin_start_stop_ws(self, web3: "Web3") -> None:
8081
def test_admin_addPeer(self, web3: "Web3") -> None:
8182
with pytest.warns(DeprecationWarning):
8283
result = web3.geth.admin.addPeer(
83-
'enode://f1a6b0bdbf014355587c3018454d070ac57801f05d3b39fe85da574f002a32e929f683d72aa5a8318382e4d3c7a05c9b91687b0d997a39619fb8a6e7ad88e512@1.1.1.1:30303', # noqa: E501
84+
EnodeURI('enode://f1a6b0bdbf014355587c3018454d070ac57801f05d3b39fe85da574f002a32e929f683d72aa5a8318382e4d3c7a05c9b91687b0d997a39619fb8a6e7ad88e512@1.1.1.1:30303'), # noqa: E501
8485
)
8586
assert result is True
8687

@@ -98,3 +99,44 @@ def test_admin_nodeInfo(self, web3: "Web3") -> None:
9899
})
99100
# Test that result gives at least the keys that are listed in `expected`
100101
assert not set(expected.keys()).difference(result.keys())
102+
103+
104+
class GoEthereumAsyncAdminModuleTest:
105+
106+
@pytest.mark.asyncio
107+
async def test_async_datadir(self, async_w3: "Web3") -> None:
108+
datadir = await async_w3.geth.admin.datadir() # type: ignore
109+
assert isinstance(datadir, str)
110+
111+
@pytest.mark.asyncio
112+
async def test_async_nodeinfo(self, async_w3: "Web3") -> None:
113+
node_info = await async_w3.geth.admin.node_info() # type: ignore
114+
assert "Geth" in node_info["name"]
115+
116+
@pytest.mark.asyncio
117+
async def test_async_nodes(self, async_w3: "Web3") -> None:
118+
nodes = await async_w3.geth.admin.peers() # type: ignore
119+
assert isinstance(nodes, List)
120+
121+
@pytest.mark.asyncio
122+
async def test_admin_peers(self, web3: "Web3") -> None:
123+
enode = await web3.geth.admin.node_info()['enode'] # type: ignore
124+
web3.geth.admin.add_peer(enode)
125+
result = await web3.geth.admin.peers() # type: ignore
126+
assert len(result) == 1
127+
128+
@pytest.mark.asyncio
129+
async def test_admin_start_stop_rpc(self, web3: "Web3") -> None:
130+
stop = await web3.geth.admin.stop_rpc() # type: ignore
131+
assert stop is True
132+
133+
start = await web3.geth.admin.start_rpc() # type: ignore
134+
assert start is True
135+
136+
@pytest.mark.asyncio
137+
async def test_admin_start_stop_ws(self, web3: "Web3") -> None:
138+
stop = await web3.geth.admin.stop_ws() # type: ignore
139+
assert stop is True
140+
141+
start = await web3.geth.admin.start_ws() # type: ignore
142+
assert start is True

web3/geth.py

Lines changed: 116 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@
7777
Module,
7878
)
7979
from web3.types import (
80+
EnodeURI,
8081
GethWallet,
82+
NodeInfo,
83+
Peer,
8184
TxParams,
8285
TxPoolContent,
8386
TxPoolInspect,
@@ -258,25 +261,123 @@ async def status(self) -> Awaitable[Any]:
258261
return await self._status() # type: ignore
259262

260263

261-
class GethAdmin(Module):
264+
class BaseGethAdmin(Module):
262265
"""
263266
https://github.com/ethereum/go-ethereum/wiki/Management-APIs#admin
264267
"""
265-
add_peer = add_peer
266-
node_info = node_info
267-
start_rpc = start_rpc
268-
start_ws = start_ws
269-
stop_ws = stop_ws
270-
stop_rpc = stop_rpc
268+
_add_peer = add_peer
269+
_datadir = datadir
270+
_node_info = node_info
271+
_peers = peers
272+
_start_rpc = start_rpc
273+
_start_ws = start_ws
274+
_stop_ws = stop_ws
275+
_stop_rpc = stop_rpc
271276
# deprecated
272-
addPeer = addPeer
273-
datadir = datadir
274-
nodeInfo = nodeInfo
275-
peers = peers
276-
startRPC = startRPC
277-
startWS = startWS
278-
stopRPC = stopRPC
279-
stopWS = stopWS
277+
_addPeer = addPeer
278+
_nodeInfo = nodeInfo
279+
_startRPC = startRPC
280+
_startWS = startWS
281+
_stopRPC = stopRPC
282+
_stopWS = stopWS
283+
284+
285+
class GethAdmin(BaseGethAdmin):
286+
is_async = False
287+
288+
def add_peer(self, node_url: EnodeURI) -> bool:
289+
return self._add_peer(node_url)
290+
291+
def datadir(self) -> str:
292+
return self._datadir()
293+
294+
def node_info(self) -> NodeInfo:
295+
return self._node_info()
296+
297+
def peers(self) -> List[Peer]:
298+
return self._peers()
299+
300+
def start_rpc(self,
301+
host: str = "localhost",
302+
port: int = 8546,
303+
cors: str = "",
304+
apis: str = "eth,net,web3") -> bool:
305+
return self._start_rpc(host, port, cors, apis)
306+
307+
def start_ws(self,
308+
host: str = "localhost",
309+
port: int = 8546,
310+
cors: str = "",
311+
apis: str = "eth,net,web3") -> bool:
312+
return self._start_ws(host, port, cors, apis)
313+
314+
def stop_rpc(self) -> bool:
315+
return self._stop_rpc()
316+
317+
def stop_ws(self) -> bool:
318+
return self._stop_ws()
319+
320+
def addPeer(self, node_url: EnodeURI) -> bool:
321+
return self._addPeer(node_url)
322+
323+
def nodeInfo(self) -> NodeInfo:
324+
return self._nodeInfo()
325+
326+
def startRPC(self,
327+
host: str = "localhost",
328+
port: int = 8546,
329+
cors: str = "",
330+
apis: str = "eth,net,web3") -> bool:
331+
return self._startRPC(host, port, cors, apis)
332+
333+
def startWS(self,
334+
host: str = "localhost",
335+
port: int = 8546,
336+
cors: str = "",
337+
apis: str = "eth,net,web3") -> bool:
338+
return self._startWS(host, port, cors, apis)
339+
340+
def stopRPC(self) -> bool:
341+
return self._stopRPC()
342+
343+
def stopWS(self) -> bool:
344+
return self._stopWS()
345+
346+
347+
class AsyncGethAdmin(BaseGethAdmin):
348+
is_async = True
349+
350+
async def add_peer(self, node_url: EnodeURI) -> Awaitable[bool]:
351+
return await self._add_peer(node_url) # type: ignore
352+
353+
async def datadir(self) -> Awaitable[str]:
354+
return await self._datadir() # type: ignore
355+
356+
async def node_info(self) -> Awaitable[NodeInfo]:
357+
return await self._node_info() # type: ignore
358+
359+
async def peers(self) -> Awaitable[List[Peer]]:
360+
return await self._peers() # type: ignore
361+
362+
async def start_rpc(self,
363+
host: str = "localhost",
364+
port: int = 8546,
365+
cors: str = "",
366+
apis: str = "eth,net,web3") -> Awaitable[bool]:
367+
return await self._start_rpc(host, port, cors, apis) # type: ignore
368+
369+
async def start_ws(self,
370+
host: str = "localhost",
371+
port: int = 8546,
372+
cors: str = "",
373+
apis: str = "eth,net,web3") -> Awaitable[bool]:
374+
return await self._start_ws(host, port, cors, apis) # type: ignore
375+
376+
async def stop_rpc(self) -> Awaitable[bool]:
377+
return await self._stop_rpc() # type: ignore
378+
379+
async def stop_ws(self) -> Awaitable[bool]:
380+
return await self._stop_ws() # type: ignore
280381

281382

282383
class GethMiner(Module):

0 commit comments

Comments
 (0)