changed underscore to lodash
This commit is contained in:
parent
367b0ed1d2
commit
ad9998753c
52
lib/node.js
52
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');
|
||||
|
@ -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": {
|
||||
|
Loading…
Reference in New Issue
Block a user