added getHistory

This commit is contained in:
cubedro 2015-04-28 10:41:48 +03:00
parent 2ac7cba9f4
commit c73b7b89cd

View File

@ -44,6 +44,7 @@ var MAX_BLOCKS_HISTORY = 40;
var UPDATE_INTERVAL = 5000;
var PING_INTERVAL = 2000;
var MINERS_LIMIT = 5;
var MAX_HISTORY_UPDATE = 100;
function Node()
{
@ -71,6 +72,7 @@ function Node()
os: os.platform(),
os_v: os.release(),
client: pjson.version,
canUpdateHistory: true,
coinbase: COINBASE
};
@ -137,14 +139,21 @@ function Node()
})
.on('data', function incoming(data) {
console.info('Received some data', data);
});
socket.on('ready', function()
})
.on('ready', function()
{
self._socket = true;
self.sendUpdate(true);
console.info('The connection has been established.');
})
.on('history', function(data)
{
console.info('Getting history.');
var reqHistory = self.getHistory(data);
console.info('Sending history.');
socket.emit('history', {id: self.id, history: reqHistory});
});
this.init();
@ -426,6 +435,31 @@ Node.prototype.getStats = function()
this.uptime();
}
Node.prototype.getHistory = function(interval)
{
var history = [];
console.log('interval', interval);
if(typeof interval === 'undefined' || interval === null)
{
interval = {
min: this.stats.block.number - MAX_HISTORY_UPDATE,
max: this.stats.block.number - 1
}
}
for(var number = interval.min; number <= interval.max; number++)
{
var block = this.getBlock(number);
if(block !== null && block.number !== this.blocks[0].number)
{
history.push(block);
}
}
return history.reverse();
}
Node.prototype.changed = function()
{
var changed = ! _.isEqual(this._lastStats, JSON.stringify(this.stats));