feat: Instead of delisting, push stale nodes to bottom (#161)

This commit is contained in:
Maciej Hirsz
2019-07-02 14:52:06 +02:00
committed by GitHub
parent c817a16d31
commit 983919fd87
8 changed files with 70 additions and 32 deletions
+14 -2
View File
@@ -2,7 +2,7 @@ import { Types, Maybe, SortedCollection } from '@dotstats/common';
export class Node {
public static compare(a: Node, b: Node): number {
if (a.pinned === b.pinned) {
if (a.pinned === b.pinned && a.stale === b.stale) {
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;
@@ -11,7 +11,10 @@ export class Node {
return aPropagation - bPropagation;
}
} else {
return +b.pinned - +a.pinned;
const bSort = (b.pinned ? -2 : 0) + +b.stale;
const aSort = (a.pinned ? -2 : 0) + +a.stale;
return aSort - bSort;
}
// Descending sort by block number
@@ -26,6 +29,7 @@ export class Node {
public readonly validator: Maybe<Types.Address>;
public readonly networkId: Maybe<Types.NetworkId>;
public stale: boolean;
public pinned: boolean;
public peers: Types.PeerCount;
public txs: Types.TransactionCount;
@@ -110,6 +114,7 @@ export class Node {
this.blockTime = blockTime;
this.blockTimestamp = blockTimestamp;
this.propagationTime = propagationTime;
this.stale = false;
this.trigger();
}
@@ -143,6 +148,13 @@ export class Node {
}
}
public setStale(stale: boolean) {
if (this.stale !== stale) {
this.stale = stale;
this.trigger();
}
}
public subscribe(handler: (node: Node) => void) {
this.subscriptions.add(handler);
}