Merge pull request #107 from cubedro/develop

Version 0.0.8
This commit is contained in:
Marian OANCΞA 2015-05-05 09:33:27 +03:00
commit 3fb3ba8cc4
4 changed files with 429 additions and 280 deletions

7
app.js
View File

@ -5,13 +5,14 @@ var nodeModel = require('./lib/node');
var node = new nodeModel(); var node = new nodeModel();
var gracefulShutdown = function() { var gracefulShutdown = function() {
console.warn("Received kill signal, shutting down gracefully."); console.log('');
console.error("xxx", "sys", "Received kill signal, shutting down gracefully.");
node.stop(); node.stop();
console.info("Closed node watcher"); console.info("xxx", "sys", "Closed node watcher");
setTimeout(function(){ setTimeout(function(){
console.info("Closed out remaining connections."); console.info("xxx", "sys", "Closed out remaining connections.");
process.exit(0); process.exit(0);
}, 5*1000); }, 5*1000);
} }

View File

@ -1,9 +1,11 @@
'use strict'; 'use strict';
require('./utils/logger.js');
var os = require('os');
var web3 = require('web3'); var web3 = require('web3');
var async = require('async'); var async = require('async');
var _ = require('lodash'); var _ = require('lodash');
var os = require('os');
var shelljs = require('shelljs'); var shelljs = require('shelljs');
var debounce = require('debounce'); var debounce = require('debounce');
var registrar = require('./registrar.js'); var registrar = require('./registrar.js');
@ -46,6 +48,11 @@ if(process.env.NODE_ENV === 'production' && INSTANCE_NAME === "")
INSTANCE_NAME = shelljs.exec('ec2metadata --instance-id', {silent: true}).output; INSTANCE_NAME = shelljs.exec('ec2metadata --instance-id', {silent: true}).output;
} }
console.log('');
console.info('NET STATS CLIENT');
console.success('v' + pjson.version);
console.log('');
console.log('');
function Node () function Node ()
{ {
@ -111,7 +118,7 @@ function Node ()
Node.prototype.startWeb3Connection = function() Node.prototype.startWeb3Connection = function()
{ {
console.info(chalk.blue('==>'), chalk.bold('Starting eth connection')); console.info('Starting web3 connection');
web3.setProvider( new web3.providers.HttpProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080')) ); web3.setProvider( new web3.providers.HttpProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080')) );
@ -122,15 +129,15 @@ Node.prototype.checkWeb3Connection = function()
{ {
var self = this; var self = this;
if(!this._web3) if (!this._web3)
{ {
try { try {
var tmp = web3.version.client; var tmp = web3.version.client;
if( !_.isUndefined(tmp) ) if( !_.isUndefined(tmp) )
{ {
console.log('eth', tmp); console.log(' ', tmp);
console.info('==>', 'Ethereum connection established'); console.success('Web3 connection established');
this._web3 = true; this._web3 = true;
this.init(); this.init();
@ -138,11 +145,11 @@ Node.prototype.checkWeb3Connection = function()
return true; return true;
} }
} }
catch(err) catch (err)
{ {
console.error('xx>', 'Ethereum connection attempt #' + this._called++ + ' failed;'); console.error('Web3 connection attempt', chalk.cyan('#' + this._called++), 'failed');
process.nextTick( function() process.nextTick(function ()
{ {
self.checkWeb3Connection(); self.checkWeb3Connection();
}); });
@ -152,7 +159,7 @@ Node.prototype.checkWeb3Connection = function()
Node.prototype.startSocketConnection = function() Node.prototype.startSocketConnection = function()
{ {
console.info("==> Starting socket connection"); console.info('wsc', 'Starting socket connection');
socket = new Socket( process.env.WS_SERVER || 'ws://localhost:3000' ); socket = new Socket( process.env.WS_SERVER || 'ws://localhost:3000' );
@ -166,8 +173,8 @@ Node.prototype.setupSockets = function()
// Setup events // Setup events
socket.on('open', function open() socket.on('open', function open()
{ {
console.info('==> The connection has been opened.'); console.info('wsc', 'The connection has been opened.');
console.info('Trying to login'); console.log(' ', 'Trying to login');
socket.emit('hello', { socket.emit('hello', {
id: self.id, id: self.id,
@ -178,7 +185,7 @@ Node.prototype.setupSockets = function()
.on('ready', function() .on('ready', function()
{ {
self._socket = true; self._socket = true;
console.info('==> The connection has been established.'); console.success('wsc', 'The connection has been established.');
self.updateBlock(); self.updateBlock();
self.update(true); self.update(true);
@ -189,14 +196,9 @@ Node.prototype.setupSockets = function()
}) })
.on('history', function (data) .on('history', function (data)
{ {
console.info('==> Getting history'); console.info('his', 'Got history request');
var reqHistory = self.getHistory( data ); self.getHistory( data );
socket.emit('history', {
id: self.id,
history: reqHistory
});
}) })
.on('node-pong', function(data) .on('node-pong', function(data)
{ {
@ -210,15 +212,15 @@ Node.prototype.setupSockets = function()
.on('end', function end() .on('end', function end()
{ {
self._socket = false; self._socket = false;
console.error('xx> Socket connection closed'); console.error('wsc', 'Socket connection closed');
}) })
.on('error', function error(err) .on('error', function error(err)
{ {
console.error("socket:", err); console.error('wsc', 'Socket error:', err);
}) })
.on('reconnecting', function reconnecting(opts) .on('reconnecting', function reconnecting(opts)
{ {
console.warn('!!!', 'We are scheduling a reconnect operation', opts); console.warn('wsc', 'We are scheduling a reconnect operation', opts);
}); });
} }
@ -230,11 +232,35 @@ Node.prototype.emit = function(message, payload)
socket.emit(message, payload); socket.emit(message, payload);
} }
catch (err) { catch (err) {
console.error("socket.emit:", err); console.error('wsc', 'Socket emit error:', err);
} }
} }
} }
Node.prototype.getInfo = function()
{
console.info('==>', 'Getting info');
console.time('Got info');
try {
this.info.coinbase = web3.eth.coinbase;
this.info.node = web3.version.client;
this.info.net = web3.version.network;
this.info.protocol = web3.toDecimal(web3.version.ethereum);
this.info.api = web3.version.api;
console.timeEnd('Got info');
console.info(this.info);
return true;
}
catch (err) {
console.error("Couldn't get version");
}
return false;
}
Node.prototype.setInactive = function() Node.prototype.setInactive = function()
{ {
this.stats.active = false; this.stats.active = false;
@ -243,35 +269,15 @@ Node.prototype.setInactive = function()
this.stats.mining = false; this.stats.mining = false;
this.stats.hashrate = 0; this.stats.hashrate = 0;
this.stats.gasPrice = 0; this.stats.gasPrice = 0;
this.stats.miner = false; this.stats.minerName = false;
this._down++; this._down++;
return this; return this;
} }
Node.prototype.getInfo = function() Node.prototype.setUptime = function()
{ {
var start = _.now(); this.stats.uptime = ((this._tries - this._down) / this._tries) * 100;
console.info('==>', 'Getting info');
try {
this.info.coinbase = web3.eth.coinbase;
this.info.node = web3.version.client;
this.info.net = web3.version.network;
this.info.protocol = web3.toDecimal(web3.version.ethereum);
this.info.api = web3.version.api;
console.info('==>', 'Got info in', _.now() - start, 'ms');
console.info(' i ', this.info);
return true;
}
catch (err) {
console.error("Couldn't get version");
}
return false;
} }
Node.prototype.getBlock = function(number) Node.prototype.getBlock = function(number)
@ -288,16 +294,10 @@ Node.prototype.getBlock = function(number)
number = "latest"; number = "latest";
try { try {
block = web3.eth.getBlock(number, true); block = this.formatBlock( web3.eth.getBlock(number, true) );
if( block.hash != '?' && !_.isUndefined(block.difficulty) && !_.isUndefined(block.totalDifficulty) )
{
block.difficulty = web3.toDecimal( block.difficulty );
block.totalDifficulty = web3.toDecimal( block.totalDifficulty );
}
} }
catch (err) { catch (err) {
console.error("getBlock(" + number + "):", err); console.error('getBlock(' + chalk.reset.cyan(number) + '):', err);
return false; return false;
} }
@ -305,6 +305,251 @@ Node.prototype.getBlock = function(number)
return block; return block;
} }
Node.prototype.formatBlock = function (block)
{
if( !_.isUndefined(block) && !_.isUndefined(block.number) && block.number >= 0 && !_.isUndefined(block.difficulty) && !_.isUndefined(block.totalDifficulty) )
{
block.difficulty = web3.toDecimal( block.difficulty );
block.totalDifficulty = web3.toDecimal( block.totalDifficulty );
return block;
}
return false;
}
Node.prototype.getStatsBlock = function ()
{
if(this._socket)
this._lastStats = JSON.stringify(this.stats);
if(this._web3)
{
var start = _.now();
var block = this.getBlock();
if( block )
{
this.stats.block = block;
console.success("==>", "Got block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - start, 'ms'));
this.sendUpdate();
}
else
{
console.error("xx>", "getStatsBlock couldn't fetch block...");
console.log(block);
}
}
}
Node.prototype.getStats = function(forced)
{
var self = this;
var now = _.now();
var lastFetchAgo = now - this._lastFetch;
this._lastFetch = now;
if (this._socket)
this._lastStats = JSON.stringify(this.stats);
if (this._web3 && (lastFetchAgo >= UPDATE_INTERVAL || forced === true))
{
console.info('==>', 'Getting stats')
console.log(' ', 'last update:', chalk.reset.cyan(lastFetchAgo));
console.log(' ', 'forced:', chalk.reset.cyan(forced === true));
async.parallel({
start: function (callback)
{
callback(null, _.now());
},
peers: function (callback)
{
async.nextTick(function () {
var peers;
try {
peers = web3.toDecimal(web3.net.peerCount);
}
catch (err) {
console.error('xx>', 'PeerCount failed: ', err);
callback(err, null);
}
callback(null, peers);
});
},
pending: function (callback)
{
async.nextTick(function () {
try {
web3.eth.getBlockTransactionCount('pending', callback);
}
catch (err) {
console.error('xx>', 'Pending failed: ', err);
callback(err, null);
}
});
},
mining: function (callback)
{
async.nextTick(function () {
var mining;
try {
mining = web3.eth.mining;
}
catch (err) {
console.error('xx>', 'Mining failed: ', err);
callback(err, null);
}
callback(null, mining);
});
},
hashrate: function (callback)
{
if(self.stats.mining) {
async.nextTick(function () {
var hashrate;
try {
hashrate = web3.eth.hashrate;
}
catch (err) {
console.error('xx>', 'Hashrate failed: ', err);
callback(err, null);
}
callback(null, hashrate);
});
}
else {
callback(null, 0);
}
},
gasPrice: function (callback)
{
async.nextTick(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);
});
},
// minerName: function (callback)
// {
// async.nextTick(function () {
// var minerName;
// try {
// minerName = self.getMinerName(self.stats.block.miner);
// }
// catch (err) {
// console.error('xx>', 'minerName failed: ', err);
// callback(err, null);
// }
// callback(null, minerName);
// });
// }
},
function (err, results)
{
self._tries++;
if (err) {
console.error('xx>', 'getStats error: ', err);
self.setInactive();
return false;
}
results.end = _.now();
results.diff = results.end - results.start;
console.success('==>', 'Got getStats results in', chalk.reset.cyan(results.diff, 'ms'));
if(results.peers !== null)
{
self.stats.active = true;
self.stats.peers = results.peers;
self.stats.pending = results.pending;
self.stats.mining = results.mining;
self.stats.hashrate = results.hashrate;
self.stats.gasPrice = results.gasPrice;
}
else {
self.setInactive();
}
self.setUptime();
self.sendUpdate();
});
}
}
Node.prototype.getHistory = function (range)
{
var self = this;
var history = [];
var interv = {};
console.time('=H=', 'his', 'Got history in');
if ( _.isUndefined(range) || range === null)
interv = _.range(this.stats.block.number - 1, MAX_HISTORY_UPDATE);
if (!_.isUndefined(range.list))
interv = range.list;
console.info('his', 'Getting history from', chalk.reset.cyan(interv[0]), 'to', chalk.reset.cyan(interv[interv.length - 1]));
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);
});
},
function (err, results)
{
console.timeEnd('=H=', 'his', 'Got history in');
if (err) {
console.error('his', 'history fetch failed:', err);
results = false;
}
socket.emit('history', {
id: self.id,
history: results.reverse()
});
});
}
Node.prototype.getMinerName = function(miner) Node.prototype.getMinerName = function(miner)
{ {
var result = _.find(this._knownMiners, { miner: miner }); var result = _.find(this._knownMiners, { miner: miner });
@ -332,224 +577,6 @@ Node.prototype.getMinerName = function(miner)
return false; return false;
} }
Node.prototype.uptime = function()
{
this.stats.uptime = ((this._tries - this._down) / this._tries) * 100;
}
Node.prototype.getStats = function(forced)
{
var self = this;
var now = _.now();
var lastFetchAgo = now - this._lastFetch;
this._lastFetch = now;
if (this._socket)
this._lastStats = JSON.stringify(this.stats);
if (this._web3 && (lastFetchAgo >= UPDATE_INTERVAL || forced === true))
{
console.log('==> Getting stats; last update:', lastFetchAgo, '- forced:', (forced === true));
async.parallel({
start: function (callback)
{
callback(null, _.now());
},
peers: function (callback)
{
async.nextTick(function () {
var peers;
try {
peers = web3.toDecimal(web3.net.peerCount);
}
catch (err) {
console.error('xx> PeerCount failed: ', err);
callback(err, null);
}
callback(null, peers);
});
},
pending: function (callback)
{
async.nextTick(function () {
try {
web3.eth.getBlockTransactionCount('pending', callback);
}
catch (err) {
console.error('xx> Pending failed: ', err);
callback(err, null);
}
});
},
mining: function (callback)
{
async.nextTick(function () {
var mining;
try {
mining = web3.eth.mining;
}
catch (err) {
console.error('xx> Mining failed: ', err);
callback(err, null);
}
callback(null, mining);
});
},
hashrate: function (callback)
{
async.nextTick(function () {
var hashrate;
try {
hashrate = web3.eth.hashrate;
}
catch (err) {
console.error('xx> Hashrate failed: ', err);
callback(err, null);
}
callback(null, hashrate);
});
},
gasPrice: function (callback)
{
async.nextTick(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);
});
},
minerName: function (callback)
{
async.nextTick(function () {
var minerName;
try {
minerName = self.getMinerName(self.stats.block.miner);
}
catch (err) {
console.error('xx> minerName failed: ', err);
callback(err, null);
}
callback(null, minerName);
});
}
},
function (err, results)
{
self._tries++;
if (err) {
console.error('xx> getStats error: ', err);
self.setInactive();
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.active = true;
self.stats.peers = results.peers;
self.stats.pending = results.pending;
self.stats.mining = results.mining;
self.stats.hashrate = results.hashrate;
self.stats.gasPrice = results.gasPrice;
}
else {
self.setInactive();
}
self.uptime();
self.sendUpdate();
});
}
}
Node.prototype.getStatsBlock = function ()
{
if(this._socket)
this._lastStats = JSON.stringify(this.stats);
if(this._web3)
{
var start = _.now();
var block = this.getBlock();
var end = _.now();
if( !_.isUndefined(block) && !_.isUndefined(block.number) && !_.isUndefined(block.hash) && block.hash !== '?' )
{
console.log("==> Got block:", block.number, 'in', end - start, 'ms');
this.stats.block = block;
}
else
{
console.error("xx> getStatsBlock: couldn't fetch block...");
}
}
this.sendUpdate();
}
Node.prototype.getHistory = function (range)
{
var history = [];
var interv = {};
console.time('=== Got history in');
if( _.isUndefined(range) || range === null)
{
interv = {
min: this.stats.block.number - MAX_HISTORY_UPDATE,
max: this.stats.block.number - 1
};
}
if( !_.isUndefined(range.list) )
{
interv = {
min: 0,
max: range.list.length - 1
};
}
for (var i = interv.min; i <= interv.max; i++)
{
var block = this.getBlock(( !_.isUndefined(range.list) ? range.list[i] : i));
if( block !== null && !_.isUndefined(block.number) )
{
history.push( block );
}
}
console.timeEnd('=== Got history in');
return history.reverse();
}
Node.prototype.updateBlock = function() Node.prototype.updateBlock = function()
{ {
this.getStatsBlock(); this.getStatsBlock();
@ -603,7 +630,7 @@ Node.prototype.setWatches = function()
var time = now - self._lastChainLog; var time = now - self._lastChainLog;
self._lastChainLog = now; self._lastChainLog = now;
console.log('>>> Chain Filter triggered: ', now, '- last trigger:', time); console.info('>>>', 'Chain Filter triggered: ', chalk.reset.cyan(now), '- last trigger:', chalk.reset.cyan(time));
if(time > 50) if(time > 50)
{ {
@ -631,7 +658,7 @@ Node.prototype.setWatches = function()
var time = now - self._lastPendingLog; var time = now - self._lastPendingLog;
self._lastPendingLog = now; self._lastPendingLog = now;
console.log('>>> Pending Filter triggered: ', now, '- last trigger:', time); console.info('>>>', 'Pending Filter triggered', chalk.reset.cyan(now), '- last trigger:', chalk.reset.cyan(time));
if(time > 50) if(time > 50)
{ {
@ -668,11 +695,11 @@ Node.prototype.installContract = function()
Contract = web3.eth.contract( registrar.desc ); Contract = web3.eth.contract( registrar.desc );
this._Registrar = new Contract( registrar.address ); this._Registrar = new Contract( registrar.address );
console.log('==>', 'Installed Registrar contract in', _.now() - start, 'ms'); console.success('Installed Registrar contract in', chalk.reset.cyan(_.now() - start, 'ms'));
} }
catch (err) catch (err)
{ {
console.error("!!!", "Couldn't set up registrar contract"); console.error("!!!", "eth", "Couldn't set up registrar contract");
console.error(err); console.error(err);
} }
} }

121
lib/utils/logger.js Normal file
View File

@ -0,0 +1,121 @@
'use strict';
var util = require('util');
var chalk = require('chalk');
var signs = [
'==>',
'!!!',
'xx>',
'===',
'>>>',
'xxx',
'=H='
];
var types = [
'eth',
'wsc',
'sys',
'his'
];
[
{
name: "info",
sign: '=i=',
signColor: chalk.blue,
messageColor: chalk.bold,
formatter: function (sign, message)
{
return [sign, message];
}
},
{
name: "success",
inherit: 'log',
sign: '=s=',
signColor: chalk.green,
messageColor: chalk.bold.green,
formatter: function (sign, message)
{
return [sign, message];
}
},
{
name: "warn",
sign: '=!=',
signColor: chalk.yellow,
messageColor: chalk.bold.yellow,
formatter: function (sign, message)
{
return [sign, message];
}
},
{
name: "error",
sign: '=x=',
signColor: chalk.red,
messageColor: chalk.bold.red,
formatter: function (sign, message)
{
return [sign, message];
}
},
{
name: "time",
sign: '=T=',
signColor: chalk.cyan,
messageColor: chalk.bold,
formatter: function (sign, message)
{
return [util.format.apply(util, [sign, message])];
}
},
{
name: "timeEnd",
sign: '=T=',
signColor: chalk.cyan,
messageColor: chalk.bold,
formatter: function (sign, message)
{
return [util.format.apply(util, [sign, message])];
}
}
].forEach( function (item)
{
if(item.inherit !== undefined)
console[item.name] = console[item.inherit];
var fn = console[item.name];
console[item.name] = function ()
{
var args = Array.prototype.slice.call(arguments);
var sign = item.sign;
var section = 'eth';
var message = '';
if (signs.indexOf(args[0]) >= 0)
{
sign = args.splice(0, 1);
}
if (types.indexOf(args[0]) >= 0)
{
section = args.splice(0, 1);
}
sign = item.signColor( '[' + section + '] ' + sign );
if (typeof args[0] === 'object')
{
message = util.inspect( args[0], { depth: null, colors: true } );
}
else {
message = item.messageColor( util.format.apply(util, args) );
}
return fn.apply( this, item.formatter(sign, message) );
}
});

View File

@ -1,7 +1,7 @@
{ {
"name": "eth-net-intelligence-api", "name": "eth-net-intelligence-api",
"description": "Ethereum Network Intelligence API", "description": "Ethereum Network Intelligence API",
"version": "0.0.7", "version": "0.0.8",
"private": true, "private": true,
"main": "./app.js", "main": "./app.js",
"directories": { "directories": {