txdb: refactor.

This commit is contained in:
Christopher Jeffrey 2016-10-20 08:34:18 -07:00
parent a80c438995
commit 2cc2ec4bf3
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

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