Use node name as backup in case pubkey is not present (#50)

This commit is contained in:
Maciej Hirsz
2018-09-24 21:33:06 +02:00
committed by GitHub
parent 1559b82eb0
commit 5e995ea21d
3 changed files with 22 additions and 19 deletions
+2 -2
View File
@@ -60,7 +60,7 @@ export default class Node {
messages: Array<Message>, messages: Array<Message>,
) { ) {
this.ip = ip; this.ip = ip;
this.id = getId(address); this.id = getId(address, name);
this.name = name; this.name = name;
this.chain = chain; this.chain = chain;
this.config = config; this.config = config;
@@ -205,7 +205,7 @@ export default class Node {
this.socket.close(); this.socket.close();
this.socket.terminate(); this.socket.terminate();
refreshId(this.address, this.id); refreshId(this.address, this.name, this.id);
this.events.emit('disconnect'); this.events.emit('disconnect');
} }
+19 -16
View File
@@ -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_LIFETIME = (24 * 3600 * 1000) as Types.Milliseconds; // 24h
const CACHE_INTERVAL = (3600 * 1000) as Types.Milliseconds; // 1h const CACHE_INTERVAL = (3600 * 1000) as Types.Milliseconds; // 1h
@@ -8,8 +8,10 @@ interface NodeIdCache {
ts: Types.Timestamp; ts: Types.Timestamp;
} }
type SaltedName = Opaque<string, 'SaltedName'>;
const nextId = idGenerator<Types.NodeId>(); const nextId = idGenerator<Types.NodeId>();
const idCache = new Map<Types.Address, NodeIdCache>(); const idCache = new Map<Types.Address | SaltedName, NodeIdCache>();
function clearCache() { function clearCache() {
const now = timestamp(); const now = timestamp();
@@ -25,31 +27,32 @@ function clearCache() {
clearCache(); clearCache();
export function getId(pubkey: Maybe<Types.Address>): Types.NodeId { export function getId(pubkey: Maybe<Types.Address>, name: Types.NodeName): Types.NodeId {
if (!pubkey) { let cachekey: Types.Address | SaltedName;
return nextId();
}
const cached = idCache.get(pubkey); if (pubkey) {
const cached = idCache.get(pubkey);
if (cached) { if (cached) {
return cached.id; return cached.id;
}
cachekey = pubkey;
} else {
cachekey = `name:${name}` as SaltedName;
} }
const id = nextId(); const id = nextId();
const ts = timestamp(); const ts = timestamp();
idCache.set(pubkey, { id, ts }); idCache.set(cachekey, { id, ts });
return id; return id;
} }
export function refreshId(pubkey: Maybe<Types.Address>, id: Types.NodeId) { export function refreshId(pubkey: Maybe<Types.Address>, name: Types.NodeName, id: Types.NodeId) {
if (!pubkey) { const cachekey = pubkey ? pubkey : `name:${name}` as SaltedName;
return;
}
const ts = timestamp(); const ts = timestamp();
idCache.set(pubkey, { id, ts }); idCache.set(cachekey, { id, ts });
} }
+1 -1
View File
@@ -9,4 +9,4 @@ import * as FeedMessage from './feed';
export { Types, FeedMessage }; export { Types, FeedMessage };
// Increment this if breaking changes were made to types in `feed.ts` // 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;