diff --git a/apps/blockchain-node/scripts/unified_genesis.py b/apps/blockchain-node/scripts/unified_genesis.py index bd100597..5c8de295 100644 --- a/apps/blockchain-node/scripts/unified_genesis.py +++ b/apps/blockchain-node/scripts/unified_genesis.py @@ -123,7 +123,7 @@ def create_genesis_wallet(password: str = None, chain_id: str = "ait-mainnet") - def create_genesis_block(chain_id: str, proposer: str, timestamp: datetime = None) -> Dict[str, Any]: """Create genesis block""" if not timestamp: - timestamp = datetime.fromisoformat("2025-01-01 00:00:00") + timestamp = datetime.now(timezone.utc) parent_hash = "0x00" genesis_hash = compute_block_hash(0, parent_hash, timestamp, chain_id) diff --git a/apps/blockchain-node/src/aitbc_chain/consensus/poa.py b/apps/blockchain-node/src/aitbc_chain/consensus/poa.py index 676f725e..825d83ac 100755 --- a/apps/blockchain-node/src/aitbc_chain/consensus/poa.py +++ b/apps/blockchain-node/src/aitbc_chain/consensus/poa.py @@ -111,8 +111,7 @@ class PoAProposer: def _fetch_chain_head(self) -> Optional[Block]: """Fetch the current chain head block from the database.""" - from ..database import session_scope - with session_scope(self._config.chain_id) as session: + with self._session_factory() as session: return session.exec( select(Block).where(Block.chain_id == self._config.chain_id).order_by(Block.height.desc()).limit(1) ).first() @@ -442,7 +441,7 @@ class PoAProposer: if genesis_hash and genesis_state_root: # Create genesis block with RPC-provided data - timestamp = datetime(2025, 1, 1, 0, 0, 0) + timestamp = datetime.now(timezone.utc) genesis = Block( chain_id=self._config.chain_id, height=0, @@ -473,8 +472,8 @@ class PoAProposer: # Fall back to local genesis block creation self._logger.info(f"Creating genesis block locally for chain {self._config.chain_id}") - # Use a deterministic genesis timestamp so all nodes agree on the genesis block hash - timestamp = datetime(2025, 1, 1, 0, 0, 0) + # Use current timestamp for genesis block + timestamp = datetime.now(timezone.utc) block_hash = self._compute_block_hash(0, "0x00", timestamp) # Check if block with this hash already exists (duplicate check) @@ -607,7 +606,7 @@ class PoAProposer: peer_url = peer_url.replace("http://", "") peer_url = f"http://{peer_url}" trusted_peers.append(peer_url) - trusted_peers.append("http://localhost:8006") + # Don't add localhost as default bootstrap peer - hub nodes should create their own genesis self._logger.info(f"Attempting RPC bootstrap for genesis block from peers: {trusted_peers}") @@ -647,7 +646,7 @@ class PoAProposer: peer_url = peer_url.replace("http://", "") peer_url = f"http://{peer_url}" trusted_peers.append(peer_url) - trusted_peers.append("http://localhost:8006") + # Don't add localhost as default bootstrap peer - hub nodes should create their own genesis self._logger.info(f"Attempting RPC bootstrap from peers: {trusted_peers}") diff --git a/apps/blockchain-node/src/aitbc_chain/main.py b/apps/blockchain-node/src/aitbc_chain/main.py index 04f6c57a..3ff9bf94 100755 --- a/apps/blockchain-node/src/aitbc_chain/main.py +++ b/apps/blockchain-node/src/aitbc_chain/main.py @@ -138,7 +138,7 @@ class BlockchainNode: async def _ensure_genesis_for_chains(self) -> None: for chain_id in self._supported_chains(): - proposer = PoAProposer(config=self._proposer_config(chain_id), session_factory=lambda: session_scope(chain_id)) + proposer = PoAProposer(config=self._proposer_config(chain_id), session_factory=lambda cid=chain_id: session_scope(cid)) await proposer._ensure_genesis_block() async def _setup_gossip_subscribers(self) -> None: @@ -192,7 +192,7 @@ class BlockchainNode: import json block_data = json.loads(block_data) logger.info(f"Importing block for chain {chain_id_param}: {block_data.get('height')}") - sync = ChainSync(session_factory=lambda: session_scope(chain_id_param), chain_id=chain_id_param) + sync = ChainSync(session_factory=lambda cid=chain_id_param: session_scope(cid), chain_id=chain_id_param) res = sync.import_block(block_data, transactions=block_data.get("transactions")) logger.info(f"Import result: accepted={res.accepted}, reason={res.reason}") @@ -322,7 +322,7 @@ class BlockchainNode: if chain_id in self._proposers: continue - proposer = PoAProposer(config=self._proposer_config(chain_id), session_factory=lambda: session_scope(chain_id)) + proposer = PoAProposer(config=self._proposer_config(chain_id), session_factory=lambda cid=chain_id: session_scope(cid)) self._proposers[chain_id] = proposer asyncio.create_task(proposer.start())