txdb: refactor.
This commit is contained in:
parent
a80c438995
commit
2cc2ec4bf3
@ -2056,17 +2056,15 @@ TXDB.prototype.getPending = co(function* getPending(account) {
|
||||
* @returns {Promise} - Returns {@link Coin}[].
|
||||
*/
|
||||
|
||||
TXDB.prototype.getCredits = co(function* getCredits(account, all) {
|
||||
TXDB.prototype.getCredits = function getCredits(account) {
|
||||
var self = this;
|
||||
var unspent = [];
|
||||
var i, credits, credit;
|
||||
|
||||
// Slow case
|
||||
if (account != null)
|
||||
return this.getAccountCredits(account, all);
|
||||
return this.getAccountCredits(account);
|
||||
|
||||
// Fast case
|
||||
credits = yield this.range({
|
||||
return this.range({
|
||||
gte: layout.c(constants.NULL_HASH, 0x00000000),
|
||||
lte: layout.c(constants.HIGH_HASH, 0xffffffff),
|
||||
parse: function(key, value) {
|
||||
@ -2081,19 +2079,7 @@ TXDB.prototype.getCredits = co(function* getCredits(account, all) {
|
||||
return credit;
|
||||
}
|
||||
});
|
||||
|
||||
if (all)
|
||||
return credits;
|
||||
|
||||
for (i = 0; i < credits.length; i++) {
|
||||
credit = credits[i];
|
||||
if (credit.spent)
|
||||
continue;
|
||||
unspent.push(credit);
|
||||
}
|
||||
|
||||
return unspent;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Get coins by account.
|
||||
@ -2101,23 +2087,18 @@ TXDB.prototype.getCredits = co(function* getCredits(account, all) {
|
||||
* @returns {Promise} - Returns {@link Coin}[].
|
||||
*/
|
||||
|
||||
TXDB.prototype.getAccountCredits = co(function* getAccountCredits(account, all) {
|
||||
var prevout = yield this.getOutpoints(account);
|
||||
TXDB.prototype.getAccountCredits = co(function* getAccountCredits(account) {
|
||||
var outpoints = yield this.getOutpoints(account);
|
||||
var credits = [];
|
||||
var i, op, credit;
|
||||
var i, prevout, credit;
|
||||
|
||||
for (i = 0; i < prevout.length; i++) {
|
||||
op = prevout[i];
|
||||
credit = yield this.getCredit(op.hash, op.index);
|
||||
for (i = 0; i < outpoints.length; i++) {
|
||||
prevout = outpoints[i];
|
||||
credit = yield this.getCredit(prevout.hash, prevout.index);
|
||||
|
||||
if (!credit)
|
||||
continue;
|
||||
|
||||
if (!all) {
|
||||
if (credit.spent)
|
||||
continue;
|
||||
}
|
||||
|
||||
credits.push(credit);
|
||||
}
|
||||
|
||||
@ -2165,13 +2146,17 @@ TXDB.prototype.getSpentCredits = co(function* getSpentCredits(tx) {
|
||||
* @returns {Promise} - Returns {@link Coin}[].
|
||||
*/
|
||||
|
||||
TXDB.prototype.getCoins = co(function* getCoins(account, all) {
|
||||
var credits = yield this.getCredits(account, all);
|
||||
TXDB.prototype.getCoins = co(function* getCoins(account) {
|
||||
var credits = yield this.getCredits(account);
|
||||
var coins = [];
|
||||
var i, credit;
|
||||
|
||||
for (i = 0; i < credits.length; i++) {
|
||||
credit = credits[i];
|
||||
|
||||
if (credit.spent)
|
||||
continue;
|
||||
|
||||
coins.push(credit.coin);
|
||||
}
|
||||
|
||||
@ -2184,13 +2169,17 @@ TXDB.prototype.getCoins = co(function* getCoins(account, all) {
|
||||
* @returns {Promise} - Returns {@link Coin}[].
|
||||
*/
|
||||
|
||||
TXDB.prototype.getAccountCoins = co(function* getAccountCoins(account, all) {
|
||||
var credits = yield this.getAccountCredits(account, all);
|
||||
TXDB.prototype.getAccountCoins = co(function* getAccountCoins(account) {
|
||||
var credits = yield this.getAccountCredits(account);
|
||||
var coins = [];
|
||||
var i, credit;
|
||||
|
||||
for (i = 0; i < credits.length; i++) {
|
||||
credit = credits[i];
|
||||
|
||||
if (credit.spent)
|
||||
continue;
|
||||
|
||||
coins.push(credit.coin);
|
||||
}
|
||||
|
||||
@ -2306,34 +2295,42 @@ TXDB.prototype.getDetails = co(function* getDetails(hash) {
|
||||
|
||||
/**
|
||||
* Convert transaction to transaction details.
|
||||
* @param {TX|TX[]} tx
|
||||
* @param {TX|TX[]} txs
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
TXDB.prototype.toDetails = co(function* toDetails(tx) {
|
||||
var i, out, txs, details;
|
||||
var coins, coin, path, output;
|
||||
TXDB.prototype.toDetails = co(function* toDetails(txs) {
|
||||
var i, out, tx, details;
|
||||
|
||||
if (Array.isArray(tx)) {
|
||||
out = [];
|
||||
txs = tx;
|
||||
if (!Array.isArray(txs))
|
||||
return yield this._toDetails(txs);
|
||||
|
||||
for (i = 0; i < txs.length; i++) {
|
||||
tx = txs[i];
|
||||
details = yield this.toDetails(tx);
|
||||
out = [];
|
||||
|
||||
if (!details)
|
||||
continue;
|
||||
for (i = 0; i < txs.length; i++) {
|
||||
tx = txs[i];
|
||||
details = yield this._toDetails(tx);
|
||||
|
||||
out.push(details);
|
||||
}
|
||||
if (!details)
|
||||
continue;
|
||||
|
||||
return out;
|
||||
out.push(details);
|
||||
}
|
||||
|
||||
details = new Details(this, tx);
|
||||
return out;
|
||||
});
|
||||
|
||||
coins = yield this.fillHistory(tx);
|
||||
/**
|
||||
* Convert transaction to transaction details.
|
||||
* @private
|
||||
* @param {TX} tx
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
TXDB.prototype._toDetails = co(function* _toDetails(tx) {
|
||||
var details = new Details(this, tx);
|
||||
var coins = yield this.fillHistory(tx);
|
||||
var i, coin, path, output;
|
||||
|
||||
for (i = 0; i < tx.inputs.length; i++) {
|
||||
coin = coins[i];
|
||||
@ -2502,18 +2499,19 @@ TXDB.prototype.getBalance = co(function* getBalance(account) {
|
||||
*/
|
||||
|
||||
TXDB.prototype.getWalletBalance = co(function* getWalletBalance() {
|
||||
var credits = yield this.getCredits(null, true);
|
||||
var credits = yield this.getCredits();
|
||||
var balance = new Balance(this.wallet.wid, this.wallet.id, -1);
|
||||
var i, credit;
|
||||
var i, credit, coin;
|
||||
|
||||
for (i = 0; i < credits.length; i++) {
|
||||
credit = credits[i];
|
||||
coin = credit.coin;
|
||||
|
||||
if (credit.coin.height !== -1)
|
||||
balance.confirmed += credit.coin.value;
|
||||
if (coin.height !== -1)
|
||||
balance.confirmed += coin.value;
|
||||
|
||||
if (!credit.spent)
|
||||
balance.unconfirmed += credit.coin.value;
|
||||
balance.unconfirmed += coin.value;
|
||||
}
|
||||
|
||||
return balance;
|
||||
@ -2526,18 +2524,19 @@ TXDB.prototype.getWalletBalance = co(function* getWalletBalance() {
|
||||
*/
|
||||
|
||||
TXDB.prototype.getAccountBalance = co(function* getAccountBalance(account) {
|
||||
var credits = yield this.getAccountCredits(account, true);
|
||||
var credits = yield this.getAccountCredits(account);
|
||||
var balance = new Balance(this.wallet.wid, this.wallet.id, account);
|
||||
var i, credit;
|
||||
var i, credit, coin;
|
||||
|
||||
for (i = 0; i < credits.length; i++) {
|
||||
credit = credits[i];
|
||||
coin = credit.coin;
|
||||
|
||||
if (credit.coin.height !== -1)
|
||||
balance.confirmed += credit.coin.value;
|
||||
if (coin.height !== -1)
|
||||
balance.confirmed += coin.value;
|
||||
|
||||
if (!credit.spent)
|
||||
balance.unconfirmed += credit.coin.value;
|
||||
balance.unconfirmed += coin.value;
|
||||
}
|
||||
|
||||
return balance;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user