From 43a01a7b0a320468b5c6ef74198631179ab34882 Mon Sep 17 00:00:00 2001
From: Benjamin Himes <37844818+thewhaleking@users.noreply.github.com>
Date: Tue, 5 Nov 2024 16:29:54 +0200
Subject: [PATCH] Handle SSL Error on Connection (#2384)
---
bittensor/core/async_subtensor.py | 14 ++++++++++----
bittensor/core/subtensor.py | 3 ++-
2 files changed, 12 insertions(+), 5 deletions(-)
diff --git a/bittensor/core/async_subtensor.py b/bittensor/core/async_subtensor.py
index 508ae0243..40c77ae30 100644
--- a/bittensor/core/async_subtensor.py
+++ b/bittensor/core/async_subtensor.py
@@ -1,10 +1,10 @@
import asyncio
+import ssl
from typing import Optional, Any, Union, TypedDict, Iterable
import aiohttp
import numpy as np
import scalecodec
-import typer
from bittensor_wallet import Wallet
from bittensor_wallet.utils import SS58_FORMAT
from numpy.typing import NDArray
@@ -134,7 +134,13 @@ async def __aenter__(self):
logging.error(
f"Error: Timeout occurred connecting to substrate. Verify your chain and network settings: {self}"
)
- raise typer.Exit(code=1)
+ raise ConnectionError
+ except (ConnectionRefusedError, ssl.SSLError) as error:
+ logging.error(
+ f"Error: Connection refused when connecting to substrate. "
+ f"Verify your chain and network settings: {self}. Error: {error}"
+ )
+ raise ConnectionError
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.substrate.close()
@@ -245,7 +251,7 @@ async def get_total_subnets(
module="SubtensorModule",
storage_function="TotalNetworks",
params=[],
- block_hash=block_hash
+ block_hash=block_hash,
)
return result
@@ -1298,7 +1304,7 @@ async def get_uid_for_hotkey_on_subnet(
module="SubtensorModule",
storage_function="Uids",
params=[netuid, hotkey_ss58],
- block_hash=block_hash
+ block_hash=block_hash,
)
# extrinsics
diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py
index fcbb4147d..61a3c3938 100644
--- a/bittensor/core/subtensor.py
+++ b/bittensor/core/subtensor.py
@@ -23,6 +23,7 @@
import argparse
import copy
import socket
+import ssl
from typing import Union, Optional, TypedDict, Any
import numpy as np
@@ -231,7 +232,7 @@ def _get_substrate(self):
except (AttributeError, TypeError, socket.error, OSError) as e:
logging.warning(f"Error setting timeout: {e}")
- except ConnectionRefusedError as error:
+ except (ConnectionRefusedError, ssl.SSLError) as error:
logging.error(
f"Could not connect to {self.network} network with {self.chain_endpoint} chain endpoint.",
)