Merge pull request #145 from cubedro/develop

Version 0.0.9
This commit is contained in:
Marian OANCΞA 2015-05-26 23:14:10 +03:00
commit 614f22aee9
2 changed files with 39 additions and 55 deletions

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,

View File

@ -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": {