-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexplorer.js
282 lines (233 loc) · 8.02 KB
/
explorer.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* IZZZIO Blockchain explorer
* @author Andrey Nedobylsky
*/
const MAX_BLOCKS_ON_PAGE = 15;
let nodes = ['wss://xn--90absg.xn--p1ai/vitamin/', "ws://localhost:6001"];
let candy = null;
let lastestBlocks = [];
let parsers = {};
let lastBlockInTable = 0;
let searchHooks = [];
$(document).ready(function () {
$('#loadingModal').modal('show');
$.get('nodes.json', function (data) {
console.log(data);
nodes = data;
if(typeof data === 'string') {
nodes = JSON.parse(data);
}
}).always(function () {
startCandyConnection(nodes);
});
//startCandyConnection(nodes);
$('.returnButton').click(function () {
$('#lastestBlocksPage').fadeIn();
$('#blockDetailPage').hide();
$('#modalModulePage').hide();
});
$('.searchForm').on('submit', async function (event) {
event.preventDefault();
let search = $('#search').val();
for (let hook of searchHooks) {
if(await hook(search)) {
window.location.hash = search;
return;
}
}
if(!isNaN(search)) {
loadBlockPreview(search);
} else {
if(confirm('Search by hash may take a long time. Are you sure?')) {
let blockId = 1;
function checkBlock() {
candy.loadResource(blockId, function (err, block, rawBlock) {
if(rawBlock.hash.indexOf(search) !== -1) {
//alert('Block found: ' + blockId);
setTimeout(function () {
loadBlockPreview(blockId);
}, 500);
return;
}
blockId++;
$('#height').text(blockId + '/' + candy.blockHeight);
if(blockId > candy.blockHeight) {
alert('Block hash not found');
return;
}
checkBlock();
});
}
checkBlock();
}
}
});
});
/**
* Initiats candy connection
* @param nodes
*/
function startCandyConnection(nodes) {
function hideModal() {
$('#loadingModal').fadeOut(1000);
$('.modal-backdrop').fadeOut(1000);
$('.modal-open').removeClass('modal-open');
}
candy = new Candy(nodes).start();
candy.onready = function () {
setInterval(function () {
$('#height').text(candy.blockHeight);
$('#connections').text(candy.getActiveConnections().length);
if(candy.getActiveConnections().length === 0) {
$('#loadingModal').modal('show').show();
$('.modal-backdrop').show();
} else {
hideModal();
}
}, 1000);
setInterval(function () {
updateLatestBlocks();
}, 5000);
updateLatestBlocks();
setTimeout(function () {
if(window.location.hash.length !== 0) {
$('#search').val(window.location.hash.replace('#', ''));
$('.searchForm').submit();
}
}, 1000);
};
/*candy.onmessage = function (messageBody) {
for (let a in waitingMessages) {
if(waitingMessages.hasOwnProperty(a)) {
if(waitingMessages[a].id === messageBody.id) {
if(waitingMessages[a].handle(messageBody)) {
delete waitingMessages[a];
}
return;
}
}
}
}*/
}
/**
* Detect block type by params
* @param rawBlock
* @return {*}
*/
function detectBlockType(rawBlock) {
try {
let data = JSON.parse(rawBlock.data);
if(typeof data.type !== 'undefined') {
return data.type;
}
return "Unknown";
} catch (e) {
return "Unknown without data";
}
}
/**
* Block view href event
*/
function loadBlockPreview(index) {
index = (isNaN(index) ? $(this).text() : index);
candy.loadResource(index, function (err, block, rawBlock) {
window.location.hash = index;
let blockType = detectBlockType(rawBlock);
$('#lastestBlocksPage').hide();
$('#modalModulePage').hide();
$('#blockDetailPage').fadeIn();
$('.blockIndex').text(rawBlock.index);
$('.blockSize').text(rawBlock.data.length);
$('.blockHash').text(rawBlock.hash);
$('.blockPrevHash').text(rawBlock.previousHash);
$('.blockPrevious').text(rawBlock.index - 1);
$('.blockNext').text(rawBlock.index + 1);
$('.blockType').text(blockType);
$('.blockTimestamp').text(moment(rawBlock.timestamp).format('LLLL'));
$('.blockStartTimestamp').text(moment(rawBlock.startTimestamp).format('LLLL'));
$('.blockData').text(rawBlock.data);
$('.blockSign').text(rawBlock.sign);
if(typeof parsers[blockType] !== 'undefined') {
let parserResult = parsers[blockType](rawBlock);
Promise.resolve(parserResult).then((value) => {
$('.blockParserOutput').html(value);
});
} else {
$('.blockParserOutput').text('No parser for this block type');
}
});
}
/**
* Update latest blocks list
*/
function updateLatestBlocks() {
function lastBlocksTableFormat() {
function insertBlock(rawBlock) {
if(rawBlock.index > lastBlockInTable) {
lastBlockInTable = rawBlock.index;
}
return $('#lastTransactions tbody > tr:last').after(
" <tr>\n" +
" <td> <a href='#' class='blockHref'>" + rawBlock.index + "</a></td>\n" +
" <td>" + detectBlockType(rawBlock) + "</td>\n" +
" <td>" + moment().from(moment(rawBlock.timestamp)) + "</td>\n" +
" <td>" + rawBlock.data.length + "</td>\n" +
" </tr>"
);
}
let old;
old = $('#lastTransactions tbody > tr').fadeOut(500);
setTimeout(function () {
lastestBlocks.forEach(function (block) {
insertBlock(block.raw).hide().fadeIn(100);
});
old.remove();
$('.blockHref').click(loadBlockPreview);
}, 500);
}
lastestBlocks = [];
if(candy.blockHeight !== 0 && lastBlockInTable !== candy.blockHeight) {
let maxBLocksOnPageLimited = MAX_BLOCKS_ON_PAGE;
if(maxBLocksOnPageLimited > candy.blockHeight) {
maxBLocksOnPageLimited = candy.blockHeight /*- 1*/;
}
for (let i = candy.blockHeight; i > candy.blockHeight - maxBLocksOnPageLimited; i--) {
candy.loadResource(i, function (err, block, rawBlock) {
lastestBlocks.push({id: rawBlock.index, raw: rawBlock, data: block});
if(lastestBlocks.length >= maxBLocksOnPageLimited || lastestBlocks.length >= candy.blockHeight) {
lastestBlocks = lastestBlocks.sort(function (b1, b2) {
return (b2.id - b1.id)
});
lastBlocksTableFormat();
}
});
}
}
}
/**
* Loads parser module
* Example: loadParser('parsers/EcmaContractDeploy.js');
* @param {string} uri
*/
function loadParser(uri) {
$.getScript(uri);
}
/**
* Registers search hook
* @param {Function} hookFunction
*/
function registerSearchHook(hookFunction) {
searchHooks.push(hookFunction);
}
/**
* Opens modal page with custom content
* @param {string} modalHeader
* @param {string} modalContent
*/
function showModalPage(modalHeader, modalContent) {
$('#lastestBlocksPage').hide();
$('#blockDetailPage').hide();
$('#modalModulePage').fadeIn();
$('#modalHeader').html(modalHeader);
$('#modalContent').html(modalContent);
}