ethstats-server/models/node.js

69 lines
1.4 KiB
JavaScript
Raw Normal View History

2014-12-04 22:35:09 +01:00
var geoip = require('geoip-lite');
2015-01-20 19:29:31 +01:00
var Node = function Node(options, id)
{
2015-02-09 00:09:41 +01:00
this.options = options;
this.info = {
name: options.name,
ip: options.rpcHost,
type: options.type,
os: options.os
};
this.info.geo = geoip.lookup(this.info.ip);
2015-02-05 00:30:16 +01:00
this.info.id = parseInt(id);
2015-01-20 19:29:31 +01:00
this.info.stats = {
active: false,
peers: 0,
mining: false,
block: {
2015-02-08 22:00:51 +01:00
number: 0,
hash: '?',
timestamp: 0
2015-02-05 12:05:21 +01:00
},
uptime: {
down: 0,
inc: 0,
total: 0
2015-01-20 19:29:31 +01:00
}
}
this.web3 = require('ethereum.js');
return this;
}
2015-01-28 01:41:04 +01:00
Node.prototype.update = function()
2015-01-20 19:29:31 +01:00
{
2015-02-09 00:09:41 +01:00
var sock = new this.web3.providers.HttpSyncProvider('http://' + this.options.rpcHost + ':' + this.options.rpcPort);
2015-01-29 00:41:52 +01:00
this.web3.setProvider(sock);
2015-01-20 19:29:31 +01:00
var eth = this.web3.eth;
try {
2015-01-29 00:41:52 +01:00
this.info.stats.peers = eth.peerCount;
}
catch (err) {
2015-01-29 00:41:52 +01:00
this.info.stats.peers = null;
}
2015-01-29 00:41:52 +01:00
if(this.info.stats.peers != null) {
this.info.stats.block = eth.block(parseInt(eth.number));
if(this.info.stats.block.hash != '?' && typeof this.info.stats.block.difficulty !== 'undefined'){
2015-02-08 22:00:51 +01:00
this.info.stats.block.difficulty = this.web3.toDecimal(this.info.stats.block.difficulty);
}
this.info.stats.mining = eth.mining;
this.info.stats.active = true;
} else {
this.info.stats.active = false;
2015-02-05 12:05:21 +01:00
this.info.stats.uptime.down++;
}
2015-01-20 19:29:31 +01:00
2015-02-05 12:05:21 +01:00
this.info.stats.uptime.inc++;
this.info.stats.uptime.total = ((this.info.stats.uptime.inc - this.info.stats.uptime.down) / this.info.stats.uptime.inc) * 100;
2015-01-28 01:41:04 +01:00
return this.info;
2015-01-20 19:29:31 +01:00
};
2014-12-04 22:35:09 +01:00
module.exports = Node;