consensus: add error handling for state root computation and database initialization
Some checks failed
Integration Tests / test-service-integration (push) Has been cancelled
Python Tests / test-python (push) Has been cancelled
Security Scanning / security-scan (push) Has been cancelled

- Wrap _compute_state_root in try/except to handle failures during genesis block creation
- Return None and log warning when state root computation fails (e.g., when accounts don't exist yet)
- Add error handling in init_db to ignore "already exists" errors when creating tables
- Improve robustness of database initialization for concurrent or repeated startup scenarios
This commit is contained in:
aitbc
2026-04-13 19:35:09 +02:00
parent b74dfd76e3
commit 4a7936d201
2 changed files with 27 additions and 16 deletions

View File

@@ -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