diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 4eb65376..85bcf2cc 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -60,29 +60,26 @@ AddressService.prototype.getAPIMethods = function() { ]; }; -/** - * Called by the Bus to get the available events for this service. - */ AddressService.prototype.getPublishEvents = function() { return []; }; -AddressService.prototype._getAddress = function(opts, input) { +AddressService.prototype._getAddress = function(opts, item) { if(opts.tx.isCoinbase()) { - log.debug('Coinbase Tx, no input available.'); + log.debug('Coinbase Tx, no input/output available.'); return; } - if(!input.script) { + if(!item.script) { log.debug('Invalid script'); return; } - var address = this.getAddressString(input.script); + var address = this.getAddressString(item.script); if(!address) { - log.debug('Address not available from input script.'); + log.debug('Address not available from input/output script.'); return; } @@ -107,54 +104,55 @@ AddressService.prototype._getActions = function(connect) { AddressService.prototype._processInput = function(opts, input, cb) { - // there are no valid inputs on coinbase tx's for the purposes of address indexing var self = this; - var address = this._getAddress(input.script); + var address = this._getAddress(opts, input); if (!address) { - return cb([]); + return setImemdiate(cb); } + var action = self._getAddress(opts.connect); + + var operations = []; + + // address index var addressKey = self._encoding.encodeAddressIndexKey(address, opts.block.height, opts.tx.id); - if (connect) { - operations.push({ - type: 'del', - key: addressKey - }); - operations.push({ - type: 'del', - key: utxoKey - }); - return operations; - } + operations.push({ + type: action.action, + key: addressKey + }); var prevTxId = input.prevTxId.toString('hex'); self._txService.getTransaction(prevTxId, {}, function(err, tx) { + if (err) { + log.debug('Error saving tx inputs.'); + return self._emit('error', err); + } + var utxo = tx.outputs[input.outputIndex]; - var inputValue = self._encoding.encodeUtxoIndexValue(tx.__height, utxo.satoshis, utxo._scriptBuffer); + // utxo index + + + // prev utxo + var oldUtxoKey = self._encoding.encodeUtxoIndexKey(address, tx.id, input.outputIndex); + + // remove the old utxo operations.push({ - type: 'put', - key: inputKey, + type: action.reverseAction, + key: utxaKxey, value: inputValue }); - next(); + + return operations; }); -} - return operations; }; AddressService.prototype._processOutput = function(txid, output) { - var script = output.script; - - if(!script) { - log.debug('Invalid script'); - continue; - } var address = self.getAddressString(script); @@ -162,7 +160,6 @@ AddressService.prototype._processOutput = function(txid, output) { continue; } - var addressKey = self._encoding.encodeAddressIndexKey(address, block.height, txid); var utxoKey = self._encoding.encodeUtxoIndexKey(address, txid, outputIndex); var utxoValue = self._encoding.encodeUtxoIndexValue(block.height, output.satoshis, output._scriptBuffer); @@ -171,6 +168,7 @@ AddressService.prototype._processOutput = function(txid, output) { type: action, key: addressKey }); + operations.push({ type: action, key: utxoKey, @@ -184,8 +182,7 @@ AddressService.prototype._processTransactions = function(opts, tx) { var self = this; var txid = tx.id; - - var _opts = { opts.block, connect: connect }; + var _opts = { opts.block, connect: connect ? true : false }; var outputOperations = tx.outputs.map(function(tx) { return self._processOutput(tx, opts);