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 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,