fix db connections
This commit is contained in:
1 parent
3a6f787a78
commit
4401916104
4 files changed
+49
-129
No files matched your search
+21
-18
@@ -8,8 +8,10 @@ from tonsdk.utils import Address
|
||||
|
||||
from app.core._blockchain.ton.wallet_v3cr3 import WalletV3CR3
|
||||
from app.core.logger import make_log
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy import create_engine, inspect
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import Optional
|
||||
from app.core.models._config import ServiceConfigValue
|
||||
|
||||
|
||||
def _load_seed_from_env_or_generate() -> bytes:
|
||||
@@ -32,8 +34,8 @@ def _init_seed_via_db() -> bytes:
|
||||
|
||||
def db_ready(conn) -> bool:
|
||||
try:
|
||||
r = conn.execute(text("SELECT to_regclass('public.service_config')")).scalar()
|
||||
return r is not None
|
||||
inspector = inspect(conn)
|
||||
return inspector.has_table('service_config')
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
@@ -46,16 +48,16 @@ def _init_seed_via_db() -> bytes:
|
||||
raise TimeoutError("service_config table not available")
|
||||
|
||||
def read_seed() -> Optional[bytes]:
|
||||
row = conn.execute(text("SELECT packed_value FROM service_config WHERE key = :k LIMIT 1"), {"k": "private_key"}).first()
|
||||
if not row:
|
||||
return None
|
||||
packed = row[0] or {}
|
||||
try:
|
||||
# packed_value is JSON; ensure dict
|
||||
if isinstance(packed, str):
|
||||
packed = json.loads(packed)
|
||||
seed_hex = packed.get("value")
|
||||
return bytes.fromhex(seed_hex) if seed_hex else None
|
||||
with Session(bind=conn) as s:
|
||||
row = s.query(ServiceConfigValue).filter(ServiceConfigValue.key == 'private_key').first()
|
||||
if not row:
|
||||
return None
|
||||
packed = row.packed_value or {}
|
||||
if isinstance(packed, str):
|
||||
packed = json.loads(packed)
|
||||
seed_hex = packed.get('value')
|
||||
return bytes.fromhex(seed_hex) if seed_hex else None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
@@ -67,15 +69,16 @@ def _init_seed_via_db() -> bytes:
|
||||
seed = _load_seed_from_env_or_generate()
|
||||
# Try insert; if another primary raced, ignore
|
||||
try:
|
||||
conn.execute(
|
||||
text("INSERT INTO service_config (key, packed_value) VALUES (:k, CAST(:v AS JSON))"),
|
||||
{"k": "private_key", "v": json.dumps({"value": seed.hex()})}
|
||||
)
|
||||
conn.commit()
|
||||
with Session(bind=conn) as s:
|
||||
s.add(ServiceConfigValue(key='private_key', packed_value={"value": seed.hex()}))
|
||||
s.commit()
|
||||
make_log("HotWallet", "Seed saved in service_config by primary", level='info')
|
||||
return seed
|
||||
except Exception:
|
||||
conn.rollback()
|
||||
try:
|
||||
conn.rollback()
|
||||
except Exception:
|
||||
pass
|
||||
# Read again in case of race
|
||||
seed2 = read_seed()
|
||||
if seed2:
|
||||
|
||||
@@ -5,7 +5,6 @@ from httpx import AsyncClient
|
||||
|
||||
from app.core._config import PROJECT_HOST
|
||||
from app.core._crypto.signer import Signer
|
||||
from app.core._secrets import hot_seed
|
||||
from app.core.logger import make_log
|
||||
|
||||
|
||||
@@ -17,6 +16,8 @@ async def send_status(service: str, status: str):
|
||||
'status': status,
|
||||
}
|
||||
message_bytes = dumps(message).encode()
|
||||
# Lazy import to avoid triggering _secrets before DB is ready
|
||||
from app.core._secrets import hot_seed
|
||||
signer = Signer(hot_seed)
|
||||
message_signature = signer.sign(message_bytes)
|
||||
async with AsyncClient() as client:
|
||||
|
||||
Reference in new issue
Block a user