added block propagation history

This commit is contained in:
cubedro
2015-04-17 12:10:20 +03:00
parent b6bb6359c8
commit e21a6c9df4
8 changed files with 342 additions and 216 deletions

View File

@@ -1,9 +1,11 @@
var _ = require('lodash');
var Blockchain = require('./history');
var Node = require('./node');
var Collection = function Collection()
{
this._list = [];
this._history = new Blockchain();
this._bestBlock = null;
return this;
@@ -24,36 +26,14 @@ Collection.prototype.update = function(id, stats)
if(!node)
return false;
if(this._bestBlock === null)
{
stats.block.received = (new Date()).getTime();
stats.block.propagation = 0;
this._bestBlock = stats.block;
}
else
{
var oldStats = node.getStats();
var block = this._history.add(stats.block, id);
var propagationHistory = this._history.getNodePropagation(id);
if(stats.block.number !== oldStats.stats.block.number)
{
stats.block.received = (new Date()).getTime();
stats.block.arrived = block.arrived;
stats.block.received = block.received;
stats.block.propagation = block.propagation;
if(this._bestBlock.number < stats.block.number)
{
stats.block.propagation = 0;
this._bestBlock = stats.block;
}
else
{
stats.block.propagation = stats.block.received - this._bestBlock.received;
}
} else {
stats.block.received = oldStats.stats.block.received;
stats.block.propagation = oldStats.stats.block.propagation;
}
}
return node.setStats(stats);
return node.setStats(stats, propagationHistory);
}
Collection.prototype.updateLatency = function(id, latency)
@@ -118,4 +98,9 @@ Collection.prototype.all = function()
return this._list;
}
Collection.prototype.blockPropagationChart = function()
{
return this._history.getBlockPropagation();
}
module.exports = Collection;

159
models/history.js Normal file
View File

@@ -0,0 +1,159 @@
var _ = require('lodash');
var MAX_HISTORY = 1008;
var MAX_PROPAGATION = 36;
var MAX_BLOCK_PROPAGATION = 96;
var History = function History(data)
{
// this._items = new Array(MAX_HISTORY);
this._items = [];
var item = {
height: 0,
block: {
number: 0,
hash: '0x?',
arrived: 0,
received: 0,
propagation: 0,
difficulty: 0,
gasUsed: 0,
transactions: [],
uncles: []
},
propagTimes: []
};
// _.fill(this._items, item);
}
History.prototype.add = function(block, id)
{
var historyBlock = this.search(block.number);
var now = (new Date()).getTime();
block.arrived = now;
block.received = now;
block.propagation = 0;
if(historyBlock)
{
var propIndex = _.findIndex(historyBlock.propagTimes, {node: id});
if(propIndex === -1)
{
block.arrived = historyBlock.block.arrived;
block.received = now;
block.propagation = now - historyBlock.block.received;
historyBlock.propagTimes.push({node: id, received: now, propagation: block.propagation});
}
else
{
block.arrived = historyBlock.block.arrived;
block.received = historyBlock.propagTimes[propIndex].received;
block.propagation = historyBlock.propagTimes[propIndex].propagation;
}
}
else
{
var item = {
height: block.number,
block: block,
propagTimes: []
}
item.propagTimes.push({node: id, received: now, propagation: block.propagation});
console.log('item: ', item);
this._save(item);
}
this.getNodePropagation(id);
return block;
}
History.prototype._save = function(block)
{
this._items.push(block);
if(this._items.length > MAX_HISTORY){
this._items.shift();
}
}
History.prototype.search = function(number)
{
var index = _.findIndex(this._items, {height: number});
if(index < 0)
return false;
return this._items[index];
}
History.prototype.bestBlock = function(obj)
{
return _.max(this._items, 'height');
}
History.prototype.getNodePropagation = function(id)
{
var propagation = new Array(MAX_PROPAGATION);
var bestBlock = this.bestBlock().height;
_.fill(propagation, -1);
var sorted = _(this._items)
.sortByOrder('height', false)
.slice(0, MAX_PROPAGATION)
.reverse()
.forEach(function(n, key)
{
var index = MAX_PROPAGATION - 1 - bestBlock + n.height;
if(index > 0)
{
propagation[index] = _.result(_.find(n.propagTimes, 'node', id), 'propagation', -1);
}
})
.value();
return propagation;
}
History.prototype.getBlockPropagation = function()
{
var propagation = new Array(MAX_BLOCK_PROPAGATION);
var bestBlock = this.bestBlock().height;
var i = 0;
_.fill(propagation, -1);
var sorted = _(this._items)
.sortByOrder('height', false)
.slice(0, MAX_PROPAGATION)
.reverse()
.forEach(function(n, key)
{
if(i < MAX_BLOCK_PROPAGATION)
{
_.forEach(n.propagTimes, function(p, i)
{
propagation.push({block: n.height, propagation: _.result(p, 'propagation', -1)});
propagation.shift();
i++;
});
}
})
.value();
return propagation;
}
History.prototype.history = function()
{
return _.chain(this._items).sortBy('number').reverse().value();
}
module.exports = History;

View File

@@ -1,4 +1,5 @@
var geoip = require('geoip-lite');
var _ = require('lodash');
var MAX_HISTORY = 36;
@@ -31,13 +32,13 @@ var Node = function Node(data)
uptime: 0,
lastUpdate: 0
};
this.blockHistory = [];
this.history = new Array(MAX_HISTORY);
this.uptime = {
started: null,
history: []
};
this.initBlockHistory();
_.fill(this.history, -1);
if(this.id === null) {
this.uptime.started = (new Date()).getTime();
@@ -90,41 +91,14 @@ Node.prototype.setInfo = function(data)
Node.prototype.getInfo = function()
{
return {id: this.id, info: this.info, geo: this.geo, stats: this.stats, history: this.blockHistory};
return {id: this.id, info: this.info, geo: this.geo, stats: this.stats, history: this.history};
}
Node.prototype.initBlockHistory = function()
{
for(var i=0; i < MAX_HISTORY; i++)
{
this.blockHistory.push({
number: 0,
received: 0,
propagation: 0
});
}
}
Node.prototype.setStats = function(stats)
Node.prototype.setStats = function(stats, history)
{
if(typeof stats !== 'undefined' && typeof stats.block !== 'undefined' && typeof stats.block.number !== 'undefined')
{
stats.block.hash = stats.block.hash.replace('0x', '');
if(stats.block.number > this.stats.block.number)
{
if(this.blockHistory.length === MAX_HISTORY )
this.blockHistory.shift();
var history = {
number: stats.block.number,
received: stats.block.received,
propagation: stats.block.propagation
};
this.blockHistory.push(history);
}
this.history = history;
this.stats = stats;
return this.getStats();
@@ -147,7 +121,7 @@ Node.prototype.setLatency = function(latency)
Node.prototype.getStats = function()
{
return {id: this.id, stats: this.stats, history: this.blockHistory};
return {id: this.id, stats: this.stats, history: this.history};
}
Node.prototype.setState = function(active)