This repository has been archived by the owner on Jan 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
blockchain.html
311 lines (299 loc) · 12.1 KB
/
blockchain.html
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<div class="cardpad card">
<div class="card-header">
<div class="row">
<div class="col-sm-3">
<span data-toggle="popover" data-trigger="hover" data-placement="top"
data-content="Current mined block height">
<i class="fa fa-bars"></i> Height: <span id="blockchainHeight" data-height="0"></span>
</span>
</div>
<div class="col-sm-3">
<span data-toggle="popover" data-trigger="hover" data-placement="top"
data-content="Based on the current mined block">
<i class="fas fa-tachometer-alt"></i> Instant Hash Rate: <span id="networkHashrate"></span>
</span>
</div>
<div class="col-sm-3">
<span data-toggle="popover" data-trigger="hover" data-placement="top"
data-content="Difficulty of the current mined block">
<i class="fas fa-microchip"></i> Next Difficulty: <span id="nextDifficulty"></span>
</span>
</div>
<div class="col-sm-3">
<span data-toggle="popover" data-trigger="hover" data-placement="top"
data-content="On the last visible blocks">
<i class="fas fa-percent"></i> Average Hash Rate: <span id="averageHashrate"></span>
</span>
</div>
</div>
</div>
<div class="card-body">
<div class="input-group search">
<input id="txt_search" class="form-control" placeholder="Search for block number / block hash / hash transaction">
<div class="input-group-append">
<button id="btn_search" class="btn btn-outline-secondary" type="button"><i class="fa fa-search"></i> Search
</button>
</div>
</div>
</div>
</div>
<div class="cardpad card">
<div class="card-header">
Last <span id="nb_blocks"></span> blocks found
</div>
<div id="block_table" class="card-body table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th><i class="fa fa-bars"></i> Height</th>
<th><i class="fas fa-microchip"></i> Difficulty</th>
<th><i class="fas fa-ellipsis-h"></i> Block Hash</th>
<th><i class="far fa-clock"></i> Time found</th>
<th><i class="far fa-hdd"></i> Size</th>
<th><i class="fas fa-exchange-alt"></i> tx</th>
</tr>
</thead>
<tbody id="blocks_rows">
</tbody>
</table>
</div>
</div>
<p class="text-center">
<button type="button" class="btn btn-default invisible" id="loadMoreBlocks">Load More</button>
</p>
<script>
// Current page handlers (whe should know lastMinedBlockHeader)
currentPage = {
destroy: function () {
if (xhrGetCurrentHeight) xhrGetCurrentHeight.abort();
if (xhrGetLastBlocks) xhrGetLastBlocks.abort();
},
init: function (){
if(typeof poolUpdate !== 'undefined') {
clearTimeout(poolUpdate);
}
if(typeof poolPulse !== 'undefined') {
clearTimeout(poolPulse);
}
if(typeof txPulse !== 'undefined') {
clearTimeout(txPulse);
}
if(typeof tx_wheel !== 'undefined') {
clearTimeout(tx_wheel);
}
currentPage.update();
},
update: function () {
if ((typeof lastMinedBlockHeader !== 'undefined') && parseInt(lastMinedBlockHeader.block_header.height, 10) !== parseInt($('#blockchainHeight').attr('data-height'), 10)) {
$('#blockchainHeight').attr('data-height', lastMinedBlockHeader.block_header.height);
getCurrentMinedBlock();
getBlocks(lastMinedBlockHeader.block_header.height);
}
}
};
// Current page functions.
var xhrGetCurrentHeight;
// get last block template (current mining block)
function getCurrentMinedBlock() {
if (xhrGetCurrentHeight) xhrGetCurrentHeight.abort();
xhrGetCurrentHeight = $.ajax({
url: api + '/getinfo',
method: "GET",
dataType: 'json',
cache: 'false'
})
.done(function (data) {
if (data.hasOwnProperty('difficulty')) {
updateText('networkHashrate', getReadableHashRateString(data.difficulty / coinDifficultyTarget));
$('#networkHashrate').attr('data-globalHR', (data.difficulty / coinDifficultyTarget).toString());
updateText('nextDifficulty', data.difficulty.toString());
$('#blockchainHeight').attr('data-height', data.network_height);
updateText('blockchainHeight', data.network_height);
} else {
showalert('alert-danger','Core error : ',data.error.message);
$('#loading').hide();
}
})
.fail(function (xhrGetCurrentHeight, textStatus) {
if (textStatus != "abort") {
showalert('alert-danger','Request error : ',textStatus);
$('#loading').hide();
}
});
}
// Get last 31 blocks
$('#loadMoreBlocks').click(function() {
getBlocks($('#blocks_rows').children().last().data('height'));
});
var xhrGetLastBlocks;
function getBlocks(heightAsked) {
if (xhrGetLastBlocks) xhrGetLastBlocks.abort();
xhrGetLastBlocks = $.ajax({
url: api + '/json_rpc',
method: "POST",
data: JSON.stringify({
jsonrpc: "2.0",
id: "4",
method: "f_blocks_list_json",
params: {
height:heightAsked
}
}),
dataType: 'json',
cache: 'false'
})
.done(function (data) {
if (data.hasOwnProperty('result')) {
let lastBlocksList = data.result.blocks;
renderBlocks(lastBlocksList);
$('#loading').hide();
} else {
showalert('alert-danger','Core error : ',data.error.message);
$('#loading').hide();
}
})
.fail(function (xhrGetLastBlocks, textStatus) {
if (textStatus != "abort") {
showalert('alert-danger','Can\'t connect to api : ',textStatus);
$('#loading').hide();
}
});
}
// Handle Blocks list
function renderBlocks(blocksResults){
let sumDiff = 0;
let totalblocks = 0;
let blocksRows = $('#blocks_rows');
for ( let i = 0; i < blocksResults.length; i ++){
let block = blocksResults[i];
let blockJson = JSON.stringify(block);
let existingRow = document.getElementById('blockRow' + block.height);
if (existingRow && existingRow.getAttribute('data-json') !== blockJson){
$(existingRow).replaceWith(getBlockRowElement(block, blockJson));
}
else if (!existingRow){
const blockElement = getBlockRowElement(block, blockJson);
let inserted = false;
const rows = blocksRows.children().get();
for (let f = 0; f < rows.length; f++) {
const bHeight = parseInt(rows[f].getAttribute('data-height'));
if (bHeight < block.height){
let inserted = true;
$(rows[f]).before(blockElement);
break;
}
}
if (!inserted) {
totalblocks++;
blocksRows.append(blockElement);
}
}
sumDiff += blocksResults[i].difficulty;
}
let averageHashRate = sumDiff / totalblocks;
updateText('averageHashrate', getReadableHashRateString(averageHashRate / coinDifficultyTarget));
updateText('nb_blocks', totalblocks);
$('#loadMoreBlocks').removeClass('invisible');
}
function getBlockRowElement(block,jsonString) {
const row = document.createElement('tr');
row.setAttribute('data-json', jsonString);
row.setAttribute('data-height', block.height);
row.setAttribute('id', 'blockRow' + block.height);
row.setAttribute('title', block.hash);
const columns =
'<td>' + block.height + '</td>' +
'<td>' + block.difficulty + '</td>' +
'<td>' + formatBlockLink(block.hash) + '</td>' +
'<td>' + formatDate(block.timestamp) + '</td>' +
'<td>' + block.cumul_size + '</td>' +
'<td>' + block.tx_count + '</td>';
row.innerHTML = columns;
return row;
}
// search
$('#btn_search').click(function(e) {
console.warn('search !');
const text = document.getElementById('txt_search').value;
// by height
var xhrGetSearchBlockbyHeight;
function GetSearchBlockbyHeight() {
if (xhrGetSearchBlockbyHeight) xhrGetSearchBlockbyHeight.abort();
xhrGetSearchBlockbyHeight = $.ajax({
url: api + '/json_rpc',
method: "POST",
data: JSON.stringify({
jsonrpc: "2.0",
id: "blockbyheight",
method: "getblockheaderbyheight",
params: {
height: parseInt(text)
}
}),
dataType: 'json',
cache: 'false'
})
.done(function (data) {
console.log(data);
if (data.hasOwnProperty('result')) {
let block = data.result.block_header;
window.location.href = getBlockchainUrl(block.hash);
} else {
showalert('alert-danger','Core error : ',data.error.message);
$('#loading').hide();
}
})
.fail(function (xhrGetLastBlocks, textStatus) {
if (textStatus != "abort") {
showalert('alert-danger','Can\'t connect to api : ',textStatus);
$('#loading').hide();
}
});
}
// by hash
function GetSearchBlock(){
var block, xhrGetSearchBlock;
if (xhrGetSearchBlock) xhrGetSearchBlock.abort();
xhrGetSearchBlock = $.ajax({
url: api + '/json_rpc',
method: "POST",
data: JSON.stringify({
jsonrpc:"2.0",
id: "GetSearchBlock",
method:"f_block_json",
params: {
hash: text // document.getElementById('txt_search').value // $('#txt_search').attr('value')
}
}),
dataType: 'json',
cache: 'false',
success: function(data){
if(data.result){
block = data.result.block;
window.location.href = getBlockchainUrl(block.hash);
} else if(data.error) {
window.location.href = transactionExplorer.replace('{id}', text);
}
}
});
}
// height detected
if ( text.length < 64 ) {
GetSearchBlockbyHeight();
}
// hash detected
if ( text.length == 64 ) {
GetSearchBlock();
}
e.preventDefault();
});
// enable enter key to search
$('#txt_search').keyup(function(e){
if(e.keyCode === 13)
$('#btn_search').click();
});
// enable popover
$(document).ready(function () {
$('[data-toggle="popover"]').popover({})
});
</script>