do not store unspendable coins.

This commit is contained in:
Christopher Jeffrey 2016-05-05 03:41:22 -07:00
parent 2faf8513c4
commit 079d82e80c
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 56 additions and 24 deletions

View File

@ -844,6 +844,10 @@ ChainDB.prototype.connectBlock = function connectBlock(block, batch, callback) {
for (j = 0; j < tx.outputs.length; j++) {
output = tx.outputs[j];
key = hash + '/' + j;
if (output.script.isUnspendable())
continue;
coin = bcoin.coin(tx, j);
if (self.options.indexAddress) {
@ -931,6 +935,9 @@ ChainDB.prototype.disconnectBlock = function disconnectBlock(block, batch, callb
output = tx.outputs[j];
key = hash + '/' + j;
if (output.script.isUnspendable())
continue;
if (self.options.indexAddress) {
address = output.getAddress();
if (address)

View File

@ -1374,10 +1374,11 @@ Mempool.prototype._addUnchecked = function addUnchecked(tx, callback, force) {
batch.put('t/' + hash, tx.toExtended());
batch.put('m/' + pad32(tx.ps) + '/' + hash, DUMMY);
addresses = tx.getAddresses();
for (i = 0; i < addresses.length; i++)
batch.put('T/' + addresses[i] + '/' + hash, DUMMY);
if (this.options.indexAddress) {
addresses = tx.getAddresses();
for (i = 0; i < addresses.length; i++)
batch.put('T/' + addresses[i] + '/' + hash, DUMMY);
}
for (i = 0; i < tx.inputs.length; i++) {
input = tx.inputs[i];
@ -1388,25 +1389,32 @@ Mempool.prototype._addUnchecked = function addUnchecked(tx, callback, force) {
assert(input.coin);
address = input.getAddress();
batch.del('c/' + key);
batch.put('s/' + key, tx.hash());
if (address)
batch.del('C/' + address + '/' + key);
if (this.options.indexAddress) {
address = input.getAddress();
if (address)
batch.del('C/' + address + '/' + key);
}
}
for (i = 0; i < tx.outputs.length; i++) {
output = tx.outputs[i];
key = hash + '/' + i;
address = output.getAddress();
if (output.script.isUnspendable())
continue;
coin = bcoin.coin(tx, i).toRaw();
batch.put('c/' + key, coin);
if (address)
batch.put('C/' + address + '/' + key, DUMMY);
if (this.options.indexAddress) {
address = output.getAddress();
if (address)
batch.put('C/' + address + '/' + key, DUMMY);
}
}
return batch.write(callback);
@ -1445,10 +1453,11 @@ Mempool.prototype._removeUnchecked = function removeUnchecked(hash, callback, fo
batch.del('t/' + hash);
batch.del('m/' + pad32(tx.ps) + '/' + hash);
addresses = tx.getAddresses();
for (i = 0; i < addresses.length; i++)
batch.del('T/' + addresses[i] + '/' + hash);
if (self.options.indexAddress) {
addresses = tx.getAddresses();
for (i = 0; i < addresses.length; i++)
batch.del('T/' + addresses[i] + '/' + hash);
}
utils.forEachSerial(tx.inputs, function(input, next) {
var key = input.prevout.hash + '/' + input.prevout.index;
@ -1460,8 +1469,6 @@ Mempool.prototype._removeUnchecked = function removeUnchecked(hash, callback, fo
if (!input.coin)
return next();
address = input.getAddress();
batch.del('s/' + key);
self.hasTX(input.prevout.hash, function(err, result) {
@ -1470,12 +1477,18 @@ Mempool.prototype._removeUnchecked = function removeUnchecked(hash, callback, fo
if (result) {
batch.put('c/' + key, input.coin.toRaw());
if (address)
batch.put('C/' + address + '/' + key, DUMMY);
if (self.options.indexAddress) {
address = input.getAddress();
if (address)
batch.put('C/' + address + '/' + key, DUMMY);
}
} else {
batch.del('c/' + key);
if (address)
batch.del('C/' + address + '/' + key);
if (self.options.indexAddress) {
address = input.getAddress();
if (address)
batch.del('C/' + address + '/' + key);
}
}
next();
@ -1487,12 +1500,17 @@ Mempool.prototype._removeUnchecked = function removeUnchecked(hash, callback, fo
for (i = 0; i < tx.outputs.length; i++) {
output = tx.outputs[i];
key = hash + '/' + i;
address = output.getAddress();
if (output.script.isUnspendable())
continue;
batch.del('c/' + key);
if (address)
batch.del('C/' + address + '/' + key);
if (self.options.indexAddress) {
address = output.getAddress();
if (address)
batch.del('C/' + address + '/' + key);
}
}
return batch.write(callback);

View File

@ -469,6 +469,10 @@ TXDB.prototype._add = function add(tx, map, callback, force) {
}
key = hash + '/' + i;
if (output.script.isUnspendable())
return next();
coin = bcoin.coin(tx, i);
self._getOrphans(key, function(err, orphans) {
@ -914,6 +918,9 @@ TXDB.prototype._remove = function remove(tx, map, callback, force) {
return;
}
if (output.script.isUnspendable())
return;
if (self.options.indexAddress && address) {
map.table[address].forEach(function(id) {
batch.del('C/' + id + '/' + key);