added best block check before requesting history

This commit is contained in:
cubedro 2015-04-29 02:33:49 +03:00
parent 4f08b3ae6f
commit 2fa175c24d
4 changed files with 39 additions and 13 deletions

23
app.js
View File

@ -64,16 +64,6 @@ api.on('connection', function(spark) {
var info = Nodes.add(data);
spark.emit('ready');
if(Nodes.getHistory().requiresUpdate() && Nodes.canNodeUpdate(data.id) && (!askedForHistory || (new Date()).getTime() - askedForHistoryTime > 120000))
{
var range = Nodes.getHistory().getHistoryRequestInterval();
console.log("asked " + data.id + " for history");
console.log('interval', range);
spark.emit('history', range);
askedForHistory = true;
askedForHistoryTime = (new Date()).getTime();
}
client.write({action: 'add', data: info});
client.write({action: 'charts', data: Nodes.getCharts()});
}
@ -110,7 +100,8 @@ api.on('connection', function(spark) {
askedForHistory = false;
});
spark.on('node-ping', function(data){
spark.on('node-ping', function(data)
{
spark.emit('node-pong');
});
@ -121,6 +112,16 @@ api.on('connection', function(spark) {
var stats = Nodes.updateLatency(data.id, data.latency);
client.write({action: 'latency', data: stats});
if(Nodes.getHistory().requiresUpdate() && Nodes.canNodeUpdate(data.id) && (!askedForHistory || (new Date()).getTime() - askedForHistoryTime > 120000))
{
var range = Nodes.getHistory().getHistoryRequestInterval();
console.log("asked " + data.id + " for history");
console.log('interval', range);
spark.emit('history', range);
askedForHistory = true;
askedForHistoryTime = (new Date()).getTime();
}
}
});

View File

@ -139,9 +139,19 @@ Collection.prototype.getHistory = function()
Collection.prototype.canNodeUpdate = function(id)
{
var node = this.getNode({id: id});
if(!node)
return false;
return node.canUpdate();
if(node.canUpdate())
{
var diff = this._history.bestBlockNumber() - node.getBlockNumber();
if(diff <= 0)
return true;
}
return false;
}
module.exports = Collection;

View File

@ -128,11 +128,21 @@ History.prototype.prevMaxBlock = function(number)
return this._items[index];
}
History.prototype.bestBlock = function(obj)
History.prototype.bestBlock = function()
{
return _.max(this._items, 'height');
}
History.prototype.bestBlockNumber = function()
{
var best = this.bestBlock();
if(typeof best.height !== 'undefined')
return best.height;
return 0;
}
History.prototype.getNodePropagation = function(id)
{
var propagation = new Array(MAX_PEER_PROPAGATION);

View File

@ -150,6 +150,11 @@ Node.prototype.setState = function(active)
this.uptime.history.push({status: (active ? 'up' : 'down'), time: (new Date()).getTime()});
}
Node.prototype.getBlockNumber = function()
{
return this.stats.block.number;
}
Node.prototype.canUpdate = function()
{
return this.info.canUpdateHistory || false;