From 519280e1bd468caee92757a1ea6f81cc1e538859 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 18 Oct 2016 18:56:19 -0700 Subject: [PATCH] txdb: refactor x3. --- lib/wallet/txdb.js | 47 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 68d81934..eaa4ab8d 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -1800,8 +1800,8 @@ TXDB.prototype.getHistory = function getHistory(account) { */ TXDB.prototype.getAccountHistory = co(function* getAccountHistory(account) { - var txs = []; var hashes = yield this.getHistoryHashes(account); + var txs = []; var i, hash, tx; for (i = 0; i < hashes.length; i++) { @@ -1824,8 +1824,8 @@ TXDB.prototype.getAccountHistory = co(function* getAccountHistory(account) { */ TXDB.prototype.getPending = co(function* getPending(account) { - var txs = []; var hashes = yield this.getPendingHashes(account); + var txs = []; var i, hash, tx; for (i = 0; i < hashes.length; i++) { @@ -1868,7 +1868,7 @@ TXDB.prototype.getCoins = co(function* getCoins(account, all) { TXDB.prototype.getCredits = co(function* getCredits(account, all) { var self = this; - var out = []; + var unspent = []; var i, credits, credit; // Slow case @@ -1899,10 +1899,10 @@ TXDB.prototype.getCredits = co(function* getCredits(account, all) { credit = credits[i]; if (credit.spent) continue; - out.push(credit); + unspent.push(credit); } - return out; + return unspent; }); /** @@ -1960,30 +1960,24 @@ TXDB.prototype.getAccountCredits = co(function* getAccountCredits(account, all) */ TXDB.prototype.fillHistory = co(function* fillHistory(tx) { - var coins = []; - var hash; + var i, input, credits, credit; if (tx.isCoinbase()) - return coins; + return tx; - hash = tx.hash('hex'); + credits = yield this.getSpentCredits(tx); - yield this.range({ - gte: layout.d(hash, 0x00000000), - lte: layout.d(hash, 0xffffffff), - parse: function(key, value) { - var index = layout.dd(key)[1]; - var coin = Coin.fromRaw(value); - var input = tx.inputs[index]; - assert(input); - coin.hash = input.prevout.hash; - coin.index = input.prevout.index; - input.coin = coin; - coins[index] = coin; - } - }); + for (i = 0; i < tx.inputs.length; i++) { + input = tx.inputs[i]; + credit = credits[i]; - return coins; + if (!credit) + continue; + + input.coin = credit.coin; + } + + return tx; }); /** @@ -1994,7 +1988,10 @@ TXDB.prototype.fillHistory = co(function* fillHistory(tx) { TXDB.prototype.getSpentCredits = co(function* getSpentCredits(tx) { var credits = []; - var hash; + var i, hash; + + for (i = 0; i < tx.inputs.length; i++) + credits.push(null); if (tx.isCoinbase()) return credits;