Non-blocking collection

This commit is contained in:
cubedro
2015-06-09 02:40:59 +03:00
parent 8673b636e7
commit ca513d71dd
4 changed files with 447 additions and 280 deletions

View File

@@ -6,117 +6,148 @@ var Collection = function Collection()
{
this._items = [];
this._blockchain = new Blockchain();
this._askedForHistory = false;
this._debounced = null;
return this;
}
Collection.prototype.add = function(data)
Collection.prototype.add = function(data, callback)
{
var node = this.getNodeOrNew({ id : data.id }, data);
node.setInfo(data);
return node.getInfo();
node.setInfo(data, callback);
}
Collection.prototype.update = function(id, stats)
Collection.prototype.update = function(id, stats, callback)
{
var node = this.getNode({ id: id });
if (!node)
return false;
var block = this._blockchain.add(stats.block, id);
if (!block)
return false;
var propagationHistory = this._blockchain.getNodePropagation(id);
stats.block.arrived = block.arrived;
stats.block.received = block.received;
stats.block.propagation = block.propagation;
return node.setStats(stats, propagationHistory);
}
Collection.prototype.addBlock = function(id, block)
{
var node = this.getNode({ id: id });
if (!node)
return false;
var block = this._blockchain.add(block, id);
if (!block)
return false;
var propagationHistory = this._blockchain.getNodePropagation(id);
block.arrived = block.arrived;
block.received = block.received;
block.propagation = block.propagation;
return node.setBlock(block, propagationHistory);
}
Collection.prototype.updatePending = function(id, stats)
{
var node = this.getNode({ id: id });
if (!node)
return false;
return node.setPending(stats);
}
Collection.prototype.updateStats = function(id, stats)
{
var node = this.getNode({ id: id });
if (!node)
return false;
return node.setBasicStats(stats);
}
Collection.prototype.addHistory = function(id, blocks)
{
var node = this.getNode({ id: id });
if (!node)
return false;
blocks = blocks.reverse();
for (var i = 0; i <= blocks.length - 1; i++)
{
this._blockchain.add(blocks[i], id);
};
callback('Node not found', null);
}
else
{
var block = this._blockchain.add(stats.block, id);
return this.getCharts();
if (!block)
{
callback('Block data wrong', null);
}
else
{
var propagationHistory = this._blockchain.getNodePropagation(id);
stats.block.arrived = block.block.arrived;
stats.block.received = block.block.received;
stats.block.propagation = block.block.propagation;
node.setStats(stats, propagationHistory, callback);
}
}
}
Collection.prototype.updateLatency = function(id, latency)
Collection.prototype.addBlock = function(id, stats, callback)
{
var node = this.getNode({ id: id });
if (!node)
{
callback('Node not found', null);
}
else
{
var block = this._blockchain.add(stats, id);
if (!block)
{
console.log(block);
callback('Block undefined', null);
}
else
{
var propagationHistory = this._blockchain.getNodePropagation(id);
stats.arrived = block.block.arrived;
stats.received = block.block.received;
stats.propagation = block.block.propagation;
node.setBlock(stats, propagationHistory, callback);
}
}
}
Collection.prototype.updatePending = function(id, stats, callback)
{
var node = this.getNode({ id: id });
if (!node)
return false;
return node.setLatency(latency);
node.setPending(stats, callback);
}
Collection.prototype.inactive = function(id)
Collection.prototype.updateStats = function(id, stats, callback)
{
var node = this.getNode({ id: id });
if (!node)
{
callback('Node not found', null);
}
else
{
node.setBasicStats(stats, callback);
}
}
// TODO: Async series
Collection.prototype.addHistory = function(id, blocks, callback)
{
var node = this.getNode({ id: id });
if (!node)
{
callback('Node not found', null)
}
else
{
blocks = blocks.reverse();
for (var i = 0; i <= blocks.length - 1; i++)
{
this._blockchain.add(blocks[i], id);
};
this.getCharts(callback);
}
this.askedForHistory(false);
}
Collection.prototype.updateLatency = function(id, latency, callback)
{
var node = this.getNode({ id: id });
if (!node)
return false;
node.setLatency(latency, callback);
}
Collection.prototype.inactive = function(id, callback)
{
var node = this.getNode({ spark: id });
if (!node)
return false;
node.setState(false);
return node.getStats();
{
callback('Node not found', null);
}
else
{
node.setState(false);
callback(null, node.getStats());
}
}
Collection.prototype.getIndex = function(search)
@@ -192,9 +223,31 @@ Collection.prototype.getUncleCount = function()
return this._blockchain.getUncleCount();
}
Collection.prototype.setChartsCallback = function(callback)
{
this._blockchain.setCallback(callback);
}
Collection.prototype.getCharts = function()
{
return this._blockchain.getCharts();
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();
}
Collection.prototype.getHistory = function()
@@ -224,4 +277,14 @@ Collection.prototype.requiresUpdate = function(id)
return ( this.canNodeUpdate(id) && this._blockchain.requiresUpdate() );
}
Collection.prototype.askedForHistory = function(set)
{
if( !_.isUndefined(set) )
{
this._askedForHistory = set;
}
return this._askedForHistory;
}
module.exports = Collection;

