258
oracle.js
Archivo normal
258
oracle.js
Archivo normal
@@ -0,0 +1,258 @@
|
||||
#!/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> Port to listen on (default: random)
|
||||
--id <id> Node ID (default: auto-generated)
|
||||
--bootstrap <addr> Bootstrap node address (host:port)
|
||||
--position <pos> 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 <key> <value>'));
|
||||
}
|
||||
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 <key>'));
|
||||
}
|
||||
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 <proposal text>'));
|
||||
}
|
||||
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 <proposalId> <yes|no>'));
|
||||
}
|
||||
break;
|
||||
|
||||
case 'help':
|
||||
console.log(chalk.yellow(`
|
||||
🔮 Oracle Commands:
|
||||
send <msg> - 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 <key> <value> - Store data in distributed storage
|
||||
get <key> - Retrieve data from storage
|
||||
propose <text> - Create a consensus proposal
|
||||
vote <id> <yes|no> - 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.
|
||||
`));
|
||||
Referencia en una nueva incidencia
Block a user