diff --git a/lib/node.js b/lib/node.js index 303efaf..03e4117 100644 --- a/lib/node.js +++ b/lib/node.js @@ -129,7 +129,7 @@ Node.prototype.checkWeb3Connection = function() { var self = this; - if(!this._web3) + if (!this._web3) { try { var tmp = web3.version.client; @@ -145,11 +145,11 @@ Node.prototype.checkWeb3Connection = function() return true; } } - catch(err) + catch (err) { console.error('Web3 connection attempt', chalk.cyan('#' + this._called++), 'failed'); - process.nextTick( function() + process.nextTick(function () { self.checkWeb3Connection(); }); @@ -196,7 +196,7 @@ Node.prototype.setupSockets = function() }) .on('history', function (data) { - console.info('wsc', 'Getting history'); + console.info('his', 'Got history request'); var reqHistory = self.getHistory( data ); @@ -242,20 +242,6 @@ Node.prototype.emit = function(message, payload) } } -Node.prototype.setInactive = function() -{ - this.stats.active = 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 this; -} - Node.prototype.getInfo = function() { console.info('==>', 'Getting info'); @@ -280,6 +266,25 @@ Node.prototype.getInfo = function() return false; } +Node.prototype.setInactive = function() +{ + this.stats.active = false; + this.stats.peers = 0; + this.stats.pending = 0; + this.stats.mining = false; + this.stats.hashrate = 0; + this.stats.gasPrice = 0; + this.stats.minerName = false; + this._down++; + + return this; +} + +Node.prototype.setUptime = function() +{ + this.stats.uptime = ((this._tries - this._down) / this._tries) * 100; +} + Node.prototype.getBlock = function(number) { var block = { @@ -294,13 +299,7 @@ Node.prototype.getBlock = function(number) number = "latest"; try { - 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 ); - } + block = this.formatBlock( web3.eth.getBlock(number, true) ); } catch (err) { console.error('getBlock(' + chalk.reset.cyan(number) + '):', err); @@ -311,36 +310,42 @@ Node.prototype.getBlock = function(number) return block; } -Node.prototype.getMinerName = function(miner) +Node.prototype.formatBlock = function (block) { - var result = _.find(this._knownMiners, { miner: miner }); - - if (result !== undefined) + if( !_.isUndefined(block) && !_.isUndefined(block.number) && block.number >= 0 && !_.isUndefined(block.difficulty) && !_.isUndefined(block.totalDifficulty) ) { - return result.name; - } - else - { - if (this._Registrar !== null) - { - var name = this._Registrar.name(miner); + block.difficulty = web3.toDecimal( block.difficulty ); + block.totalDifficulty = web3.toDecimal( block.totalDifficulty ); - if(name.length > 0) - { - this._knownMiners.push({ miner: miner, name: name }); - return name; - } - } - - this._knownMiners.push({ miner: miner, name: false }); + return block; } return false; } -Node.prototype.uptime = function() +Node.prototype.getStatsBlock = function () { - this.stats.uptime = ((this._tries - this._down) / this._tries) * 100; + if(this._socket) + this._lastStats = JSON.stringify(this.stats); + + if(this._web3) + { + var start = _.now(); + var block = this.getBlock(); + + if( block ) + { + this.stats.block = block; + console.success("==>", "Got block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - start, 'ms')); + + this.sendUpdate(); + } + else + { + console.error("xx>", "getStatsBlock couldn't fetch block..."); + console.log(block); + } + } } Node.prototype.getStats = function(forced) @@ -383,17 +388,13 @@ Node.prototype.getStats = function(forced) pending: function (callback) { async.nextTick(function () { - var pending; - try { - pending = web3.eth.getBlockTransactionCount('pending'); + web3.eth.getBlockTransactionCount('pending', callback); } catch (err) { console.error('xx>', 'Pending failed: ', err); callback(err, null); } - - callback(null, pending); }); }, mining: function (callback) @@ -414,19 +415,24 @@ Node.prototype.getStats = function(forced) }, hashrate: function (callback) { - async.nextTick(function () { - var hashrate; + if(self.stats.mining) { + async.nextTick(function () { + var hashrate; - try { - hashrate = web3.eth.hashrate; - } - catch (err) { - console.error('xx>', 'Hashrate failed: ', err); - callback(err, null); - } + try { + hashrate = web3.eth.hashrate; + } + catch (err) { + console.error('xx>', 'Hashrate failed: ', err); + callback(err, null); + } - callback(null, hashrate); - }); + callback(null, hashrate); + }); + } + else { + callback(null, 0); + } }, gasPrice: function (callback) { @@ -444,22 +450,22 @@ Node.prototype.getStats = function(forced) callback(null, gasPrice); }); }, - minerName: function (callback) - { - async.nextTick(function () { - var minerName; + // 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); - } + // try { + // minerName = self.getMinerName(self.stats.block.miner); + // } + // catch (err) { + // console.error('xx>', 'minerName failed: ', err); + // callback(err, null); + // } - callback(null, minerName); - }); - } + // callback(null, minerName); + // }); + // } }, function (err, results) { @@ -476,7 +482,6 @@ Node.prototype.getStats = function(forced) results.end = _.now(); results.diff = results.end - results.start; - // console.log('==> Got getStats results: ', results); console.success('==>', 'Got getStats results in', chalk.reset.cyan(results.diff, 'ms')); if(results.peers !== null) @@ -492,38 +497,13 @@ Node.prototype.getStats = function(forced) self.setInactive(); } - self.uptime(); + self.setUptime(); self.sendUpdate(); }); } } -Node.prototype.getStatsBlock = function () -{ - if(this._socket) - this._lastStats = JSON.stringify(this.stats); - - if(this._web3) - { - var start = _.now(); - var block = this.getBlock(); - var end = _.now(); - - if( !_.isUndefined(block) && !_.isUndefined(block.number) && !_.isUndefined(block.hash) && block.hash !== '?' ) - { - console.success("==>", "Got block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(end - start, 'ms')); - this.stats.block = block; - } - else - { - console.error("xx>", "eth", "getStatsBlock: couldn't fetch block..."); - } - } - - this.sendUpdate(); -} - Node.prototype.getHistory = function (range) { var history = []; @@ -531,25 +511,17 @@ Node.prototype.getHistory = function (range) console.time('=H=', 'his', 'Got history in'); - if( _.isUndefined(range) || range === null) - { - interv = { - min: this.stats.block.number - MAX_HISTORY_UPDATE, - max: this.stats.block.number - 1 - }; - } + if ( _.isUndefined(range) || range === null) + interv = _.range(this.stats.block.number - 1, MAX_HISTORY_UPDATE); - if( !_.isUndefined(range.list) ) - { - interv = { - min: 0, - max: range.list.length - 1 - }; - } + if (!_.isUndefined(range.list)) + interv = range.list; - for (var i = interv.min; i <= interv.max; i++) + console.info('his', 'Getting history from', chalk.reset.cyan(interv[0]), 'to', chalk.reset.cyan(interv[interv.length - 1])); + + for (var i = 0; i < interv.length; i++) { - var block = this.getBlock(( !_.isUndefined(range.list) ? range.list[i] : i)); + var block = this.getBlock( interv[i] ); if( block !== null && !_.isUndefined(block.number) ) { @@ -562,6 +534,33 @@ Node.prototype.getHistory = function (range) return history.reverse(); } +Node.prototype.getMinerName = function(miner) +{ + var result = _.find(this._knownMiners, { miner: miner }); + + if (result !== undefined) + { + return result.name; + } + else + { + if (this._Registrar !== null) + { + var name = this._Registrar.name(miner); + + if(name.length > 0) + { + this._knownMiners.push({ miner: miner, name: name }); + return name; + } + } + + this._knownMiners.push({ miner: miner, name: false }); + } + + return false; +} + Node.prototype.updateBlock = function() { this.getStatsBlock(); diff --git a/lib/utils/logger.js b/lib/utils/logger.js index fc2d736..371e9bc 100644 --- a/lib/utils/logger.js +++ b/lib/utils/logger.js @@ -9,7 +9,8 @@ var signs = [ 'xx>', '===', '>>>', - 'xxx' + 'xxx', + '=H=' ]; var types = [