ethstats-server/lib/collection.js

303 lines
5.5 KiB
JavaScript
Raw Normal View History

2015-02-17 03:04:15 +01:00
var _ = require('lodash');
2015-04-17 11:10:20 +02:00
var Blockchain = require('./history');
2015-02-17 03:04:15 +01:00
var Node = require('./node');
var Collection = function Collection()
{
2015-04-29 07:49:43 +02:00
this._items = [];
this._blockchain = new Blockchain();
2015-06-09 01:40:59 +02:00
this._askedForHistory = false;
this._debounced = null;
2015-02-17 03:04:15 +01:00
return this;
}
2015-06-09 01:40:59 +02:00
Collection.prototype.add = function(data, callback)
2015-02-17 03:04:15 +01:00
{
2015-02-17 06:12:44 +01:00
var node = this.getNodeOrNew({ id : data.id }, data);
2015-06-09 01:40:59 +02:00
node.setInfo(data, callback);
2015-02-17 03:04:15 +01:00
}
2015-06-09 01:40:59 +02:00
Collection.prototype.update = function(id, stats, callback)
2015-02-17 03:04:15 +01:00
{
2015-02-17 06:12:44 +01:00
var node = this.getNode({ id: id });
2015-05-19 10:07:48 +02:00
if (!node)
2015-06-09 01:40:59 +02:00
{
callback('Node not found', null);
}
else
{
this._blockchain.clean(this.getBestBlockFromItems());
2015-06-09 18:28:29 +02:00
var block = this._blockchain.add(stats.block, id, node.trusted);
2015-04-28 01:50:28 +02:00
2015-06-09 01:40:59 +02:00
if (!block)
{
callback('Block data wrong', null);
}
else
{
var propagationHistory = this._blockchain.getNodePropagation(id);
2015-04-17 11:10:20 +02:00
2015-06-09 01:40:59 +02:00
stats.block.arrived = block.block.arrived;
stats.block.received = block.block.received;
stats.block.propagation = block.block.propagation;
2015-04-17 11:10:20 +02:00
2015-06-09 01:40:59 +02:00
node.setStats(stats, propagationHistory, callback);
}
}
2015-02-17 03:04:15 +01:00
}
2015-06-09 01:40:59 +02:00
Collection.prototype.addBlock = function(id, stats, callback)
2015-06-01 21:51:31 +02:00
{
var node = this.getNode({ id: id });
if (!node)
2015-06-09 01:40:59 +02:00
{
callback('Node not found', null);
}
else
{
this._blockchain.clean(this.getBestBlockFromItems());
2015-06-09 18:28:29 +02:00
var block = this._blockchain.add(stats, id, node.trusted);
2015-06-01 21:51:31 +02:00
2015-06-09 01:40:59 +02:00
if (!block)
{
callback('Block undefined', null);
}
else
{
var propagationHistory = this._blockchain.getNodePropagation(id);
2015-06-01 21:51:31 +02:00
2015-06-09 01:40:59 +02:00
stats.arrived = block.block.arrived;
stats.received = block.block.received;
stats.propagation = block.block.propagation;
2015-06-01 21:51:31 +02:00
2015-06-09 01:40:59 +02:00
node.setBlock(stats, propagationHistory, callback);
}
}
2015-06-01 21:51:31 +02:00
}
2015-06-09 01:40:59 +02:00
Collection.prototype.updatePending = function(id, stats, callback)
2015-06-01 20:42:50 +02:00
{
var node = this.getNode({ id: id });
if (!node)
return false;
2015-06-09 01:40:59 +02:00
node.setPending(stats, callback);
2015-06-01 20:42:50 +02:00
}
2015-06-09 01:40:59 +02:00
Collection.prototype.updateStats = function(id, stats, callback)
2015-06-01 23:04:34 +02:00
{
var node = this.getNode({ id: id });
if (!node)
2015-06-09 01:40:59 +02:00
{
callback('Node not found', null);
}
else
{
node.setBasicStats(stats, callback);
}
2015-06-01 23:04:34 +02:00
}
2015-06-09 01:40:59 +02:00
// TODO: Async series
Collection.prototype.addHistory = function(id, blocks, callback)
2015-04-28 09:43:56 +02:00
{
var node = this.getNode({ id: id });
2015-05-19 10:07:48 +02:00
if (!node)
2015-06-09 01:40:59 +02:00
{
callback('Node not found', null)
}
else
{
blocks = blocks.reverse();
2015-04-28 09:43:56 +02:00
this._blockchain.clean(this.getBestBlockFromItems());
2015-06-09 01:40:59 +02:00
for (var i = 0; i <= blocks.length - 1; i++)
{
2015-06-09 18:28:29 +02:00
this._blockchain.add(blocks[i], id, node.trusted, true);
2015-06-09 01:40:59 +02:00
};
2015-04-29 07:58:11 +02:00
2015-06-09 03:03:31 +02:00
this.getCharts();
2015-06-09 01:40:59 +02:00
}
2015-04-28 09:43:56 +02:00
2015-06-09 01:40:59 +02:00
this.askedForHistory(false);
2015-04-28 09:43:56 +02:00
}
2015-06-09 01:40:59 +02:00
Collection.prototype.updateLatency = function(id, latency, callback)
2015-04-03 06:00:06 +02:00
{
var node = this.getNode({ id: id });
2015-05-19 10:07:48 +02:00
if (!node)
2015-04-03 06:00:06 +02:00
return false;
2015-06-09 01:40:59 +02:00
node.setLatency(latency, callback);
2015-04-03 06:00:06 +02:00
}
2015-06-09 01:40:59 +02:00
Collection.prototype.inactive = function(id, callback)
2015-02-18 07:06:41 +01:00
{
var node = this.getNode({ spark: id });
2015-05-19 10:07:48 +02:00
if (!node)
2015-06-09 01:40:59 +02:00
{
callback('Node not found', null);
}
else
{
node.setState(false);
callback(null, node.getStats());
}
2015-02-18 07:06:41 +01:00
}
2015-02-17 03:04:15 +01:00
Collection.prototype.getIndex = function(search)
{
2015-04-29 07:49:43 +02:00
return _.findIndex(this._items, search);
2015-02-17 03:04:15 +01:00
}
Collection.prototype.getNode = function(search)
{
var index = this.getIndex(search);
2015-02-17 06:12:44 +01:00
if(index >= 0)
2015-04-29 07:49:43 +02:00
return this._items[index];
2015-02-17 03:04:15 +01:00
return false;
}
2015-02-17 06:12:44 +01:00
Collection.prototype.getNodeByIndex = function(index)
{
2015-04-29 07:49:43 +02:00
if(this._items[index])
return this._items[index];
2015-02-17 06:12:44 +01:00
return false;
}
Collection.prototype.getIndexOrNew = function(search, data)
{
var index = this.getIndex(search);
2015-04-29 07:49:43 +02:00
return (index >= 0 ? index : this._items.push(new Node(data)) - 1);
2015-02-17 06:12:44 +01:00
}
Collection.prototype.getNodeOrNew = function(search, data)
2015-02-17 03:04:15 +01:00
{
2015-02-17 06:12:44 +01:00
return this.getNodeByIndex(this.getIndexOrNew(search, data));
2015-02-17 03:04:15 +01:00
}
2015-02-17 06:12:44 +01:00
Collection.prototype.all = function()
2015-02-17 03:04:15 +01:00
{
2015-05-11 20:41:37 +02:00
this.removeOldNodes();
2015-04-29 07:49:43 +02:00
return this._items;
2015-02-17 03:04:15 +01:00
}
2015-05-11 20:41:37 +02:00
Collection.prototype.removeOldNodes = function()
{
var deleteList = []
for(var i = this._items.length - 1; i >= 0; i--)
{
2015-06-03 02:51:19 +02:00
if( this._items[i].isInactiveAndOld() )
2015-05-11 20:41:37 +02:00
{
deleteList.push(i);
}
}
if(deleteList.length > 0)
{
for(var i = 0; i < deleteList.length; i++)
{
this._items.splice(deleteList[i], 1);
}
}
}
2015-04-17 11:10:20 +02:00
Collection.prototype.blockPropagationChart = function()
{
2015-04-29 07:49:43 +02:00
return this._blockchain.getBlockPropagation();
2015-04-17 11:10:20 +02:00
}
2015-04-24 05:23:26 +02:00
Collection.prototype.getUncleCount = function()
{
2015-04-29 07:49:43 +02:00
return this._blockchain.getUncleCount();
2015-04-24 05:23:26 +02:00
}
2015-06-09 01:40:59 +02:00
Collection.prototype.setChartsCallback = function(callback)
{
this._blockchain.setCallback(callback);
}
2015-04-28 09:43:56 +02:00
Collection.prototype.getCharts = function()
{
2015-06-09 01:40:59 +02:00
this.getChartsDebounced();
}
Collection.prototype.getChartsDebounced = function()
{
var self = this;
if( this._debounced === null) {
this._debounced = _.debounce(function(){
self._blockchain.getCharts();
}, 1000, {
leading: false,
maxWait: 5000,
trailing: true
});
}
this._debounced();
2015-04-28 09:43:56 +02:00
}
Collection.prototype.getHistory = function()
{
2015-04-29 07:49:43 +02:00
return this._blockchain;
2015-04-28 09:43:56 +02:00
}
Collection.prototype.getBestBlockFromItems = function()
{
2015-06-09 18:28:29 +02:00
return Math.max(this._blockchain.bestBlockNumber(), _.result(_.max(this._items, function(item) {
return ( !item.trusted ? 0 : item.stats.block.number );
}), 'stats.block.number', 0));
}
2015-04-28 10:12:47 +02:00
Collection.prototype.canNodeUpdate = function(id)
{
var node = this.getNode({id: id});
2015-04-28 10:12:47 +02:00
if(!node)
return false;
if(node.canUpdate())
{
2015-04-29 07:49:43 +02:00
var diff = this._blockchain.bestBlockNumber() - node.getBlockNumber();
2015-04-29 07:49:43 +02:00
return (diff <= 0);
}
return false;
2015-04-28 10:12:47 +02:00
}
2015-04-29 07:49:43 +02:00
Collection.prototype.requiresUpdate = function(id)
{
return ( this.canNodeUpdate(id) && this._blockchain.requiresUpdate() );
}
2015-06-09 01:40:59 +02:00
Collection.prototype.askedForHistory = function(set)
{
if( !_.isUndefined(set) )
{
this._askedForHistory = set;
}
return this._askedForHistory;
}
2015-04-29 11:00:01 +02:00
module.exports = Collection;