From c73b7b89cd318c5226ba7c96ec57917f8b500449 Mon Sep 17 00:00:00 2001 From: cubedro Date: Tue, 28 Apr 2015 10:41:48 +0300 Subject: [PATCH] added getHistory --- lib/node.js | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/node.js b/lib/node.js index 3773a01..4139b2f 100644 --- a/lib/node.js +++ b/lib/node.js @@ -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));