Merge pull request #557 from isocolsky/ref/confirmations

Fix blockheight cache
This commit is contained in:
Matias Alejo Garcia 2016-08-10 11:21:08 -03:00 committed by GitHub
commit 811246a36f
3 changed files with 25 additions and 7 deletions

View File

@ -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,
},
});

View File

@ -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();
};
@ -2448,16 +2449,31 @@ WalletService.prototype._normalizeTxHistory = function(txs) {
});
};
WalletService._cachedBlockheight = {};
WalletService._clearBlockchainHeightCache = function() {
WalletService._cachedBlockheight.current = null;
WalletService._cachedBlockheight;
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) {
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);

View File

@ -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) {
@ -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
@ -5989,7 +5990,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);