added external api for best block

This commit is contained in:
cubedro 2015-06-23 23:33:33 +03:00
parent 0e13a477ff
commit 18905b0718
2 changed files with 22 additions and 2 deletions

11
app.js
View File

@ -43,9 +43,18 @@ client = new Primus(server, {
client.use('emit', require('primus-emit')); client.use('emit', require('primus-emit'));
// Init external API
external = new Primus(server, {
transformer: 'websockets',
pathname: '/external',
parser: 'JSON'
});
external.use('emit', require('primus-emit'));
// Init collections // Init collections
var Collection = require('./lib/collection'); var Collection = require('./lib/collection');
var Nodes = new Collection(); var Nodes = new Collection(external);
Nodes.setChartsCallback(function (err, charts) Nodes.setChartsCallback(function (err, charts)
{ {

View File

@ -2,13 +2,15 @@ var _ = require('lodash');
var Blockchain = require('./history'); var Blockchain = require('./history');
var Node = require('./node'); var Node = require('./node');
var Collection = function Collection() var Collection = function Collection(externalAPI)
{ {
this._items = []; this._items = [];
this._blockchain = new Blockchain(); this._blockchain = new Blockchain();
this._askedForHistory = false; this._askedForHistory = false;
this._askedForHistoryTime = 0; this._askedForHistoryTime = 0;
this._debounced = null; this._debounced = null;
this._externalAPI = externalAPI;
this._highestBlock = 0;
return this; return this;
} }
@ -76,6 +78,15 @@ Collection.prototype.addBlock = function(id, stats, callback)
stats.received = block.block.received; stats.received = block.block.received;
stats.propagation = block.block.propagation; stats.propagation = block.block.propagation;
if(block.block.number > this._highestBlock)
{
this._highestBlock = block.block.number;
this._externalAPI.write({
action:"bestBlock",
number: this._highestBlock
});
}
node.setBlock(stats, propagationHistory, callback); node.setBlock(stats, propagationHistory, callback);
} }
} }