Browse Source

Merge pull request #557 from isocolsky/ref/confirmations

Fix blockheight cache
activeAddress
Matias Alejo Garcia 9 years ago
committed by GitHub
parent
commit
811246a36f
  1. 1
      lib/blockchainmonitor.js
  2. 26
      lib/server.js
  3. 5
      test/integration/server.js

1
lib/blockchainmonitor.js

@ -205,6 +205,7 @@ BlockchainMonitor.prototype._handleNewBlock = function(network, hash) {
walletId: network, // use network name as wallet id for global notifications walletId: network, // use network name as wallet id for global notifications
data: { data: {
hash: hash, hash: hash,
network: network,
}, },
}); });

26
lib/server.js

@ -157,7 +157,8 @@ WalletService.handleIncomingNotification = function(notification, cb) {
cb = cb || function() {}; cb = cb || function() {};
if (!notification || notification.type != 'NewBlock') return cb(); if (!notification || notification.type != 'NewBlock') return cb();
WalletService._clearBlockchainHeightCache();
WalletService._clearBlockchainHeightCache(notification.data.network);
return cb(); return cb();
}; };
@ -2448,16 +2449,31 @@ WalletService.prototype._normalizeTxHistory = function(txs) {
}); });
}; };
WalletService._cachedBlockheight = {}; WalletService._cachedBlockheight;
WalletService._clearBlockchainHeightCache = function() {
WalletService._cachedBlockheight.current = null; WalletService._initBlockchainHeightCache = function() {
if (WalletService._cachedBlockheight) return;
WalletService._cachedBlockheight = {
livenet: {},
testnet: {}
};
};
WalletService._clearBlockchainHeightCache = function(network) {
WalletService._initBlockchainHeightCache();
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) { WalletService.prototype._getBlockchainHeight = function(network, cb) {
var self = this; var self = this;
var now = Date.now(); var now = Date.now();
var cache = WalletService._cachedBlockheight; WalletService._initBlockchainHeightCache();
var cache = WalletService._cachedBlockheight[network];
function fetchFromBlockchain(cb) { function fetchFromBlockchain(cb) {
var bc = self._getBlockchainExplorer(network); var bc = self._getBlockchainExplorer(network);

5
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) { it('should get real # of confirmations based on current block height', function(done) {
var _confirmations = Defaults.CONFIRMATIONS_TO_START_CACHING; var _confirmations = Defaults.CONFIRMATIONS_TO_START_CACHING;
Defaults.CONFIRMATIONS_TO_START_CACHING = 6; Defaults.CONFIRMATIONS_TO_START_CACHING = 6;
WalletService._cachedBlockheight = {}; WalletService._cachedBlockheight = null;
var h = helpers.historyCacheTest(20); var h = helpers.historyCacheTest(20);
_.each(h, function(x, i) { _.each(h, function(x, i) {
@ -5962,6 +5962,7 @@ describe('Wallet service', function() {
blockchainExplorer.getBlockchainHeight = sinon.stub().callsArgWith(0, null, 200); blockchainExplorer.getBlockchainHeight = sinon.stub().callsArgWith(0, null, 200);
server._notify('NewBlock', { server._notify('NewBlock', {
network: 'livenet',
hash: 'dummy hash', hash: 'dummy hash',
}, { }, {
isGlobal: true isGlobal: true
@ -5989,7 +5990,7 @@ describe('Wallet service', function() {
it('should get cached # of confirmations if current height unknown', function(done) { it('should get cached # of confirmations if current height unknown', function(done) {
var _confirmations = Defaults.CONFIRMATIONS_TO_START_CACHING; var _confirmations = Defaults.CONFIRMATIONS_TO_START_CACHING;
Defaults.CONFIRMATIONS_TO_START_CACHING = 6; Defaults.CONFIRMATIONS_TO_START_CACHING = 6;
WalletService._cachedBlockheight = {}; WalletService._cachedBlockheight = null;
var h = helpers.historyCacheTest(20); var h = helpers.historyCacheTest(20);
helpers.stubHistory(h); helpers.stubHistory(h);

Loading…
Cancel
Save