optimize getBalance.

This commit is contained in:
Christopher Jeffrey 2016-05-16 17:56:35 -07:00
parent 66569df692
commit 599bbffe1e
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 43 additions and 13 deletions

View File

@ -1146,14 +1146,15 @@ Mempool.prototype.getBalance = function getBalance(callback) {
lte: 'c~',
values: true,
parse: function(data, key) {
return bcoin.coin.fromRaw(data);
assert(data.length >= 16);
return utils.read64N(data, 8);
}
}, function(err, coins) {
if (err)
return callback(err);
for (i = 0; i < coins.length; i++)
total += coins[i].value;
total += coins[i];
return callback(null, {
confirmed: 0,

View File

@ -1609,6 +1609,7 @@ TXDB.prototype.hasCoin = function hasCoin(hash, index, callback) {
*/
TXDB.prototype.getBalance = function getBalance(address, callback) {
var self = this;
var confirmed = 0;
var unconfirmed = 0;
var i;
@ -1618,21 +1619,49 @@ TXDB.prototype.getBalance = function getBalance(address, callback) {
address = null;
}
return this.getCoins(address, function(err, coins) {
// return this.getCoins(address, function(err, coins) {
// if (err)
// return callback(err);
//
// for (i = 0; i < coins.length; i++) {
// if (coins[i].height === -1)
// unconfirmed += coins[i].value;
// else
// confirmed += coins[i].value;
// }
//
// return callback(null, {
// confirmed: confirmed,
// unconfirmed: unconfirmed,
// total: confirmed + unconfirmed
// });
// });
return this.getCoinHashes(address, function(err, hashes) {
if (err)
return callback(err);
for (i = 0; i < coins.length; i++) {
if (coins[i].height === -1)
unconfirmed += coins[i].value;
else
confirmed += coins[i].value;
}
utils.forEachSerial(hashes, function(hash, next) {
self.db.fetch('c/' + hash[0] + '/' + hash[1], function(data, key) {
var height = utils.readU32(data, 4);
var value = utils.read64N(data, 8);
return callback(null, {
confirmed: confirmed,
unconfirmed: unconfirmed,
total: confirmed + unconfirmed
assert(data.length >= 16);
if (height === 0x7fffffff)
unconfirmed += value;
else
confirmed += value;
}, next);
}, function(err) {
if (err)
return callback(err);
return callback(null, {
confirmed: confirmed,
unconfirmed: unconfirmed,
total: confirmed + unconfirmed
});
});
});
};