diff --git a/lib/services/address/index.js b/lib/services/address/index.js index eac977d3..a13b0967 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -421,15 +421,46 @@ AddressService.prototype._onReorg = function(oldBlockList, newBlockList, commonA value: tipOps.value }]; - // remove all the old blocks that we reorg from - oldBlockList.forEach(function(block) { - removalOps.push([ - { - type: 'del', - key: this.encoding.encodeTransactionKey(), - }, - ]); - }); + // for every tx, remove the address index key for every input and output + for(var i = 0; i < oldBlockList.length; i++) { + var block = oldBlockList[i]; + //txs + for(var j = 0; j < block.transactions.length; j++) { + var tx = block.transactions[j]; + + //inputs + var address; + for(var k = 0; k < tx.inputs.length; k++) { + var input = tx.inputs[k]; + address = utils.getAddressString({ tx: tx, item: input, network: this._network }); + + if (!address) { + continue; + } + + removalOps.push({ + type: 'del', + key: this.encoding.encodeTransactionKey(address, block.height, tx.id, k, 1) + }); + } + + //outputs + for(k = 0; k < tx.outputs.length; k++) { + var output = tx.outputs[k]; + address = utils.getAddressString({ tx: tx, item: output, network: this._network }); + + if (!address) { + continue; + } + + removalOps.push({ + type: 'del', + key: this.encoding.encodeTransactionKey(address, block.height, tx.id, k, 0) + }); + + } + } + } this._db.batch(removalOps); diff --git a/lib/services/transaction/index.js b/lib/services/transaction/index.js index 9995a4bb..8d80925f 100644 --- a/lib/services/transaction/index.js +++ b/lib/services/transaction/index.js @@ -184,19 +184,16 @@ TransactionService.prototype._onReorg = function(oldBlockList, newBlockList, com value: tipOps.value }]; - // remove all the old blocks that we reorg from - oldBlockList.forEach(function(block) { - removalOps.concat([ - { + for(var i = 0; i < oldBlockList.length; i++) { + var block = oldBlockList[i]; + for(var j = 0; j < block.transactions.length; j++) { + var tx = block.transactions[j]; + removalOps.push({ type: 'del', - key: this.encoding.encodeAddressIndexKey(block.hash), - }, - { - type: 'del', - key: this.encoding.encodeBlockTimestampKey(block.hash), - } - ]); - }); + key: this._encoding.encodeTransactionKey(tx.id) + }); + } + } this._db.batch(removalOps); diff --git a/lib/utils.js b/lib/utils.js index 08193595..a21c705e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -65,7 +65,8 @@ utils.getAddressString = function(opts) { return; } - if (opts.tx && opts.tx.isCoinbase()) { + // is coinbase and is input, no address + if (opts.tx && opts.tx.isCoinbase() && opts.item.prevTxId) { return; } @@ -153,28 +154,6 @@ utils.toIntIfNumberLike = function(a) { return a; }; -utils.getAddressString = function(script, output) { - var address = script.toAddress(this.node.network.name); - if(address) { - return address.toString(); - } - - try { - var pubkey = script.getPublicKey(); - if(pubkey) { - return pubkey.toString('hex'); - } - } catch(e) { - } - - //TODO add back in P2PK, but for this we need to look up the utxo for this script - if(output && output.script && output.script.isPublicKeyOut()) { - return output.script.getPublicKey().toString('hex'); - } - - return null; -}; - utils.getBlockInfoString = function(tip, best) { var diff = best - tip;