View File

@@ -3,7 +3,7 @@ var d3 = require('d3');
var MAX_HISTORY = 1000;
var MAX_PEER_PROPAGATION = 36;
var MAX_PEER_PROPAGATION = 40;
var MIN_PROPAGATION_RANGE = 0;
var MAX_PROPAGATION_RANGE = 10000;
@@ -13,6 +13,7 @@ var MAX_BINS = 40;
var History = function History(data)
{
this._items = [];
this._callback = null;
var item = {
height: 0,
@@ -33,6 +34,8 @@ var History = function History(data)
History.prototype.add = function(block, id)
{
var changed = false;
if( !_.isUndefined(block) && !_.isUndefined(block.number) && !_.isUndefined(block.uncles) && !_.isUndefined(block.transactions) && !_.isUndefined(block.difficulty) && block.number > 0 )
{
var historyBlock = this.search(block.number);
@@ -57,6 +60,8 @@ History.prototype.add = function(block, id)
received: now,
propagation: block.propagation
});
changed = true;
}
else
{
@@ -84,6 +89,8 @@ History.prototype.add = function(block, id)
this._items[index].timestamp = block.timestamp;
this._items[index].transactions = block.transactions;
this._items[index].uncles = block.uncles;
changed = true;
}
}
}
@@ -103,14 +110,14 @@ History.prototype.add = function(block, id)
block.time = 0;
}
var item = {
height: block.number,
block: block,
propagTimes: []
}
if( this._items.length === 0 || block.number >= (this.bestBlockNumber() - MAX_HISTORY + 1) )
{
var item = {
height: block.number,
block: block,
propagTimes: []
}
item.propagTimes.push({
node: id,
received: now,
@@ -118,10 +125,15 @@ History.prototype.add = function(block, id)
});
this._save(item);
changed = true;
}
}
return block;
return {
block: block,
changed: changed
};
}
return false;
@@ -386,39 +398,47 @@ History.prototype.getMinersCount = function()
.value();
}
History.prototype.getCharts = function(callback)
History.prototype.setCallback = function(callback)
{
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 = callback;
}
return {
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()
};
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()
});
}
}
History.prototype.requiresUpdate = function()

View File

