Skip to content

Feature/async geth admin #2329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
32fa866
Added BaseGethPersonal to geth.py
dbfreem Jan 9, 2022
5ea5f94
Added AsyncGethPersonal and test
dbfreem Jan 12, 2022
8b01e99
Added Docs
dbfreem Jan 12, 2022
806010f
lint fixes
dbfreem Jan 12, 2022
419c3d1
news fragment update
dbfreem Jan 12, 2022
67bd658
removed import_raw_key test for now
dbfreem Jan 12, 2022
9b95927
mypy typing issues
dbfreem Jan 12, 2022
7c62d60
typo
dbfreem Jan 12, 2022
8fef94d
Added AsyncGethAdmin and BaseGethAdmin. Also, fixed test due to typing
dbfreem Jan 17, 2022
5059d5b
Merge branch 'ethereum:master' into feature/async_geth_personal
dbfreem Jan 26, 2022
ddca2cc
made suggested changes
dbfreem Jan 28, 2022
bc96154
Merge branch 'feature/async_geth_personal' of https://github.com/dbfr…
dbfreem Jan 28, 2022
f17b13d
made suggested changes
dbfreem Jan 29, 2022
f874bff
Merge branch 'feature/async_geth_personal' into feature/async-geth-admin
dbfreem Jan 30, 2022
204cf1e
fixed spelling errors
dbfreem Jan 31, 2022
6d9e2d2
Merge branch 'feature/async_geth_personal' into feature/async-geth-admin
dbfreem Jan 31, 2022
eb6e6ae
added test and docs
dbfreem Feb 2, 2022
c72997e
Merge branch 'master' into feature/async-geth-admin
dbfreem Feb 2, 2022
f8b05ae
newsfragment
dbfreem Feb 2, 2022
c27fdb1
merge conflict
dbfreem Feb 2, 2022
179d437
remove setSolc
dbfreem Feb 2, 2022
eb890fa
copy in suggested test
dbfreem Feb 5, 2022
6ade56b
forgot to check liniting before commit
dbfreem Feb 5, 2022
553d847
linting
dbfreem Feb 5, 2022
40aba83
linting
dbfreem Feb 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ AsyncHTTPProvider
... 'net': (AsyncNet,),
... 'geth': (Geth,
... {'txpool': (AsyncGethTxPool,),
... 'personal': (AsyncGethPersonal,)})
... 'personal': (AsyncGethPersonal,),
... 'admin' : (AsyncGethAdmin,)})
... },
... middlewares=[]) # See supported middleware section below for middleware options

Expand Down Expand Up @@ -439,6 +440,14 @@ Net

Geth
****
- :meth:`web3.geth.admin.add_peer() <web3.geth.admin.add_peer>`
- :meth:`web3.geth.admin.datadir() <web3.geth.admin.datadir>`
- :meth:`web3.geth.admin.node_info() <web3.geth.admin.node_info>`
- :meth:`web3.geth.admin.peers() <web3.geth.admin.peers>`
- :meth:`web3.geth.admin.start_rpc() <web3.geth.admin.start_rpc>`
- :meth:`web3.geth.admin.start_ws() <web3.geth.admin.start_ws>`
- :meth:`web3.geth.admin.stop_rpc() <web3.geth.admin.stop_rpc>`
- :meth:`web3.geth.admin.stop_ws() <web3.geth.admin.stop_ws>`
- :meth:`web3.geth.personal.ec_recover()`
- :meth:`web3.geth.personal.import_raw_key() <web3.geth.personal.import_raw_key>`
- :meth:`web3.geth.personal.list_accounts() <web3.geth.personal.list_accounts>`
Expand Down
4 changes: 0 additions & 4 deletions docs/web3.geth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ The ``web3.geth.admin`` object exposes methods to interact with the RPC APIs und

.. warning:: Deprecated: This method is deprecated in favor of :meth:`~web3.geth.admin.add_peer()`

.. py:method:: setSolc(solc_path)

.. Warning:: This method has been removed from Geth

.. py:method:: start_rpc(host='localhost', port=8545, cors="", apis="eth,net,web3")

* Delegates to ``admin_startRPC`` RPC Method
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 Personal and Admin modules
1 change: 0 additions & 1 deletion newsfragments/2299.feature.rst

This file was deleted.

