improve d

This commit is contained in:
cubedro 2015-05-05 00:51:16 +03:00
parent 28821abc89
commit 76b4870c90
1 changed files with 69 additions and 88 deletions

View File

@ -79,6 +79,7 @@ function Node()
};
this._lastStats = JSON.stringify(this.stats);
this._lastFetch = 0;
this._tries = 0;
this._down = 0;
@ -232,32 +233,18 @@ Node.prototype.emit = function(message, payload)
}
}
Node.prototype.isActive = function()
Node.prototype.setInactive = function()
{
this._tries++;
try {
var peers = web3.toDecimal(web3.net.peerCount);
if(peers !== null)
{
this.stats.peers = peers;
this.stats.active = true;
return true;
}
}
catch (err) {
console.error("peerCount:", err);
}
this.stats.active = false;
this.stats.listening = false;
this.stats.mining = false;
this.stats.peers = 0;
this.stats.pending = 0;
this.stats.mining = false;
this.stats.hashrate = 0;
this.stats.gasPrice = 0;
this.stats.miner = false;
this._down++;
return false;
return this;
}
Node.prototype.getInfo = function()
@ -294,36 +281,17 @@ Node.prototype.getBlock = function(number)
try {
block = web3.eth.getBlock(number, true);
console.log('====> block:', block);
if( block.hash != '?' && !_.isUndefined(block.difficulty) && !_.isUndefined(block.totalDifficulty) )
{
block.difficulty = web3.toDecimal( block.difficulty );
block.totalDifficulty = web3.toDecimal( block.totalDifficulty );
}
console.log('====> block:', block);
}
catch (err) {
console.error("getBlock(" + number + "):", err);
if(number > 0)
{
try {
number--;
block = web3.eth.getBlock(number, true);
if( block.hash !== '?' && !_.isUndefined(block.difficulty) && !_.isUndefined(block.totalDifficulty) )
{
block.difficulty = web3.toDecimal( block.difficulty );
block.totalDifficulty = web3.toDecimal( block.totalDifficulty );
}
}
catch (err) {
console.error("getBlock(" + number + "):", err);
}
}
return false;
}
return block;
@ -331,26 +299,26 @@ Node.prototype.getBlock = function(number)
Node.prototype.getMinerName = function(miner)
{
var result = _.find(this._knownMiners, {miner: miner});
var result = _.find(this._knownMiners, { miner: miner });
if(result !== undefined)
if (result !== undefined)
{
return result.name;
}
else
{
if(this._Registrar !== null)
if (this._Registrar !== null)
{
var name = this._Registrar.name(miner);
if(name.length > 0)
{
this._knownMiners.push({miner: miner, name: name});
this._knownMiners.push({ miner: miner, name: name });
return name;
}
}
this._knownMiners.push({miner: miner, name: false});
this._knownMiners.push({ miner: miner, name: false });
}
return false;
@ -361,15 +329,20 @@ Node.prototype.uptime = function()
this.stats.uptime = ((this._tries - this._down) / this._tries) * 100;
}
Node.prototype.getStats = function()
Node.prototype.getStats = function(forced)
{
var self = this;
var now = _.now();
var lastFetchAgo = now - this._lastFetch;
this._lastFetch = now;
if(this._socket)
if (this._socket)
this._lastStats = JSON.stringify(this.stats);
if(this._web3)
if (this._web3 && (lastFetchAgo >= UPDATE_INTERVAL || forced === true))
{
console.log('==> Getting stats; last update:', lastFetchAgo, '- forced:', (forced === true));
async.parallel({
start: function (callback)
{
@ -377,7 +350,7 @@ Node.prototype.getStats = function()
},
peers: function (callback)
{
setTimeout(function () {
async.nextTick(function () {
var peers;
try {
@ -389,11 +362,11 @@ Node.prototype.getStats = function()
}
callback(null, peers);
}, 1);
});
},
pending: function (callback)
{
setTimeout(function() {
async.nextTick(function () {
try {
web3.eth.getBlockTransactionCount('pending', callback);
}
@ -401,11 +374,11 @@ Node.prototype.getStats = function()
console.error('xx> Pending failed: ', err);
callback(err, null);
}
}, 1);
});
},
mining: function (callback)
{
setTimeout(function () {
async.nextTick(function () {
var mining;
try {
@ -417,11 +390,11 @@ Node.prototype.getStats = function()
}
callback(null, mining);
}, 1);
});
},
hashrate: function (callback)
{
setTimeout(function () {
async.nextTick(function () {
var hashrate;
try {
@ -433,11 +406,11 @@ Node.prototype.getStats = function()
}
callback(null, hashrate);
}, 1);
});
},
gasprice: function (callback)
gasPrice: function (callback)
{
setTimeout(function () {
async.nextTick(function () {
var gasPrice;
try {
@ -449,7 +422,23 @@ Node.prototype.getStats = function()
}
callback(null, gasPrice);
}, 1);
});
},
minerName: function (callback)
{
async.nextTick(function () {
var minerName;
try {
minerName = self.getMinerName(self.stats.block.miner);
}
catch (err) {
console.error('xx> minerName failed: ', err);
callback(err, null);
}
callback(null, minerName);
});
}
},
function (err, results)
@ -459,13 +448,7 @@ Node.prototype.getStats = function()
if (err) {
console.error('xx> getStats error: ', err);
self.stats.peers = 0;
self.stats.active = false;
self.stats.pending = 0;
self.stats.mining = false;
self.stats.hashrate = 0;
self.stats.gasPrice = 0;
self._down++;
self.setInactive();
return false;
}
@ -478,21 +461,15 @@ Node.prototype.getStats = function()
if(results.peers !== null)
{
self.stats.peers = results.peers;
self.stats.active = true;
self.stats.peers = results.peers;
self.stats.pending = results.pending;
self.stats.mining = results.mining;
self.stats.hashrate = results.hashrate;
self.stats.gasPrice = results.gasPrice;
}
else {
self.stats.peers = 0;
self.stats.active = false;
self.stats.pending = 0;
self.stats.mining = false;
self.stats.hashrate = 0;
self.stats.gasPrice = 0;
self._down++;
self.setInactive();
}
self.uptime();
@ -509,12 +486,13 @@ Node.prototype.getStatsBlock = function ()
if(this._web3)
{
console.log("==> Getting block");
var start = _.now();
var block = this.getBlock();
var end = _.now();
if( !_.isUndefined(block) && !_.isUndefined(block.number) && !_.isUndefined(block.hash) && block.hash !== '?' )
{
console.log("==> Got block:", block.number);
console.log("==> Got block:", block.number, 'in', end - start, 'ms');
this.stats.block = block;
}
else
@ -523,7 +501,6 @@ Node.prototype.getStatsBlock = function ()
}
}
this.uptime();
this.sendUpdate();
}
@ -563,18 +540,16 @@ Node.prototype.getHistory = function (range)
Node.prototype.updateBlock = function()
{
console.log('==> Getting block stats');
this.getStatsBlock();
this.update(true);
return this;
};
Node.prototype.update = function()
Node.prototype.update = function(forced)
{
console.log('==> Getting stats');
this.getStats();
this.getStats(forced);
return this;
};
@ -618,8 +593,7 @@ Node.prototype.setWatches = function()
var time = now - self._lastChainLog;
self._lastChainLog = now;
console.log('>>> Chain Filter triggered: ', now);
console.log('>>> Last chain Filter triggered: ', time);
console.log('>>> Chain Filter triggered: ', now, '- last trigger:', time);
if(time > 50)
{
@ -692,12 +666,20 @@ Node.prototype.installContract = function()
Node.prototype.init = function()
{
// Fetch node info
this.getInfo();
// Start socket connection
this.startSocketConnection();
// Install Registrar contract
this.installContract();
// Set filters
this.setWatches();
// Update stats and send info
this.updateBlock();
this.update();
}
Node.prototype.stop = function()
@ -705,7 +687,6 @@ Node.prototype.stop = function()
if(this._socket)
socket.end();
if(this.updateInterval)
clearInterval(this.updateInterval);