diff --git a/lib/bcoin/tx-pool.js b/lib/bcoin/tx-pool.js index a0549436..91ac5ebb 100644 --- a/lib/bcoin/tx-pool.js +++ b/lib/bcoin/tx-pool.js @@ -217,65 +217,19 @@ TXPool.prototype.getCoin = function getCoin(hash, index) { return this._unspent[hash + '/' + index]; }; -TXPool.prototype.addCoin = function addCoin(coin, noWrite) { - var id = coin.hash + '/' + coin.index; +TXPool.prototype.getOutput = function getOutput(hash, index) { + var id = hash + '/' + index; + var tx; - // Do not add TX two times - if (this._unspent[id]) { - // Transaction was confirmed, update it in storage - if (coin.height !== -1 && this._unspent[id].height === -1) { - this._unspent[key].height = coin.height; - this._lastHeight = Math.max(tx.height, this._lastHeight); - this.emit('update', this._lastTs, this._lastHeight, tx); - this.emit('confirmed', tx); - // this._storeCoin(coin, noWrite); - } - return; - } + if (this._unspent[id]) + return this._unspent[id]; - // Problem here: could in some cases add - // an unspent that was already redeemed. - if (this._all[coin.hash]) + tx = this._all[hash]; + + if (!tx) return; - this._unspent[id] = coin; - this._addOutput(coin); - this._lastHeight = Math.max(coin.height, this._lastHeight); - this.emit('update', this._lastTs, this._lastHeight); - // Weird workaround to get addresses to update - if (coin.height !== -1) - this.emit('confirmed', coin); - // this._storeCoin(coin, noWrite); -}; - -TXPool.prototype._storeCoin = function _storeCoin(id, coin, noWrite) { - var self = this; - - if (!this._storage || noWrite) - return; - - this._storage.put(this._prefix + id, coin.toJSON(), function(err) { - if (err) - self.emit('error', err); - }); -}; - -TXPool.prototype._removeCoin = function _removeCoin(id, noWrite) { - var self = this; - var key; - - if (this._unspent[id]) { - this._removeOutput(this._unspent[id]); - delete this._unspent[id]; - } - - if (!this._storage || noWrite) - return; - - this._storage.del(this._prefix + id, function(err) { - if (err) - self.emit('error', err); - }); + return bcoin.coin(tx, index); }; TXPool.prototype._storeTX = function _storeTX(hash, tx, noWrite) {