improve d

This commit is contained in:
cubedro 2015-05-05 00:51:16 +03:00
parent 28821abc89
commit 76b4870c90

View File

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