From 28821abc89d42f1adbe1325d435530da96c1e3c9 Mon Sep 17 00:00:00 2001 From: cubedro Date: Mon, 4 May 2015 23:16:02 +0300 Subject: [PATCH] made stats calls async --- lib/node.js | 326 +++++++++++++++++++++++++++++++++++---------------- package.json | 1 + 2 files changed, 225 insertions(+), 102 deletions(-) diff --git a/lib/node.js b/lib/node.js index bff0496..7725cfd 100644 --- a/lib/node.js +++ b/lib/node.js @@ -1,6 +1,7 @@ 'use strict'; var web3 = require('web3'); +var async = require('async'); var _ = require('lodash'); var os = require('os'); var shelljs = require('shelljs'); @@ -97,7 +98,7 @@ function Node() this.pingInterval = false; this.connectionInterval = false; - this._lastLatestLog = 0; + this._lastChainLog = 0; this._lastPendingLog = 0; this._called = 0 @@ -293,11 +294,15 @@ Node.prototype.getBlock = function(number) try { block = web3.eth.getBlock(number, true); + console.log('====> block:', block); - if( block.hash != '?' && !_.isUndefined(block.difficulty) ) + 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); @@ -309,9 +314,10 @@ Node.prototype.getBlock = function(number) block = web3.eth.getBlock(number, true); - if(block.hash !== '?' && !_.isUndefined(block.difficulty) ) + if( block.hash !== '?' && !_.isUndefined(block.difficulty) && !_.isUndefined(block.totalDifficulty) ) { block.difficulty = web3.toDecimal( block.difficulty ); + block.totalDifficulty = web3.toDecimal( block.totalDifficulty ); } } catch (err) { @@ -357,72 +363,171 @@ Node.prototype.uptime = function() Node.prototype.getStats = function() { - var start = _.now(); - console.log('==> statsstart: ', start); + var self = this; + if(this._socket) this._lastStats = JSON.stringify(this.stats); if(this._web3) { - var blockStart = _.now(); - console.log('==> blockstart: ', blockStart); - console.log('==> blockstart diff: ', (blockStart - start)); + async.parallel({ + start: function (callback) + { + callback(null, _.now()); + }, + peers: function (callback) + { + setTimeout(function () { + var peers; + + try { + peers = web3.toDecimal(web3.net.peerCount); + } + catch (err) { + console.error('xx> PeerCount failed: ', err); + callback(err, null); + } + + callback(null, peers); + }, 1); + }, + pending: function (callback) + { + setTimeout(function() { + try { + web3.eth.getBlockTransactionCount('pending', callback); + } + catch (err) { + console.error('xx> Pending failed: ', err); + callback(err, null); + } + }, 1); + }, + mining: function (callback) + { + setTimeout(function () { + var mining; + + try { + mining = web3.eth.mining; + } + catch (err) { + console.error('xx> Mining failed: ', err); + callback(err, null); + } + + callback(null, mining); + }, 1); + }, + hashrate: function (callback) + { + setTimeout(function () { + var hashrate; + + try { + hashrate = web3.eth.hashrate; + } + catch (err) { + console.error('xx> Hashrate failed: ', err); + callback(err, null); + } + + callback(null, hashrate); + }, 1); + }, + gasprice: function (callback) + { + setTimeout(function () { + var gasPrice; + + try { + gasPrice = web3.toBigNumber(web3.eth.gasPrice).toString(10); + } + catch (err) { + console.error('xx> gasPrice failed: ', err); + callback(err, null); + } + + callback(null, gasPrice); + }, 1); + } + }, + function (err, results) + { + self._tries++; + + 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++; + + return false; + } + + results.end = _.now(); + results.diff = results.end - results.start; + + // console.log('==> Got getStats results: ', results); + console.log('==> Got getStats results in', results.diff, 'ms'); + + if(results.peers !== null) + { + self.stats.peers = results.peers; + self.stats.active = true; + 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.uptime(); + + self.sendUpdate(); + }); + } +} + +Node.prototype.getStatsBlock = function () +{ + if(this._socket) + this._lastStats = JSON.stringify(this.stats); + + if(this._web3) + { + console.log("==> Getting block"); var block = this.getBlock(); - var blockEnd = _.now(); - console.log('==> blockend: ', blockEnd); - console.log('==> block diff: ', (blockEnd - blockStart)); if( !_.isUndefined(block) && !_.isUndefined(block.number) && !_.isUndefined(block.hash) && block.hash !== '?' ) { + console.log("==> Got block:", block.number); this.stats.block = block; - this.sendUpdate(); - - this.isActive(); - - if(PENDING_WORKS) { - try - { - this.stats.pending = web3.eth.getBlockTransactionCount('pending'); - } - catch (err) - { - PENDING_WORKS = false; - console.error("getBlockTransactionCount('pending'):", err); - } - } - - this.stats.mining = web3.eth.mining; - - if(this.stats.mining) - { - try { - this.stats.hashrate = web3.eth.hashrate; - } - catch (err) - { - console.error('hashrate: ', err); - this.stats.hashrate = 0; - } - } - else - this.stats.hashrate = 0; - - this.stats.gasPrice = web3.toBigNumber(web3.eth.gasPrice).toString(10); } else { - console.error("getStats: couldn't fetch block..."); + console.error("xx> getStatsBlock: couldn't fetch block..."); } } this.uptime(); - - var end = _.now(); - console.log('==> end: ', end); - console.log('==> stats diff: ', (end - start)); + this.sendUpdate(); } -Node.prototype.getHistory = function(range) +Node.prototype.getHistory = function (range) { var history = []; var interv = {}; @@ -443,7 +548,7 @@ Node.prototype.getHistory = function(range) }; } - for(var i = interv.min; i <= interv.max; i++) + for (var i = interv.min; i <= interv.max; i++) { var block = this.getBlock(( !_.isUndefined(range.list) ? range.list[i] : i)); @@ -456,21 +561,32 @@ Node.prototype.getHistory = function(range) return history.reverse(); } -Node.prototype.changed = function() +Node.prototype.updateBlock = function() +{ + console.log('==> Getting block stats'); + + this.getStatsBlock(); + + return this; +}; + +Node.prototype.update = function() +{ + console.log('==> Getting stats'); + + this.getStats(); + + return this; +}; + +Node.prototype.changed = function () { var changed = ! _.isEqual( this._lastStats, JSON.stringify(this.stats) ); - if(this._tries - this._lastSent > 5) - { - this._lastSent = this._tries; - - return true; - } - return changed; } -Node.prototype.prepareStats = function() +Node.prototype.prepareStats = function () { return { id: this.id, @@ -478,36 +594,12 @@ Node.prototype.prepareStats = function() }; } -Node.prototype.sendUpdate = function(force) +Node.prototype.sendUpdate = function (force) { if( this.changed() || force ) this.emit('update', this.prepareStats()); } -Node.prototype.update = function() -{ - this.getStats(); - - this.sendUpdate(); - - return this.stats; -}; - -Node.prototype.updatePending = function() -{ - if(PENDING_WORKS) - { - try { - this.stats.pending = web3.eth.getBlockTransactionCount('pending'); - this.sendUpdate(); - } - catch (err) { - PENDING_WORKS = false; - console.error("getBlockTransactionCount('pending'):", err); - } - } -} - Node.prototype.ping = function() { this._latency = _.now(); @@ -519,34 +611,63 @@ Node.prototype.setWatches = function() var self = this; try { - this.pendingFilter = web3.eth.filter('chain'); - this.pendingFilter.watch( function (log) + this.chainFilter = web3.eth.filter('chain'); + this.chainFilter.watch( function (log) { - if(PENDING_WORKS) { - var now = _.now(); - var time = now - self._lastPendingLog; + var now = _.now(); + var time = now - self._lastChainLog; + self._lastChainLog = now; - if(time > 50) - { - self.update(); - } - else - { - debounce(function() { - self.updatePending(); - }, 50); - } + console.log('>>> Chain Filter triggered: ', now); + console.log('>>> Last chain Filter triggered: ', time); - self._lastPendingLog = now; + if(time > 50) + { + self.updateBlock(); + } + else + { + debounce(function() { + self.updateBlock(); + }, 50); } }); } catch (err) { - console.error("Couldn't set up pending filter"); + console.error("Couldn't set up chain filter"); console.error(err); } + // try { + // this.pendingFilter = web3.eth.filter('pending'); + // this.pendingFilter.watch( function (log) + // { + // var now = _.now(); + // var time = now - self._lastPendingLog; + + // console.log('>>> Pending Filter triggered: ', now); + + // if(time > 50) + // { + // self.update(); + // } + // else + // { + // debounce(function() { + // self.update(); + // }, 50); + // } + + // self._lastPendingLog = now; + // }); + // } + // catch (err) + // { + // console.error("Couldn't set up pending filter"); + // console.error(err); + // } + this.updateInterval = setInterval( function(){ self.update(); }, UPDATE_INTERVAL); @@ -575,6 +696,7 @@ Node.prototype.init = function() this.startSocketConnection(); this.installContract(); this.setWatches(); + this.updateBlock(); this.update(); } diff --git a/package.json b/package.json index 9478efa..9d630b2 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "lib": "./lib" }, "dependencies": { + "async": "^0.9.0", "debounce": "1.0.0", "debug": "2.1.3", "lodash": "3.7.0",