-
Notifications
You must be signed in to change notification settings - Fork 10
/
graph.js
140 lines (135 loc) · 6.07 KB
/
graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const Node = require('./node');
const async = require('async');
const BitcoinNet = require('./net');
const { DeasyncObject } = require('./utils');
const BitcoinGraph = function(net) {
this.net = net;
};
const space = ' ';
const lpad = (s, len) => {
const x = (space + s);
return s.length >= len ? s : x.substr(x.length - len);
};
BitcoinGraph.prototype = {
printConnectionMatrix(nodes) {
// sort nodes by port
let sorted = [];
for (const n of nodes) sorted.push(n);
sorted.sort((a,b) => a.port < b.port);
const map = {};
let iter = 0;
for (const n of sorted) {
iter++;
map[n.port] = '' + iter;
}
let s = " ";
for (const n of sorted) s += lpad(map[n.port], 4);
console.log(s);
for (const a of sorted) {
s = lpad(map[a.port], 4);
for (const b of sorted) {
let z = '-';
if (a.port !== b.port && (a.isConnected(b) || b.isConnected(a))) {
z = '⤴';
}
s += lpad(z, 4);
}
console.log(s);
}
},
printBlockChains(grpA, grpB, c) {
const printUnified = (v) => console.log(` ${v.substr(0,8)}`);
const printLeft = (v) => console.log(`${v.substr(0,8)}`);
const printRight = (v) => console.log(` ${v.substr(0,8)}`);
const printLeftRight = (v, w) => console.log(`${v.substr(0,8)} ${w.substr(0,8)}`)
grpA[0].getGenesisBlockHash((gerr, genesis) => {
if (gerr) return c(err);
grpA[0].client.listSinceBlock(genesis, (err, infoA) => {
if (err) return c(err);
const lastA = infoA.result.lastblock;
grpB[0].client.listSinceBlock(genesis, (err2, infoB) => {
if (err2) return c(err);
const lastB = infoB.result.lastblock;
// from genesis and onward until we run into the last block for both groups
let ia = 1;
let ib = 1;
// genesis is shared
printUnified(genesis);
let unifiedPrinted = 1;
let unifiedKept = [];
async.whilst(
() => (ia > 0 && ib > 0),
(whilstCallback) => {
grpA[0].client.getBlockHash(ia, (err, bhInfoA) => {
grpB[0].client.getBlockHash(ib, (err, bhInfoB) => {
const a = bhInfoA.result;
const b = bhInfoB.result;
if (a === b) {
unifiedPrinted++;
if (unifiedPrinted > 3) {
unifiedKept.push(a);
} else printUnified(a);
} else {
if (unifiedKept.length > 0) {
if (unifiedKept.length > 3) {
console.log(' ...');
unifiedKept = unifiedKept.slice(unifiedKept.length - 3);
}
for (const k of unifiedKept)
printUnified(k);
unifiedKept = [];
}
unifiedPrinted = 0;
printLeftRight(''+a, ''+b);
}
ia = a === lastA ? 0 : ia + 1;
ib = b === lastB ? 0 : ib + 1;
whilstCallback();
});
});
},
(err) => {
if (unifiedKept.length > 0) {
if (unifiedKept.length > 3) {
console.log(' ...');
unifiedKept = unifiedKept.slice(unifiedKept.length - 3);
}
for (const k of unifiedKept)
printUnified(k);
}
async.whilst(
() => ia > 0,
(whilstCallback) => {
grpA[0].client.getBlockHash(ia, (err, bhInfoA) => {
const a = bhInfoA.result;
printLeft('' + a);
ia = a === lastA ? 0 : ia + 1;
whilstCallback();
});
},
() => {
async.whilst(
() => ib > 0,
(whilstCallback) => {
grpB[0].client.getBlockHash(ib, (err, bhInfoB) => {
const b = bhInfoB.result;
printRight('' + b);
ib = b === lastB ? 0 : ib + 1;
whilstCallback();
});
},
c
);
}
);
}
);
});
});
});
},
};
DeasyncObject(BitcoinGraph, [
'printConnectionMatrix',
]);
module.exports = BitcoinGraph;