ethstats-server/models/node.js

44 lines
873 B
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
require('es6-promise').polyfill();
var self;
var Node = function Node(options, id)
{
this.info = options;
this.info.geo = geoip.lookup(this.info.rpcHost);
this.info.id = id;
this.info.stats = {
active: false,
peers: 0,
mining: false,
block: {
height: 0,
hash: '?'
}
}
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-01-28 01:41:04 +01:00
if( ! this.web3.haveProvider()) {
var sock = new this.web3.providers.HttpSyncProvider('http://' + this.info.rpcHost + ':' + this.info.rpcPort);
2015-01-20 19:29:31 +01:00
this.web3.setProvider(sock);
}
var eth = this.web3.eth;
2015-01-28 01:41:04 +01:00
this.info.stats.peers = eth.peerCount;
this.info.stats.mining = eth.mining;
this.info.stats.block.height = eth.number;
this.info.stats.block.hash = eth.block(this.info.stats.block.height).hash;
2015-01-20 19:29:31 +01:00
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;