From 7ff5159e94498f5d8e44502786c737e5d70453d0 Mon Sep 17 00:00:00 2001 From: aitbc Date: Mon, 13 Apr 2026 23:35:04 +0200 Subject: [PATCH] Fix chain import to preserve account chain_id and conditionally clear accounts - Add chain_id field to account export data - Use account's chain_id from import data if available, fallback to root chain_id - Only clear existing accounts if import data contains accounts to replace - Prevents clearing accounts when importing chain data without account information --- apps/blockchain-node/src/aitbc_chain/rpc/router.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/blockchain-node/src/aitbc_chain/rpc/router.py b/apps/blockchain-node/src/aitbc_chain/rpc/router.py index 32e9e90f..eda8dc8f 100755 --- a/apps/blockchain-node/src/aitbc_chain/rpc/router.py +++ b/apps/blockchain-node/src/aitbc_chain/rpc/router.py @@ -711,6 +711,7 @@ async def export_chain(chain_id: str = None) -> Dict[str, Any]: ], "accounts": [ { + "chain_id": a.chain_id, "address": a.address, "balance": a.balance, "nonce": a.nonce @@ -773,9 +774,10 @@ async def import_chain(import_data: dict) -> Dict[str, Any]: } _logger.info(f"Backing up existing chain with {existing_count} blocks") - # Clear existing data + # Clear existing data - only clear accounts if we have accounts to import session.execute(delete(Block)) - session.execute(delete(Account)) + if accounts: + session.execute(delete(Account)) session.execute(delete(Transaction)) # Import blocks @@ -795,7 +797,7 @@ async def import_chain(import_data: dict) -> Dict[str, Any]: # Import accounts for account_data in accounts: account = Account( - chain_id=chain_id, + chain_id=account_data.get("chain_id", chain_id), address=account_data["address"], balance=account_data["balance"], nonce=account_data["nonce"]