Files
aitbc/apps/blockchain-node/src/aitbc_chain/combined_main.py
aitbc 337c143e25
Some checks failed
Multi-Node Blockchain Health Monitoring / health-check (push) Successful in 2s
P2P Network Verification / p2p-verification (push) Successful in 1s
Python Tests / test-python (push) Successful in 8s
Security Scanning / security-scan (push) Successful in 58s
Blockchain Synchronization Verification / sync-verification (push) Failing after 2s
Integration Tests / test-service-integration (push) Failing after 9s
fix: add nosec B104 comment to suppress Bandit warning
- Add # nosec B104 comment for host="0.0.0.0" in combined_main.py
- Binding to all interfaces is intentional for blockchain node service
- Resolves security-scanning CI failure
2026-04-24 12:13:38 +02:00

91 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""
Combined blockchain node and P2P service launcher
Runs both the main blockchain node, P2P placeholder service, and HTTP RPC server
"""
import asyncio
import logging
import sys
from pathlib import Path
# Add src to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from aitbc_chain.main import BlockchainNode, _run as run_node
from aitbc_chain.config import settings
from aitbc_chain.app import create_app
import uvicorn
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class CombinedService:
def __init__(self):
self._tasks = []
self._http_server = None
async def start(self):
"""Start both blockchain node and HTTP RPC server"""
logger.info("Starting combined blockchain service")
# Start blockchain node in background
node_task = asyncio.create_task(run_node())
self._tasks.append(node_task)
# Start HTTP RPC server in background
app = create_app()
config = uvicorn.Config(
app,
host="0.0.0.0", # nosec B104 - binding to all interfaces is intentional for blockchain node
port=8005,
log_level="info"
)
self._http_server = uvicorn.Server(config)
http_task = asyncio.create_task(self._http_server.serve())
self._tasks.append(http_task)
logger.info("Combined service started - Node on mainnet, RPC server on port 8005")
try:
# Wait for any task to complete (should not happen in normal operation)
await asyncio.gather(*self._tasks, return_exceptions=True)
except KeyboardInterrupt:
logger.info("Received keyboard interrupt")
finally:
await self.stop()
async def stop(self):
"""Stop all services"""
logger.info("Stopping combined blockchain service")
# Shutdown HTTP server if running
if self._http_server:
self._http_server.should_exit = True
# Cancel all tasks
for task in self._tasks:
if not task.done():
task.cancel()
# Wait for tasks to complete
if self._tasks:
await asyncio.gather(*self._tasks, return_exceptions=True)
self._tasks.clear()
logger.info("Combined service stopped")
async def main():
"""Main entry point"""
service = CombinedService()
try:
await service.start()
except KeyboardInterrupt:
logger.info("Received keyboard interrupt")
finally:
await service.stop()
if __name__ == "__main__":
asyncio.run(main())