Skip to content

Commit

Permalink
Add get_network_info to daemon (#17717)
Browse files Browse the repository at this point in the history
* its also useful to have network info available from the daemon, vs having to ask whichever service(s) happen to be up

* Add test for get_network_info on daemon

* Fix precommit
  • Loading branch information
cmmarslender authored Mar 18, 2024
1 parent 4df4496 commit 71ced4c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
13 changes: 12 additions & 1 deletion chia/_tests/core/daemon/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from chia._tests.util.misc import Marks, datacases
from chia._tests.util.time_out_assert import time_out_assert_not_none
from chia.daemon.client import connect_to_daemon
from chia.daemon.client import DaemonProxy, connect_to_daemon
from chia.daemon.keychain_server import (
DeleteLabelRequest,
GetKeyRequest,
Expand Down Expand Up @@ -585,6 +585,17 @@ async def test_get_routes(mock_lonely_daemon):
}


@pytest.mark.anyio
async def test_get_network_info(daemon_client_with_config_and_keys: DaemonProxy):
client = daemon_client_with_config_and_keys
response = await client.get_network_info()
assert response["data"] == {
"success": True,
"network_name": "testnet0",
"network_prefix": "txch",
}


@datacases(
WalletAddressCase(
id="no params",
Expand Down
6 changes: 6 additions & 0 deletions chia/daemon/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ async def get_version(self) -> WsRpcMessage:
response = await self._get(request)
return response

async def get_network_info(self) -> WsRpcMessage:
data: Dict[str, Any] = {}
request = self.format_request("get_network_info", data)
response = await self._get(request)
return response

async def start_service(self, service_name: str) -> WsRpcMessage:
data = {"service": service_name}
request = self.format_request("start_service", data)
Expand Down
7 changes: 7 additions & 0 deletions chia/daemon/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,15 @@ def get_command_mapping(self) -> Dict[str, Command]:
"get_routes": self.get_routes,
"get_wallet_addresses": self.get_wallet_addresses,
"get_keys_for_plotting": self.get_keys_for_plotting,
"get_network_info": self.get_network_info,
}

async def get_network_info(self, websocket: WebSocketResponse, request: Dict[str, Any]) -> Dict[str, Any]:
network_name = self.net_config["selected_network"]
address_prefix = self.net_config["network_overrides"]["config"][network_name]["address_prefix"]
response: Dict[str, Any] = {"success": True, "network_name": network_name, "network_prefix": address_prefix}
return response

async def is_keyring_locked(self, websocket: WebSocketResponse, request: Dict[str, Any]) -> Dict[str, Any]:
locked: bool = Keychain.is_keyring_locked()
response: Dict[str, Any] = {"success": True, "is_keyring_locked": locked}
Expand Down

0 comments on commit 71ced4c

Please sign in to comment.