diff --git a/e-voting-system/validator/validator.py b/e-voting-system/validator/validator.py index c6b9aac..b2a0818 100644 --- a/e-voting-system/validator/validator.py +++ b/e-voting-system/validator/validator.py @@ -411,6 +411,38 @@ class PoAValidator: except Exception as e: logger.error(f"Error creating block: {e}") + async def broadcast_transaction(self, transaction: Transaction) -> None: + """Broadcast transaction to all peers""" + if not self.peer_connections: + logger.debug("No peers to broadcast transaction to") + return + + payload = json.dumps({ + "type": "new_transaction", + "transaction": { + "voter_id": transaction.voter_id, + "election_id": transaction.election_id, + "encrypted_vote": transaction.encrypted_vote, + "ballot_hash": transaction.ballot_hash, + "proof": transaction.proof, + "timestamp": transaction.timestamp + } + }) + + async with aiohttp.ClientSession() as session: + for node_id, peer_url in self.peer_connections.items(): + try: + async with session.post( + f"{peer_url}/p2p/new_transaction", + data=payload, + headers={"Content-Type": "application/json"}, + timeout=aiohttp.ClientTimeout(total=5) + ) as resp: + if resp.status == 200: + logger.debug(f"Transaction broadcast to {node_id}") + except Exception as e: + logger.debug(f"Failed to broadcast transaction to {node_id}: {e}") + async def broadcast_block(self, block: Block) -> None: """Broadcast block to all peers""" if not self.peer_connections: @@ -586,6 +618,9 @@ async def handle_json_rpc(request: dict): transaction = Transaction(**tx_dict) tx_id = validator.add_transaction(transaction) + # Broadcast transaction to other validators + asyncio.create_task(validator.broadcast_transaction(transaction)) + return { "jsonrpc": "2.0", "result": tx_id,