ethstats-server/lib/history.js

471 lines
10 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-06-09 01:40:59 +02:00
var MAX_PEER_PROPAGATION = 40;
2015-04-24 05:23:26 +02:00
var MIN_PROPAGATION_RANGE = 0;
2015-05-13 01:44:47 +02:00
var MAX_PROPAGATION_RANGE = 10000;
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 = [];
2015-06-09 01:40:59 +02:00
this._callback = null;
2015-04-17 11:10:20 +02:00
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-06-09 01:40:59 +02:00
var changed = false;
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-06-09 01:40:59 +02:00
changed = true;
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-06-01 23:50:03 +02:00
if(historyBlock.hash !== block.hash || historyBlock.totalDifficulty !== block.totalDifficulty || historyBlock.transactions.length !== block.transactions.length)
{
index = _.findIndex( this._items, { height: block.number } );
this._items[index].hash = block.hash;
this._items[index].parentHash = block.parentHash;
this._items[index].nonce = block.nonce;
this._items[index].sha3Uncles = block.sha3Uncles;
this._items[index].transactionsRoot = block.transactionsRoot;
this._items[index].stateRoot = block.stateRoot;
this._items[index].miner = block.miner;
this._items[index].difficulty = block.difficulty;
this._items[index].totalDifficulty = block.totalDifficulty;
this._items[index].size = block.size;
this._items[index].extraData = block.extraData;
this._items[index].gasLimit = block.gasLimit;
this._items[index].gasUsed = block.gasUsed;
this._items[index].timestamp = block.timestamp;
this._items[index].transactions = block.transactions;
this._items[index].uncles = block.uncles;
2015-06-09 01:40:59 +02:00
changed = true;
2015-06-01 23:50:03 +02:00
}
2015-04-28 01:50:28 +02:00
}
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
{
2015-05-11 19:02:21 +02:00
block.time = Math.max(block.arrived - prevBlock.block.arrived, 0);
2015-04-28 09:43:56 +02:00
if(block.number < this.bestBlock().height)
2015-05-11 19:02:21 +02:00
block.time = Math.max((block.timestamp - prevBlock.block.timestamp) * 1000, 0);
2015-04-28 05:51:04 +02:00
}
else
{
block.time = 0;
}
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-06-09 01:40:59 +02:00
var item = {
height: block.number,
block: block,
propagTimes: []
}
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-06-09 01:40:59 +02:00
changed = true;
2015-04-29 01:10:23 +02:00
}
2015-04-17 11:10:20 +02:00
}
2015-06-09 01:40:59 +02:00
return {
block: block,
changed: changed
};
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-05-27 11:22:17 +02:00
this._items = _.sortByOrder( this._items, 'height', false );
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
}
}
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();
var lastBlocktime = _.now();
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 )
.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
if(index >= 0)
2015-04-17 11:10:20 +02:00
{
var tmpPropagation = _.result(_.find(item.propagTimes, 'node', id), 'propagation', false);
if (_.result(_.find(item.propagTimes, 'node', id), 'propagation', false) !== false)
{
propagation[index] = tmpPropagation;
lastBlocktime = item.block.arrived;
}
else
{
propagation[index] = Math.max(0, lastBlocktime - item.block.arrived);
}
2015-04-17 11:10:20 +02:00
}
})
.reverse()
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-05-13 01:44:47 +02:00
var prop = Math.min(MAX_PROPAGATION_RANGE, _.result(p, 'propagation', -1));
2015-04-24 02:08:12 +02:00
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 data = d3.layout.histogram()
2015-04-29 07:49:43 +02:00
.frequency( false )
2015-05-13 01:44:47 +02:00
.range([ MIN_PROPAGATION_RANGE, MAX_PROPAGATION_RANGE ])
.bins( MAX_BINS )
2015-04-29 07:49:43 +02:00
( 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-06-09 01:40:59 +02:00
History.prototype.setCallback = function(callback)
2015-04-28 03:45:23 +02:00
{
2015-06-09 01:40:59 +02:00
this._callback = callback;
}
2015-04-28 03:45:23 +02:00
2015-06-09 01:40:59 +02:00
History.prototype.getCharts = function()
{
if(this._callback !== null)
{
var chartHistory = _( this._items )
.sortByOrder( 'height', false )
.slice(0, MAX_BINS)
.reverse()
.map(function (item)
{
return {
height: item.height,
blocktime: item.block.time / 1000,
difficulty: item.block.difficulty,
uncles: item.block.uncles.length,
transactions: item.block.transactions.length,
gasSpending: item.block.gasUsed,
miner: item.block.miner
};
})
.value();
this._callback(null, {
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-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;