#!/usr/bin/env node import { OracleNode } from './src/oracle-node.js'; import chalk from 'chalk'; const args = process.argv.slice(2); const options = {}; // Parse command line arguments for (let i = 0; i < args.length; i++) { switch (args[i]) { case '--port': options.port = parseInt(args[++i]); break; case '--id': options.id = args[++i]; break; case '--bootstrap': options.bootstrap = args[++i]; break; case '--position': options.ringPosition = parseInt(args[++i]); break; case '--help': console.log(` ${chalk.yellow('Ring Network Oracle Node')} Usage: node oracle.js [options] Options: --port Port to listen on (default: random) --id Node ID (default: auto-generated) --bootstrap Bootstrap node address (host:port) --position Initial ring position (default: 0) --help Show this help message Examples: node oracle.js --port 8080 node oracle.js --port 8081 --bootstrap localhost:8080 node oracle.js --id oracle1 --port 8082 --bootstrap localhost:8080 Oracle Services: - network-analysis: Analyze network topology and health - data-storage: Distributed data storage with replication - consensus: Consensus mechanisms for network decisions - routing: Advanced routing strategies - health-check: Network health monitoring - network-metrics: Collect and provide network metrics `); process.exit(0); break; } } console.log(chalk.yellow('šŸš€ Starting Ring Network Oracle Node...')); const oracle = new OracleNode(options); // Handle graceful shutdown process.on('SIGINT', async () => { console.log(chalk.yellow('\nšŸ›‘ Received shutdown signal...')); await oracle.destroy(); process.exit(0); }); process.on('SIGTERM', async () => { console.log(chalk.yellow('\nšŸ›‘ Received termination signal...')); await oracle.destroy(); process.exit(0); }); // Connect to bootstrap node if specified if (options.bootstrap) { console.log(chalk.cyan(`šŸ”— Connecting to bootstrap node: ${options.bootstrap}`)); setTimeout(async () => { try { const success = await oracle.joinRing(options.bootstrap); if (success) { console.log(chalk.green('āœ… Successfully joined the ring network as Oracle!')); } else { console.log(chalk.red('āŒ Failed to join the ring network')); } } catch (error) { console.error(chalk.red('Error joining network:'), error.message); } }, 2000); } // Set up event handlers oracle.on('peerConnected', (peerId) => { console.log(chalk.blue(`šŸ‘‹ New peer connected: ${peerId.substring(0, 8)}`)); }); oracle.on('peerDisconnected', (peerId) => { console.log(chalk.red(`šŸ‘‹ Peer disconnected: ${peerId.substring(0, 8)}`)); }); oracle.on('ringMessage', ({ from, payload }) => { console.log(chalk.magenta(`šŸ“Ø Ring message from ${from.substring(0, 8)}: ${JSON.stringify(payload)}`)); }); oracle.on('oracleResponse', ({ from, payload }) => { console.log(chalk.cyan(`šŸ”® Oracle response from ${from.substring(0, 8)}: ${JSON.stringify(payload)}`)); }); // Interactive commands process.stdin.setEncoding('utf8'); process.stdin.on('readable', () => { const chunk = process.stdin.read(); if (chunk !== null) { const command = chunk.trim(); handleCommand(command); } }); async function handleCommand(command) { const [cmd, ...args] = command.split(' '); switch (cmd) { case 'send': if (args.length > 0) { const message = args.join(' '); oracle.sendRingMessage({ type: 'chat', content: message }); console.log(chalk.green(`šŸ“¤ Sent: ${message}`)); } break; case 'info': const info = oracle.getOracleInfo(); console.log(chalk.yellow('\nšŸ”® Oracle Info:')); console.log(JSON.stringify(info, null, 2)); break; case 'peers': const peers = oracle.webrtc.getConnectedPeers(); console.log(chalk.blue(`\nšŸ‘„ Connected Peers (${peers.length}):`)); peers.forEach(peer => { console.log(` - ${peer.substring(0, 8)}...`); }); break; case 'health': const health = oracle.performHealthCheck(); console.log(chalk.green('\nšŸ„ Health Check:')); console.log(JSON.stringify(health, null, 2)); break; case 'metrics': const metrics = oracle.getNetworkMetrics(); console.log(chalk.cyan('\nšŸ“Š Network Metrics:')); console.log(JSON.stringify(metrics, null, 2)); break; case 'analyze': const analysis = oracle.analyzeNetwork(); console.log(chalk.magenta('\nšŸ” Network Analysis:')); console.log(JSON.stringify(analysis, null, 2)); break; case 'store': if (args.length >= 2) { const [key, ...valueParts] = args; const value = valueParts.join(' '); const result = oracle.handleDataStorage({ operation: 'set', key, value }); console.log(chalk.green(`šŸ’¾ Stored: ${key} = ${value}`)); console.log(JSON.stringify(result, null, 2)); } else { console.log(chalk.red('Usage: store ')); } break; case 'get': if (args.length >= 1) { const key = args[0]; const result = oracle.handleDataStorage({ operation: 'get', key }); console.log(chalk.blue(`šŸ“„ Retrieved: ${key}`)); console.log(JSON.stringify(result, null, 2)); } else { console.log(chalk.red('Usage: get ')); } break; case 'propose': if (args.length >= 1) { const proposal = args.join(' '); const proposalId = `proposal-${Date.now()}`; const result = oracle.handleConsensus({ proposalId, proposal }); console.log(chalk.yellow(`šŸ“‹ Created proposal: ${proposalId}`)); console.log(JSON.stringify(result, null, 2)); } else { console.log(chalk.red('Usage: propose ')); } break; case 'vote': if (args.length >= 2) { const [proposalId, vote] = args; const result = oracle.handleConsensus({ proposalId, vote }); console.log(chalk.yellow(`šŸ—³ļø Voted ${vote} on ${proposalId}`)); console.log(JSON.stringify(result, null, 2)); } else { console.log(chalk.red('Usage: vote ')); } break; case 'help': console.log(chalk.yellow(` šŸ”® Oracle Commands: send - Send a message through the ring info - Show oracle information peers - List connected peers health - Perform health check metrics - Show network metrics analyze - Analyze network topology store - Store data in distributed storage get - Retrieve data from storage propose - Create a consensus proposal vote - Vote on a proposal help - Show this help quit - Exit the oracle `)); break; case 'quit': case 'exit': console.log(chalk.yellow('šŸ‘‹ Oracle shutting down...')); process.exit(0); break; default: if (command.length > 0) { console.log(chalk.red(`ā“ Unknown command: ${cmd}. Type 'help' for available commands.`)); } } } console.log(chalk.yellow(` šŸ”® Ring Network Oracle Node Started! Oracle ID: ${oracle.id.substring(0, 8)}... Port: ${oracle.port} Services: ${Array.from(oracle.oracleServices.keys()).join(', ')} Type 'help' for available commands. `));