diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index f4c19621..232e963b 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -548,7 +548,7 @@ Chain.prototype.verifyDuplicates = async function verifyDuplicates(block, prev, // Check all transactions. for (let tx of block.txs) { - let result = await this.db.hasCoins(tx.hash()); + let result = await this.db.hasCoins(tx); if (result) { let height = prev.height + 1; diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index 8b5828b7..dbe2e0d7 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -894,22 +894,17 @@ ChainDB.prototype.getCoin = async function getCoin(hash, index) { /** * Check whether coins are still unspent. Necessary for bip30. * @see https://bitcointalk.org/index.php?topic=67738.0 - * @param {Hash} hash + * @param {TX} tx * @returns {Promise} - Returns Boolean. */ -ChainDB.prototype.hasCoins = async function hasCoins(hash) { - let iter = this.db.iterator({ - gte: layout.c(hash, 0x00000000), - lte: layout.c(hash, 0xffffffff) - }); - - if (!(await iter.next())) - return false; - - await iter.end(); - - return true; +ChainDB.prototype.hasCoins = async function hasCoins(tx) { + for (let i = 0; i < tx.outputs.length; i++) { + let key = layout.c(tx.hash(), i); + if (await this.db.has(key)) + return true; + } + return false; }; /** diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index 384fbc7a..ff7804b1 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -803,7 +803,7 @@ Mempool.prototype.insertTX = async function insertTX(tx, id) { // We can test whether this is an // non-fully-spent transaction on // the chain. - if (await this.chain.db.hasCoins(hash)) { + if (await this.chain.db.hasCoins(tx)) { throw new VerifyError(tx, 'alreadyknown', 'txn-already-known',