refactoring

This commit is contained in:
cubedro 2015-05-26 23:02:45 +03:00
parent 48ea88945a
commit d0cb83ed24

View File

@ -26,8 +26,6 @@ var ETH_VERSION,
var INSTANCE_NAME = process.env.INSTANCE_NAME; var INSTANCE_NAME = process.env.INSTANCE_NAME;
var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret"; var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret";
var Contract = null;
var PENDING_WORKS = true; var PENDING_WORKS = true;
var MAX_BLOCKS_HISTORY = 40; var MAX_BLOCKS_HISTORY = 40;
var UPDATE_INTERVAL = 5000; var UPDATE_INTERVAL = 5000;
@ -35,6 +33,7 @@ var PING_INTERVAL = 2000;
var MINERS_LIMIT = 5; var MINERS_LIMIT = 5;
var MAX_HISTORY_UPDATE = 50; var MAX_HISTORY_UPDATE = 50;
var MAX_CONNECTION_ATTEMPTS = 15; var MAX_CONNECTION_ATTEMPTS = 15;
var CONNECTION_ATTEMPTS_TIMEOUT = 1000;
Socket = Primus.createSocket({ Socket = Primus.createSocket({
transformer: 'websockets', transformer: 'websockets',
@ -82,7 +81,14 @@ function Node ()
peers: 0, peers: 0,
pending: 0, pending: 0,
gasPrice: 0, gasPrice: 0,
block: {}, block: {
number: 0,
hash: '?',
difficulty: 0,
totalDifficulty: 0,
transactions: [],
uncles: []
},
miners: [], miners: [],
uptime: 0 uptime: 0
}; };
@ -96,10 +102,6 @@ function Node ()
this._lastSent = 0; this._lastSent = 0;
this._latency = 0; this._latency = 0;
this._Registrar = null;
this._knownMiners = [];
this._web3 = false; this._web3 = false;
this._socket = false; this._socket = false;
@ -157,7 +159,7 @@ Node.prototype.checkWeb3Connection = function()
setTimeout(function () setTimeout(function ()
{ {
self.checkWeb3Connection(); self.checkWeb3Connection();
}, 1000 * this._connection_attempts); }, CONNECTION_ATTEMPTS_TIMEOUT * this._connection_attempts);
} }
else else
{ {
@ -325,31 +327,6 @@ Node.prototype.setUptime = function()
this.stats.uptime = ((this._tries - this._down) / this._tries) * 100; 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) Node.prototype.formatBlock = function (block)
{ {
if( !_.isUndefined(block) && !_.isUndefined(block.number) && block.number >= 0 && !_.isUndefined(block.difficulty) && !_.isUndefined(block.totalDifficulty) ) if( !_.isUndefined(block) && !_.isUndefined(block.number) && block.number >= 0 && !_.isUndefined(block.difficulty) && !_.isUndefined(block.totalDifficulty) )
@ -379,19 +356,32 @@ Node.prototype.getStatsBlock = function ()
} }
} }
Node.prototype.sendStatsBlock = function (error, block) Node.prototype.sendStatsBlock = function (error, result)
{ {
if( !error ) if( !error )
{ {
if( _.isUndefined(this.stats.block.number) || (!_.isUndefined(block.number) && this.stats.block.number != block.number) ) var block = this.formatBlock(result);
if(block !== false)
{ {
this.stats.block = this.formatBlock(block); 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')); console.success("==>", "Got block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - this._startBlockFetch, 'ms'));
this.sendUpdate(); this.sendUpdate();
} }
else
{
console.warn("==>", "Got same block:", chalk.reset.cyan(block.number), '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
{
console.error("xx>", "Got bad block:", chalk.reset.cyan(result), 'in', chalk.reset.cyan(_.now() - this._startBlockFetch, 'ms'));
}
}
else else
{ {
console.error("xx>", "getStatsBlock couldn't fetch block..."); console.error("xx>", "getStatsBlock couldn't fetch block...");
@ -497,20 +487,7 @@ Node.prototype.getHistory = function (range)
async.mapSeries(interv, function (number, callback) async.mapSeries(interv, function (number, callback)
{ {
async.nextTick(function () web3.eth.getBlock(number, false, callback);
{
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);
});
}, },
function (err, results) function (err, results)
{ {
@ -521,6 +498,13 @@ Node.prototype.getHistory = function (range)
results = false; results = false;
} }
else
{
for(var i=0; i < results.length; i++)
{
results[i] = self.formatBlock(results[i]);
}
}
socket.emit('history', { socket.emit('history', {
id: self.id, id: self.id,