26 changes: 25 additions & 1 deletion tests/integration/go_ethereum/test_goethereum_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
get_open_port,
)
from web3 import Web3
from web3._utils.module_testing.go_ethereum_admin_module import (
GoEthereumAsyncAdminModuleTest,
)
from web3._utils.module_testing.go_ethereum_personal_module import (
GoEthereumAsyncPersonalModuleTest,
)
from web3.eth import (
AsyncEth,
)
from web3.geth import (
AsyncGethAdmin,
AsyncGethPersonal,
AsyncGethTxPool,
Geth,
Expand Down Expand Up @@ -102,7 +106,8 @@ async def async_w3(geth_process, endpoint_uri):
'async_net': AsyncNet,
'geth': (Geth,
{'txpool': (AsyncGethTxPool,),
'personal': (AsyncGethPersonal,)}
'personal': (AsyncGethPersonal,),
'admin': (AsyncGethAdmin,)}
)
}
)
Expand All @@ -129,6 +134,25 @@ def test_admin_start_stop_ws(self, web3: "Web3") -> None:
super().test_admin_start_stop_ws(web3)


class TestGoEthereumAsyncAdminModuleTest(GoEthereumAsyncAdminModuleTest):
@pytest.mark.asyncio
@pytest.mark.xfail(reason="running geth with the --nodiscover flag doesn't allow peer addition")
async def test_admin_peers(self, web3: "Web3") -> None:
await super().test_admin_peers(web3)

@pytest.mark.asyncio
async def test_admin_start_stop_rpc(self, web3: "Web3") -> None:
# This test causes all tests after it to fail on CI if it's allowed to run
pytest.xfail(reason='Only one RPC endpoint is allowed to be active at any time')
await super().test_admin_start_stop_rpc(web3)

@pytest.mark.asyncio
async def test_admin_start_stop_ws(self, web3: "Web3") -> None:
# This test causes all tests after it to fail on CI if it's allowed to run
pytest.xfail(reason='Only one WS endpoint is allowed to be active at any time')
await super().test_admin_start_stop_ws(web3)


class TestGoEthereumEthModuleTest(GoEthereumEthModuleTest):
pass

Expand Down
44 changes: 43 additions & 1 deletion web3/_utils/module_testing/go_ethereum_admin_module.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from typing import (
TYPE_CHECKING,
List,
)

from web3.datastructures import (
Expand Down Expand Up @@ -80,7 +81,7 @@ def test_admin_start_stop_ws(self, web3: "Web3") -> None:
def test_admin_addPeer(self, web3: "Web3") -> None:
with pytest.warns(DeprecationWarning):
result = web3.geth.admin.addPeer(
'enode://f1a6b0bdbf014355587c3018454d070ac57801f05d3b39fe85da574f002a32e929f683d72aa5a8318382e4d3c7a05c9b91687b0d997a39619fb8a6e7ad88e512@1.1.1.1:30303', # noqa: E501
EnodeURI('enode://f1a6b0bdbf014355587c3018454d070ac57801f05d3b39fe85da574f002a32e929f683d72aa5a8318382e4d3c7a05c9b91687b0d997a39619fb8a6e7ad88e512@1.1.1.1:30303'), # noqa: E501
)
assert result is True

Expand All @@ -98,3 +99,44 @@ def test_admin_nodeInfo(self, web3: "Web3") -> None:
})
# Test that result gives at least the keys that are listed in `expected`
assert not set(expected.keys()).difference(result.keys())


class GoEthereumAsyncAdminModuleTest:

@pytest.mark.asyncio
async def test_async_datadir(self, async_w3: "Web3") -> None:
datadir = await async_w3.geth.admin.datadir() # type: ignore
assert isinstance(datadir, str)

@pytest.mark.asyncio
async def test_async_nodeinfo(self, async_w3: "Web3") -> None:
node_info = await async_w3.geth.admin.node_info() # type: ignore
assert "Geth" in node_info["name"]

@pytest.mark.asyncio
async def test_async_nodes(self, async_w3: "Web3") -> None:
nodes = await async_w3.geth.admin.peers() # type: ignore
assert isinstance(nodes, List)

@pytest.mark.asyncio
async def test_admin_peers(self, web3: "Web3") -> None:
enode = await web3.geth.admin.node_info()['enode'] # type: ignore
web3.geth.admin.add_peer(enode)
result = await web3.geth.admin.peers() # type: ignore
assert len(result) == 1

@pytest.mark.asyncio
async def test_admin_start_stop_rpc(self, web3: "Web3") -> None:
stop = await web3.geth.admin.stop_rpc() # type: ignore
assert stop is True

