mirror of
https://github.com/pezkuwichain/pezkuwi-telemetry.git
synced 2026-04-29 19:37:58 +00:00
Use node name as backup in case pubkey is not present (#50)
This commit is contained in:
@@ -60,7 +60,7 @@ export default class Node {
|
||||
messages: Array<Message>,
|
||||
) {
|
||||
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');
|
||||
}
|
||||
|
||||
@@ -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<string, 'SaltedName'>;
|
||||
|
||||
const nextId = idGenerator<Types.NodeId>();
|
||||
const idCache = new Map<Types.Address, NodeIdCache>();
|
||||
const idCache = new Map<Types.Address | SaltedName, NodeIdCache>();
|
||||
|
||||
function clearCache() {
|
||||
const now = timestamp();
|
||||
@@ -25,31 +27,32 @@ function clearCache() {
|
||||
|
||||
clearCache();
|
||||
|
||||
export function getId(pubkey: Maybe<Types.Address>): Types.NodeId {
|
||||
if (!pubkey) {
|
||||
return nextId();
|
||||
}
|
||||
export function getId(pubkey: Maybe<Types.Address>, 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<Types.Address>, id: Types.NodeId) {
|
||||
if (!pubkey) {
|
||||
return;
|
||||
}
|
||||
|
||||
export function refreshId(pubkey: Maybe<Types.Address>, 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 });
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user