From ad9998753c6e9b98c8c30f3c67133a3687db6d72 Mon Sep 17 00:00:00 2001 From: cubedro Date: Tue, 17 Feb 2015 03:07:40 +0200 Subject: [PATCH] changed underscore to lodash --- lib/node.js | 52 +++++++++++++++++++++++++++------------------------- package.json | 3 +-- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/lib/node.js b/lib/node.js index 7df2972..bf86773 100644 --- a/lib/node.js +++ b/lib/node.js @@ -1,7 +1,6 @@ var web3 = require('ethereum.js'); -var _ = require('underscore'); +var _ = require('lodash'); var os = require('os'); -var slugs = require('slugs'); var Primus = require('primus'), Emitter = require('primus-emit'), @@ -23,14 +22,16 @@ function Node() var self = this; 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', os: os.platform(), os_v: os.release() }; - this.info.id = this.makeId(); + this.info.id = _.camelCase(this.info.name); + console.log(this.info); + this.stats = { active: false, listening: false, @@ -39,7 +40,7 @@ function Node() pending: 0, gasPrice: 0, block: {}, - blocks: [], + blocktimeAvg: 0, difficulty: [], uptime: { down: 0, @@ -49,6 +50,8 @@ function Node() errors: [] } + this.blocks = []; + this._socket = null; this.pendingWatch = 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'))); - // this.init(); - socket.on('open', function open() { self._socket = true; self.emit('hello', self.info); @@ -72,12 +73,9 @@ function Node() console.log('Received some data', data); }); - return this; -} + this.init(); -Node.prototype.makeId = function() -{ - return slugs(this.info.name + ' ' + this.info.type + ' ' + this.info.os + ' ' + this.info.os_v); + return this; } Node.prototype.isActive = function() @@ -155,9 +153,9 @@ Node.prototype.getLatestBlocks = function() var maxIterations = MAX_BLOCKS_HISTORY; 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); @@ -176,24 +174,27 @@ Node.prototype.getLatestBlocks = function() 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; - this.stats.blocks.pop(); - } + if(this.blocks.length === MAX_BLOCKS_HISTORY) + { + 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() { 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); - self.stats.blocks[key].blocktime = diff; + self.blocks[key].blocktime = diff; return diff; }); @@ -203,14 +204,14 @@ Node.prototype.calculateBlockTimes = 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() { - return difficulty = _.map(this.stats.blocks, function(block) + return difficulty = _.map(this.blocks, function(block) { return block.difficulty; }); @@ -259,10 +260,11 @@ Node.prototype.update = function() Node.prototype.setWatches = function() { var self = this; + this.pendingWatch = web3.eth.watch('pending'); this.pendingWatch.changed(function(log) { console.log('pending changed'); - self.stats.pending = parseInt(log.length); + self.stats.pending = parseInt(log.number); }); this.chainWatch = web3.eth.watch('chain'); diff --git a/package.json b/package.json index a7f3a81..8885523 100644 --- a/package.json +++ b/package.json @@ -10,10 +10,9 @@ "dependencies": { "debug": "~2.0.0", "ethereum.js": "*", + "lodash": "^3.2.0", "primus": "^2.4.12", "primus-emit": "^0.1.2", - "slugs": "^0.1.3", - "underscore": "^1.7.0", "ws": "^0.7.1" }, "scripts": {