ethstats-server/models/history.js

421 lines
8.3 KiB
JavaScript
Raw Normal View History

2015-04-17 11:10:20 +02:00
var _ = require('lodash');
2015-04-24 02:08:12 +02:00
var d3 = require('d3');
2015-04-17 11:10:20 +02:00
2015-04-24 05:23:26 +02:00
var MAX_HISTORY = 1000;
2015-04-24 05:37:52 +02:00
2015-04-23 15:17:31 +02:00
var MAX_PEER_PROPAGATION = 36;
2015-04-24 05:23:26 +02:00
var MIN_PROPAGATION_RANGE = 0;
2015-04-23 15:17:31 +02:00
var MAX_PROPAGATION_RANGE = 20000;
2015-04-24 05:37:52 +02:00
var MAX_UNCLES_PER_BIN = 25;
2015-04-23 15:17:31 +02:00
var MAX_BINS = 40;
2015-04-17 11:10:20 +02:00
var History = function History(data)
{
this._items = [];
var item = {
height: 0,
block: {
number: 0,
hash: '0x?',
arrived: 0,
received: 0,
propagation: 0,
difficulty: 0,
gasUsed: 0,
transactions: [],
uncles: []
},
propagTimes: []
};
}
History.prototype.add = function(block, id)
{
2015-04-29 11:00:01 +02:00
if( !_.isUndefined(block) && !_.isUndefined(block.number) && !_.isUndefined(block.uncles) && !_.isUndefined(block.transactions) && !_.isUndefined(block.difficulty) && block.number > 0 )
2015-04-17 11:10:20 +02:00
{
2015-04-28 01:50:28 +02:00
var historyBlock = this.search(block.number);
2015-04-17 11:10:20 +02:00
2015-04-29 07:49:43 +02:00
var now = _.now();
2015-04-28 01:50:28 +02:00
block.arrived = now;
block.received = now;
block.propagation = 0;
2015-04-29 07:49:43 +02:00
if( historyBlock )
2015-04-17 11:10:20 +02:00
{
2015-04-29 11:00:01 +02:00
var propIndex = _.findIndex( historyBlock.propagTimes, { node: id } );
2015-04-28 01:50:28 +02:00
2015-04-29 07:49:43 +02:00
if( propIndex === -1 )
2015-04-28 01:50:28 +02:00
{
block.arrived = historyBlock.block.arrived;
block.received = now;
block.propagation = now - historyBlock.block.received;
2015-04-17 11:10:20 +02:00
2015-04-29 07:49:43 +02:00
historyBlock.propagTimes.push({
node: id,
received: now,
propagation: block.propagation
});
2015-04-28 01:50:28 +02:00
}
else
{
block.arrived = historyBlock.block.arrived;
block.received = historyBlock.propagTimes[propIndex].received;
block.propagation = historyBlock.propagTimes[propIndex].propagation;
}
2015-04-17 11:10:20 +02:00
}
else
{
2015-04-28 05:51:04 +02:00
var prevBlock = this.prevMaxBlock(block.number);
2015-04-29 07:49:43 +02:00
if( prevBlock )
2015-04-28 05:51:04 +02:00
{
block.time = block.arrived - prevBlock.block.arrived;
2015-04-28 09:43:56 +02:00
if(block.number < this.bestBlock().height)
2015-04-29 07:49:43 +02:00
block.time = (block.timestamp - prevBlock.block.timestamp) * 1000;
2015-04-28 05:51:04 +02:00
}
else
{
block.time = 0;
}
2015-04-29 07:49:43 +02:00
2015-04-28 01:50:28 +02:00
var item = {
height: block.number,
block: block,
propagTimes: []
}
2015-04-29 07:49:43 +02:00
if( this._items.length === 0 || block.number >= (this.bestBlockNumber() - MAX_HISTORY + 1) )
2015-04-29 01:10:23 +02:00
{
2015-04-29 07:49:43 +02:00
item.propagTimes.push({
node: id,
received: now,
propagation: block.propagation
});
2015-04-29 01:10:23 +02:00
this._save(item);
}
2015-04-17 11:10:20 +02:00
}
2015-04-28 01:50:28 +02:00
return block;
2015-04-17 11:10:20 +02:00
}
2015-04-28 01:50:28 +02:00
return false;
2015-04-17 11:10:20 +02:00
}
History.prototype._save = function(block)
{
2015-04-28 09:43:56 +02:00
this._items.unshift(block);
2015-04-17 11:10:20 +02:00
2015-04-29 07:49:43 +02:00
if(this._items.length > MAX_HISTORY)
{
2015-04-28 09:43:56 +02:00
this._items.pop();
2015-04-17 11:10:20 +02:00
}
2015-04-28 05:51:04 +02:00
2015-04-29 07:49:43 +02:00
this._items = _.sortByOrder( this._items, 'height', false );
2015-04-17 11:10:20 +02:00
}
History.prototype.search = function(number)
{
2015-04-29 11:00:01 +02:00
var index = _.findIndex( this._items, { height: number } );
2015-04-17 11:10:20 +02:00
if(index < 0)
return false;
return this._items[index];
}
2015-04-28 05:51:04 +02:00
History.prototype.prevMaxBlock = function(number)
{
2015-04-29 11:00:01 +02:00
var index = _.findIndex(this._items, function (item) {
2015-04-28 05:51:04 +02:00
return item.height < number;
});
if(index < 0)
return false;
return this._items[index];
}
History.prototype.bestBlock = function()
2015-04-17 11:10:20 +02:00
{
return _.max(this._items, 'height');
}
History.prototype.bestBlockNumber = function()
{
var best = this.bestBlock();
2015-04-29 07:49:43 +02:00
if( !_.isUndefined(best.height) )
return best.height;
return 0;
}
2015-04-17 11:10:20 +02:00
History.prototype.getNodePropagation = function(id)
{
2015-04-29 07:49:43 +02:00
var propagation = new Array( MAX_PEER_PROPAGATION );
var bestBlock = this.bestBlockNumber();
2015-04-23 15:17:31 +02:00
2015-04-17 11:10:20 +02:00
_.fill(propagation, -1);
2015-04-29 07:49:43 +02:00
var sorted = _( this._items )
.sortByOrder( 'height', false )
.slice( 0, MAX_PEER_PROPAGATION )
2015-04-17 11:10:20 +02:00
.reverse()
2015-04-29 07:49:43 +02:00
.forEach(function (item, key)
2015-04-17 11:10:20 +02:00
{
2015-04-29 07:49:43 +02:00
var index = MAX_PEER_PROPAGATION - 1 - bestBlock + item.height;
2015-04-23 15:17:31 +02:00
2015-04-17 11:10:20 +02:00
if(index > 0)
{
2015-04-29 07:49:43 +02:00
propagation[index] = _.result(_.find(item.propagTimes, 'node', id), 'propagation', -1);
2015-04-17 11:10:20 +02:00
}
})
.value();
return propagation;
}
History.prototype.getBlockPropagation = function()
{
2015-04-23 15:17:31 +02:00
var propagation = [];
2015-04-28 17:44:41 +02:00
var avgPropagation = 0;
2015-04-17 11:10:20 +02:00
2015-04-29 07:49:43 +02:00
_.forEach(this._items, function (n, key)
2015-04-24 02:08:12 +02:00
{
2015-04-29 07:49:43 +02:00
_.forEach(n.propagTimes, function (p, i)
2015-04-17 11:10:20 +02:00
{
2015-04-24 02:08:12 +02:00
var prop = _.result(p, 'propagation', -1);
if(prop >= 0)
propagation.push(prop);
});
});
2015-04-28 17:44:41 +02:00
if(propagation.length > 0)
{
2015-04-29 07:49:43 +02:00
var avgPropagation = Math.round( _.sum(propagation) / propagation.length );
2015-04-28 17:44:41 +02:00
}
2015-04-24 02:08:12 +02:00
var x = d3.scale.linear()
2015-04-29 07:49:43 +02:00
.domain([ MIN_PROPAGATION_RANGE, MAX_PROPAGATION_RANGE ])
.interpolate( d3.interpolateRound );
2015-04-24 02:08:12 +02:00
var data = d3.layout.histogram()
2015-04-29 07:49:43 +02:00
.frequency( false )
.bins( x.ticks(MAX_BINS) )
( propagation );
2015-04-24 02:08:12 +02:00
var freqCum = 0;
2015-04-29 07:49:43 +02:00
var histogram = data.map(function (val) {
2015-04-24 02:08:12 +02:00
freqCum += val.length;
2015-04-29 07:49:43 +02:00
var cumPercent = ( freqCum / Math.max(1, propagation.length) );
return {
x: val.x,
dx: val.dx,
y: val.y,
frequency: val.length,
cumulative: freqCum,
cumpercent: cumPercent
};
2015-04-24 02:08:12 +02:00
});
2015-04-29 07:49:43 +02:00
return {
histogram: histogram,
avg: avgPropagation
};
2015-04-24 05:23:26 +02:00
}
2015-04-28 03:58:19 +02:00
History.prototype.getUncleCount = function()
2015-04-24 05:23:26 +02:00
{
2015-04-29 07:49:43 +02:00
var uncles = _( this._items )
.sortByOrder( 'height', false )
.map(function (item)
2015-04-24 05:23:26 +02:00
{
return item.block.uncles.length;
})
.value();
2015-04-29 07:49:43 +02:00
var uncleBins = _.fill( Array(MAX_BINS), 0 );
2015-04-24 05:23:26 +02:00
2015-04-29 07:49:43 +02:00
var sumMapper = function (array, key)
{
2015-04-24 05:23:26 +02:00
uncleBins[key] = _.sum(array);
return _.sum(array);
};
2015-04-29 07:49:43 +02:00
_.map(_.chunk( uncles, MAX_UNCLES_PER_BIN ), sumMapper);
2015-04-24 05:23:26 +02:00
return uncleBins;
2015-04-17 11:10:20 +02:00
}
2015-04-28 05:51:04 +02:00
History.prototype.getBlockTimes = function()
{
2015-04-29 07:49:43 +02:00
var blockTimes = _( this._items )
.sortByOrder( 'height', false )
2015-04-28 05:51:04 +02:00
.slice(0, MAX_BINS)
.reverse()
2015-04-29 07:49:43 +02:00
.map(function (item)
2015-04-28 05:51:04 +02:00
{
return item.block.time;
})
.value();
return blockTimes;
}
2015-04-28 06:25:15 +02:00
History.prototype.getDifficulty = function()
{
2015-04-29 07:49:43 +02:00
var difficultyHistory = _( this._items )
.sortByOrder( 'height', false )
2015-04-28 06:25:15 +02:00
.slice(0, MAX_BINS)
.reverse()
2015-04-29 07:49:43 +02:00
.map(function (item)
2015-04-28 06:25:15 +02:00
{
return item.block.difficulty;
})
.value();
return difficultyHistory;
}
2015-04-28 03:58:19 +02:00
History.prototype.getTransactionsCount = function()
2015-04-28 02:52:41 +02:00
{
2015-04-29 07:49:43 +02:00
var txCount = _( this._items )
.sortByOrder( 'height', false )
2015-04-28 03:50:21 +02:00
.slice(0, MAX_BINS)
2015-04-28 02:52:41 +02:00
.reverse()
2015-04-29 07:49:43 +02:00
.map(function (item)
2015-04-28 02:52:41 +02:00
{
return item.block.transactions.length;
})
.value();
2015-04-28 03:02:12 +02:00
return txCount;
2015-04-28 02:52:41 +02:00
}
2015-04-28 03:58:19 +02:00
History.prototype.getGasSpending = function()
2015-04-28 03:49:43 +02:00
{
2015-04-29 07:49:43 +02:00
var gasSpending = _( this._items )
.sortByOrder( 'height', false )
2015-04-28 03:50:21 +02:00
.slice(0, MAX_BINS)
2015-04-28 03:49:43 +02:00
.reverse()
2015-04-29 07:49:43 +02:00
.map(function (item)
2015-04-28 03:49:43 +02:00
{
return item.block.gasUsed;
})
.value();
return gasSpending;
}
2015-04-28 13:04:39 +02:00
History.prototype.getAvgHashrate = function()
{
2015-04-29 07:49:43 +02:00
if( _.isEmpty(this._items) )
2015-04-28 13:04:39 +02:00
return 0;
2015-04-29 07:49:43 +02:00
var blocktimeHistory = _( this._items )
2015-05-05 13:19:33 +02:00
.sortByOrder( 'height', false )
.slice(0, 64)
2015-04-29 07:49:43 +02:00
.map(function (item)
2015-04-28 13:04:39 +02:00
{
return item.block.time;
})
.value();
2015-04-29 11:39:51 +02:00
var avgBlocktime = (_.sum(blocktimeHistory) / blocktimeHistory.length)/1000;
2015-04-28 13:04:39 +02:00
2015-05-05 13:19:33 +02:00
return this.bestBlock().block.difficulty / avgBlocktime;
2015-04-28 13:04:39 +02:00
}
2015-04-29 11:00:01 +02:00
History.prototype.getMinersCount = function()
{
var miners = _( this._items )
.sortByOrder( 'height', false )
.slice(0, MAX_BINS)
.map(function (item)
{
return item.block.miner;
})
.value();
var minerCount = [];
_.forEach( _.countBy(miners), function (cnt, miner)
{
minerCount.push({ miner: miner, name: false, blocks: cnt });
});
return _(minerCount)
.sortByOrder( 'blocks', false )
.slice(0, 5)
.value();
}
2015-04-28 06:25:15 +02:00
History.prototype.getCharts = function()
2015-04-28 03:45:23 +02:00
{
2015-04-29 07:49:43 +02:00
var chartHistory = _( this._items )
.sortByOrder( 'height', false )
2015-04-28 03:50:21 +02:00
.slice(0, MAX_BINS)
2015-04-28 03:45:23 +02:00
.reverse()
2015-04-29 07:49:43 +02:00
.map(function (item)
2015-04-28 03:45:23 +02:00
{
2015-04-29 07:49:43 +02:00
return {
2015-04-28 09:43:56 +02:00
height: item.height,
2015-04-29 07:49:43 +02:00
blocktime: item.block.time / 1000,
2015-04-28 06:25:15 +02:00
difficulty: item.block.difficulty,
uncles: item.block.uncles.length,
transactions: item.block.transactions.length,
2015-04-29 11:00:01 +02:00
gasSpending: item.block.gasUsed,
miner: item.block.miner
2015-04-29 07:49:43 +02:00
};
2015-04-28 03:45:23 +02:00
})
.value();
2015-04-29 07:49:43 +02:00
return {
2015-04-29 11:00:01 +02:00
height : _.pluck( chartHistory, 'height' ),
blocktime : _.pluck( chartHistory, 'blocktime' ),
avgBlocktime : _.sum(_.pluck( chartHistory, 'blocktime' )) / (chartHistory.length === 0 ? 1 : chartHistory.length),
difficulty : _.pluck( chartHistory, 'difficulty' ),
uncles : _.pluck( chartHistory, 'uncles' ),
transactions : _.pluck( chartHistory, 'transactions' ),
gasSpending : _.pluck( chartHistory, 'gasSpending' ),
miners : this.getMinersCount(),
propagation : this.getBlockPropagation(),
uncleCount : this.getUncleCount(),
avgHashrate : this.getAvgHashrate()
2015-04-29 07:49:43 +02:00
};
2015-04-28 09:43:56 +02:00
}
History.prototype.requiresUpdate = function()
{
2015-04-29 07:49:43 +02:00
return ( this._items.length < MAX_HISTORY && !_.isEmpty(this._items) );
2015-04-28 09:43:56 +02:00
}
2015-04-29 07:49:43 +02:00
History.prototype.getHistoryRequestRange = function()
2015-04-28 09:43:56 +02:00
{
2015-04-29 07:49:43 +02:00
if( _.isEmpty(this._items) )
return false;
var blocks = _.pluck( this._items, 'height' );
var best = _.max( blocks );
var range = _.range( _.max([ 0, best - MAX_HISTORY ]), best + 1);
var missing = _.difference( range, blocks );
2015-04-29 07:58:11 +02:00
var max = _.max(missing);
2015-04-29 07:49:43 +02:00
var min = max - Math.min( 50, (MAX_HISTORY - this._items.length + 1) ) + 1;
2015-04-28 09:43:56 +02:00
2015-04-29 11:00:01 +02:00
return {
max: max,
min: min,
list: _( missing ).reverse().slice(0, 50).reverse().value()
};
2015-04-17 11:10:20 +02:00
}
module.exports = History;