ethstats-client/lib/node.js

653 lines
14 KiB
JavaScript
Raw Normal View History

2015-04-29 22:34:52 +02:00
'use strict';
2015-05-05 05:59:48 +02:00
require('./utils/logger.js');
var os = require('os');
2015-04-14 16:29:51 +02:00
var web3 = require('web3');
2015-05-04 22:16:02 +02:00
var async = require('async');
2015-02-17 02:07:40 +01:00
var _ = require('lodash');
2015-02-17 23:15:34 +01:00
var shelljs = require('shelljs');
2015-04-03 18:34:31 +02:00
var debounce = require('debounce');
2015-04-06 20:11:22 +02:00
var registrar = require('./registrar.js');
2015-04-16 23:36:38 +02:00
var pjson = require('./../package.json');
2015-05-05 02:12:41 +02:00
var chalk = require('chalk');
2015-02-12 13:26:47 +01:00
2015-02-16 22:30:21 +01:00
var Primus = require('primus'),
Emitter = require('primus-emit'),
2015-02-23 14:57:59 +01:00
Latency = require('primus-spark-latency'),
2015-04-29 22:34:52 +02:00
Socket, socket;
2015-02-16 22:30:21 +01:00
2015-03-22 22:09:34 +01:00
var ETH_VERSION,
NET_VERSION,
2015-04-17 00:25:47 +02:00
PROTOCOL_VERSION,
2015-04-29 22:34:52 +02:00
API_VERSION,
COINBASE;
2015-04-17 01:10:27 +02:00
2015-03-22 22:09:34 +01:00
var INSTANCE_NAME = process.env.INSTANCE_NAME;
2015-04-29 22:34:52 +02:00
var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret";
2015-03-22 22:09:34 +01:00
2015-04-29 22:34:52 +02:00
var PENDING_WORKS = true;
var MAX_BLOCKS_HISTORY = 40;
var UPDATE_INTERVAL = 5000;
var PING_INTERVAL = 2000;
var MINERS_LIMIT = 5;
var MAX_HISTORY_UPDATE = 50;
var MAX_CONNECTION_ATTEMPTS = 15;
2015-05-26 22:02:45 +02:00
var CONNECTION_ATTEMPTS_TIMEOUT = 1000;
2015-03-22 22:09:34 +01:00
2015-02-16 22:30:21 +01:00
Socket = Primus.createSocket({
transformer: 'websockets',
pathname: '/api',
2015-05-11 15:24:18 +02:00
timeout: 120000,
2015-05-11 19:55:12 +02:00
strategy: 'disconnect,online,timeout',
2015-02-23 14:57:59 +01:00
plugin: {emitter: Emitter, sparkLatency: Latency}
2015-02-16 22:30:21 +01:00
});
2015-03-22 22:09:34 +01:00
if(process.env.NODE_ENV === 'production' && INSTANCE_NAME === "")
2015-02-17 23:15:34 +01:00
{
2015-05-09 16:46:30 +02:00
INSTANCE_NAME = shelljs.exec('ec2metadata --instance-id 2>/dev/null', {silent: true}).output;
2015-02-17 23:15:34 +01:00
}
2015-05-05 05:59:48 +02:00
console.log('');
console.info('NET STATS CLIENT');
console.success('v' + pjson.version);
console.log('');
console.log('');
2015-02-12 13:26:47 +01:00
2015-05-05 02:12:41 +02:00
function Node ()
2015-02-11 23:54:33 +01:00
{
this.info = {
2015-02-17 23:15:34 +01:00
name: INSTANCE_NAME || (process.env.EC2_INSTANCE_ID || os.hostname()),
2015-04-16 23:36:38 +02:00
contact: (process.env.CONTACT_DETAILS || ""),
2015-04-29 22:34:52 +02:00
coinbase: null,
node: null,
net: null,
protocol: null,
api: null,
2015-04-16 23:36:38 +02:00
port: (process.env.LISTENING_PORT || 30303),
2015-02-12 13:26:47 +01:00
os: os.platform(),
2015-04-16 23:36:38 +02:00
os_v: os.release(),
2015-04-24 11:38:26 +02:00
client: pjson.version,
2015-04-28 09:41:48 +02:00
canUpdateHistory: true,
2015-02-11 23:54:33 +01:00
};
this.id = _.camelCase(this.info.name);
2015-02-17 02:07:40 +01:00
2015-02-12 13:26:47 +01:00
this.stats = {
2015-02-11 23:54:33 +01:00
active: false,
2015-02-12 13:26:47 +01:00
listening: false,
2015-02-11 23:54:33 +01:00
mining: false,
2015-04-24 00:37:42 +02:00
hashrate: 0,
2015-02-12 13:26:47 +01:00
peers: 0,
pending: 0,
gasPrice: 0,
2015-05-26 22:02:45 +02:00
block: {
number: 0,
hash: '?',
difficulty: 0,
totalDifficulty: 0,
transactions: [],
uncles: []
},
2015-04-06 01:20:14 +02:00
miners: [],
2015-04-16 21:15:42 +02:00
uptime: 0
};
2015-04-29 11:14:40 +02:00
this._lastStats = JSON.stringify(this.stats);
2015-05-04 23:51:16 +02:00
this._lastFetch = 0;
2015-05-26 20:45:33 +02:00
this._startBlockFetch = 0;
this._tries = 0;
this._down = 0;
2015-02-18 03:59:03 +01:00
this._lastSent = 0;
2015-04-03 06:00:26 +02:00
this._latency = 0;
2015-02-11 23:54:33 +01:00
2015-04-29 22:34:52 +02:00
this._web3 = false;
this._socket = false;
2015-03-27 20:11:21 +01:00
this.pendingFilter = false;
this.chainFilter = false;
2015-02-12 13:26:47 +01:00
this.updateInterval = false;
2015-04-02 17:22:33 +02:00
this.pingInterval = false;
2015-04-29 22:34:52 +02:00
this.connectionInterval = false;
2015-02-12 13:26:47 +01:00
2015-05-04 22:16:02 +02:00
this._lastChainLog = 0;
2015-04-28 06:05:04 +02:00
this._lastPendingLog = 0;
2015-05-13 15:39:29 +02:00
this._connection_attempts = 0
2015-04-29 22:34:52 +02:00
this.startWeb3Connection();
2015-04-28 06:05:04 +02:00
2015-04-29 22:34:52 +02:00
return this;
}
Node.prototype.startWeb3Connection = function()
{
2015-05-05 05:59:48 +02:00
console.info('Starting web3 connection');
2015-04-29 22:34:52 +02:00
2015-05-08 15:23:54 +02:00
web3.setProvider( new web3.providers.HttpProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8545')) );
2015-04-29 22:34:52 +02:00
this.checkWeb3Connection();
}
Node.prototype.checkWeb3Connection = function()
{
var self = this;
2015-05-05 08:04:27 +02:00
if (!this._web3)
2015-04-29 22:34:52 +02:00
{
try {
var tmp = web3.version.client;
if( !_.isUndefined(tmp) )
{
2015-05-05 05:59:48 +02:00
console.log(' ', tmp);
console.success('Web3 connection established');
2015-04-29 22:34:52 +02:00
this._web3 = true;
this.init();
return true;
}
}
2015-05-05 08:04:27 +02:00
catch (err)
2015-04-29 22:34:52 +02:00
{
2015-05-13 15:39:29 +02:00
if(this._connection_attempts < MAX_CONNECTION_ATTEMPTS)
{
console.error('Web3 connection attempt', chalk.cyan('#' + this._connection_attempts++), 'failed');
2015-05-13 15:46:47 +02:00
console.error('Trying again in', chalk.cyan(500 * this._connection_attempts + ' ms'));
2015-04-29 22:34:52 +02:00
2015-05-13 15:39:29 +02:00
setTimeout(function ()
{
self.checkWeb3Connection();
2015-05-26 22:02:45 +02:00
}, CONNECTION_ATTEMPTS_TIMEOUT * this._connection_attempts);
2015-05-13 15:39:29 +02:00
}
else
2015-04-29 22:34:52 +02:00
{
2015-05-13 15:46:47 +02:00
console.error('Web3 connection failed', chalk.cyan(MAX_CONNECTION_ATTEMPTS), 'times. Aborting...');
2015-05-13 15:39:29 +02:00
}
2015-04-29 22:34:52 +02:00
}
}
}
Node.prototype.startSocketConnection = function()
{
2015-05-05 05:59:48 +02:00
console.info('wsc', 'Starting socket connection');
2015-04-29 22:34:52 +02:00
socket = new Socket( process.env.WS_SERVER || 'ws://localhost:3000' );
this.setupSockets();
}
Node.prototype.setupSockets = function()
{
var self = this;
// Setup events
2015-04-29 11:14:40 +02:00
socket.on('open', function open()
{
2015-05-11 19:55:12 +02:00
console.info('wsc', 'The socket connection has been opened.');
2015-05-05 05:59:48 +02:00
console.log(' ', 'Trying to login');
2015-04-29 11:14:40 +02:00
socket.emit('hello', {
id: self.id,
info: self.info,
secret: WS_SECRET
});
2015-03-27 12:38:41 +01:00
})
2015-04-29 22:34:52 +02:00
.on('ready', function()
2015-04-29 11:14:40 +02:00
{
2015-04-29 22:34:52 +02:00
self._socket = true;
2015-05-11 19:55:12 +02:00
console.success('wsc', 'The socket connection has been established.');
2015-05-05 00:31:19 +02:00
self.updateBlock();
self.update(true);
2015-03-27 12:38:41 +01:00
})
2015-04-29 22:34:52 +02:00
.on('data', function incoming(data)
2015-04-29 11:14:40 +02:00
{
2015-05-11 19:55:12 +02:00
console.info('Socket received some data', data);
2015-03-27 12:38:41 +01:00
})
2015-04-29 22:34:52 +02:00
.on('history', function (data)
2015-04-29 11:14:40 +02:00
{
2015-05-05 08:04:27 +02:00
console.info('his', 'Got history request');
2015-04-29 22:34:52 +02:00
2015-05-05 08:24:27 +02:00
self.getHistory( data );
2015-03-27 12:38:41 +01:00
})
2015-04-29 11:14:40 +02:00
.on('node-pong', function(data)
{
var latency = Math.ceil( (_.now() - self._latency) / 2 );
socket.emit('latency', {
id: self.id,
latency: latency
});
2015-04-03 06:00:26 +02:00
})
2015-04-29 22:34:52 +02:00
.on('end', function end()
2015-04-29 11:14:40 +02:00
{
2015-04-29 22:34:52 +02:00
self._socket = false;
2015-05-11 19:55:12 +02:00
console.error('wsc', 'Socket connection end received');
2015-04-28 09:41:48 +02:00
})
2015-04-29 22:34:52 +02:00
.on('error', function error(err)
{
2015-05-05 05:59:48 +02:00
console.error('wsc', 'Socket error:', err);
2015-04-28 09:41:48 +02:00
})
2015-05-11 19:55:12 +02:00
.on('timeout', function ()
2015-04-28 09:41:48 +02:00
{
2015-05-11 19:55:12 +02:00
console.error('wsc', 'Socket connection timeout');
})
.on('close', function ()
{
self._socket = false;
console.error('wsc', 'Socket connection has been closed');
})
.on('offline', function ()
{
self._socket = false;
console.error('wsc', 'Network connection is offline');
})
.on('online', function ()
{
console.info('wsc', 'Network connection is online');
})
.on('reconnect', function ()
{
console.info('wsc', 'Socket reconnect attempt started');
})
.on('reconnect scheduled', function (opts)
{
console.warn('wsc', 'Reconnecting in', opts.scheduled, 'ms');
console.warn('wsc', 'This is attempt', opts.attempt, 'out of', opts.retries);
})
.on('reconnected', function (opts)
{
console.success('wsc', 'Socket reconnected successfully after', opts.duration, 'ms');
})
.on('reconnect timeout', function (err, opts)
{
console.error('wsc', 'Socket reconnect atempt took too long:', err.message);
})
.on('reconnect failed', function (err, opts)
{
console.error('wsc', 'Socket reconnect failed:', err.message);
2015-02-26 22:45:24 +01:00
});
2015-04-29 22:34:52 +02:00
}
2015-04-29 22:34:52 +02:00
Node.prototype.emit = function(message, payload)
{
if(this._socket)
{
try {
socket.emit(message, payload);
2015-05-27 09:00:23 +02:00
console.success('wsc', 'Socket emited message:', chalk.reset.cyan(message));
console.success('wsc', payload);
2015-04-29 22:34:52 +02:00
}
catch (err) {
2015-05-05 05:59:48 +02:00
console.error('wsc', 'Socket emit error:', err);
2015-04-29 22:34:52 +02:00
}
}
2015-02-12 14:38:09 +01:00
}
2015-04-29 22:34:52 +02:00
Node.prototype.getInfo = function()
{
2015-05-05 00:31:19 +02:00
console.info('==>', 'Getting info');
2015-05-05 05:59:48 +02:00
console.time('Got info');
2015-05-05 00:31:19 +02:00
2015-04-29 22:34:52 +02:00
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;
2015-05-05 05:59:48 +02:00
console.timeEnd('Got info');
console.info(this.info);
2015-04-29 22:34:52 +02:00
return true;
}
catch (err) {
console.error("Couldn't get version");
}
return false;
}
2015-05-05 08:04:27 +02:00
Node.prototype.setInactive = function()
{
this.stats.active = false;
this.stats.peers = 0;
this.stats.pending = 0;
this.stats.mining = false;
this.stats.hashrate = 0;
this.stats.gasPrice = 0;
this.stats.minerName = false;
this._down++;
return this;
}
Node.prototype.setUptime = function()
{
this.stats.uptime = ((this._tries - this._down) / this._tries) * 100;
}
Node.prototype.formatBlock = function (block)
2015-04-06 20:11:22 +02:00
{
2015-05-05 08:04:27 +02:00
if( !_.isUndefined(block) && !_.isUndefined(block.number) && block.number >= 0 && !_.isUndefined(block.difficulty) && !_.isUndefined(block.totalDifficulty) )
2015-04-06 20:11:22 +02:00
{
2015-05-26 23:36:48 +02:00
block.difficulty = block.difficulty.toString(10);
block.totalDifficulty = block.totalDifficulty.toString(10);
2015-04-24 11:38:26 +02:00
2015-05-05 08:04:27 +02:00
return block;
2015-04-06 20:11:22 +02:00
}
return false;
}
2015-05-05 08:04:27 +02:00
Node.prototype.getStatsBlock = function ()
2015-02-12 13:26:47 +01:00
{
2015-05-26 20:45:33 +02:00
var self = this;
2015-05-05 08:04:27 +02:00
if(this._socket)
this._lastStats = JSON.stringify(this.stats);
if(this._web3)
{
2015-05-26 20:45:33 +02:00
this._startBlockFetch = _.now();
web3.eth.getBlock('latest', false, function(error, result) {
self.sendStatsBlock(error, result);
});
}
}
2015-05-05 08:04:27 +02:00
2015-05-26 22:02:45 +02:00
Node.prototype.sendStatsBlock = function (error, result)
2015-05-26 20:45:33 +02:00
{
if( !error )
{
2015-05-26 22:02:45 +02:00
var block = this.formatBlock(result);
if(block !== false)
2015-05-05 08:04:27 +02:00
{
2015-05-26 22:02:45 +02:00
if( this.stats.block.number !== block.number )
{
this.stats.block = block;
2015-05-05 08:04:27 +02:00
2015-05-26 22:02:45 +02:00
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'));
2015-05-05 08:04:27 +02:00
}
2015-05-26 20:45:33 +02:00
}
else
{
console.error("xx>", "getStatsBlock couldn't fetch block...");
console.error("xx>", error);
2015-05-05 08:04:27 +02:00
}
2015-02-12 13:26:47 +01:00
}
2015-05-04 23:51:16 +02:00
Node.prototype.getStats = function(forced)
2015-02-12 13:26:47 +01:00
{
2015-05-04 22:16:02 +02:00
var self = this;
2015-05-04 23:51:16 +02:00
var now = _.now();
var lastFetchAgo = now - this._lastFetch;
this._lastFetch = now;
2015-05-04 22:16:02 +02:00
2015-05-04 23:51:16 +02:00
if (this._socket)
this._lastStats = JSON.stringify(this.stats);
2015-05-04 23:51:16 +02:00
if (this._web3 && (lastFetchAgo >= UPDATE_INTERVAL || forced === true))
2015-02-12 13:26:47 +01:00
{
2015-05-05 05:59:48 +02:00
console.info('==>', 'Getting stats')
console.log(' ', 'last update:', chalk.reset.cyan(lastFetchAgo));
console.log(' ', 'forced:', chalk.reset.cyan(forced === true));
2015-05-04 23:51:16 +02:00
2015-05-04 22:16:02 +02:00
async.parallel({
peers: function (callback)
{
2015-05-26 20:45:33 +02:00
web3.net.getPeerCount(callback);
2015-05-04 22:16:02 +02:00
},
pending: function (callback)
{
2015-05-26 20:45:33 +02:00
web3.eth.getBlockTransactionCount('pending', callback);
2015-05-04 22:16:02 +02:00
},
mining: function (callback)
{
2015-05-26 20:45:33 +02:00
web3.eth.getMining(callback);
2015-05-04 22:16:02 +02:00
},
hashrate: function (callback)
{
2015-05-26 20:45:33 +02:00
web3.eth.getHashrate(callback);
2015-05-04 22:16:02 +02:00
},
2015-05-04 23:51:16 +02:00
gasPrice: function (callback)
2015-05-04 22:16:02 +02:00
{
2015-05-26 20:45:33 +02:00
web3.eth.getGasPrice(callback);
}
2015-05-04 22:16:02 +02:00
},
function (err, results)
2015-04-29 11:14:40 +02:00
{
2015-05-04 22:16:02 +02:00
self._tries++;
2015-05-04 16:49:41 +02:00
2015-05-04 22:16:02 +02:00
if (err) {
2015-05-05 05:59:48 +02:00
console.error('xx>', 'getStats error: ', err);
2015-04-16 22:45:23 +02:00
2015-05-04 23:51:16 +02:00
self.setInactive();
2015-05-04 22:16:02 +02:00
return false;
2015-04-03 00:12:28 +02:00
}
2015-05-04 22:16:02 +02:00
results.end = _.now();
2015-05-27 05:46:32 +02:00
results.diff = results.end - self._lastFetch;
2015-05-04 22:16:02 +02:00
2015-05-05 05:59:48 +02:00
console.success('==>', 'Got getStats results in', chalk.reset.cyan(results.diff, 'ms'));
2015-04-24 00:37:42 +02:00
2015-05-04 22:16:02 +02:00
if(results.peers !== null)
2015-04-24 00:37:42 +02:00
{
2015-05-04 22:16:02 +02:00
self.stats.active = true;
2015-05-26 23:36:48 +02:00
self.stats.peers = results.peers;
2015-05-04 22:16:02 +02:00
self.stats.pending = results.pending;
self.stats.mining = results.mining;
self.stats.hashrate = results.hashrate;
2015-05-26 23:36:48 +02:00
self.stats.gasPrice = results.gasPrice.toString(10);
2015-05-04 22:16:02 +02:00
}
else {
2015-05-04 23:51:16 +02:00
self.setInactive();
2015-04-24 00:37:42 +02:00
}
2015-05-05 08:04:27 +02:00
self.setUptime();
2015-05-04 22:16:02 +02:00
self.sendUpdate();
});
}
}
Node.prototype.getHistory = function (range)
2015-04-28 09:41:48 +02:00
{
2015-05-05 08:24:27 +02:00
var self = this;
2015-04-28 09:41:48 +02:00
var history = [];
2015-04-29 11:14:40 +02:00
var interv = {};
2015-05-05 05:59:48 +02:00
console.time('=H=', 'his', 'Got history in');
2015-05-05 02:12:41 +02:00
2015-05-05 08:04:27 +02:00
if ( _.isUndefined(range) || range === null)
interv = _.range(this.stats.block.number - 1, MAX_HISTORY_UPDATE);
2015-04-29 11:14:40 +02:00
2015-05-05 08:04:27 +02:00
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]));
2015-04-28 09:41:48 +02:00
2015-05-05 08:24:27 +02:00
async.mapSeries(interv, function (number, callback)
2015-04-28 09:41:48 +02:00
{
2015-05-26 22:02:45 +02:00
web3.eth.getBlock(number, false, callback);
2015-05-05 08:24:27 +02:00
},
function (err, results)
{
console.timeEnd('=H=', 'his', 'Got history in');
if (err) {
console.error('his', 'history fetch failed:', err);
results = false;
}
2015-05-26 22:02:45 +02:00
else
{
for(var i=0; i < results.length; i++)
{
results[i] = self.formatBlock(results[i]);
}
}
2015-05-05 08:24:27 +02:00
2015-05-27 09:00:23 +02:00
self.emit('history', {
2015-05-05 08:24:27 +02:00
id: self.id,
history: results.reverse()
});
});
2015-04-28 09:41:48 +02:00
}
2015-05-04 22:16:02 +02:00
Node.prototype.updateBlock = function()
{
2015-05-04 22:16:02 +02:00
this.getStatsBlock();
2015-02-18 03:59:03 +01:00
2015-05-04 22:16:02 +02:00
return this;
};
2015-05-04 23:51:16 +02:00
Node.prototype.update = function(forced)
2015-05-04 22:16:02 +02:00
{
2015-05-04 23:51:16 +02:00
this.getStats(forced);
2015-05-04 22:16:02 +02:00
return this;
};
Node.prototype.changed = function ()
{
var changed = ! _.isEqual( this._lastStats, JSON.stringify(this.stats) );
2015-02-18 03:59:03 +01:00
return changed;
}
2015-05-04 22:16:02 +02:00
Node.prototype.prepareStats = function ()
2015-02-12 15:02:44 +01:00
{
return {
id: this.id,
2015-02-12 15:02:44 +01:00
stats: this.stats
};
}
2015-05-04 22:16:02 +02:00
Node.prototype.sendUpdate = function (force)
{
2015-05-27 09:00:23 +02:00
if( this.changed() || force ) {
console.info("wsc", "Sending", chalk.reset.blue((force ? "forced" : "changed")), chalk.bold.white("update"));
this.emit('update', this.prepareStats());
2015-05-27 09:00:23 +02:00
}
}
2015-04-02 17:22:33 +02:00
Node.prototype.ping = function()
{
2015-04-29 11:14:40 +02:00
this._latency = _.now();
2015-05-27 09:00:23 +02:00
socket.emit('node-ping', { id: this.id });
2015-04-02 17:22:33 +02:00
};
2015-02-12 13:26:47 +01:00
Node.prototype.setWatches = function()
{
var self = this;
2015-02-17 02:07:40 +01:00
2015-04-24 11:38:26 +02:00
try {
2015-05-05 02:12:41 +02:00
this.chainFilter = web3.eth.filter('latest');
2015-05-04 22:16:02 +02:00
this.chainFilter.watch( function (log)
2015-04-29 11:14:40 +02:00
{
2015-05-04 22:16:02 +02:00
var now = _.now();
var time = now - self._lastChainLog;
self._lastChainLog = now;
2015-04-28 06:05:04 +02:00
2015-05-05 05:59:48 +02:00
console.info('>>>', 'Chain Filter triggered: ', chalk.reset.cyan(now), '- last trigger:', chalk.reset.cyan(time));
2015-04-28 06:05:04 +02:00
2015-05-04 22:16:02 +02:00
if(time > 50)
{
self.updateBlock();
}
else
{
debounce(function() {
self.updateBlock();
}, 50);
2015-04-24 11:38:26 +02:00
}
});
}
catch (err)
{
2015-05-04 22:16:02 +02:00
console.error("Couldn't set up chain filter");
2015-04-24 11:38:26 +02:00
console.error(err);
}
2015-05-05 00:00:01 +02:00
try {
this.pendingFilter = web3.eth.filter('pending');
this.pendingFilter.watch( function (log)
{
var now = _.now();
var time = now - self._lastPendingLog;
2015-05-05 00:31:19 +02:00
self._lastPendingLog = now;
2015-05-05 00:00:01 +02:00
2015-05-05 05:59:48 +02:00
console.info('>>>', 'Pending Filter triggered', chalk.reset.cyan(now), '- last trigger:', chalk.reset.cyan(time));
2015-05-05 00:00:01 +02:00
if(time > 50)
{
self.update(true);
}
else
{
debounce(function() {
self.update(true);
}, 50);
}
});
}
catch (err)
{
console.error("Couldn't set up pending filter");
console.error(err);
}
2015-05-04 22:16:02 +02:00
2015-04-29 11:14:40 +02:00
this.updateInterval = setInterval( function(){
2015-02-12 13:26:47 +01:00
self.update();
2015-04-02 17:22:33 +02:00
}, UPDATE_INTERVAL);
2015-04-29 11:14:40 +02:00
this.pingInterval = setInterval( function(){
2015-04-02 17:22:33 +02:00
self.ping();
}, PING_INTERVAL);
2015-02-12 13:26:47 +01:00
}
2015-02-16 17:44:26 +01:00
Node.prototype.init = function()
{
2015-05-04 23:51:16 +02:00
// Fetch node info
2015-04-29 22:34:52 +02:00
this.getInfo();
2015-05-04 23:51:16 +02:00
2015-05-05 00:31:19 +02:00
// Start socket connection
this.startSocketConnection();
2015-05-04 23:51:16 +02:00
// Set filters
2015-02-12 13:26:47 +01:00
this.setWatches();
}
Node.prototype.stop = function()
{
2015-02-16 22:53:32 +01:00
if(this._socket)
2015-02-18 07:05:58 +01:00
socket.end();
2015-02-12 15:02:44 +01:00
2015-02-12 13:26:47 +01:00
if(this.updateInterval)
clearInterval(this.updateInterval);
2015-04-02 17:22:33 +02:00
if(this.pingInterval)
clearInterval(this.pingInterval);
2015-03-22 22:22:08 +01:00
web3.reset();
2015-02-12 13:26:47 +01:00
}
2015-04-29 11:14:40 +02:00
module.exports = Node;