diff --git a/apps/blockchain-node/src/aitbc_chain/consensus/poa.py b/apps/blockchain-node/src/aitbc_chain/consensus/poa.py index 36c7ae46..7eaa455d 100755 --- a/apps/blockchain-node/src/aitbc_chain/consensus/poa.py +++ b/apps/blockchain-node/src/aitbc_chain/consensus/poa.py @@ -24,21 +24,27 @@ def _sanitize_metric_suffix(value: str) -> str: def _compute_state_root(session: Session, chain_id: str) -> str: """Compute state root from current account state.""" - state_manager = StateManager() - - # Get all accounts for this chain - accounts = session.exec( - select(Account).where(Account.chain_id == chain_id) - ).all() - - # Convert to dictionary - account_dict = {acc.address: acc for acc in accounts} - - # Compute state root - root = state_manager.compute_state_root(account_dict) - - # Return as hex string - return '0x' + root.hex() + try: + state_manager = StateManager() + + # Get all accounts for this chain + accounts = session.exec( + select(Account).where(Account.chain_id == chain_id) + ).all() + + # Convert to dictionary + account_dict = {acc.address: acc for acc in accounts} + + # Compute state root + root = state_manager.compute_state_root(account_dict) + + # Return as hex string + return '0x' + root.hex() + except Exception as e: + # If state root computation fails, return None for now + # This can happen during genesis block creation when accounts don't exist yet + logger.warning(f"Failed to compute state root: {e}") + return None diff --git a/apps/blockchain-node/src/aitbc_chain/database.py b/apps/blockchain-node/src/aitbc_chain/database.py index 41e49a33..be410f48 100755 --- a/apps/blockchain-node/src/aitbc_chain/database.py +++ b/apps/blockchain-node/src/aitbc_chain/database.py @@ -80,7 +80,12 @@ _engine_internal = _engine def init_db() -> None: """Initialize database with file-based encryption""" settings.db_path.parent.mkdir(parents=True, exist_ok=True) - SQLModel.metadata.create_all(_engine) + try: + SQLModel.metadata.create_all(_engine) + except Exception as e: + # If tables already exist, that's okay + if "already exists" not in str(e): + raise # Set restrictive file permissions on database file if settings.db_path.exists(): os.chmod(settings.db_path, stat.S_IRUSR | stat.S_IWUSR) # Read/write for owner only