From ca7da25b9dfd4f858759b2df4314bb4d148514d2 Mon Sep 17 00:00:00 2001 From: aitbc Date: Thu, 9 Apr 2026 14:13:59 +0200 Subject: [PATCH] feat: add mempool initialization to P2P service and increase sync frequency - Added mempool initialization in main() using settings configuration - Added support for database backend with mempool.db path resolution - Added debug logging for mempool sync loop iteration with transaction count - Reduced mempool sync interval from 2 seconds to 1 second for faster propagation - Fixed indentation in mempool sync loop transaction iteration --- .../src/aitbc_chain/p2p_network.py | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/apps/blockchain-node/src/aitbc_chain/p2p_network.py b/apps/blockchain-node/src/aitbc_chain/p2p_network.py index 069e1475..29be3d48 100644 --- a/apps/blockchain-node/src/aitbc_chain/p2p_network.py +++ b/apps/blockchain-node/src/aitbc_chain/p2p_network.py @@ -124,7 +124,8 @@ class P2PNetworkService: import json txs_to_broadcast.append(json.loads(row[1])) - for tx in txs_to_broadcast: + logger.debug(f"Mempool sync loop iteration. txs_to_broadcast: {len(txs_to_broadcast)}") + for tx in txs_to_broadcast: msg = {'type': 'new_transaction', 'tx': tx} writers = list(self.active_connections.values()) for writer in writers: @@ -133,7 +134,7 @@ class P2PNetworkService: except Exception as e: logger.error(f"Error in mempool sync loop: {e}") - await asyncio.sleep(2) + await asyncio.sleep(1) async def _dial_peers_loop(self): """Background loop to continually try connecting to disconnected initial peers""" @@ -387,6 +388,21 @@ def main(): ) try: + from .config import settings + from .mempool import init_mempool + import pathlib + + db_path = "" + if settings.mempool_backend == "database": + db_path = str(settings.db_path.parent / "mempool.db") + + init_mempool( + backend=settings.mempool_backend, + db_path=db_path, + max_size=settings.mempool_max_size, + min_fee=settings.min_fee + ) + asyncio.run(run_p2p_service(args.host, args.port, args.node_id, args.peers)) except KeyboardInterrupt: logger.info("P2P service stopped by user")