diff --git a/packages/backend/src/Node.ts b/packages/backend/src/Node.ts index f5188b2..eac4432 100644 --- a/packages/backend/src/Node.ts +++ b/packages/backend/src/Node.ts @@ -1,10 +1,9 @@ import * as WebSocket from 'ws'; import * as EventEmitter from 'events'; -import { noop, timestamp, Maybe, Types, NumStats } from '@dotstats/common'; +import { noop, timestamp, idGenerator, Maybe, Types, NumStats } from '@dotstats/common'; import { parseMessage, getBestBlock, Message, BestBlock, SystemInterval } from './message'; import { locate, Location } from './location'; -import { getId, refreshId } from './nodeId'; import { MeanList } from './MeanList'; const BLOCK_TIME_HISTORY = 10; @@ -12,6 +11,8 @@ const MEMORY_RECORDS = 20; const CPU_RECORDS = 20; const TIMEOUT = (1000 * 60 * 1) as Types.Milliseconds; // 1 minute +const nextId = idGenerator(); + export interface NodeEvents { on(event: 'location', fn: (location: Location) => void): void; emit(event: 'location', location: Location): void; @@ -64,7 +65,7 @@ export default class Node { messages: Array, ) { this.ip = ip; - this.id = getId(address, name); + this.id = nextId(); this.name = name; this.chain = chain; this.config = config; @@ -213,8 +214,6 @@ export default class Node { this.socket.close(); this.socket.terminate(); - refreshId(this.address, this.name, this.id); - this.events.emit('disconnect'); } diff --git a/packages/backend/src/nodeId.ts b/packages/backend/src/nodeId.ts deleted file mode 100644 index e55594f..0000000 --- a/packages/backend/src/nodeId.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { timestamp, Maybe, Types, idGenerator, Opaque } from '@dotstats/common'; - -const CACHE_LIFETIME = (24 * 3600 * 1000) as Types.Milliseconds; // 24h -const CACHE_INTERVAL = (3600 * 1000) as Types.Milliseconds; // 1h - -interface NodeIdCache { - id: Types.NodeId; - ts: Types.Timestamp; -} - -type SaltedName = Opaque; - -const nextId = idGenerator(); -const idCache = new Map(); - -function clearCache() { - const now = timestamp(); - - for (const [pubkey, { ts }] of idCache.entries()) { - if ((now - ts) > CACHE_LIFETIME) { - idCache.delete(pubkey); - } - } - - setTimeout(clearCache, CACHE_INTERVAL); -} - -clearCache(); - -export function getId(_pubkey: Maybe, _name: Types.NodeName): Types.NodeId { - // let cachekey: Types.Address | SaltedName; - - // if (pubkey) { - // const cached = idCache.get(pubkey); - - // if (cached) { - // return cached.id; - // } - - // cachekey = pubkey; - // } else { - // cachekey = `name:${name}` as SaltedName; - // } - - const id = nextId(); - // const ts = timestamp(); - - // idCache.set(cachekey, { id, ts }); - - return id; -} - -export function refreshId(_pubkey: Maybe, _name: Types.NodeName, _id: Types.NodeId) { - // const cachekey = pubkey ? pubkey : `name:${name}` as SaltedName; - // const cachekey = `name:${name}` as SaltedName; - // const ts = timestamp(); - - // idCache.set(cachekey, { id, ts }); -}