|
|
@ -108,11 +108,11 @@ WalletService.initialize = function(opts, cb) { |
|
|
|
}; |
|
|
|
|
|
|
|
function initMessageBroker(cb) { |
|
|
|
if (opts.messageBroker) { |
|
|
|
messageBroker = opts.messageBroker; |
|
|
|
} else { |
|
|
|
messageBroker = new MessageBroker(opts.messageBrokerOpts); |
|
|
|
messageBroker = opts.messageBroker || new MessageBroker(opts.messageBrokerOpts); |
|
|
|
if (messageBroker) { |
|
|
|
messageBroker.onMessage(WalletService.handleIncomingNotification); |
|
|
|
} |
|
|
|
|
|
|
|
return cb(); |
|
|
|
}; |
|
|
|
|
|
|
@ -153,6 +153,14 @@ WalletService.initialize = function(opts, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
WalletService.handleIncomingNotification = function(notification, cb) { |
|
|
|
cb = cb || function() {}; |
|
|
|
|
|
|
|
if (!notification || notification.type != 'NewBlock') return cb(); |
|
|
|
WalletService._clearBlockchainHeightCache(); |
|
|
|
return cb(); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
WalletService.shutDown = function(cb) { |
|
|
|
if (!initialized) return cb(); |
|
|
@ -2626,19 +2634,37 @@ WalletService.prototype._normalizeTxHistory = function(txs) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
var _lastKnownBlockchainHeight; |
|
|
|
WalletService.prototype._getLastKnownBlockchainHeight = function(network, cb) { |
|
|
|
WalletService._cachedBlockheight; |
|
|
|
WalletService._clearBlockchainHeightCache = function() { |
|
|
|
WalletService._cachedBlockheight.current = null; |
|
|
|
}; |
|
|
|
|
|
|
|
WalletService.prototype._getBlockchainHeight = function(network, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
var now = Date.now(); |
|
|
|
if (!WalletService._cachedBlockheight) WalletService._cachedBlockheight = {}; |
|
|
|
var cache = WalletService._cachedBlockheight; |
|
|
|
|
|
|
|
function fetchFromBlockchain(cb) { |
|
|
|
var bc = self._getBlockchainExplorer(network); |
|
|
|
bc.getBlockchainHeight(function(err, height) { |
|
|
|
if (!err && height > 0) { |
|
|
|
_lastKnownBlockchainHeight = height; |
|
|
|
cache.current = height; |
|
|
|
cache.last = height; |
|
|
|
cache.updatedOn = now; |
|
|
|
} |
|
|
|
return cb(null, _lastKnownBlockchainHeight); |
|
|
|
return cb(null, cache.last); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
if (!cache.current || (now - cache.updatedOn) > Defaults.BLOCKHEIGHT_CACHE_TIME * 1000) { |
|
|
|
return fetchFromBlockchain(cb); |
|
|
|
} |
|
|
|
|
|
|
|
return cb(null, cache.current); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Retrieves all transactions (incoming & outgoing) |
|
|
|
* Times are in UNIX EPOCH |
|
|
@ -2834,7 +2860,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) { |
|
|
|
if (!txs) return next(); |
|
|
|
|
|
|
|
// Fix tx confirmations
|
|
|
|
self._getLastKnownBlockchainHeight(network, function(err, height) { |
|
|
|
self._getBlockchainHeight(network, function(err, height) { |
|
|
|
if (err || !height) return next(err); |
|
|
|
_.each(txs, function(tx) { |
|
|
|
if (tx.blockheight >= 0) { |
|
|
|