-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
98 lines (76 loc) · 3.07 KB
/
server.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
const express = require('express');
const axios = require('axios');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
const P2POOL_URL = process.env.P2POOL_URL || "http://127.0.0.1:19000";
// Set EJS as the templating engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Route to fetch and display data
app.get('/', async (req, res) => {
try {
const tip_response = await axios.get(P2POOL_URL + '/stats');
const tipData = tip_response.data;
let shaTip = tipData.sha3x_stats.height;
let rxTip = tipData.randomx_stats.height;
let page = 10;
// Take an extra 5 so that we can find the previous block for hashrate calc
let from_rx = Math.max(rxTip - page - 5, 0);
let from_sha = Math.max(shaTip - page - 5, 0);
let data = { sha3x: [], randomX: [] };
let actual_page = page + 5
const response = await axios.get(`${P2POOL_URL}/chain?algo=randomx&height=${from_rx}&count=${actual_page}`);
calcHashrates(response.data);
data.randomX = response.data.reverse().slice(0, page);
const response2 = await axios.get(`${P2POOL_URL}/chain?algo=sha3x&height=${from_sha}&count=${actual_page}`);
calcHashrates(response2.data);
data.sha3x = response2.data.reverse().slice(0, page);
res.render('index', { data });
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).send('Error fetching data');
}
});
// Route to fetch and display data
app.get('/blocks', async (req, res) => {
try {
let algo = req.query.algo;
let height = Math.max(0, req.query.height || 0);
let page = req.query.page || 10;
let actualPage = page * 1 + 5;
let data = { page: page, algo: algo, height: height };
let from = Math.max(height - 5, 0);
const response = await axios.get(`${P2POOL_URL}/chain?algo=${algo}&height=${from}&count=${actualPage}`);
calcHashrates(response.data);
data.blocks = response.data.reverse().slice(0, page);
res.render('blocks', { data });
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).send('Error fetching data');
}
});
function calcHashrates(data) {
let blocks = {};
for (let i = 0; i < data.length; i++) {
blocks[data[i].hash] = data[i];
}
for (let i = 1; i < data.length; i++) {
let block = data[i];
let prev_block = blocks[block.prev_hash];
if (!prev_block) {
console.error("Missing previous block", block.prev_hash);
continue
}
// let diff = block.target_difficulty - prev_block.target_difficulty;
// let time = block.timestamp - prev_block.timestamp;
let hashrate = block.target_difficulty / 10;
block.hashrate = hashrate;
}
}
// Start the server
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});