Before calling gethostbyname(); check if host is an address (#8765)

* Before calling gethostbyname(), check to see if we already have an address.
That prevents an unnecessary call, and also allows for specifying IPv6
addresses which otherwise cause exceptions here.

* Also recognize addresses when passed to timelord spawn_process, so that
IPv6 addresses can be used.

* Missed importing PeerInfo

* Adjust style to match requirements and existing code

* Cast PeerInfo port to uint16
This commit is contained in:
Chris Ross 2021-10-07 19:56:34 -04:00 committed by GitHub
parent ccb2442ae8
commit 0a2294841d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View File

@ -9,7 +9,11 @@ def start_reconnect_task(server: ChiaServer, peer_info_arg: PeerInfo, log, auth:
"""
Start a background task that checks connection and reconnects periodically to a peer.
"""
peer_info = PeerInfo(socket.gethostbyname(peer_info_arg.host), peer_info_arg.port)
# If peer_info_arg is already an address, use it, otherwise resolve it here.
if peer_info_arg.is_valid():
peer_info = peer_info_arg
else:
peer_info = PeerInfo(socket.gethostbyname(peer_info_arg.host), peer_info_arg.port)
async def connection_check():
while True:

View File

@ -8,9 +8,11 @@ from typing import Dict, List
import pkg_resources
from chia.types.peer_info import PeerInfo
from chia.util.chia_logging import initialize_logging
from chia.util.config import load_config
from chia.util.default_root import DEFAULT_ROOT_PATH
from chia.util.ints import uint16
from chia.util.setproctitle import setproctitle
active_processes: List = []
@ -49,7 +51,11 @@ async def spawn_process(host: str, port: int, counter: int):
try:
dirname = path_to_vdf_client.parent
basename = path_to_vdf_client.name
resolved = socket.gethostbyname(host)
check_addr = PeerInfo(host, uint16(port))
if check_addr.is_valid():
resolved = host
else:
resolved = socket.gethostbyname(host)
proc = await asyncio.create_subprocess_shell(
f"{basename} {resolved} {port} {counter}",
stdout=asyncio.subprocess.PIPE,

View File

@ -395,7 +395,12 @@ class WalletNode:
self.config["full_node_peer"]["port"],
)
peers = [c.get_peer_info() for c in self.server.get_full_node_connections()]
full_node_resolved = PeerInfo(socket.gethostbyname(full_node_peer.host), full_node_peer.port)
# If full_node_peer is already an address, use it, otherwise
# resolve it here.
if full_node_peer.is_valid():
full_node_resolved = full_node_peer
else:
full_node_resolved = PeerInfo(socket.gethostbyname(full_node_peer.host), full_node_peer.port)
if full_node_peer in peers or full_node_resolved in peers:
self.log.info(f"Will not attempt to connect to other nodes, already connected to {full_node_peer}")
for connection in self.server.get_full_node_connections():