mirror of
https://github.com/pezkuwichain/pezkuwi-telemetry.git
synced 2026-04-30 18:58:02 +00:00
Use node name as backup in case pubkey is not present (#50)
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user