2021-07-19 20:12:46 +02:00
|
|
|
import asyncio
|
2023-06-25 08:01:43 +02:00
|
|
|
import sys
|
2025-03-18 17:14:35 +01:00
|
|
|
import traceback
|
2024-02-22 20:43:15 +01:00
|
|
|
from contextlib import asynccontextmanager
|
2021-07-19 20:12:46 +02:00
|
|
|
|
2026-07-11 12:07:21 +02:00
|
|
|
from fastapi import FastAPI, Request, WebSocket
|
2021-10-27 19:23:21 +02:00
|
|
|
from fastapi.encoders import jsonable_encoder
|
2025-03-18 17:14:35 +01:00
|
|
|
from fastapi.exceptions import HTTPException, RequestValidationError
|
2022-12-18 20:55:31 +01:00
|
|
|
from loguru import logger
|
2024-02-25 09:52:17 +01:00
|
|
|
from pydantic import BaseModel
|
2025-04-01 20:42:55 +02:00
|
|
|
from redis.asyncio import Redis
|
2021-07-19 20:12:46 +02:00
|
|
|
from starlette import status
|
2026-07-12 11:38:09 +02:00
|
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
2021-10-05 19:36:09 +02:00
|
|
|
from starlette.middleware.cors import CORSMiddleware
|
2026-07-12 11:38:09 +02:00
|
|
|
from starlette.responses import RedirectResponse
|
2026-07-11 12:07:21 +02:00
|
|
|
from starlette.websockets import WebSocketDisconnect
|
2021-07-10 17:15:02 +02:00
|
|
|
|
2025-02-16 20:35:21 +01:00
|
|
|
from app.api.config import config as dconfig
|
2025-03-18 17:14:35 +01:00
|
|
|
from app.api.error_report.report import Report
|
2026-07-12 11:38:09 +02:00
|
|
|
from app.api.error_report.response import build_error_response
|
2026-07-12 16:46:47 +02:00
|
|
|
from app.api.models import ApiErrors, StartupState
|
|
|
|
|
from app.api.startup_status import api_startup_status
|
2026-07-11 12:07:21 +02:00
|
|
|
from app.api.utils import Event, broadcast_msg
|
2022-10-03 20:22:00 +02:00
|
|
|
from app.api.warmup import (
|
|
|
|
|
get_bitcoin_client_warmup_data,
|
|
|
|
|
get_full_client_warmup_data,
|
|
|
|
|
get_full_client_warmup_data_bitcoinonly,
|
|
|
|
|
)
|
2026-07-11 12:07:21 +02:00
|
|
|
from app.api.ws_manager import ws_mgr
|
2025-04-01 20:42:55 +02:00
|
|
|
from app.apps.router import register_app_status_update_handlers
|
2022-10-03 20:22:00 +02:00
|
|
|
from app.apps.router import router as app_router
|
2021-11-28 08:42:52 +01:00
|
|
|
from app.auth.auth_handler import (
|
|
|
|
|
handle_local_cookie,
|
|
|
|
|
register_cookie_updater,
|
|
|
|
|
remove_local_cookie,
|
|
|
|
|
)
|
2022-10-03 20:22:00 +02:00
|
|
|
from app.bitcoind.router import router as bitcoin_router
|
|
|
|
|
from app.bitcoind.service import (
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
initialize_bitcoin_repo,
|
2021-09-05 08:56:53 +02:00
|
|
|
register_bitcoin_status_gatherer,
|
|
|
|
|
register_bitcoin_zmq_sub,
|
|
|
|
|
)
|
2026-02-02 12:25:30 +01:00
|
|
|
from app.external.fastapi_plugins_redis import (
|
|
|
|
|
RedisSettings,
|
|
|
|
|
get_config as get_redis_config,
|
|
|
|
|
redis_plugin,
|
|
|
|
|
registered_configuration,
|
|
|
|
|
)
|
2026-07-12 17:36:08 +02:00
|
|
|
from app.external.result_type.src.result.result import Err, Ok
|
2022-10-03 20:22:00 +02:00
|
|
|
from app.lightning.models import LnInitState
|
|
|
|
|
from app.lightning.router import router as ln_router
|
|
|
|
|
from app.lightning.service import initialize_ln_repo, register_lightning_listener
|
2022-12-18 20:55:31 +01:00
|
|
|
from app.logging import configure_logger
|
2022-10-03 20:22:00 +02:00
|
|
|
from app.setup.router import router as setup_router
|
|
|
|
|
from app.system.router import router as system_router
|
|
|
|
|
from app.system.service import get_hardware_info, register_hardware_info_gatherer
|
2021-06-10 21:13:26 +02:00
|
|
|
|
2022-12-18 20:55:31 +01:00
|
|
|
configure_logger()
|
|
|
|
|
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
|
2025-02-16 20:35:21 +01:00
|
|
|
remote_debugging = dconfig("BAPI_REMOTE_DEBUGGING", cast=bool, default=False)
|
2023-06-25 08:01:43 +02:00
|
|
|
if remote_debugging:
|
|
|
|
|
logger.warning(
|
|
|
|
|
(
|
|
|
|
|
"Remote debugging is enabled, this can be a security issue. "
|
|
|
|
|
"Only enable on development machines."
|
|
|
|
|
)
|
|
|
|
|
)
|
2025-02-16 20:35:21 +01:00
|
|
|
remote_debugging_port = dconfig(
|
|
|
|
|
"BAPI_REMOTE_DEBUGGING_PORT", cast=int, default=5678
|
|
|
|
|
)
|
2023-06-25 08:01:43 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import debugpy
|
|
|
|
|
except ImportError:
|
|
|
|
|
logger.error("Remote debugging is enabled, but debugpy is not installed.")
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
debugpy.listen(("0.0.0.0", remote_debugging_port))
|
|
|
|
|
|
2025-02-16 20:35:21 +01:00
|
|
|
node_type = dconfig("BAPI_LN_NODE", default="none").lower()
|
2022-12-01 18:51:03 +01:00
|
|
|
if node_type == "":
|
|
|
|
|
node_type = "none"
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
|
2021-06-22 21:07:15 +02:00
|
|
|
|
|
|
|
|
@registered_configuration
|
|
|
|
|
class AppSettings(RedisSettings):
|
|
|
|
|
api_name: str = str(__name__)
|
|
|
|
|
|
|
|
|
|
|
2025-02-16 20:35:21 +01:00
|
|
|
config = get_redis_config()
|
2021-06-10 21:13:26 +02:00
|
|
|
|
|
|
|
|
|
2024-02-22 20:43:15 +01:00
|
|
|
@asynccontextmanager
|
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
|
# setup
|
|
|
|
|
await redis_plugin.init_app(app, config=config)
|
|
|
|
|
await redis_plugin.init()
|
2025-04-01 20:42:55 +02:00
|
|
|
redis = redis_plugin.redis
|
|
|
|
|
if not isinstance(redis, Redis):
|
|
|
|
|
raise RuntimeError("Redis not initialized correctly, got a Sentinel")
|
|
|
|
|
|
2024-02-22 20:43:15 +01:00
|
|
|
register_cookie_updater()
|
2026-07-11 12:07:21 +02:00
|
|
|
await broadcast_msg(Event.SYSTEM_STARTUP_INFO, api_startup_status.model_dump())
|
2026-07-03 22:34:44 +02:00
|
|
|
btc_task = asyncio.create_task(_initialize_bitcoin())
|
|
|
|
|
ln_task = asyncio.create_task(_initialize_lightning())
|
2024-02-22 20:43:15 +01:00
|
|
|
await register_all_handlers()
|
|
|
|
|
handle_local_cookie()
|
|
|
|
|
|
|
|
|
|
yield
|
2021-06-10 21:13:26 +02:00
|
|
|
|
2024-02-22 20:43:15 +01:00
|
|
|
# cleanup
|
|
|
|
|
await redis_plugin.terminate()
|
2025-04-01 20:42:55 +02:00
|
|
|
await btc_task
|
|
|
|
|
await ln_task
|
2024-02-22 20:43:15 +01:00
|
|
|
remove_local_cookie()
|
|
|
|
|
|
|
|
|
|
|
2024-02-24 15:30:32 +01:00
|
|
|
app = FastAPI(lifespan=lifespan)
|
2024-02-22 20:43:15 +01:00
|
|
|
app.include_router(app_router)
|
|
|
|
|
app.include_router(bitcoin_router)
|
|
|
|
|
if node_type != "none":
|
|
|
|
|
app.include_router(ln_router)
|
|
|
|
|
app.include_router(system_router)
|
|
|
|
|
if setup_router is not None:
|
|
|
|
|
app.include_router(setup_router)
|
2021-09-07 19:52:57 +02:00
|
|
|
|
2021-10-04 17:36:56 +02:00
|
|
|
origins = [
|
|
|
|
|
"http://localhost",
|
|
|
|
|
"http://localhost:3000",
|
|
|
|
|
]
|
|
|
|
|
|
2021-10-04 17:30:01 +02:00
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
2021-10-04 17:36:56 +02:00
|
|
|
allow_origins=origins,
|
2021-10-04 17:30:01 +02:00
|
|
|
allow_credentials=True,
|
|
|
|
|
allow_methods=["*"],
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
|
|
|
|
|
2021-09-07 19:52:57 +02:00
|
|
|
|
2026-07-12 11:38:09 +02:00
|
|
|
@app.exception_handler(StarletteHTTPException)
|
2025-03-18 17:14:35 +01:00
|
|
|
@app.exception_handler(HTTPException)
|
|
|
|
|
async def http_e_handler(_: Request, e: HTTPException):
|
2026-07-12 11:38:09 +02:00
|
|
|
# Normalize every HTTPException into the ErrorMessage shape.
|
|
|
|
|
detail = e.detail
|
|
|
|
|
if isinstance(detail, dict) and "detail" in detail:
|
|
|
|
|
# already an ErrorMessage-shaped dict (e.g. the apps code)
|
|
|
|
|
return build_error_response(
|
|
|
|
|
e.status_code,
|
|
|
|
|
detail=str(detail.get("detail", "")),
|
|
|
|
|
error_code=str(detail.get("error_code", "") or ""),
|
|
|
|
|
report=detail.get("report"),
|
|
|
|
|
trace=detail.get("trace"),
|
|
|
|
|
report_sensitive=True,
|
|
|
|
|
)
|
|
|
|
|
return build_error_response(e.status_code, detail=str(detail))
|
2025-03-18 17:14:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
|
|
|
async def valid_e_handler(_: Request, exc: RequestValidationError):
|
|
|
|
|
errors = exc.errors()
|
|
|
|
|
try:
|
2026-07-12 11:38:09 +02:00
|
|
|
return build_error_response(
|
|
|
|
|
status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
|
|
|
detail=errors[0]["msg"],
|
|
|
|
|
error_code=ApiErrors.INVALID_REQUEST_INPUT,
|
|
|
|
|
report=errors,
|
|
|
|
|
report_sensitive=False, # field-error array is non-sensitive
|
2025-03-18 17:14:35 +01:00
|
|
|
)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
tb = traceback.format_exception(e)
|
|
|
|
|
logger.error(f"error processing RequestValidationError: {e}\n{tb}")
|
|
|
|
|
report = Report("error while processing RequestValidationError", e).attach(exc)
|
2026-07-12 11:38:09 +02:00
|
|
|
return build_error_response(
|
|
|
|
|
status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
|
|
|
detail="error processing RequestValidationError",
|
|
|
|
|
error_code=ApiErrors.UNABLE_TO_PROCESS_ERROR,
|
|
|
|
|
report=report.format_verbose(),
|
|
|
|
|
trace=tb,
|
2025-03-18 17:14:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-07-12 11:38:09 +02:00
|
|
|
@app.exception_handler(Exception)
|
|
|
|
|
async def unhandled_e_handler(_: Request, exc: Exception):
|
|
|
|
|
tb = traceback.format_exception(exc)
|
|
|
|
|
logger.error(f"unhandled exception: {exc}\n{''.join(tb)}")
|
|
|
|
|
report = Report("unhandled exception", exc)
|
|
|
|
|
return build_error_response(
|
|
|
|
|
status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
|
|
|
detail="Internal server error",
|
|
|
|
|
error_code=ApiErrors.UNKNOWN,
|
|
|
|
|
report=report.format_verbose(),
|
|
|
|
|
trace=tb,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2022-06-10 19:45:55 +02:00
|
|
|
async def _set_startup_status(
|
2024-02-22 20:43:15 +01:00
|
|
|
bitcoin: None | StartupState = None,
|
|
|
|
|
bitcoin_msg: None | str = None,
|
|
|
|
|
lightning: None | StartupState = None,
|
|
|
|
|
lightning_msg: None | str = None,
|
2022-06-10 19:45:55 +02:00
|
|
|
):
|
|
|
|
|
# We must know when both bitcoin and lightning are initialized
|
|
|
|
|
# to trigger the warmup method for new SSE clients
|
|
|
|
|
if bitcoin is not None:
|
|
|
|
|
api_startup_status.bitcoin = bitcoin
|
|
|
|
|
if bitcoin_msg is not None:
|
|
|
|
|
api_startup_status.bitcoin_msg = bitcoin_msg
|
|
|
|
|
if lightning is not None:
|
|
|
|
|
api_startup_status.lightning = lightning
|
|
|
|
|
if lightning_msg is not None:
|
|
|
|
|
api_startup_status.lightning_msg = lightning_msg
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
|
2026-07-03 22:34:44 +02:00
|
|
|
asyncio.create_task(warmup_new_connections())
|
2026-07-11 12:07:21 +02:00
|
|
|
await broadcast_msg(Event.SYSTEM_STARTUP_INFO, api_startup_status.model_dump())
|
2022-06-10 19:45:55 +02:00
|
|
|
|
|
|
|
|
|
2022-12-18 20:55:31 +01:00
|
|
|
@logger.catch
|
2022-06-10 19:45:55 +02:00
|
|
|
async def _initialize_bitcoin():
|
|
|
|
|
await _set_startup_status(bitcoin=StartupState.OFFLINE)
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
await initialize_bitcoin_repo()
|
|
|
|
|
await register_bitcoin_zmq_sub()
|
|
|
|
|
await register_bitcoin_status_gatherer()
|
2022-06-10 19:45:55 +02:00
|
|
|
await _set_startup_status(bitcoin=StartupState.DONE)
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
|
|
|
|
|
|
2022-12-18 20:55:31 +01:00
|
|
|
@logger.catch
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
async def _initialize_lightning():
|
2022-12-01 18:51:03 +01:00
|
|
|
if node_type == "none":
|
2022-06-08 20:39:12 +02:00
|
|
|
api_startup_status.lightning = StartupState.DISABLED
|
|
|
|
|
api_startup_status.lightning_msg = ""
|
2022-06-10 19:45:55 +02:00
|
|
|
await _set_startup_status(lightning=StartupState.DISABLED)
|
2022-12-18 20:55:31 +01:00
|
|
|
logger.info("Lightning node is disabled, skipping initialization")
|
2022-06-08 20:39:12 +02:00
|
|
|
return
|
|
|
|
|
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
try:
|
|
|
|
|
async for u in initialize_ln_repo():
|
2022-06-10 19:45:55 +02:00
|
|
|
ln_status = None
|
|
|
|
|
ln_msg = None
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
changed = False
|
|
|
|
|
if (
|
|
|
|
|
u.state == LnInitState.OFFLINE
|
|
|
|
|
and api_startup_status.lightning != StartupState.OFFLINE
|
|
|
|
|
):
|
2022-06-10 19:45:55 +02:00
|
|
|
ln_status = StartupState.OFFLINE
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
changed = True
|
|
|
|
|
elif (
|
|
|
|
|
u.state == LnInitState.BOOTSTRAPPING
|
|
|
|
|
and api_startup_status.lightning != StartupState.BOOTSTRAPPING
|
|
|
|
|
):
|
2022-06-10 19:45:55 +02:00
|
|
|
ln_status = StartupState.BOOTSTRAPPING
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
changed = True
|
|
|
|
|
elif (
|
|
|
|
|
u.state == LnInitState.LOCKED
|
|
|
|
|
and api_startup_status.lightning != StartupState.LOCKED
|
|
|
|
|
):
|
2022-06-10 19:45:55 +02:00
|
|
|
ln_status = StartupState.LOCKED
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
changed = True
|
2022-11-26 14:03:52 +01:00
|
|
|
elif (
|
|
|
|
|
u.state == LnInitState.BOOTSTRAPPING_AFTER_UNLOCK
|
|
|
|
|
and api_startup_status != LnInitState.BOOTSTRAPPING_AFTER_UNLOCK
|
|
|
|
|
):
|
|
|
|
|
ln_status = LnInitState.BOOTSTRAPPING_AFTER_UNLOCK
|
|
|
|
|
changed = True
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
elif (
|
|
|
|
|
u.state == LnInitState.DONE
|
|
|
|
|
and api_startup_status.lightning != StartupState.DONE
|
|
|
|
|
):
|
|
|
|
|
# We've successfully connected to the lightning node
|
|
|
|
|
# We can now register all lightning listeners
|
|
|
|
|
await register_lightning_listener()
|
2022-06-10 19:45:55 +02:00
|
|
|
ln_status = StartupState.DONE
|
|
|
|
|
ln_msg = ""
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
|
|
if api_startup_status.lightning_msg != u.msg:
|
2022-06-10 19:45:55 +02:00
|
|
|
ln_msg = u.msg
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
|
|
if changed:
|
2022-06-10 19:45:55 +02:00
|
|
|
await _set_startup_status(lightning=ln_status, lightning_msg=ln_msg)
|
|
|
|
|
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
except HTTPException as r:
|
2022-12-18 20:55:31 +01:00
|
|
|
logger.error(r.detail)
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
except NotImplementedError as r:
|
|
|
|
|
raise HTTPException(status.HTTP_501_NOT_IMPLEMENTED, detail=r.args[0])
|
2021-06-22 21:07:15 +02:00
|
|
|
|
|
|
|
|
|
2021-09-07 19:52:57 +02:00
|
|
|
@app.get("/")
|
2021-09-26 18:11:32 +02:00
|
|
|
def index(req: Request):
|
2025-02-16 20:35:21 +01:00
|
|
|
logger.info(req.url)
|
|
|
|
|
p = req.scope.get("root_path")
|
2021-09-26 18:11:32 +02:00
|
|
|
return RedirectResponse(
|
2025-02-16 20:35:21 +01:00
|
|
|
f"{p}/docs",
|
2021-10-02 10:01:17 +02:00
|
|
|
status_code=status.HTTP_307_TEMPORARY_REDIRECT,
|
2021-09-26 18:11:32 +02:00
|
|
|
)
|
2021-07-11 20:32:49 +02:00
|
|
|
|
|
|
|
|
|
2021-10-05 19:36:09 +02:00
|
|
|
new_connections = []
|
2021-09-26 21:26:39 +02:00
|
|
|
|
|
|
|
|
|
2026-07-11 12:07:21 +02:00
|
|
|
async def _send_ws_event(id, event, data):
|
|
|
|
|
return await ws_mgr.send_to_single(id, event, data)
|
2022-09-10 15:48:03 +02:00
|
|
|
|
|
|
|
|
|
2026-07-11 12:07:21 +02:00
|
|
|
@app.websocket("/ws")
|
|
|
|
|
async def stream(websocket: WebSocket):
|
|
|
|
|
conn_id, authed = await ws_mgr.connect(websocket)
|
|
|
|
|
if not authed:
|
|
|
|
|
return # ws_mgr already closed the socket
|
2022-10-22 11:08:54 +02:00
|
|
|
|
2026-07-11 12:07:21 +02:00
|
|
|
new_connections.append(conn_id)
|
|
|
|
|
await _send_ws_event(
|
|
|
|
|
conn_id,
|
|
|
|
|
Event.SYSTEM_STARTUP_INFO,
|
|
|
|
|
jsonable_encoder(api_startup_status.model_dump()),
|
2022-09-10 13:53:09 +02:00
|
|
|
)
|
2026-07-03 22:34:44 +02:00
|
|
|
asyncio.create_task(warmup_new_connections())
|
2021-10-05 19:36:09 +02:00
|
|
|
|
2026-07-11 12:07:21 +02:00
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
# server-push only; receive loop just detects disconnect
|
|
|
|
|
await websocket.receive_text()
|
|
|
|
|
except WebSocketDisconnect:
|
|
|
|
|
ws_mgr.disconnect(conn_id)
|
|
|
|
|
if conn_id in new_connections:
|
|
|
|
|
new_connections.remove(conn_id)
|
2021-06-22 21:07:15 +02:00
|
|
|
|
2021-08-18 19:05:41 +02:00
|
|
|
|
2022-06-12 07:59:40 +02:00
|
|
|
warmup_running = False
|
|
|
|
|
|
|
|
|
|
|
2021-10-05 19:36:09 +02:00
|
|
|
async def warmup_new_connections():
|
2022-06-12 07:59:40 +02:00
|
|
|
# This doesn't keep track of which connection has received
|
|
|
|
|
# which data already, so it may send data twice data to the client
|
|
|
|
|
# when the startup state changes. Especially the hardware info
|
|
|
|
|
# is rather data intensive. This is OK for now, to keep the code simple.
|
2021-10-05 19:36:09 +02:00
|
|
|
|
2024-02-25 09:52:17 +01:00
|
|
|
async def _handle(id, event, res):
|
2025-04-01 20:42:55 +02:00
|
|
|
match res:
|
|
|
|
|
case BaseModel():
|
2026-07-11 12:07:21 +02:00
|
|
|
return await _send_ws_event(id, event, res.model_dump())
|
2025-04-01 20:42:55 +02:00
|
|
|
case dict() | list():
|
2026-07-11 12:07:21 +02:00
|
|
|
return await _send_ws_event(id, event, res)
|
2025-04-01 20:42:55 +02:00
|
|
|
case Ok(data) if data and isinstance(data, BaseModel):
|
2026-07-11 12:07:21 +02:00
|
|
|
return await _send_ws_event(id, event, data.model_dump())
|
2025-04-01 20:42:55 +02:00
|
|
|
case Ok(data) if not data:
|
|
|
|
|
logger.debug(f"No data to send for warmup event {event}")
|
|
|
|
|
return
|
2026-07-12 17:36:08 +02:00
|
|
|
case HTTPException():
|
|
|
|
|
# A data source failed while gathering warmup data. The
|
|
|
|
|
# underlying error is already logged in _convert_warmup_exceptions;
|
|
|
|
|
# forward a clear error so the client isn't left waiting for this
|
|
|
|
|
# event.
|
|
|
|
|
logger.info(f"Error while fetching warmup data for {event}: {res}")
|
|
|
|
|
return await _send_ws_event(id, event, {"error": f"{res}"})
|
|
|
|
|
case Err(report):
|
|
|
|
|
logger.error(
|
|
|
|
|
f"Error while fetching warmup data for {event}: {report.format()}"
|
|
|
|
|
)
|
|
|
|
|
return await _send_ws_event(id, event, {"error": report.format()})
|
|
|
|
|
case _:
|
2025-04-01 20:42:55 +02:00
|
|
|
logger.warning(
|
|
|
|
|
f"Got unknown data type while handling warmup "
|
|
|
|
|
f"data {event}: {type(res)}"
|
|
|
|
|
)
|
2026-07-12 17:36:08 +02:00
|
|
|
return await _send_ws_event(id, event, {"error": f"{res}"})
|
2024-02-25 09:52:17 +01:00
|
|
|
|
2022-06-12 07:59:40 +02:00
|
|
|
global new_connections
|
|
|
|
|
if len(new_connections) == 0:
|
|
|
|
|
return
|
2021-10-05 19:36:09 +02:00
|
|
|
|
2022-06-12 07:59:40 +02:00
|
|
|
global warmup_running
|
|
|
|
|
if warmup_running:
|
2022-12-18 20:55:31 +01:00
|
|
|
logger.debug("Warmup already running, skipping")
|
2022-06-12 07:59:40 +02:00
|
|
|
return
|
2021-10-05 19:36:09 +02:00
|
|
|
|
2022-06-12 07:59:40 +02:00
|
|
|
warmup_running = True
|
2026-07-03 15:28:57 +02:00
|
|
|
try:
|
|
|
|
|
is_ready = api_startup_status.is_fully_initialized()
|
|
|
|
|
|
|
|
|
|
if is_ready:
|
|
|
|
|
# when lightning is active
|
|
|
|
|
if node_type != "" and node_type != "none":
|
|
|
|
|
res = await get_full_client_warmup_data()
|
|
|
|
|
for id in new_connections:
|
|
|
|
|
await asyncio.gather(
|
|
|
|
|
*[
|
2026-07-11 12:07:21 +02:00
|
|
|
_handle(id, Event.SYSTEM_INFO, res[0]),
|
|
|
|
|
_handle(id, Event.BTC_INFO, res[1]),
|
|
|
|
|
_handle(id, Event.LN_INFO, res[2]),
|
|
|
|
|
_handle(id, Event.LN_FEE_REVENUE, res[3]),
|
|
|
|
|
_handle(id, Event.WALLET_BALANCE, res[4]),
|
|
|
|
|
_handle(id, Event.APP_STATE_MESSAGE, res[5]),
|
|
|
|
|
_handle(id, Event.HARDWARE_INFO, res[6]),
|
2026-07-03 15:28:57 +02:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# when its bitcoin only
|
|
|
|
|
else:
|
|
|
|
|
res = await get_full_client_warmup_data_bitcoinonly()
|
|
|
|
|
for id in new_connections:
|
|
|
|
|
await asyncio.gather(
|
|
|
|
|
*[
|
2026-07-11 12:07:21 +02:00
|
|
|
_handle(id, Event.SYSTEM_INFO, res[0]),
|
|
|
|
|
_handle(id, Event.BTC_INFO, res[1]),
|
|
|
|
|
_handle(id, Event.APP_STATE_MESSAGE, res[2]),
|
|
|
|
|
_handle(id, Event.HARDWARE_INFO, res[3]),
|
2026-07-03 15:28:57 +02:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
new_connections.clear()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
api_startup_status.bitcoin == StartupState.DONE
|
|
|
|
|
and api_startup_status.lightning != StartupState.DONE
|
|
|
|
|
):
|
|
|
|
|
res = await get_bitcoin_client_warmup_data()
|
2022-09-10 15:48:03 +02:00
|
|
|
for id in new_connections:
|
2022-06-16 15:30:14 +02:00
|
|
|
await asyncio.gather(
|
|
|
|
|
*[
|
2026-07-11 12:07:21 +02:00
|
|
|
_handle(id, Event.BTC_INFO, res[0]),
|
|
|
|
|
_handle(id, Event.HARDWARE_INFO, res[1]),
|
2022-06-16 15:30:14 +02:00
|
|
|
]
|
|
|
|
|
)
|
2025-02-16 20:35:21 +01:00
|
|
|
|
2026-07-03 15:28:57 +02:00
|
|
|
# don't clear new_connections,
|
|
|
|
|
# we'll try again later when api is initialized
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
api_startup_status.bitcoin != StartupState.DONE
|
|
|
|
|
and api_startup_status.lightning != StartupState.DONE
|
|
|
|
|
):
|
|
|
|
|
# send only the most minimal available data without
|
|
|
|
|
# Bitcoin Core and Lightning running
|
|
|
|
|
res = await get_hardware_info()
|
2022-09-10 15:48:03 +02:00
|
|
|
for id in new_connections:
|
2026-07-11 12:07:21 +02:00
|
|
|
await _send_ws_event(id, Event.HARDWARE_INFO, res)
|
2022-06-12 07:59:40 +02:00
|
|
|
|
2026-07-03 15:28:57 +02:00
|
|
|
# don't clear new_connections,
|
|
|
|
|
# we'll try again later when api is initialized
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# never let an error escape: this runs as a fire-and-forget task and
|
|
|
|
|
# a stuck warmup_running flag would starve all future SSE clients
|
|
|
|
|
logger.exception(f"Error during warmup of new SSE connections: {e}")
|
|
|
|
|
finally:
|
|
|
|
|
warmup_running = False
|
2021-10-05 19:36:09 +02:00
|
|
|
|
|
|
|
|
|
2021-11-24 11:12:10 +01:00
|
|
|
register_handlers_finished = False
|
|
|
|
|
|
|
|
|
|
|
2022-09-10 13:53:09 +02:00
|
|
|
async def register_all_handlers():
|
2021-11-24 11:12:10 +01:00
|
|
|
global register_handlers_finished
|
|
|
|
|
|
|
|
|
|
if register_handlers_finished:
|
|
|
|
|
raise RuntimeError("register_all_handlers() must not be called twice.")
|
|
|
|
|
|
2025-04-01 20:42:55 +02:00
|
|
|
await register_app_status_update_handlers()
|
refactor: improve startup procedure
During startup the API will try to connect to Bitcoin Core and the
Lightning Node. If it can't connect it will check every "n" seconds
(currently 2s) and connect when available. A new SSE event
called "system_startup_info" is introduced. This event contains
all startup status information during the startup procedure.
The old wallet_locked event is obsolete.
Sample:
--------------------------
event: system_startup_info
data: {"bitcoin": "offline", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "offline", "lightning_msg": "Unable to connect to LND daemon, waiting..."}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "locked", "lightning_msg": "Wallet locked, unlock it to enable full RPC access"}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "done", "lightning_msg": ""}
--------------------------
event: system_startup_info
data: {"bitcoin": "done", "bitcoin_msg": "", "lightning": "bootstraping", "lightning_msg": "RPC not yet available"}
--------------------------
refs #97
2022-06-06 19:29:21 +02:00
|
|
|
await register_hardware_info_gatherer()
|
2021-09-26 21:26:39 +02:00
|
|
|
|
2021-11-24 11:12:10 +01:00
|
|
|
register_handlers_finished = True
|