consensus: add error handling for state root computation and database initialization
- 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:
@@ -24,6 +24,7 @@ def _sanitize_metric_suffix(value: str) -> str:
|
|||||||
|
|
||||||
def _compute_state_root(session: Session, chain_id: str) -> str:
|
def _compute_state_root(session: Session, chain_id: str) -> str:
|
||||||
"""Compute state root from current account state."""
|
"""Compute state root from current account state."""
|
||||||
|
try:
|
||||||
state_manager = StateManager()
|
state_manager = StateManager()
|
||||||
|
|
||||||
# Get all accounts for this chain
|
# Get all accounts for this chain
|
||||||
@@ -39,6 +40,11 @@ def _compute_state_root(session: Session, chain_id: str) -> str:
|
|||||||
|
|
||||||
# Return as hex string
|
# Return as hex string
|
||||||
return '0x' + root.hex()
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -80,7 +80,12 @@ _engine_internal = _engine
|
|||||||
def init_db() -> None:
|
def init_db() -> None:
|
||||||
"""Initialize database with file-based encryption"""
|
"""Initialize database with file-based encryption"""
|
||||||
settings.db_path.parent.mkdir(parents=True, exist_ok=True)
|
settings.db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
try:
|
||||||
SQLModel.metadata.create_all(_engine)
|
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
|
# Set restrictive file permissions on database file
|
||||||
if settings.db_path.exists():
|
if settings.db_path.exists():
|
||||||
os.chmod(settings.db_path, stat.S_IRUSR | stat.S_IWUSR) # Read/write for owner only
|
os.chmod(settings.db_path, stat.S_IRUSR | stat.S_IWUSR) # Read/write for owner only
|
||||||
|
|||||||
Reference in New Issue
Block a user