txdb: refactor balance.

This commit is contained in:
Christopher Jeffrey 2016-08-25 14:09:22 -07:00
parent 7c1c2b2d15
commit e5f1dc79ed
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -2126,21 +2126,9 @@ TXDB.prototype.getBalance = function getBalance(account, callback) {
var parts = layout.cc(key);
var hash = parts[0];
var index = parts[1];
var height = data.readUInt32LE(4, true);
var value = utils.read64N(data, 8);
assert(data.length >= 16);
balance.total += value;
if (height === 0x7fffffff)
balance.unconfirmed += value;
else
balance.confirmed += value;
key = hash + index;
self.coinCache.set(key, data);
var ckey = hash + index;
balance.addRaw(data);
self.coinCache.set(ckey, data);
}
}, function(err) {
if (err)
@ -2161,20 +2149,6 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
var balance = new Balance(this.wallet);
var key, coin;
function parse(data) {
var height = data.readUInt32LE(4, true);
var value = utils.read64N(data, 8);
assert(data.length >= 16);
balance.total += value;
if (height === 0x7fffffff)
balance.unconfirmed += value;
else
balance.confirmed += value;
}
this.getCoinHashes(account, function(err, hashes) {
if (err)
return callback(err);
@ -2185,7 +2159,7 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
if (coin) {
try {
parse(coin);
balance.addRaw(coin);
} catch (e) {
return next(e);
}
@ -2200,7 +2174,7 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
return next();
try {
parse(data);
balance.addRaw(data);
} catch (e) {
return callback(e);
}
@ -2316,6 +2290,20 @@ Balance.prototype.unconfirm = function unconfirm(value) {
this.confirmed -= value;
};
Balance.prototype.addRaw = function addRaw(data) {
var height = data.readUInt32LE(4, true);
var value = utils.read64N(data, 8);
assert(data.length >= 16);
this.total += value;
if (height === 0x7fffffff)
this.unconfirmed += value;
else
this.confirmed += value;
};
Balance.prototype.toJSON = function toJSON() {
return {
wid: this.wid,