chaindb: alter hasCoins.

This commit is contained in:
Christopher Jeffrey 2017-07-03 17:39:20 -07:00
parent 82bdd73f05
commit a1601b8f3a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 10 additions and 15 deletions

View File

@ -548,7 +548,7 @@ Chain.prototype.verifyDuplicates = async function verifyDuplicates(block, prev,
// Check all transactions. // Check all transactions.
for (let tx of block.txs) { for (let tx of block.txs) {
let result = await this.db.hasCoins(tx.hash()); let result = await this.db.hasCoins(tx);
if (result) { if (result) {
let height = prev.height + 1; let height = prev.height + 1;

View File

@ -894,22 +894,17 @@ ChainDB.prototype.getCoin = async function getCoin(hash, index) {
/** /**
* Check whether coins are still unspent. Necessary for bip30. * Check whether coins are still unspent. Necessary for bip30.
* @see https://bitcointalk.org/index.php?topic=67738.0 * @see https://bitcointalk.org/index.php?topic=67738.0
* @param {Hash} hash * @param {TX} tx
* @returns {Promise} - Returns Boolean. * @returns {Promise} - Returns Boolean.
*/ */
ChainDB.prototype.hasCoins = async function hasCoins(hash) { ChainDB.prototype.hasCoins = async function hasCoins(tx) {
let iter = this.db.iterator({ for (let i = 0; i < tx.outputs.length; i++) {
gte: layout.c(hash, 0x00000000), let key = layout.c(tx.hash(), i);
lte: layout.c(hash, 0xffffffff) if (await this.db.has(key))
}); return true;
}
if (!(await iter.next())) return false;
return false;
await iter.end();
return true;
}; };
/** /**

View File

@ -803,7 +803,7 @@ Mempool.prototype.insertTX = async function insertTX(tx, id) {
// We can test whether this is an // We can test whether this is an
// non-fully-spent transaction on // non-fully-spent transaction on
// the chain. // the chain.
if (await this.chain.db.hasCoins(hash)) { if (await this.chain.db.hasCoins(tx)) {
throw new VerifyError(tx, throw new VerifyError(tx,
'alreadyknown', 'alreadyknown',
'txn-already-known', 'txn-already-known',