diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index 932936fe..74f6fd54 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -174,7 +174,7 @@ Mempool.prototype.getRangeSync = function getRangeSync(options) { var i, tx; for (i = 0; i < hashes.length; i++) { - tx = this.getTXSync(hashes[i].toString('hex')); + tx = this.getTXSync(hashes[i]); if (tx) txs.push(tx); } @@ -230,7 +230,7 @@ Mempool.prototype.getCoinSync = function getCoinSync(hash, index) { Mempool.prototype.isSpentSync = function isSpentSync(hash, index) { var key = hash + '/' + index; - return this.spent[key]; + return this.spent[key] != null; }; Mempool.prototype.isDoubleSpendSync = function isDoubleSpendSync(tx) { @@ -303,7 +303,7 @@ Mempool.prototype.getTXByAddressSync = function getTXByAddressSync(addresses, ca }; Mempool.prototype.fillTXSync = function fillTXSync(tx) { - var i, input, tx; + var i, input, prev; if (tx.isCoinbase()) return tx; @@ -314,12 +314,12 @@ Mempool.prototype.fillTXSync = function fillTXSync(tx) { if (input.coin) continue; - tx = this.getTXSync(input.prevout.hash); + prev = this.getTXSync(input.prevout.hash); - if (!tx) + if (!prev) continue; - input.coin = bcoin.coin(tx, input.prevout.index); + input.coin = bcoin.coin(prev, input.prevout.index); } return tx; @@ -468,6 +468,7 @@ Mempool.prototype.addTX = function addTX(tx, callback, force) { // Use bitcoinj-style confidence calculation Mempool.prototype.getConfidence = function getConfidence(hash, callback) { + var self = this; var tx; callback = utils.asyncify(callback); @@ -489,14 +490,28 @@ Mempool.prototype.getConfidence = function getConfidence(hash, callback) { if (this.hasTXSync(hash)) return callback(null, constants.confidence.PENDING); - this.chain.db.getTX(hash, function(err, existing) { + function getBlock(callback) { + if (tx && tx.block) + return callback(null, tx.block); + return self.chain.db.getTX(hash, function(err, existing) { + if (err) + return callback(err); + + if (!existing) + return callback(); + + return callback(null, existing.block); + }); + } + + return getBlock(function(err, block) { if (err) return callback(err); - if (!existing) + if (!block) return callback(null, constants.confidence.UNKNOWN); - self.chain.db.isMainChain(existing.block, function(err, result) { + self.chain.db.isMainChain(block, function(err, result) { if (err) return callback(err); @@ -590,7 +605,7 @@ Mempool.prototype.addUnchecked = function addUnchecked(tx, callback) { Mempool.prototype.removeUnchecked = function removeUnchecked(tx, callback) { var self = this; - var hash, input, output, i, key, coin; + var hash, input, output, i, key, coin, prev; callback = utils.asyncify(callback); @@ -622,6 +637,16 @@ Mempool.prototype.removeUnchecked = function removeUnchecked(tx, callback) { delete this.coins[key]; delete this.spent[key]; this.addressMap.removeCoin(input); + try { + prev = this.getTXSync(input.prevout.hash); + } catch (e) { + return callback(e); + } + if (prev) { + coin = bcoin.coin(prev, input.prevout.index); + this.coins[key] = coin.toRaw(); + this.addressMap.addCoin(coin); + } } for (i = 0; i < tx.outputs.length; i++) { @@ -832,7 +857,7 @@ Mempool.prototype.getOrphanSync = function getOrphanSync(hash) { orphan = this.orphans[hash]; if (!orphan) - return callback(); + return; return bcoin.tx.fromExtended(orphan, true); }; @@ -1364,7 +1389,6 @@ function BinaryIndex() { } BinaryIndex.prototype.insert = function insert(tx) { - var hash = tx.hash(); var ps = new Buffer(4); var index; @@ -1373,12 +1397,11 @@ BinaryIndex.prototype.insert = function insert(tx) { index = binarySearch(this.index, ps, true); this.index.splice(index + 1, 0, ps); - this.data.splice(index + 1, 0, hash); + this.data.splice(index + 1, 0, tx.hash()); }; BinaryIndex.prototype.remove = function remove(tx) { - var hash = tx.hash(); - var index = binarySearch(this.data, hash); + var index = binarySearch(this.data, tx.hash()); if (index !== -1) { this.index.splice(index, 1); @@ -1399,7 +1422,7 @@ BinaryIndex.prototype.range = function range(start, end) { ps = utils.readU32BE(this.index[i], 0); if (ps < start || ps > end) return hashes; - hashes.push(this.data[i]); + hashes.push(this.data[i].toString('hex')); } return hashes; @@ -1430,9 +1453,6 @@ function binarySearch(items, key, insert) { if (!insert) return -1; - if (start === 0) - return -1; - return start - 1; }