ethstats-client/lib/node.js

737 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-06 20:11:22 +02:00
var Contract = null;
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;
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-04-05 20:29:34 +02:00
strategy: 'disconnect,online',
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,
block: {},
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;
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-06 20:11:22 +02:00
this._Registrar = null;
this._knownMiners = [];
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-04-29 22:34:52 +02:00
this._called = 0
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-05 05:59:48 +02:00
console.error('Web3 connection attempt', chalk.cyan('#' + this._called++), 'failed');
2015-04-29 22:34:52 +02:00
setTimeout(function ()
2015-04-29 22:34:52 +02:00
{
self.checkWeb3Connection();
}, 500);
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-05 05:59:48 +02:00
console.info('wsc', 'The connection has been opened.');
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-05 05:59:48 +02:00
console.success('wsc', 'The 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-04-29 22:34:52 +02:00
console.info('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-05 05:59:48 +02:00
console.error('wsc', 'Socket connection closed');
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-04-29 22:34:52 +02:00
.on('reconnecting', function reconnecting(opts)
2015-04-28 09:41:48 +02:00
{
2015-05-05 05:59:48 +02:00
console.warn('wsc', 'We are scheduling a reconnect operation', opts);
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);
}
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;
}
2015-02-12 13:26:47 +01:00
Node.prototype.getBlock = function(number)
{
var block = {
number: 0,
hash: '?',
difficulty: 0,
2015-05-05 00:31:19 +02:00
timestamp: 0,
miner: ''
2015-02-12 13:26:47 +01:00
};
2015-02-11 23:54:33 +01:00
2015-04-30 11:29:40 +02:00
if( _.isUndefined(number) )
2015-04-29 22:34:52 +02:00
number = "latest";
2015-02-11 23:54:33 +01:00
2015-02-12 13:26:47 +01:00
try {
2015-05-05 08:04:27 +02:00
block = this.formatBlock( web3.eth.getBlock(number, true) );
2015-02-12 13:26:47 +01:00
}
catch (err) {
2015-05-05 05:59:48 +02:00
console.error('getBlock(' + chalk.reset.cyan(number) + '):', err);
2015-04-29 11:14:40 +02:00
2015-05-04 23:51:16 +02:00
return false;
2015-02-12 13:26:47 +01:00
}
return block;
}
2015-05-05 08:04:27 +02:00
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-05 08:04:27 +02:00
block.difficulty = web3.toDecimal( block.difficulty );
block.totalDifficulty = web3.toDecimal( block.totalDifficulty );
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-05 08:04:27 +02:00
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);
}
}
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({
start: function (callback)
{
callback(null, _.now());
},
peers: function (callback)
{
2015-05-04 23:51:16 +02:00
async.nextTick(function () {
2015-05-04 22:16:02 +02:00
var peers;
try {
peers = web3.toDecimal(web3.net.peerCount);
}
catch (err) {
2015-05-05 05:59:48 +02:00
console.error('xx>', 'PeerCount failed: ', err);
2015-05-04 22:16:02 +02:00
callback(err, null);
}
callback(null, peers);
2015-05-04 23:51:16 +02:00
});
2015-05-04 22:16:02 +02:00
},
pending: function (callback)
{
2015-05-04 23:51:16 +02:00
async.nextTick(function () {
2015-05-04 22:16:02 +02:00
try {
2015-05-05 08:04:27 +02:00
web3.eth.getBlockTransactionCount('pending', callback);
2015-05-04 22:16:02 +02:00
}
catch (err) {
2015-05-05 05:59:48 +02:00
console.error('xx>', 'Pending failed: ', err);
2015-05-04 22:16:02 +02:00
callback(err, null);
}
2015-05-04 23:51:16 +02:00
});
2015-05-04 22:16:02 +02:00
},
mining: function (callback)
{
2015-05-04 23:51:16 +02:00
async.nextTick(function () {
2015-05-04 22:16:02 +02:00
var mining;
try {
mining = web3.eth.mining;
}
catch (err) {
2015-05-05 05:59:48 +02:00
console.error('xx>', 'Mining failed: ', err);
2015-05-04 22:16:02 +02:00
callback(err, null);
}
callback(null, mining);
2015-05-04 23:51:16 +02:00
});
2015-05-04 22:16:02 +02:00
},
hashrate: function (callback)
{
2015-05-05 08:04:27 +02:00
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);
}
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-04 23:51:16 +02:00
async.nextTick(function () {
2015-05-04 22:16:02 +02:00
var gasPrice;
try {
gasPrice = web3.toBigNumber(web3.eth.gasPrice).toString(10);
}
catch (err) {
2015-05-05 05:59:48 +02:00
console.error('xx>', 'gasPrice failed: ', err);
2015-05-04 22:16:02 +02:00
callback(err, null);
}
callback(null, gasPrice);
2015-05-04 23:51:16 +02:00
});
},
2015-05-05 08:04:27 +02:00
// 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);
// });
// }
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();
results.diff = results.end - results.start;
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-04 23:51:16 +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;
self.stats.gasPrice = results.gasPrice;
}
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-05 08:24:27 +02:00
async.nextTick(function ()
2015-04-28 09:41:48 +02:00
{
2015-05-05 08:24:27 +02:00
var block;
2015-04-28 09:41:48 +02:00
2015-05-05 08:24:27 +02:00
try {
block = self.formatBlock(web3.eth.getBlock(number, true));
}
catch (err) {
console.error('xx>', 'history block failed: ', err);
callback(err, null);
}
2015-05-05 02:12:41 +02:00
2015-05-05 08:24:27 +02:00
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()
});
});
2015-04-28 09:41:48 +02:00
}
2015-05-05 08:04:27 +02:00
Node.prototype.getMinerName = function(miner)
{
var result = _.find(this._knownMiners, { miner: miner });
if (result !== undefined)
{
return result.name;
}
else
{
if (this._Registrar !== null)
{
var name = this._Registrar.name(miner);
if(name.length > 0)
{
this._knownMiners.push({ miner: miner, name: name });
return name;
}
}
this._knownMiners.push({ miner: miner, name: false });
}
return false;
}
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-04-29 11:14:40 +02:00
if( this.changed() || force )
this.emit('update', this.prepareStats());
}
2015-04-02 17:22:33 +02:00
Node.prototype.ping = function()
{
2015-04-29 11:14:40 +02:00
this._latency = _.now();
2015-04-03 05:08:54 +02:00
this.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-04-06 20:11:22 +02:00
Node.prototype.installContract = function()
{
2015-05-05 00:31:19 +02:00
var start = _.now();
2015-04-24 11:38:26 +02:00
try {
2015-04-29 11:14:40 +02:00
Contract = web3.eth.contract( registrar.desc );
this._Registrar = new Contract( registrar.address );
2015-05-05 00:31:19 +02:00
2015-05-05 05:59:48 +02:00
console.success('Installed Registrar contract in', chalk.reset.cyan(_.now() - start, 'ms'));
2015-04-24 11:38:26 +02:00
}
catch (err)
{
2015-05-05 05:59:48 +02:00
console.error("!!!", "eth", "Couldn't set up registrar contract");
2015-04-24 11:38:26 +02:00
console.error(err);
}
2015-04-06 20:11:22 +02: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
// Install Registrar contract
2015-04-06 20:11:22 +02:00
this.installContract();
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;