2021-07-19 20:12:46 +02:00
|
|
|
import asyncio
|
|
|
|
|
|
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
|
|
|
from decouple import config as dconfig
|
2022-10-22 11:08:54 +02:00
|
|
|
from fastapi import FastAPI, Request
|
2021-10-27 19:23:21 +02:00
|
|
|
from fastapi.encoders import jsonable_encoder
|
2021-11-24 11:12:10 +01:00
|
|
|
from fastapi.exceptions import HTTPException
|
2021-09-05 08:56:53 +02:00
|
|
|
from fastapi_plugins import (
|
|
|
|
|
RedisSettings,
|
|
|
|
|
get_config,
|
|
|
|
|
redis_plugin,
|
|
|
|
|
registered_configuration,
|
|
|
|
|
)
|
2022-12-18 20:55:31 +01:00
|
|
|
from loguru import logger
|
2021-07-19 20:12:46 +02:00
|
|
|
from starlette import status
|
2021-10-05 19:36:09 +02:00
|
|
|
from starlette.middleware.cors import CORSMiddleware
|
2021-09-26 18:11:32 +02:00
|
|
|
from starlette.responses import RedirectResponse
|
2021-07-10 17:15:02 +02:00
|
|
|
|
2023-04-05 22:11:29 +02:00
|
|
|
from app.api.db.database import Database
|
2022-10-03 20:22:00 +02:00
|
|
|
from app.api.models import ApiStartupStatus, StartupState
|
|
|
|
|
from app.api.utils import SSE, broadcast_sse_msg, build_sse_event, sse_mgr
|
|
|
|
|
from app.api.warmup import (
|
|
|
|
|
get_bitcoin_client_warmup_data,
|
|
|
|
|
get_full_client_warmup_data,
|
|
|
|
|
get_full_client_warmup_data_bitcoinonly,
|
|
|
|
|
)
|
|
|
|
|
from app.apps.router import router as app_router
|
2022-07-25 18:07:35 +02:00
|
|
|
from app.auth.auth_bearer import JWTBearer
|
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,
|
|
|
|
|
)
|
2022-10-03 20:22:00 +02:00
|
|
|
from app.external.fastapi_versioning import VersionedFastAPI
|
|
|
|
|
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
|
|
|
|
2022-12-01 18:51:03 +01:00
|
|
|
node_type = dconfig("ln_node").lower()
|
|
|
|
|
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__)
|
|
|
|
|
|
|
|
|
|
|
2021-10-02 10:01:17 +02:00
|
|
|
unversioned_app = FastAPI()
|
2021-06-22 21:07:15 +02:00
|
|
|
config = get_config()
|
2021-06-10 21:13:26 +02:00
|
|
|
|
2022-10-03 20:22:00 +02:00
|
|
|
unversioned_app.include_router(app_router)
|
|
|
|
|
unversioned_app.include_router(bitcoin_router)
|
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
|
|
|
if node_type != "none":
|
2022-10-03 20:22:00 +02:00
|
|
|
unversioned_app.include_router(ln_router)
|
|
|
|
|
unversioned_app.include_router(system_router)
|
|
|
|
|
if setup_router is not None:
|
|
|
|
|
unversioned_app.include_router(setup_router)
|
2021-06-10 21:13:26 +02:00
|
|
|
|
|
|
|
|
|
2021-09-07 19:52:57 +02:00
|
|
|
app = VersionedFastAPI(
|
2021-10-02 10:01:17 +02:00
|
|
|
unversioned_app,
|
|
|
|
|
version_format="{major}",
|
|
|
|
|
prefix_format="/v{major}",
|
|
|
|
|
enable_latest=True,
|
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
|
|
|
|
2021-09-05 08:56:53 +02:00
|
|
|
@app.on_event("startup")
|
2021-09-07 19:52:57 +02:00
|
|
|
async def on_startup():
|
2021-06-22 21:07:15 +02:00
|
|
|
await redis_plugin.init_app(app, config=config)
|
|
|
|
|
await redis_plugin.init()
|
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
|
|
|
register_cookie_updater()
|
2022-09-10 15:52:11 +02:00
|
|
|
await broadcast_sse_msg(SSE.SYSTEM_STARTUP_INFO, api_startup_status.dict())
|
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
|
|
|
|
2023-04-05 22:11:29 +02:00
|
|
|
db = Database(dconfig("db_url"))
|
|
|
|
|
await db.initialize()
|
|
|
|
|
|
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
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
loop.create_task(_initialize_bitcoin())
|
2022-06-08 20:39:12 +02:00
|
|
|
loop.create_task(_initialize_lightning())
|
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-09-10 13:53:09 +02:00
|
|
|
await register_all_handlers()
|
2021-11-28 08:42:52 +01:00
|
|
|
handle_local_cookie()
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
api_startup_status = ApiStartupStatus()
|
|
|
|
|
|
|
|
|
|
|
2022-06-10 19:45:55 +02:00
|
|
|
async def _set_startup_status(
|
|
|
|
|
bitcoin: StartupState = None,
|
|
|
|
|
bitcoin_msg: str = None,
|
|
|
|
|
lightning: StartupState = None,
|
|
|
|
|
lightning_msg: str = None,
|
|
|
|
|
):
|
|
|
|
|
# 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
|
|
|
|
2022-06-12 07:59:40 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
loop.create_task(warmup_new_connections())
|
2022-09-10 15:52:11 +02:00
|
|
|
await broadcast_sse_msg(SSE.SYSTEM_STARTUP_INFO, api_startup_status.dict())
|
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-05 08:56:53 +02:00
|
|
|
@app.on_event("shutdown")
|
2021-06-22 21:07:15 +02:00
|
|
|
async def on_shutdown() -> None:
|
|
|
|
|
await redis_plugin.terminate()
|
2021-11-28 08:42:52 +01:00
|
|
|
remove_local_cookie()
|
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):
|
|
|
|
|
return RedirectResponse(
|
2021-10-02 10:01:17 +02:00
|
|
|
req.url_for("latest", path="docs"),
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-09-10 15:48:03 +02:00
|
|
|
def _send_sse_event(id, event, data):
|
|
|
|
|
return sse_mgr.send_to_single(id, build_sse_event(event, data))
|
|
|
|
|
|
|
|
|
|
|
2022-10-19 21:32:47 +02:00
|
|
|
@app.get(
|
|
|
|
|
"/sse/subscribe",
|
|
|
|
|
status_code=status.HTTP_200_OK,
|
|
|
|
|
)
|
2021-09-26 21:26:39 +02:00
|
|
|
async def stream(request: Request):
|
2022-10-22 11:08:54 +02:00
|
|
|
token = request.cookies.get("access_token")
|
|
|
|
|
if not token:
|
|
|
|
|
# No token in cookies found, try to get it from the Authorization header
|
|
|
|
|
token = request.headers.get("authorization")
|
|
|
|
|
|
|
|
|
|
if not token:
|
|
|
|
|
# Raise an exception as there is not token found
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
|
detail="No authorization code.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
token = token.replace("Bearer ", "")
|
|
|
|
|
if not JWTBearer().verify_jwt(jwtoken=token):
|
|
|
|
|
# token is invalid
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
|
detail="Invalid authorization code.",
|
|
|
|
|
)
|
|
|
|
|
|
2022-09-10 15:48:03 +02:00
|
|
|
event_source, id = sse_mgr.add_connection(request)
|
|
|
|
|
new_connections.append(id)
|
2022-05-05 13:27:59 +02:00
|
|
|
|
2022-09-10 15:48:03 +02:00
|
|
|
await _send_sse_event(
|
|
|
|
|
id, SSE.SYSTEM_STARTUP_INFO, jsonable_encoder(api_startup_status.dict())
|
2022-09-10 13:53:09 +02:00
|
|
|
)
|
2021-11-30 19:57:17 +01:00
|
|
|
|
2022-06-12 07:59:40 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
loop.create_task(warmup_new_connections())
|
2021-10-05 19:36:09 +02:00
|
|
|
|
2022-09-10 15:48:03 +02:00
|
|
|
return event_source
|
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
|
|
|
|
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
|
|
|
|
|
is_ready = api_startup_status.is_fully_initialized()
|
|
|
|
|
|
|
|
|
|
if is_ready:
|
2022-06-16 15:30:14 +02:00
|
|
|
# when lightning is active
|
|
|
|
|
if node_type != "" and node_type != "none":
|
|
|
|
|
res = await get_full_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(
|
|
|
|
|
*[
|
2022-09-10 15:48:03 +02:00
|
|
|
_send_sse_event(id, SSE.SYSTEM_INFO, res[0].dict()),
|
|
|
|
|
_send_sse_event(id, SSE.BTC_INFO, res[1].dict()),
|
|
|
|
|
_send_sse_event(id, SSE.LN_INFO, res[2].dict()),
|
|
|
|
|
_send_sse_event(id, SSE.LN_INFO_LITE, res[3].dict()),
|
|
|
|
|
_send_sse_event(id, SSE.LN_FEE_REVENUE, res[4]),
|
|
|
|
|
_send_sse_event(id, SSE.WALLET_BALANCE, res[5].dict()),
|
2022-10-22 14:17:13 +02:00
|
|
|
_send_sse_event(id, SSE.INSTALLED_APP_STATUS, res[6]),
|
|
|
|
|
_send_sse_event(id, SSE.HARDWARE_INFO, res[7]),
|
2022-06-16 15:30:14 +02:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# when its bitcoin only
|
|
|
|
|
else:
|
|
|
|
|
res = await get_full_client_warmup_data_bitcoinonly()
|
2022-09-10 15:48:03 +02:00
|
|
|
for id in new_connections:
|
2022-06-16 15:30:14 +02:00
|
|
|
await asyncio.gather(
|
|
|
|
|
*[
|
2022-09-10 15:48:03 +02:00
|
|
|
_send_sse_event(id, SSE.SYSTEM_INFO, res[0].dict()),
|
|
|
|
|
_send_sse_event(id, SSE.BTC_INFO, res[1].dict()),
|
|
|
|
|
_send_sse_event(id, SSE.HARDWARE_INFO, res[2]),
|
2022-06-16 15:30:14 +02:00
|
|
|
]
|
|
|
|
|
)
|
2022-06-12 07:59:40 +02:00
|
|
|
|
|
|
|
|
new_connections.clear()
|
|
|
|
|
|
|
|
|
|
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-12 07:59:40 +02:00
|
|
|
await asyncio.gather(
|
|
|
|
|
*[
|
2022-09-10 15:48:03 +02:00
|
|
|
_send_sse_event(id, SSE.BTC_INFO, res[0].dict()),
|
|
|
|
|
_send_sse_event(id, SSE.HARDWARE_INFO, res[1]),
|
2022-06-12 07:59:40 +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:
|
|
|
|
|
await _send_sse_event(id, SSE.HARDWARE_INFO, res),
|
2022-06-12 07:59:40 +02:00
|
|
|
|
|
|
|
|
# don't clear new_connections, we'll try again later when api is initialized
|
|
|
|
|
|
|
|
|
|
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.")
|
|
|
|
|
|
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
|