txdb: refactor balance.
This commit is contained in:
parent
7c1c2b2d15
commit
e5f1dc79ed
@ -2126,21 +2126,9 @@ TXDB.prototype.getBalance = function getBalance(account, callback) {
|
|||||||
var parts = layout.cc(key);
|
var parts = layout.cc(key);
|
||||||
var hash = parts[0];
|
var hash = parts[0];
|
||||||
var index = parts[1];
|
var index = parts[1];
|
||||||
var height = data.readUInt32LE(4, true);
|
var ckey = hash + index;
|
||||||
var value = utils.read64N(data, 8);
|
balance.addRaw(data);
|
||||||
|
self.coinCache.set(ckey, data);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
@ -2161,20 +2149,6 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
|
|||||||
var balance = new Balance(this.wallet);
|
var balance = new Balance(this.wallet);
|
||||||
var key, coin;
|
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) {
|
this.getCoinHashes(account, function(err, hashes) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -2185,7 +2159,7 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
|
|||||||
|
|
||||||
if (coin) {
|
if (coin) {
|
||||||
try {
|
try {
|
||||||
parse(coin);
|
balance.addRaw(coin);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return next(e);
|
return next(e);
|
||||||
}
|
}
|
||||||
@ -2200,7 +2174,7 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
|
|||||||
return next();
|
return next();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
parse(data);
|
balance.addRaw(data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return callback(e);
|
return callback(e);
|
||||||
}
|
}
|
||||||
@ -2316,6 +2290,20 @@ Balance.prototype.unconfirm = function unconfirm(value) {
|
|||||||
this.confirmed -= 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() {
|
Balance.prototype.toJSON = function toJSON() {
|
||||||
return {
|
return {
|
||||||
wid: this.wid,
|
wid: this.wid,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user