Skip to content

Commit

Permalink
Before calling gethostbyname(); check if host is an address (Chia-Net…
Browse files Browse the repository at this point in the history
…work#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
  • Loading branch information
cross authored Oct 7, 2021
1 parent ccb2442 commit 0a22948
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion chia/server/reconnect_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion chia/timelord/timelord_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

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 = []
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion chia/wallet/wallet_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,12 @@ def has_full_node(self) -> bool:
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():
Expand Down

0 comments on commit 0a22948

Please sign in to comment.