start = await web3.geth.admin.start_rpc() # type: ignore
assert start is True

@pytest.mark.asyncio
async def test_admin_start_stop_ws(self, web3: "Web3") -> None:
stop = await web3.geth.admin.stop_ws() # type: ignore
assert stop is True

start = await web3.geth.admin.start_ws() # type: ignore
assert start is True
131 changes: 116 additions & 15 deletions web3/geth.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@
Module,
)
from web3.types import (
EnodeURI,
GethWallet,
NodeInfo,
Peer,
TxParams,
TxPoolContent,
TxPoolInspect,
Expand Down Expand Up @@ -258,25 +261,123 @@ async def status(self) -> Awaitable[Any]:
return await self._status() # type: ignore


class GethAdmin(Module):
class BaseGethAdmin(Module):
"""
https://github.com/ethereum/go-ethereum/wiki/Management-APIs#admin
"""
add_peer = add_peer
node_info = node_info
start_rpc = start_rpc
start_ws = start_ws
stop_ws = stop_ws
stop_rpc = stop_rpc
_add_peer = add_peer
_datadir = datadir
_node_info = node_info
_peers = peers
_start_rpc = start_rpc
_start_ws = start_ws
_stop_ws = stop_ws
_stop_rpc = stop_rpc
# deprecated
addPeer = addPeer
datadir = datadir
nodeInfo = nodeInfo
peers = peers
startRPC = startRPC
startWS = startWS
stopRPC = stopRPC
stopWS = stopWS
_addPeer = addPeer
_nodeInfo = nodeInfo
_startRPC = startRPC
_startWS = startWS
_stopRPC = stopRPC
_stopWS = stopWS


class GethAdmin(BaseGethAdmin):
is_async = False

def add_peer(self, node_url: EnodeURI) -> bool:
return self._add_peer(node_url)

def datadir(self) -> str:
return self._datadir()

def node_info(self) -> NodeInfo:
return self._node_info()

def peers(self) -> List[Peer]:
return self._peers()

def start_rpc(self,
host: str = "localhost",
port: int = 8546,
cors: str = "",
apis: str = "eth,net,web3") -> bool:
return self._start_rpc(host, port, cors, apis)

def start_ws(self,
host: str = "localhost",
port: int = 8546,
cors: str = "",
apis: str = "eth,net,web3") -> bool:
return self._start_ws(host, port, cors, apis)

def stop_rpc(self) -> bool:
return self._stop_rpc()

def stop_ws(self) -> bool:
return self._stop_ws()

def addPeer(self, node_url: EnodeURI) -> bool:
return self._addPeer(node_url)

def nodeInfo(self) -> NodeInfo:
return self._nodeInfo()

def startRPC(self,
host: str = "localhost",
port: int = 8546,
cors: str = "",
apis: str = "eth,net,web3") -> bool:
return self._startRPC(host, port, cors, apis)

def startWS(self,
host: str = "localhost",
port: int = 8546,
cors: str = "",
apis: str = "eth,net,web3") -> bool:
return self._startWS(host, port, cors, apis)

def stopRPC(self) -> bool:
return self._stopRPC()

def stopWS(self) -> bool:
return self._stopWS()


class AsyncGethAdmin(BaseGethAdmin):
is_async = True

async def add_peer(self, node_url: EnodeURI) -> Awaitable[bool]:
return await self._add_peer(node_url) # type: ignore

async def datadir(self) -> Awaitable[str]:
return await self._datadir() # type: ignore

async def node_info(self) -> Awaitable[NodeInfo]:
return await self._node_info() # type: ignore

async def peers(self) -> Awaitable[List[Peer]]:
return await self._peers() # type: ignore

async def start_rpc(self,
host: str = "localhost",
port: int = 8546,
cors: str = "",
apis: str = "eth,net,web3") -> Awaitable[bool]:
return await self._start_rpc(host, port, cors, apis) # type: ignore

async def start_ws(self,
host: str = "localhost",
port: int = 8546,
cors: str = "",
apis: str = "eth,net,web3") -> Awaitable[bool]:
return await self._start_ws(host, port, cors, apis) # type: ignore

async def stop_rpc(self) -> Awaitable[bool]:
return await self._stop_rpc() # type: ignore

async def stop_ws(self) -> Awaitable[bool]:
return await self._stop_ws() # type: ignore


class GethMiner(Module):
Expand Down