changed underscore to lodash

This commit is contained in:
cubedro 2015-02-17 03:07:40 +02:00
parent 367b0ed1d2
commit ad9998753c
2 changed files with 28 additions and 27 deletions

View File

@ -1,7 +1,6 @@
var web3 = require('ethereum.js'); var web3 = require('ethereum.js');
var _ = require('underscore'); var _ = require('lodash');
var os = require('os'); var os = require('os');
var slugs = require('slugs');
var Primus = require('primus'), var Primus = require('primus'),
Emitter = require('primus-emit'), Emitter = require('primus-emit'),
@ -23,14 +22,16 @@ function Node()
var self = this; var self = this;
this.info = { this.info = {
name: process.env.EC2_INSTANCE_ID || 'Local Node', name: process.env.EC2_INSTANCE_ID || os.hostname(),
node: process.env.ETH_VERSION || 'eth version 0.8.1', node: process.env.ETH_VERSION || 'eth version 0.8.1',
os: os.platform(), os: os.platform(),
os_v: os.release() os_v: os.release()
}; };
this.info.id = this.makeId(); this.info.id = _.camelCase(this.info.name);
console.log(this.info); console.log(this.info);
this.stats = { this.stats = {
active: false, active: false,
listening: false, listening: false,
@ -39,7 +40,7 @@ function Node()
pending: 0, pending: 0,
gasPrice: 0, gasPrice: 0,
block: {}, block: {},
blocks: [], blocktimeAvg: 0,
difficulty: [], difficulty: [],
uptime: { uptime: {
down: 0, down: 0,
@ -49,6 +50,8 @@ function Node()
errors: [] errors: []
} }
this.blocks = [];
this._socket = null; this._socket = null;
this.pendingWatch = false; this.pendingWatch = false;
this.chainWatch = false; this.chainWatch = false;
@ -56,8 +59,6 @@ function Node()
web3.setProvider(new web3.providers.HttpSyncProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080'))); web3.setProvider(new web3.providers.HttpSyncProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080')));
// this.init();
socket.on('open', function open() { socket.on('open', function open() {
self._socket = true; self._socket = true;
self.emit('hello', self.info); self.emit('hello', self.info);
@ -72,12 +73,9 @@ function Node()
console.log('Received some data', data); console.log('Received some data', data);
}); });
return this; this.init();
}
Node.prototype.makeId = function() return this;
{
return slugs(this.info.name + ' ' + this.info.type + ' ' + this.info.os + ' ' + this.info.os_v);
} }
Node.prototype.isActive = function() Node.prototype.isActive = function()
@ -155,9 +153,9 @@ Node.prototype.getLatestBlocks = function()
var maxIterations = MAX_BLOCKS_HISTORY; var maxIterations = MAX_BLOCKS_HISTORY;
var minBlock = 0; var minBlock = 0;
if(this.stats.blocks.length > 0) if(this.blocks.length > 0)
{ {
maxIterations = Math.min(bestBlock - this.stats.blocks[0].number, MAX_BLOCKS_HISTORY); maxIterations = Math.min(bestBlock - this.blocks[0].number, MAX_BLOCKS_HISTORY);
} }
minBlock = Math.max(0, parseInt(bestBlock) - maxIterations); minBlock = Math.max(0, parseInt(bestBlock) - maxIterations);
@ -176,24 +174,27 @@ Node.prototype.getLatestBlocks = function()
Node.prototype.addBlockHistory = function(block) Node.prototype.addBlockHistory = function(block)
{ {
if(this.stats.blocks.length === MAX_BLOCKS_HISTORY) if(this.blocks.length === 0 || block.number != this.blocks[0].number)
{ {
LOWEST_TIMESTAMP = this.stats.blocks[MAX_BLOCKS_HISTORY - 1].timestamp; if(this.blocks.length === MAX_BLOCKS_HISTORY)
this.stats.blocks.pop(); {
} LOWEST_TIMESTAMP = this.blocks[MAX_BLOCKS_HISTORY - 1].timestamp;
this.blocks.pop();
}
this.stats.blocks.unshift(block); this.blocks.unshift(block);
}
} }
Node.prototype.calculateBlockTimes = function() Node.prototype.calculateBlockTimes = function()
{ {
var self = this; var self = this;
var blockTimes = _.map(this.stats.blocks, function(block, key, list) var blockTimes = _.map(this.blocks, function(block, key, list)
{ {
var diff = block.timestamp - (key < list.length - 1 ? list[key + 1].timestamp : LOWEST_TIMESTAMP); var diff = block.timestamp - (key < list.length - 1 ? list[key + 1].timestamp : LOWEST_TIMESTAMP);
self.stats.blocks[key].blocktime = diff; self.blocks[key].blocktime = diff;
return diff; return diff;
}); });
@ -203,14 +204,14 @@ Node.prototype.calculateBlockTimes = function()
Node.prototype.blockTimesAvg = function() Node.prototype.blockTimesAvg = function()
{ {
var sum = _.reduce(this.stats.blocks, function(memo, block) { return memo + block.blocktime;}, 0); var sum = _.reduce(this.blocks, function(memo, block) { return memo + block.blocktime;}, 0);
return sum/this.stats.blocks.length; return sum/this.blocks.length;
} }
Node.prototype.difficultyChart = function() Node.prototype.difficultyChart = function()
{ {
return difficulty = _.map(this.stats.blocks, function(block) return difficulty = _.map(this.blocks, function(block)
{ {
return block.difficulty; return block.difficulty;
}); });
@ -259,10 +260,11 @@ Node.prototype.update = function()
Node.prototype.setWatches = function() Node.prototype.setWatches = function()
{ {
var self = this; var self = this;
this.pendingWatch = web3.eth.watch('pending'); this.pendingWatch = web3.eth.watch('pending');
this.pendingWatch.changed(function(log) { this.pendingWatch.changed(function(log) {
console.log('pending changed'); console.log('pending changed');
self.stats.pending = parseInt(log.length); self.stats.pending = parseInt(log.number);
}); });
this.chainWatch = web3.eth.watch('chain'); this.chainWatch = web3.eth.watch('chain');

View File

@ -10,10 +10,9 @@
"dependencies": { "dependencies": {
"debug": "~2.0.0", "debug": "~2.0.0",
"ethereum.js": "*", "ethereum.js": "*",
"lodash": "^3.2.0",
"primus": "^2.4.12", "primus": "^2.4.12",
"primus-emit": "^0.1.2", "primus-emit": "^0.1.2",
"slugs": "^0.1.3",
"underscore": "^1.7.0",
"ws": "^0.7.1" "ws": "^0.7.1"
}, },
"scripts": { "scripts": {