added block as a separate call

This commit is contained in:
cubedro 2015-06-01 22:51:31 +03:00
parent 54eacc4a9a
commit 12c089cc10
3 changed files with 77 additions and 0 deletions

21
app.js
View File

@ -144,6 +144,27 @@ api.on('connection', function(spark) {
} }
}); });
spark.on('block', function(data)
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.block) )
{
var stats = Nodes.addBlock(data.id, data.block);
if(stats !== false)
{
client.write({
action: 'block',
data: stats
});
client.write({
action: 'charts',
data: Nodes.getCharts()
});
}
}
});
spark.on('pending', function(data) spark.on('pending', function(data)
{ {
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) ) if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )

View File

@ -39,6 +39,27 @@ Collection.prototype.update = function(id, stats)
return node.setStats(stats, propagationHistory); 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) Collection.prototype.updatePending = function(id, stats)
{ {
var node = this.getNode({ id: id }); var node = this.getNode({ id: id });

View File

@ -141,6 +141,31 @@ Node.prototype.setStats = function(stats, history)
return false; return false;
} }
Node.prototype.setBlock = function(block, history)
{
if( !_.isUndefined(block) && !_.isUndefined(block.block.number) )
{
this.history = history;
var propagationAvg = 0;
var positives = _.filter(history, function(p) {
return p >= 0;
});
if(positives.length > 0)
propagationAvg = Math.round( _.sum(positives) / positives.length );
else
propagationAvg = 0;
this.stats.block = block;
this.stats.propagationAvg = propagationAvg;
return this.getBlockStats();
}
return false;
}
Node.prototype.setPending = function(stats) Node.prototype.setPending = function(stats)
{ {
if( !_.isUndefined(stats) && !_.isUndefined(stats.pending) ) if( !_.isUndefined(stats) && !_.isUndefined(stats.pending) )
@ -180,6 +205,16 @@ Node.prototype.getStats = function()
}; };
} }
Node.prototype.getBlockStats = function()
{
return {
id: this.id,
block: this.stats.block,
propagationAvg: this.stats.propagationAvg,
history: this.history
};
}
Node.prototype.setState = function(active) Node.prototype.setState = function(active)
{ {
this.stats.active = active; this.stats.active = active;