From 12c089cc10998bb24ad0f258488f6a3733e1c167 Mon Sep 17 00:00:00 2001 From: cubedro Date: Mon, 1 Jun 2015 22:51:31 +0300 Subject: [PATCH] added block as a separate call --- app.js | 21 +++++++++++++++++++++ models/collection.js | 21 +++++++++++++++++++++ models/node.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/app.js b/app.js index 9bdead0..8219aa8 100644 --- a/app.js +++ b/app.js @@ -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) { if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) ) diff --git a/models/collection.js b/models/collection.js index 924e05f..7df84a8 100644 --- a/models/collection.js +++ b/models/collection.js @@ -39,6 +39,27 @@ Collection.prototype.update = function(id, stats) 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 }); diff --git a/models/node.js b/models/node.js index f740353..559faa9 100644 --- a/models/node.js +++ b/models/node.js @@ -141,6 +141,31 @@ Node.prototype.setStats = function(stats, history) 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) { 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) { this.stats.active = active;