diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index 896d83e1..5783792a 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -854,7 +854,7 @@ ChainDB.prototype.hasCoins = function hasCoins(hash) { * @returns {Promise} - Returns {@link CoinView}. */ -ChainDB.prototype.getCoinView = co(function* getCoinView(tx, callback) { +ChainDB.prototype.getCoinView = co(function* getCoinView(tx) { var view = new CoinView(); var prevout = tx.getPrevout(); var i, hash, coins; diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index 2d9cfd08..0a29a63f 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -24,6 +24,8 @@ var Outpoint = require('../primitives/outpoint'); var TX = require('../primitives/tx'); var Coin = require('../primitives/coin'); var MempoolEntry = require('./mempoolentry'); +var CoinView = require('../blockchain/coinview'); +var Coins = require('../blockchain/coins'); /** * Represents a mempool. @@ -1656,7 +1658,7 @@ Mempool.prototype.fillAllCoins = co(function* fillAllCoins(tx) { * @returns {Promise} - Returns {@link CoinView}. */ -Mempool.prototype.getCoinView = co(function* getCoinView(tx) { +Mempool.prototype.getCoinViewSlow = co(function* getCoinViewSlow(tx) { var view = yield this.chain.db.getCoinView(tx); var entries = view.toArray(); var i, coins, entry; @@ -1678,6 +1680,41 @@ Mempool.prototype.getCoinView = co(function* getCoinView(tx) { return view; }); +/** + * Get coin viewpoint. + * @param {TX} tx + * @returns {Promise} - Returns {@link CoinView}. + */ + +Mempool.prototype.getCoinView = co(function* getCoinView(tx) { + var view = new CoinView(); + var prevout = tx.getPrevout(); + var i, entry, hash, coins; + + for (i = 0; i < prevout.length; i++) { + hash = prevout[i]; + entry = this.getEntry(hash); + + if (entry) { + view.addTX(entry.tx, -1); + continue; + } + + coins = yield this.chain.db.getCoins(hash); + + if (!coins) { + coins = new Coins(); + coins.hash = hash; + view.add(coins); + continue; + } + + view.add(coins); + } + + return view; +}); + /** * Get a snapshot of all transaction hashes in the mempool. Used * for generating INV packets in response to MEMPOOL packets.