Fix async/sync mismatch in chain export/import RPC endpoints
- Replace async session_scope with synchronous SQLAlchemy session in export_chain - Remove await from session.execute calls in export_chain (use synchronous queries) - Remove await from session.commit in import_chain - Change force_sync from async to sync function - Remove await from import_chain call in force_sync - Add proper session cleanup with try/finally block in export_chain
This commit is contained in:
@@ -677,17 +677,26 @@ async def export_chain(chain_id: str = None) -> Dict[str, Any]:
|
|||||||
chain_id = get_chain_id(chain_id)
|
chain_id = get_chain_id(chain_id)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with session_scope() as session:
|
# Use synchronous database operations in async context
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
from ..database import _db_path
|
||||||
|
|
||||||
|
engine = create_engine(f"sqlite:///{_db_path}")
|
||||||
|
Session = sessionmaker(bind=engine)
|
||||||
|
session = Session()
|
||||||
|
|
||||||
|
try:
|
||||||
# Get all blocks
|
# Get all blocks
|
||||||
blocks_result = await session.execute(select(Block).order_by(Block.height))
|
blocks_result = session.execute(select(Block).order_by(Block.height))
|
||||||
blocks = blocks_result.scalars().all()
|
blocks = blocks_result.scalars().all()
|
||||||
|
|
||||||
# Get all accounts
|
# Get all accounts
|
||||||
accounts_result = await session.execute(select(Account))
|
accounts_result = session.execute(select(Account))
|
||||||
accounts = accounts_result.scalars().all()
|
accounts = accounts_result.scalars().all()
|
||||||
|
|
||||||
# Get all transactions
|
# Get all transactions
|
||||||
txs_result = await session.execute(select(Transaction))
|
txs_result = session.execute(select(Transaction))
|
||||||
transactions = txs_result.scalars().all()
|
transactions = txs_result.scalars().all()
|
||||||
|
|
||||||
# Build export data
|
# Build export data
|
||||||
@@ -738,6 +747,8 @@ async def export_chain(chain_id: str = None) -> Dict[str, Any]:
|
|||||||
"export_data": export_data,
|
"export_data": export_data,
|
||||||
"export_size_bytes": len(json.dumps(export_data))
|
"export_size_bytes": len(json.dumps(export_data))
|
||||||
}
|
}
|
||||||
|
finally:
|
||||||
|
session.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_logger.error(f"Error exporting chain: {e}")
|
_logger.error(f"Error exporting chain: {e}")
|
||||||
raise HTTPException(status_code=500, detail=f"Failed to export chain: {str(e)}")
|
raise HTTPException(status_code=500, detail=f"Failed to export chain: {str(e)}")
|
||||||
@@ -811,7 +822,7 @@ async def import_chain(import_data: dict) -> Dict[str, Any]:
|
|||||||
)
|
)
|
||||||
session.add(tx)
|
session.add(tx)
|
||||||
|
|
||||||
await session.commit()
|
session.commit()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
@@ -827,7 +838,7 @@ async def import_chain(import_data: dict) -> Dict[str, Any]:
|
|||||||
raise HTTPException(status_code=500, detail=f"Failed to import chain: {str(e)}")
|
raise HTTPException(status_code=500, detail=f"Failed to import chain: {str(e)}")
|
||||||
|
|
||||||
@router.post("/force-sync", summary="Force reorg to specified peer")
|
@router.post("/force-sync", summary="Force reorg to specified peer")
|
||||||
async def force_sync(peer_data: dict) -> Dict[str, Any]:
|
def force_sync(peer_data: dict) -> Dict[str, Any]:
|
||||||
"""Force blockchain reorganization to sync with specified peer"""
|
"""Force blockchain reorganization to sync with specified peer"""
|
||||||
try:
|
try:
|
||||||
peer_url = peer_data.get("peer_url")
|
peer_url = peer_data.get("peer_url")
|
||||||
@@ -850,7 +861,7 @@ async def force_sync(peer_data: dict) -> Dict[str, Any]:
|
|||||||
raise HTTPException(status_code=400, detail=f"Peer only has {len(peer_blocks)} blocks, cannot sync to height {target_height}")
|
raise HTTPException(status_code=400, detail=f"Peer only has {len(peer_blocks)} blocks, cannot sync to height {target_height}")
|
||||||
|
|
||||||
# Import peer's chain
|
# Import peer's chain
|
||||||
import_result = await import_chain(peer_chain_data["export_data"])
|
import_result = import_chain(peer_chain_data["export_data"])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
|
|||||||
Reference in New Issue
Block a user