xch-blockchain/chia/server/reconnect_task.py

35 lines
1.3 KiB
Python
Raw Normal View History

2020-05-20 21:44:05 +02:00
import asyncio
Resolve v6 addresses (#8861) * Address deficiency discussed in #8552, add ability to resolve to IPv6 addresses for hostnames. * If there is no prefer_ipv6 in the config, set it True (per @hoffmang9) and write it back to config.yaml * Pass prefer_ipv6 flag to get_host_addr, which required a little digging for it in a few places that call get_host_addr. * Update a couple things for consistency * Move the load_config into Wallet's __init__ so it doesn't get called so many times as it would in has_full_node. * Pass None into get_host_addr if there's no preference in config, so we have only that one place where the coded default lives. also fix an oversight where we were building a PeerInfo from a PeerInfo in some cases. * Change the default here to match the default coded into util/network.py. It seems that github testers can't handle trying to use IPv6 and this may be easier for average users (sadly) * A test to see if manually creating the server on :: (IP6_ANY) lets tests connect to localhost with IPv6 on * Revert back to IPv4 default and remove the override inserted into TCPSite for testing. * Don't test for ip6-localhost, as it's not on all systems. * Bah. Forced formatting of commented code... * Add a type annotation for the addrset variable * If we don't quote the socket enums, pylint gets upset because it has issues figuring out where/how they're defined. So, quote them here. Co-authored-by: Chris Ross <cross+chia@distal.com> Co-authored-by: Kyle Altendorf <sda@fstab.net>
2021-11-22 23:34:03 +01:00
from typing import Optional
2020-05-20 21:44:05 +02:00
from chia.server.server import ChiaServer
from chia.types.peer_info import PeerInfo
Resolve v6 addresses (#8861) * Address deficiency discussed in #8552, add ability to resolve to IPv6 addresses for hostnames. * If there is no prefer_ipv6 in the config, set it True (per @hoffmang9) and write it back to config.yaml * Pass prefer_ipv6 flag to get_host_addr, which required a little digging for it in a few places that call get_host_addr. * Update a couple things for consistency * Move the load_config into Wallet's __init__ so it doesn't get called so many times as it would in has_full_node. * Pass None into get_host_addr if there's no preference in config, so we have only that one place where the coded default lives. also fix an oversight where we were building a PeerInfo from a PeerInfo in some cases. * Change the default here to match the default coded into util/network.py. It seems that github testers can't handle trying to use IPv6 and this may be easier for average users (sadly) * A test to see if manually creating the server on :: (IP6_ANY) lets tests connect to localhost with IPv6 on * Revert back to IPv4 default and remove the override inserted into TCPSite for testing. * Don't test for ip6-localhost, as it's not on all systems. * Bah. Forced formatting of commented code... * Add a type annotation for the addrset variable * If we don't quote the socket enums, pylint gets upset because it has issues figuring out where/how they're defined. So, quote them here. Co-authored-by: Chris Ross <cross+chia@distal.com> Co-authored-by: Kyle Altendorf <sda@fstab.net>
2021-11-22 23:34:03 +01:00
from chia.util.network import get_host_addr
2020-07-09 14:03:59 +02:00
def start_reconnect_task(server: ChiaServer, peer_info_arg: PeerInfo, log, prefer_ipv6: Optional[bool]):
2020-05-20 21:44:05 +02:00
"""
Start a background task that checks connection and reconnects periodically to a peer.
2020-05-20 21:44:05 +02:00
"""
# 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:
Resolve v6 addresses (#8861) * Address deficiency discussed in #8552, add ability to resolve to IPv6 addresses for hostnames. * If there is no prefer_ipv6 in the config, set it True (per @hoffmang9) and write it back to config.yaml * Pass prefer_ipv6 flag to get_host_addr, which required a little digging for it in a few places that call get_host_addr. * Update a couple things for consistency * Move the load_config into Wallet's __init__ so it doesn't get called so many times as it would in has_full_node. * Pass None into get_host_addr if there's no preference in config, so we have only that one place where the coded default lives. also fix an oversight where we were building a PeerInfo from a PeerInfo in some cases. * Change the default here to match the default coded into util/network.py. It seems that github testers can't handle trying to use IPv6 and this may be easier for average users (sadly) * A test to see if manually creating the server on :: (IP6_ANY) lets tests connect to localhost with IPv6 on * Revert back to IPv4 default and remove the override inserted into TCPSite for testing. * Don't test for ip6-localhost, as it's not on all systems. * Bah. Forced formatting of commented code... * Add a type annotation for the addrset variable * If we don't quote the socket enums, pylint gets upset because it has issues figuring out where/how they're defined. So, quote them here. Co-authored-by: Chris Ross <cross+chia@distal.com> Co-authored-by: Kyle Altendorf <sda@fstab.net>
2021-11-22 23:34:03 +01:00
peer_info = PeerInfo(get_host_addr(peer_info_arg, prefer_ipv6), peer_info_arg.port)
2020-05-20 21:44:05 +02:00
async def connection_check():
while True:
peer_retry = True
2020-12-02 08:50:22 +01:00
for _, connection in server.all_connections.items():
if connection.get_peer_info() == peer_info or connection.get_peer_info() == peer_info_arg:
2020-05-20 21:44:05 +02:00
peer_retry = False
if peer_retry:
log.info(f"Reconnecting to peer {peer_info}")
2020-07-09 14:03:59 +02:00
try:
await server.start_client(peer_info, None)
2020-07-09 14:03:59 +02:00
except Exception as e:
log.info(f"Failed to connect to {peer_info} {e}")
2020-12-02 13:12:28 +01:00
await asyncio.sleep(3)
2020-05-20 21:44:05 +02:00
return asyncio.create_task(connection_check())