UNPKG

universal-life-protocol-core

Version:

Revolutionary AI framework implementing living, conscious digital reality with meta-cognitive reasoning, attention economics, and autonomous learning

114 lines 3.03 kB
/** * Simple in-memory network simulation for testing the CUE system */ export class CueNetwork { peers = new Map(); eventLog = []; constructor() { // Empty constructor } /** * Add a peer to the network */ addPeer(peer) { this.peers.set(peer.credentialId, peer); // Set up broadcast callback to simulate network propagation peer.setBroadcastCallback((event) => { this.eventLog.push(event); this.propagateEvent(peer.sign(event), peer.credentialId); }); } /** * Remove a peer from the network */ removePeer(peerId) { return this.peers.delete(peerId); } /** * Propagate an event to all peers except the sender */ propagateEvent(signedEvent, senderId) { for (const [peerId, peer] of this.peers.entries()) { if (peerId !== senderId) { peer.processIncomingEvent(signedEvent); } } } /** * Get all peers in the network */ getPeers() { return Array.from(this.peers.values()); } /** * Get a specific peer by ID */ getPeer(peerId) { return this.peers.get(peerId); } /** * Get the full event log */ getEventLog() { return [...this.eventLog]; } /** * Clear the event log */ clearEventLog() { this.eventLog = []; } /** * Get network statistics */ getStats() { const eventsByType = {}; for (const event of this.eventLog) { eventsByType[event.type] = (eventsByType[event.type] || 0) + 1; } return { peerCount: this.peers.size, totalEvents: this.eventLog.length, eventsByType }; } /** * Run a simulation step on all peers */ simulationStep() { for (const peer of this.peers.values()) { // Update entity states const entityStates = peer.getEntityStates(); for (const entityId of entityStates.keys()) { peer.updateEntityState(entityId); } // Run agent decisions const agent = peer.getAgent(); if (agent) { peer.runAgentDecision(agent.id); } } } /** * Initialize CTL consensus for peers with enough validators */ initializeConsensus() { const peerIds = Array.from(this.peers.keys()); if (peerIds.length >= 7) { const validatorSet = peerIds.slice(0, 7); for (const peer of this.peers.values()) { peer.initializeCtl(validatorSet); } } } /** * Run a consensus round across all peers */ runConsensusRound(roundSeed) { const seed = roundSeed || `round-${Date.now()}`; for (const peer of this.peers.values()) { peer.runCtlConsensusRound(seed); } } } //# sourceMappingURL=cue-network.js.map