From 19771fe30d2015e90506082a017318e221d17043 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 12 Sep 2025 15:23:56 +1000 Subject: [PATCH] feat: Add better status check for internal nodes and error logging --- account.py | 37 +++++++++++++++++++++++++++++-------- main.py | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/account.py b/account.py index 664c938..839767b 100644 --- a/account.py +++ b/account.py @@ -95,6 +95,7 @@ def hsdConnected(): def hsdVersion(format=True): info = hsd.getInfo() if 'error' in info: + logger.error(f"HSD connection error: {info.get('error', 'Unknown error')}") return -1 # Check if SPV mode is enabled @@ -121,6 +122,7 @@ def check_account(cookie: str | None): # Check if the account is valid info = hsw.getAccountInfo(account, 'default') if 'error' in info: + logger.error(f"HSW error checking account {account}: {info.get('error', 'Unknown error')}") return False return account @@ -426,7 +428,7 @@ def getBalance(account: str): daemon=True ) thread.start() - + total = total - (domainValue/1000000) locked = locked - (domainValue/1000000) logger.debug(f"Adjusted balance for account {account}: total={total}, available={available}, locked={locked}") @@ -583,6 +585,7 @@ def getTransactions(account, page=1, limit=100): return [] info = hsw.getWalletTxHistory(account) if 'error' in info: + logger.error(f"Error getting transactions for account {account}: {info['error']}") return [] return info[::-1] @@ -916,6 +919,7 @@ def register(account, domain): def getNodeSync(): response = hsd.getInfo() if 'error' in response: + logger.error(f"Error getting node sync status: {response['error']}") return 0 sync = response['chain']['progress']*100 @@ -1941,13 +1945,17 @@ def hsdStart(): cmd.append(flag) # Launch process - HSD_PROCESS = subprocess.Popen( - cmd, - cwd=os.getcwd(), - text=True - ) - - logger.info(f"HSD started with PID {HSD_PROCESS.pid}") + try: + HSD_PROCESS = subprocess.Popen( + cmd, + cwd=os.getcwd(), + text=True + ) + + logger.info(f"HSD started with PID {HSD_PROCESS.pid}") + except Exception as e: + logger.error(f"Failed to start HSD: {str(e)}", exc_info=True) + return atexit.register(hsdStop) @@ -1959,6 +1967,19 @@ def hsdStart(): logger.error(f"Failed to set signal handlers: {str(e)}", exc_info=True) pass +def hsdRunning() -> bool: + global HSD_PROCESS + if not HSD_INTERNAL_NODE: + return False + if HSD_PROCESS is None: + return False + + # Check if process has terminated + poll_result = HSD_PROCESS.poll() + if poll_result is not None: + logger.error(f"HSD process has terminated with exit code: {poll_result}") + return False + return True def hsdStop(): global HSD_PROCESS diff --git a/main.py b/main.py index 471c7b8..ce0a079 100644 --- a/main.py +++ b/main.py @@ -1868,9 +1868,36 @@ def api_icon(account): def api_status(): # This doesn't require a login # Check if the node is connected - if not account_module.hsdConnected(): - return jsonify({"status":503,"error": "Node not connected"}), 503 - return jsonify({"status": 200,"result": "FireWallet is running"}) + node_status = { + "connected": account_module.hsdConnected(), + "internal": account_module.HSD_INTERNAL_NODE, + "internal_running": False, + "version": "N/A" + } + status = 200 + error = None + node_status['version'] = account_module.hsdVersion(False) # type: ignore + + if node_status['internal']: + node_status['internal_running'] = account_module.hsdRunning() + + # If the node is not connected, return an error + if not node_status['connected']: + error = "Node not connected" + status = 503 + if node_status['version'] == -1: + error = "Error connecting to HSD" + status = 503 + if node_status['internal'] and not node_status['internal_running']: + error = "Internal node not running" + status = 503 + + + return jsonify({ + "node": node_status, + "error": error, + "status": status + }), status #endregion