# Buggy code
def get_certificate(
    self,
    host,
    port,
    timeout=3,
    retry_attempts: int = 3,
    retry_rate: int = 2,
    current_attempt: int = 0,
):
    socket.setdefaulttimeout(timeout)  # Set Socket Timeout

    try:
        self.log.info(f"Fetching seednode {host}:{port} TLS certificate")
        seednode_certificate = ssl.get_server_certificate(addr=(host, port))

    except socket.timeout:
        if current_attempt == retry_attempts:
            message = f"No Response from seednode {host}:{port} after {retry_attempts} attempts"
            self.log.info(message)
            raise ConnectionRefusedError("No response from {}:{}".format(host, port))
        self.log.info(
            f"No Response from seednode {host}:{port}. Retrying in {retry_rate} seconds..."
        )
        time.sleep(retry_rate)
        return self.get_certificate(
            host, port, timeout, retry_attempts, retry_rate, current_attempt + 1
        )

    else:
        certificate = x509.load_pem_x509_certificate(
            seednode_certificate.encode(), backend=default_backend()
        )
        return certificate


# Traceback (ValueError)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]: Unhandled error in Deferred:
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]: Traceback (most recent call last):
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/internet/base.py", line 1283, in run
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     self.mainLoop()
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/internet/base.py", line 1292, in mainLoop
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     self.runUntilCurrent()
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/internet/base.py", line 913, in runUntilCurrent
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     call.func(*call.args, **call.kw)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/internet/task.py", line 239, in __call__
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     d = defer.maybeDeferred(self.f, *self.a, **self.kw)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]: --- <exception caught here> ---
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     current.result = callback(current.result, *args, **kw)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher/nucypher/network/sensors.py", line 89, in handle_measurement_errors
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     failure.raiseException()
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/python/failure.py", line 488, in raiseException
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     raise self.value.with_traceback(self.tb)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher-venv/lib/python3.6/site-packages/twisted/internet/defer.py", line 151, in maybeDeferred
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     result = f(*args, **kw)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher/nucypher/network/sensors.py", line 138, in maintain
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     self.measure()
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher/nucypher/network/sensors.py", line 191, in measure
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     certificate_filepath=certificate_filepath)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher/nucypher/network/middleware.py", line 248, in check_rest_availability
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     certificate_filepath=certificate_filepath)
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:   File "/home/ubuntu/nucypher/nucypher/network/middleware.py", line 114, in method_wrapper
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]:     raise ValueError("Don't try to pass a node with a certificate_filepath while also passing a"
Apr 02 17:02:28 ip-172-31-19-8 nucypher[29582]: builtins.ValueError: Don't try to pass a node with a certificate_filepath while also passing a different certificate_filepath.  What do you even expect?
Apr 03 00:02:29 ip-172-31-19-8 nucypher[29582]: Broadcasting CONFIRMACTIVITY Transaction (264369 gwei @ 3000000000)...

# Fixed code
def get_certificate(
    self,
    host,
    port,
    timeout=3,
    retry_attempts: int = 3,
    retry_rate: int = 2,
    current_attempt: int = 0,
):
    socket.setdefaulttimeout(timeout)  # Set Socket Timeout

    try:
        self.log.info(f"Fetching seednode {host}:{port} TLS certificate")
        seednode_certificate = ssl.get_server_certificate(addr=(host, port))

    except socket.timeout:
        if current_attempt == retry_attempts:
            message = f"No Response from seednode {host}:{port} after {retry_attempts} attempts"
            self.log.info(message)
            raise ConnectionRefusedError("No response from {}:{}".format(host, port))
        self.log.info(
            f"No Response from seednode {host}:{port}. Retrying in {retry_rate} seconds..."
        )
        time.sleep(retry_rate)
        return self.get_certificate(
            host, port, timeout, retry_attempts, retry_rate, current_attempt + 1
        )

    except OSError:
        raise  # TODO: #1835

    else:
        certificate = x509.load_pem_x509_certificate(
            seednode_certificate.encode(), backend=default_backend()
        )
        return certificate