Demystify Node state (#61)

This commit is contained in:
Maciej Hirsz
2018-09-27 21:30:51 +02:00
committed by GitHub
parent ae81485854
commit bc9d47d358
7 changed files with 157 additions and 97 deletions
+95 -26
View File
@@ -1,15 +1,100 @@
import { Types, Maybe } from '@dotstats/common';
export namespace State {
export interface Node {
pinned: boolean,
id: Types.NodeId;
nodeDetails: Types.NodeDetails;
nodeStats: Types.NodeStats;
blockDetails: Types.BlockDetails;
location: Maybe<Types.NodeLocation>;
export class Node {
public static compare(a: Node, b: Node): number {
if (a.pinned === b.pinned) {
if (a.height === b.height) {
const aPropagation = a.propagationTime == null ? Infinity : a.propagationTime as number;
const bPropagation = b.propagationTime == null ? Infinity : b.propagationTime as number;
// Ascending sort by propagation time
return aPropagation - bPropagation;
}
} else {
return +b.pinned - +a.pinned;
}
// Descending sort by block number
return b.height - a.height;
}
public readonly id: Types.NodeId;
public readonly name: Types.NodeName;
public readonly implementation: Types.NodeImplementation;
public readonly version: Types.NodeVersion;
public readonly validator: Maybe<Types.Address>;
public pinned: boolean;
public peers: Types.PeerCount;
public txs: Types.TransactionCount;
public mem: Maybe<Types.MemoryUse>;
public cpu: Maybe<Types.CPUUse>;
public height: Types.BlockNumber;
public hash: Types.BlockHash;
public blockTime: Types.Milliseconds;
public blockTimestamp: Types.Timestamp;
public propagationTime: Maybe<Types.PropagationTime>;
public lat: Maybe<Types.Latitude>;
public lon: Maybe<Types.Longitude>;
public city: Maybe<Types.City>;
constructor(
pinned: boolean,
id: Types.NodeId,
nodeDetails: Types.NodeDetails,
nodeStats: Types.NodeStats,
blockDetails: Types.BlockDetails,
location: Maybe<Types.NodeLocation>
) {
const [name, implementation, version, validator] = nodeDetails;
this.pinned = pinned;
this.id = id;
this.name = name;
this.implementation = implementation;
this.version = version;
this.validator = validator;
this.updateStats(nodeStats);
this.updateBlock(blockDetails);
if (location) {
this.updateLocation(location);
}
}
public updateStats(stats: Types.NodeStats) {
const [peers, txs, mem, cpu] = stats;
this.peers = peers;
this.txs = txs;
this.mem = mem;
this.cpu = cpu;
}
public updateBlock(block: Types.BlockDetails) {
const [height, hash, blockTime, blockTimestamp, propagationTime] = block;
this.height = height;
this.hash = hash;
this.blockTime = blockTime;
this.blockTimestamp = blockTimestamp;
this.propagationTime = propagationTime;
}
public updateLocation(location: Types.NodeLocation) {
const [lat, lon, city] = location;
this.lat = lat;
this.lon = lon;
this.city = city;
}
}
export namespace State {
export interface Settings {
location: boolean;
validator: boolean;
@@ -34,27 +119,11 @@ export interface State {
timeDiff: Types.Milliseconds;
subscribed: Maybe<Types.ChainLabel>;
chains: Map<Types.ChainLabel, Types.NodeCount>;
nodes: Map<Types.NodeId, State.Node>;
sortedNodes: State.Node[];
nodes: Map<Types.NodeId, Node>;
sortedNodes: Node[];
settings: Readonly<State.Settings>;
pins: Readonly<Set<Types.NodeName>>;
}
export type Update = <K extends keyof State>(changes: Pick<State, K> | null) => Readonly<State>;
export function compareNodes(a: State.Node, b: State.Node): number {
if (a.pinned === b.pinned) {
if (a.blockDetails[0] === b.blockDetails[0]) {
const aPropagation = a.blockDetails[4] == null ? Infinity : a.blockDetails[4] as number;
const bPropagation = b.blockDetails[4] == null ? Infinity : b.blockDetails[4] as number;
// Ascending sort by propagation time
return aPropagation - bPropagation;
}
} else {
return +b.pinned - +a.pinned;
}
// Descending sort by block number
return b.blockDetails[0] - a.blockDetails[0];
}