From 76f673bc4ca14fdf0f86bd785c784d6730fd1f81 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Wed, 10 Aug 2016 10:51:42 -0300 Subject: [PATCH 1/3] cache blockheight for each network --- lib/server.js | 18 +++++++++++++++--- test/integration/server.js | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/server.js b/lib/server.js index d30bedd..ad1c871 100644 --- a/lib/server.js +++ b/lib/server.js @@ -2448,16 +2448,28 @@ WalletService.prototype._normalizeTxHistory = function(txs) { }); }; -WalletService._cachedBlockheight = {}; +WalletService._cachedBlockheight; + +WalletService._initBlockchainHeightCache = function() { + if (WalletService._cachedBlockheight) return; + WalletService._cachedBlockheight = { + livenet: {}, + testnet: {} + }; +}; + WalletService._clearBlockchainHeightCache = function() { - WalletService._cachedBlockheight.current = null; + WalletService._initBlockchainHeightCache(); + WalletService._cachedBlockheight.livenet.current = null; + WalletService._cachedBlockheight.testnet.current = null; }; WalletService.prototype._getBlockchainHeight = function(network, cb) { var self = this; var now = Date.now(); - var cache = WalletService._cachedBlockheight; + WalletService._initBlockchainHeightCache(); + var cache = WalletService._cachedBlockheight[network]; function fetchFromBlockchain(cb) { var bc = self._getBlockchainExplorer(network); diff --git a/test/integration/server.js b/test/integration/server.js index 9d61314..b032293 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -5930,7 +5930,7 @@ describe('Wallet service', function() { it('should get real # of confirmations based on current block height', function(done) { var _confirmations = Defaults.CONFIRMATIONS_TO_START_CACHING; Defaults.CONFIRMATIONS_TO_START_CACHING = 6; - WalletService._cachedBlockheight = {}; + WalletService._cachedBlockheight = null; var h = helpers.historyCacheTest(20); _.each(h, function(x, i) { @@ -5989,7 +5989,7 @@ describe('Wallet service', function() { it('should get cached # of confirmations if current height unknown', function(done) { var _confirmations = Defaults.CONFIRMATIONS_TO_START_CACHING; Defaults.CONFIRMATIONS_TO_START_CACHING = 6; - WalletService._cachedBlockheight = {}; + WalletService._cachedBlockheight = null; var h = helpers.historyCacheTest(20); helpers.stubHistory(h); From 8494f79f897f511ded850e7ceb49144e062aede4 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Wed, 10 Aug 2016 11:14:21 -0300 Subject: [PATCH 2/3] reset cache by network --- lib/blockchainmonitor.js | 1 + lib/server.js | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/blockchainmonitor.js b/lib/blockchainmonitor.js index 0be4200..27dcb31 100644 --- a/lib/blockchainmonitor.js +++ b/lib/blockchainmonitor.js @@ -205,6 +205,7 @@ BlockchainMonitor.prototype._handleNewBlock = function(network, hash) { walletId: network, // use network name as wallet id for global notifications data: { hash: hash, + network: network, }, }); diff --git a/lib/server.js b/lib/server.js index ad1c871..672560b 100644 --- a/lib/server.js +++ b/lib/server.js @@ -157,7 +157,8 @@ WalletService.handleIncomingNotification = function(notification, cb) { cb = cb || function() {}; if (!notification || notification.type != 'NewBlock') return cb(); - WalletService._clearBlockchainHeightCache(); + + WalletService._clearBlockchainHeightCache(notification.data.network); return cb(); }; @@ -2458,10 +2459,13 @@ WalletService._initBlockchainHeightCache = function() { }; }; -WalletService._clearBlockchainHeightCache = function() { +WalletService._clearBlockchainHeightCache = function(network) { WalletService._initBlockchainHeightCache(); - WalletService._cachedBlockheight.livenet.current = null; - WalletService._cachedBlockheight.testnet.current = null; + if (!_.contains(['livenet', 'testnet'], network)) { + log.error('Incorrect network in new block: ' + network); + return; + } + WalletService._cachedBlockheight[network].current = null; }; WalletService.prototype._getBlockchainHeight = function(network, cb) { From 7914ce7485278f5377cfd34c7f5acad31073c476 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Wed, 10 Aug 2016 11:20:48 -0300 Subject: [PATCH 3/3] fix test --- test/integration/server.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/integration/server.js b/test/integration/server.js index b032293..1dc045e 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -5962,6 +5962,7 @@ describe('Wallet service', function() { blockchainExplorer.getBlockchainHeight = sinon.stub().callsArgWith(0, null, 200); server._notify('NewBlock', { + network: 'livenet', hash: 'dummy hash', }, { isGlobal: true