@@ -62,10 +62,10 @@ Node.prototype.init = function(data)
if( !_.isUndefined(data.latency) )
this.stats.latency = data.latency;
this.setInfo(data);
this.setInfo(data, null);
}
Node.prototype.setInfo = function(data)
Node.prototype.setInfo = function(data, callback)
{
if( !_.isUndefined(data.info) )
{
@@ -85,6 +85,11 @@ Node.prototype.setInfo = function(data)
this.spark = _.result(data, 'spark', null);
this.setState(true);
if(callback !== null)
{
callback(null, this.getInfo());
}
}
Node.prototype.setGeo = function(ip)
@@ -93,7 +98,7 @@ Node.prototype.setGeo = function(ip)
this.geo = geoip.lookup(ip);
}
Node.prototype.getInfo = function()
Node.prototype.getInfo = function(callback)
{
return {
id: this.id,
@@ -115,37 +120,47 @@ Node.prototype.getInfo = function()
};
}
Node.prototype.setStats = function(stats, history)
Node.prototype.setStats = function(stats, history, callback)
{
if( !_.isUndefined(stats) )
{
this.setBlock( _.result(stats, 'block', this.stats.block), history );
this.setBlock( _.result(stats, 'block', this.stats.block), history, function (err, block) {} );
this.setBasicStats(stats);
this.setBasicStats(stats, function (err, stats) {});
this.setPending( _.result(stats, 'pending', this.stats.pending) );
this.setPending( _.result(stats, 'pending', this.stats.pending), function (err, stats) {} );
return this.getStats();
callback(null, this.getStats());
}
return false;
callback('Stats undefined', null);
}
Node.prototype.setBlock = function(block, history)
Node.prototype.setBlock = function(block, history, callback)
{
if( !_.isUndefined(block) && !_.isUndefined(block.number) && ( !_.isEqual(history, this.history) || !_.isEqual(block, this.stats.block) ))
if( !_.isUndefined(block) && !_.isUndefined(block.number) )
{
if(block.number !== this.stats.block.number && block.hash !== this.stats.block.hash)
if ( !_.isEqual(history, this.history) || !_.isEqual(block, this.stats.block) )
{
this.stats.block = block;
if(block.number !== this.stats.block.number || block.hash !== this.stats.block.hash)
{
this.stats.block = block;
}
this.setHistory(history);
callback(null, this.getBlockStats());
}
else
{
callback(null, null);
}
this.setHistory(history);
return this.getBlockStats();
}
return false;
else
{
console.log(block);
callback('Block undefined', null);
}
}
Node.prototype.setHistory = function(history)
@@ -175,24 +190,35 @@ Node.prototype.setHistory = function(history)
return true;
}
Node.prototype.setPending = function(stats)
Node.prototype.setPending = function(stats, callback)
{
if( !_.isUndefined(stats) && !_.isUndefined(stats.pending) && !_.isEqual(stats.pending, this.stats.pending))
if( !_.isUndefined(stats) && !_.isUndefined(stats.pending))
{
this.stats.pending = stats.pending;
if(!_.isEqual(stats.pending, this.stats.pending))
{
this.stats.pending = stats.pending;
return {
id: this.id,
pending: this.stats.pending
};
callback(null, {
id: this.id,
pending: this.stats.pending
});
}
else
{
callback(null, null);
}
}
else
{
callback('Stats undefined', null);
}
return false;
}
Node.prototype.setBasicStats = function(stats)
Node.prototype.setBasicStats = function(stats, callback)
{
if( !_.isUndefined(stats) && !_.isEqual(stats, {
if( !_.isUndefined(stats) )
{
if( !_.isEqual(stats, {
active: this.stats.active,
mining: this.stats.mining,
hashrate: this.stats.hashrate,
@@ -200,33 +226,49 @@ Node.prototype.setBasicStats = function(stats)
gasPrice: this.stats.gasPrice,
uptime: this.stats.uptime
}) )
{
this.stats.active = stats.active;
this.stats.mining = stats.mining;
this.stats.hashrate = stats.hashrate;
this.stats.peers = stats.peers;
this.stats.gasPrice = stats.gasPrice;
this.stats.uptime = stats.uptime;
{
this.stats.active = stats.active;
this.stats.mining = stats.mining;
this.stats.hashrate = stats.hashrate;
this.stats.peers = stats.peers;
this.stats.gasPrice = stats.gasPrice;
this.stats.uptime = stats.uptime;
return this.getBasicStats();
callback(null, this.getBasicStats());
}
else
{
callback(null, null);
}
}
else
{
callback('Stats undefined', null);
}
return false;
}
Node.prototype.setLatency = function(latency)
Node.prototype.setLatency = function(latency, callback)
{
if( !_.isUndefined(latency) && !_.isEqual(latency, this.stats.latency) )
if( !_.isUndefined(latency) )
{
this.stats.latency = latency;
if( !_.isEqual(latency, this.stats.latency) )
{
this.stats.latency = latency;
return {
id: this.id,
latency: latency
};
callback(null, {
id: this.id,
latency: latency
});
}
else
{
callback(null, null);
}
}
else
{
callback('Latency undefined', null);
}
return false;
}
Node.prototype.getStats = function()