|
|
@ -7,6 +7,7 @@ var log = require('npmlog'); |
|
|
|
log.debug = log.verbose; |
|
|
|
log.disableColor(); |
|
|
|
var util = require('util'); |
|
|
|
var Bitcore = require('bitcore-lib'); |
|
|
|
|
|
|
|
var mongodb = require('mongodb'); |
|
|
|
|
|
|
@ -1065,5 +1066,64 @@ Storage.prototype._dump = function(cb, fn) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Storage.prototype._addressHash = function(addresses) { |
|
|
|
var all = addresses.join(); |
|
|
|
return Bitcore.crypto.Hash.ripemd160(new Buffer(all)).toString('hex'); |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.prototype.checkAndUseBalanceCache = function(addresses, cb) { |
|
|
|
var key = ths._addressHash(addresses); |
|
|
|
var now = Date.now(); |
|
|
|
|
|
|
|
self.db.collection(collections.CACHE).findOne({ |
|
|
|
walletId: walletId, |
|
|
|
type: 'balanceCache', |
|
|
|
key: key, |
|
|
|
}, function(err, ret) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!ret) return cb(); |
|
|
|
|
|
|
|
var validFor = ret.ts + Defauls.BALANCE_CACHE_DURATION * 1000 - now; |
|
|
|
|
|
|
|
if (validFor > 0) { |
|
|
|
log.debug('','Using Balance Cache valid for %d ms more', validFor); |
|
|
|
return cb(null, ret.result); |
|
|
|
} |
|
|
|
|
|
|
|
return cb(); |
|
|
|
|
|
|
|
log.debug('','Balance cache expired, deleting'); |
|
|
|
self.db.collection(collections.CACHE).remove({ |
|
|
|
walletId: walletId, |
|
|
|
type: 'balanceCache', |
|
|
|
key: key, |
|
|
|
}, {}, function() { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Storage.prototype.storeBalanceCache = function (addresses, balance, cb) { |
|
|
|
var key = ths._addressHash(addresses); |
|
|
|
var now = Date.now(); |
|
|
|
|
|
|
|
self.db.collection(collections.CACHE).update( { |
|
|
|
walletId: walletId, |
|
|
|
type: 'balanceCache', |
|
|
|
key: key, |
|
|
|
}, { |
|
|
|
"$set": |
|
|
|
{ |
|
|
|
ts: now, |
|
|
|
result: balance, |
|
|
|
} |
|
|
|
}, { |
|
|
|
w: 1, |
|
|
|
upsert: true, |
|
|
|
}, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
Storage.collections = collections; |
|
|
|
module.exports = Storage; |
|
|
|