more mempool work.

This commit is contained in:
Christopher Jeffrey 2016-04-01 00:57:26 -07:00
parent 39417424a2
commit 3dddfcf7ea

View File

@ -119,7 +119,7 @@ Mempool.prototype.addBlock = function addBlock(block, callback, force) {
callback = utils.wrap(callback, unlock); callback = utils.wrap(callback, unlock);
utils.forEachSerial(block.txs, function(tx, next) { utils.forEachSerial(block.txs, function(tx, next) {
self.removeUnchecked(tx, next); self.removeUnchecked(tx.hash('hex'), next);
}, callback); }, callback);
}; };
@ -539,6 +539,7 @@ Mempool.prototype.fillAllTX = function fillAllTX(tx, callback) {
Mempool.prototype.fillAllCoins = function fillAllCoins(tx, callback) { Mempool.prototype.fillAllCoins = function fillAllCoins(tx, callback) {
var self = this; var self = this;
var doubleSpend = false;
this.fillCoins(tx, function(err) { this.fillCoins(tx, function(err) {
if (err) if (err)
@ -547,7 +548,32 @@ Mempool.prototype.fillAllCoins = function fillAllCoins(tx, callback) {
if (tx.hasCoins()) if (tx.hasCoins())
return callback(null, tx); return callback(null, tx);
self.chain.db.fillCoins(tx, callback); utils.forEach(tx.inputs, function(input, next) {
var hash = input.prevout.hash;
var index = input.prevout.index;
if (self.isSpentSync(hash, index)) {
doubleSpend = true;
return next();
}
self.chain.db.getCoin(hash, index, function(err, coin) {
if (err)
return next(err);
if (!coin)
return next();
input.coin = coin;
next();
});
}, function(err) {
if (err)
return callback(err);
return callback(null, tx, doubleSpend);
});
}); });
}; };
@ -603,64 +629,64 @@ Mempool.prototype.addUnchecked = function addUnchecked(tx, callback) {
}); });
}; };
Mempool.prototype.removeUnchecked = function removeUnchecked(tx, callback) { Mempool.prototype.removeUnchecked = function removeUnchecked(hash, callback) {
var self = this; var self = this;
var hash, input, output, i, key, coin, prev; var tx, input, output, i, key, coin;
callback = utils.asyncify(callback); if (hash instanceof bcoin.tx) {
tx = hash;
try { hash = tx.hash('hex');
tx = this.getTXSync(tx); } else {
} catch (e) { try {
return callback(e); tx = this.getTXSync(hash);
} catch (e) {
return utils.asyncify(callback)(e);
}
} }
if (!tx) if (!tx)
return callback(); return utils.nextTick(callback);
hash = tx.hash('hex'); this.fillAllTX(tx, function(err, tx) {
if (err)
return callback(err);
delete this.txs[hash]; delete self.txs[hash];
try {
this.removeOrphanSync(hash);
} catch (e) {
return callback(e);
}
this.addressMap.removeTX(tx);
this.psIndex.remove(tx);
for (i = 0; i < tx.inputs.length; i++) {
inputs = tx.outputs[i];
key = input.prevout.hash + '/' + input.prevout.index;
delete this.coins[key];
delete this.spent[key];
this.addressMap.removeCoin(input);
try { try {
prev = this.getTXSync(input.prevout.hash); self.removeOrphanSync(hash);
} catch (e) { } catch (e) {
return callback(e); return callback(e);
} }
if (prev) {
coin = bcoin.coin(prev, input.prevout.index); self.addressMap.removeTX(tx);
this.coins[key] = coin.toRaw(); self.psIndex.remove(tx);
this.addressMap.addCoin(coin);
for (i = 0; i < tx.inputs.length; i++) {
inputs = tx.outputs[i];
key = input.prevout.hash + '/' + input.prevout.index;
delete self.spent[key];
delete self.coins[key];
self.addressMap.removeCoin(input);
if (self.hasTXSync(input.prevout.hash)) {
self.coins[key] = input.coin.toRaw();
self.addressMap.addCoin(input.coin);
}
} }
}
for (i = 0; i < tx.outputs.length; i++) { for (i = 0; i < tx.outputs.length; i++) {
output = tx.outputs[i]; output = tx.outputs[i];
key = hash + '/' + i; key = hash + '/' + i;
delete this.coins[key]; delete self.coins[key];
delete this.spent[key]; delete self.spent[key];
this.addressMap.removeCoin(tx, i); self.addressMap.removeCoin(tx, i);
} }
this.totalSize -= tx.getSize(); self.totalSize -= tx.getSize();
this.emit('remove tx', tx); self.emit('remove tx', tx);
return callback(); return callback();
});
}; };
Mempool.prototype.removeOrphanSync = function removeOrphanSync(tx) { Mempool.prototype.removeOrphanSync = function removeOrphanSync(tx) {
@ -1328,10 +1354,11 @@ AddressMap.prototype.removeTX = function removeTX(tx) {
if (map) { if (map) {
index = binarySearch(map, hash); index = binarySearch(map, hash);
if (index !== -1) if (index !== -1) {
map.splice(index, 1); map.splice(index, 1);
if (map.length === 0) if (map.length === 0)
delete this.map.tx[address]; delete this.map.tx[address];
}
} }
} }
}; };
@ -1372,10 +1399,11 @@ AddressMap.prototype.removeCoin = function removeCoin(tx, i) {
if (map) { if (map) {
index = binarySearch(map, key); index = binarySearch(map, key);
if (index !== -1) if (index !== -1) {
map.splice(index, 1); map.splice(index, 1);
if (map.length === 0) if (map.length === 0)
delete this.map.coin[address]; delete this.map.coin[address];
}
} }
}; };