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

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;