diff --git a/packages/backend/src/Node.ts b/packages/backend/src/Node.ts index aa073ef..ec12dd8 100644 --- a/packages/backend/src/Node.ts +++ b/packages/backend/src/Node.ts @@ -60,7 +60,7 @@ export default class Node { messages: Array, ) { this.ip = ip; - this.id = getId(address); + this.id = getId(address, name); this.name = name; this.chain = chain; this.config = config; @@ -205,7 +205,7 @@ export default class Node { this.socket.close(); this.socket.terminate(); - refreshId(this.address, this.id); + 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 index dca5788..4cc22b3 100644 --- a/packages/backend/src/nodeId.ts +++ b/packages/backend/src/nodeId.ts @@ -1,4 +1,4 @@ -import { timestamp, Maybe, Types, idGenerator } from '@dotstats/common'; +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 @@ -8,8 +8,10 @@ interface NodeIdCache { ts: Types.Timestamp; } +type SaltedName = Opaque; + const nextId = idGenerator(); -const idCache = new Map(); +const idCache = new Map(); function clearCache() { const now = timestamp(); @@ -25,31 +27,32 @@ function clearCache() { clearCache(); -export function getId(pubkey: Maybe): Types.NodeId { - if (!pubkey) { - return nextId(); - } +export function getId(pubkey: Maybe, name: Types.NodeName): Types.NodeId { + let cachekey: Types.Address | SaltedName; - const cached = idCache.get(pubkey); + if (pubkey) { + const cached = idCache.get(pubkey); - if (cached) { - return cached.id; + if (cached) { + return cached.id; + } + + cachekey = pubkey; + } else { + cachekey = `name:${name}` as SaltedName; } const id = nextId(); const ts = timestamp(); - idCache.set(pubkey, { id, ts }); + idCache.set(cachekey, { id, ts }); return id; } -export function refreshId(pubkey: Maybe, id: Types.NodeId) { - if (!pubkey) { - return; - } - +export function refreshId(pubkey: Maybe, name: Types.NodeName, id: Types.NodeId) { + const cachekey = pubkey ? pubkey : `name:${name}` as SaltedName; const ts = timestamp(); - idCache.set(pubkey, { id, ts }); + idCache.set(cachekey, { id, ts }); } diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 4aa3819..7101264 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -9,4 +9,4 @@ import * as FeedMessage from './feed'; export { Types, FeedMessage }; // Increment this if breaking changes were made to types in `feed.ts` -export const VERSION: Types.FeedVersion = 12 as Types.FeedVersion; +export const VERSION: Types.FeedVersion = 13 as Types.FeedVersion;