-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplorer.js
More file actions
210 lines (187 loc) · 5.77 KB
/
explorer.js
File metadata and controls
210 lines (187 loc) · 5.77 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var nodes = [
{
port: '3000',
peers: [],
host: '159.89.16.12',
//host: 'localhost',
ident: 'master',
name: 'master',
master: true
}
];
// var transactions = {};
// var blocks = {
// // Genesis block
// 0: [
// {
// hash: '00f82f15d9fee292860b2a37183d769efd3b617451c04017f700238fd472e8bb',
// previous_hash: 'Olivia'
// }
// ]
// };
// var stats = new Vue({
// el: '#stats-list',
// data: {transactions: transactions}
// });
// var chain = new Vue({
// el: '#blocks',
// data: {blocks: blocks}
// });
var graph = Viva.Graph.graph();
var graphics = Viva.Graph.View.svgGraphics();
var nodeSize = 24;
var loopNodes = function () {
for (i=0; i < nodes.length; ++i) {
(function () {
var currentIndex = i;
if (!nodes[i].socket) {
connectToNode(currentIndex);
}
}());
}
};
var connectToNode = function (i) {
socket = new WebSocket('ws://'+nodes[i].host+':'+nodes[i].port+'/events');
socket.addEventListener('message', function (event) {
console.log('===== now event ======');
var eventData = JSON.parse(event.data);
processEvent(eventData, i);
});
socket.addEventListener('close', function (event) {
console.log('===== now closing ======');
url = new URL(event.target.url);
removeNode(url.port);
});
nodes[i].socket = socket;
};
var processEvent = function (event, i) {
console.log('> process event');
if (event.event === 'onTxn') {
console.log('> + new txn');
// transactions[event.hash] = event.amount;
// stats.transactions = transactions;
// stats.$forceUpdate();
} else if(event.event === 'onConnection') {
console.log('> + new peer');
checkConnections([event.peer_ident], event.node_ident, event.node_is_master);
} else if(event.event === 'onNewBlock') {
console.log('> + new block');
if (!blocks[event.height]) {
blocks[event.height] = [];
}
blocks[event.height].push({hash: event.hash, previous_hash: event.previous_hash});
chain.blocks = blocks;
chain.$forceUpdate();
} else {
// this we only do because we don't know the master node ident
console.log('> + new node');
nodes[i].height = event.height;
nodes[i].hash = event.hash;
nodes[i].peers = event.peers;
nodes[i].port = event.port;
nodes[i].txn = event.txn;
nodes[i].miner = event.miner;
nodes[i].ident = event.ident;
nodes[i].host = event.host;
nodes[i].name = event.name;
nodes[i].master = event.master;
if (!nodes[i].master) {
graph.addNode(nodes[i].ident, {
height: nodes[i].height,
hash: nodes[i].hash || '-',
txn: nodes[i].txn || 0,
miner: nodes[i].miner,
name: nodes[i].name
});
}
if (event.event === 'onInitialUpdate') {
checkConnections(event.peers, nodes[i].ident, nodes[i].master);
}
}
};
var checkConnections = function (peers, parentApiIdent, parentIsMaster) {
for (i=0; i < peers.length; ++i) {
(function () {
var peer = nodes.find(node => node.ident === peers[i].ident);
if (!peer) {
nodes.push({port: peers[i].port, host: peers[i].host, ident: peers[i].ident});
// stats.nodes = nodes;
connectToNode(nodes.length - 1);
}
// also create link
if (!parentIsMaster) {
if (graph.getLink(peers[i].ident, parentApiIdent) === null && graph.getLink(parentApiIdent, peers[i].ident) === null) {
graph.addLink(peers[i].ident, parentApiIdent);
}
}
}());
}
};
var removeNode = function (port) {
graph.removeNode(port);
nodes = nodes.filter(node => parseInt(port) !== node.port);
// stats.nodes = nodes;
};
loopNodes();
graphics.node(function(node) {
// This time it's a group of elements: http://www.w3.org/TR/SVG/struct.html#Groups
var ui = Viva.Graph.svg('g');
// Create SVG text element with user id as content
var stringToColour = function(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
var colour = '#';
for (var i = 0; i < 3; i++) {
var value = (hash >> (i * 8)) & 0xFF;
colour += ('00' + value.toString(16)).substr(-2);
}
return colour;
};
var height = node.data ? node.data.height : '0';
var hash = node.data ? node.data.hash : '-';
var display_id = (!!node.data && !!node.data.name && node.data.name !== 'Unknown') ? node.data.name : node.id
//var display_id = node.id;
var svgText = Viva.Graph.svg('text').attr('y', '-4px').text(display_id+' / '+height+' / '+hash.substr(-6));
var svgCircle = Viva.Graph.svg('circle')
.attr('r', '7px')
.attr('cy', '12')
.attr('cx', '12')
.attr('fill', stringToColour(hash));
if ( node.data && node.data.miner === true ) {
svgCircle = svgCircle.attr('stroke', '#ff6859').attr('stroke-width', 3);
}
ui.append(svgText);
ui.append(svgCircle);
return ui;
}).placeNode(function(nodeUI, pos) {
// 'g' element doesn't have convenient (x,y) attributes, instead
// we have to deal with transforms: http://www.w3.org/TR/SVG/coords.html#SVGGlobalTransformAttribute
nodeUI.attr('transform',
'translate(' +
(pos.x - nodeSize/2) + ',' + (pos.y - nodeSize/2) +
')');
});
graphics.link(function(link){
return Viva.Graph.svg('line')
.attr('stroke', 'rgba(0,0,0,0.1)')
.attr('stroke-width', 1);
});
var layout = Viva.Graph.Layout.forceDirected(graph, {
springLength: 300
});
var renderer = Viva.Graph.View.renderer(graph, {
// layout: layout,
graphics : graphics,
interactive: "node,drag",
container: document.getElementById('graph')
});
renderer.run();
function zoomIn() {
renderer.zoomIn();
}
function zoomOut() {
renderer.zoomOut();
}
// ============================================================================