From e93fbd24be6cf8959bbdeb90a0d8284497cbee0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Thu, 2 Mar 2023 12:03:58 +0100 Subject: [PATCH 1/4] FEAT: add funding source fallback and VoidWallet warning for frontend --- lnbits/app.py | 26 ++++++++++++++++++++++++-- lnbits/settings.py | 5 +++-- lnbits/templates/base.html | 3 +++ pyproject.toml | 3 ++- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lnbits/app.py b/lnbits/app.py index a3f719ed..65acea2e 100644 --- a/lnbits/app.py +++ b/lnbits/app.py @@ -105,20 +105,42 @@ async def check_funding_source() -> None: signal.signal(signal.SIGINT, signal_handler) WALLET = get_wallet_class() + + # fallback to void after 30 seconds of failures + sleep_time = 5 + timeout = int(30 / sleep_time) + + balance = 0 + retry_counter = 0 + while True: try: error_message, balance = await WALLET.status() if not error_message: + retry_counter = 0 break + logger.error( f"The backend for {WALLET.__class__.__name__} isn't working properly: '{error_message}'", RuntimeWarning, ) except: pass - logger.info("Retrying connection to backend in 5 seconds...") - await asyncio.sleep(5) + + if retry_counter == timeout: + logger.warning( + f"Fallback to VoidWallet, because the backend for {WALLET.__class__.__name__} isn't working properly" + ) + set_wallet_class("VoidWallet") + WALLET = get_wallet_class() + break + else: + logger.warning(f"Retrying connection to backend in {sleep_time} seconds...") + retry_counter += 1 + await asyncio.sleep(sleep_time) + signal.signal(signal.SIGINT, original_sigint_handler) + logger.info( f"✔️ Backend {WALLET.__class__.__name__} connected and with a balance of {balance} msat." ) diff --git a/lnbits/settings.py b/lnbits/settings.py index 75c20a9a..78a0c76e 100644 --- a/lnbits/settings.py +++ b/lnbits/settings.py @@ -317,8 +317,9 @@ def set_cli_settings(**kwargs): # set wallet class after settings are loaded -def set_wallet_class(): - wallet_class = getattr(wallets_module, settings.lnbits_backend_wallet_class) +def set_wallet_class(class_name: Optional[str] = None): + backend_wallet_class = class_name or settings.lnbits_backend_wallet_class + wallet_class = getattr(wallets_module, backend_wallet_class) global WALLET WALLET = wallet_class() diff --git a/lnbits/templates/base.html b/lnbits/templates/base.html index 7cba2256..d7eb54e6 100644 --- a/lnbits/templates/base.html +++ b/lnbits/templates/base.html @@ -45,6 +45,9 @@ {% block beta %} + + VoidWallet is active! Payments disabled + Date: Thu, 2 Mar 2023 12:07:20 +0100 Subject: [PATCH 2/4] only make it fallback when adminui is enabled --- lnbits/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lnbits/app.py b/lnbits/app.py index 65acea2e..9156efda 100644 --- a/lnbits/app.py +++ b/lnbits/app.py @@ -127,7 +127,7 @@ async def check_funding_source() -> None: except: pass - if retry_counter == timeout: + if settings.lnbits_admin_ui and retry_counter == timeout: logger.warning( f"Fallback to VoidWallet, because the backend for {WALLET.__class__.__name__} isn't working properly" ) From 235513f2ef0c99b6abdc7a999657748fede90cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Tue, 7 Mar 2023 12:28:52 +0100 Subject: [PATCH 3/4] fix if for voidwallet warning --- lnbits/helpers.py | 1 + lnbits/templates/base.html | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lnbits/helpers.py b/lnbits/helpers.py index 77b72e25..cad0b4af 100644 --- a/lnbits/helpers.py +++ b/lnbits/helpers.py @@ -100,6 +100,7 @@ def template_renderer(additional_folders: List = None) -> Jinja2Templates: t.env.globals["AD_SPACE"] = settings.lnbits_ad_space.split(",") t.env.globals["AD_SPACE_TITLE"] = settings.lnbits_ad_space_title + t.env.globals["VOIDWALLET"] = settings.lnbits_backend_wallet_class == "VoidWallet" t.env.globals["HIDE_API"] = settings.lnbits_hide_api t.env.globals["SITE_TITLE"] = settings.lnbits_site_title t.env.globals["LNBITS_DENOMINATION"] = settings.lnbits_denomination diff --git a/lnbits/templates/base.html b/lnbits/templates/base.html index d7eb54e6..fcfc6e67 100644 --- a/lnbits/templates/base.html +++ b/lnbits/templates/base.html @@ -45,9 +45,11 @@ {% block beta %} + {% if VOIDWALLET %} VoidWallet is active! Payments disabled + {%endif%} Date: Tue, 7 Mar 2023 12:40:19 +0100 Subject: [PATCH 4/4] fix prettier --- lnbits/templates/base.html | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lnbits/templates/base.html b/lnbits/templates/base.html index fcfc6e67..d51185c3 100644 --- a/lnbits/templates/base.html +++ b/lnbits/templates/base.html @@ -44,8 +44,7 @@ endblock %} - {% block beta %} - {% if VOIDWALLET %} + {% block beta %} {% if VOIDWALLET %} VoidWallet is active! Payments disabled