From d0cb83ed2407a71f28ce7580037f2ceea7c69129 Mon Sep 17 00:00:00 2001 From: cubedro Date: Tue, 26 May 2015 23:02:45 +0300 Subject: [PATCH 1/2] refactoring --- lib/node.js | 92 ++++++++++++++++++++++------------------------------- 1 file changed, 38 insertions(+), 54 deletions(-) diff --git a/lib/node.js b/lib/node.js index d3fc007..dfe4a20 100644 --- a/lib/node.js +++ b/lib/node.js @@ -26,8 +26,6 @@ var ETH_VERSION, var INSTANCE_NAME = process.env.INSTANCE_NAME; var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret"; -var Contract = null; - var PENDING_WORKS = true; var MAX_BLOCKS_HISTORY = 40; var UPDATE_INTERVAL = 5000; @@ -35,6 +33,7 @@ var PING_INTERVAL = 2000; var MINERS_LIMIT = 5; var MAX_HISTORY_UPDATE = 50; var MAX_CONNECTION_ATTEMPTS = 15; +var CONNECTION_ATTEMPTS_TIMEOUT = 1000; Socket = Primus.createSocket({ transformer: 'websockets', @@ -82,7 +81,14 @@ function Node () peers: 0, pending: 0, gasPrice: 0, - block: {}, + block: { + number: 0, + hash: '?', + difficulty: 0, + totalDifficulty: 0, + transactions: [], + uncles: [] + }, miners: [], uptime: 0 }; @@ -96,10 +102,6 @@ function Node () this._lastSent = 0; this._latency = 0; - this._Registrar = null; - this._knownMiners = []; - - this._web3 = false; this._socket = false; @@ -157,7 +159,7 @@ Node.prototype.checkWeb3Connection = function() setTimeout(function () { self.checkWeb3Connection(); - }, 1000 * this._connection_attempts); + }, CONNECTION_ATTEMPTS_TIMEOUT * this._connection_attempts); } else { @@ -325,31 +327,6 @@ Node.prototype.setUptime = function() this.stats.uptime = ((this._tries - this._down) / this._tries) * 100; } -Node.prototype.getBlock = function(number) -{ - var block = { - number: 0, - hash: '?', - difficulty: 0, - timestamp: 0, - miner: '' - }; - - if( _.isUndefined(number) ) - number = "latest"; - - try { - block = this.formatBlock( web3.eth.getBlock(number, true) ); - } - catch (err) { - console.error('getBlock(' + chalk.reset.cyan(number) + '):', err); - - return false; - } - - return block; -} - Node.prototype.formatBlock = function (block) { if( !_.isUndefined(block) && !_.isUndefined(block.number) && block.number >= 0 && !_.isUndefined(block.difficulty) && !_.isUndefined(block.totalDifficulty) ) @@ -379,18 +356,31 @@ Node.prototype.getStatsBlock = function () } } -Node.prototype.sendStatsBlock = function (error, block) +Node.prototype.sendStatsBlock = function (error, result) { if( !error ) { - if( _.isUndefined(this.stats.block.number) || (!_.isUndefined(block.number) && this.stats.block.number != block.number) ) - { - this.stats.block = this.formatBlock(block); - console.success("==>", "Got block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - this._startBlockFetch, 'ms')); + var block = this.formatBlock(result); - this.sendUpdate(); + if(block !== false) + { + if( this.stats.block.number !== block.number ) + { + this.stats.block = block; + + console.success("==>", "Got block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - this._startBlockFetch, 'ms')); + + this.sendUpdate(); + } + else + { + console.warn("==>", "Got same block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - this._startBlockFetch, 'ms')); + } + } + else + { + console.error("xx>", "Got bad block:", chalk.reset.cyan(result), 'in', chalk.reset.cyan(_.now() - this._startBlockFetch, 'ms')); } - console.warn("==>", "Got same block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - this._startBlockFetch, 'ms')); } else { @@ -497,20 +487,7 @@ Node.prototype.getHistory = function (range) async.mapSeries(interv, function (number, callback) { - async.nextTick(function () - { - var block; - - try { - block = self.formatBlock(web3.eth.getBlock(number, true)); - } - catch (err) { - console.error('xx>', 'history block failed: ', err); - callback(err, null); - } - - callback(null, block); - }); + web3.eth.getBlock(number, false, callback); }, function (err, results) { @@ -521,6 +498,13 @@ Node.prototype.getHistory = function (range) results = false; } + else + { + for(var i=0; i < results.length; i++) + { + results[i] = self.formatBlock(results[i]); + } + } socket.emit('history', { id: self.id, From 145be0a80bd6a7c39af0363529f9edcf75043cb4 Mon Sep 17 00:00:00 2001 From: cubedro Date: Tue, 26 May 2015 23:07:45 +0300 Subject: [PATCH 2/2] version 0.0.9 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d24ede5..73989cc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "eth-net-intelligence-api", "description": "Ethereum Network Intelligence API", - "version": "0.0.8", + "version": "0.0.9", "private": true, "main": "./app.js", "directories": {