From f1277c8ad3e03d706a82dda3c5444aeaf6b21c35 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Wed, 18 Jan 2017 19:11:47 -0500 Subject: [PATCH] Added height into tx index. --- lib/services/address/index.js | 12 ++++++++---- lib/services/transaction.js | 13 +++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 139bb96f..f0592609 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -529,17 +529,21 @@ AddressService.prototype.blockHandler = function(block, connectBlock, callback) var address = input.script.toAddress(); - // To be able to query inputs by address and spent height var inputKey = encoding.encodeAddressIndexKey(address, true, height, txid, inputIndex, true); - self.node.services.transaction.getTransaction(input.prevTxId, function(err, tx) { + + self.node.services.transaction.getTransaction(input.prevTxId, function(err, result) { if(err) { return next(err); } + + var tx = result.transaction; + var height = result.height; var output = tx.outputs[input.outputIndex]; - var outputKey = encoding.encodeAddressIndexKey(address, true, tx.__height, tx.id, input.outputIndex, false); - var outputKeyToDelete = encoding.encodeAddressIndexKey(address, false, tx.__height, tx.id, input.outputIndex, false); + var outputKey = encoding.encodeAddressIndexKey(address, true, height, tx.id, input.outputIndex, false); + var outputKeyToDelete = encoding.encodeAddressIndexKey(address, false, height, tx.id, input.outputIndex, false); var outputValue = encoding.encodeAdressIndexValue(output.satoshis, output._scriptBuffer); var inputValue = encoding.encodeAddressIndexValue(output.satoshis, input._scriptBuffer); + operations = operations.concat([{ type: action, key: inputKey, diff --git a/lib/services/transaction.js b/lib/services/transaction.js index 2dc6ea44..dd17e053 100644 --- a/lib/services/transaction.js +++ b/lib/services/transaction.js @@ -64,12 +64,17 @@ TransactionService.prototype._decodeTransactionKey = function(buffer) { return buffer.slice(1).toString('hex'); }; -TransactionService.prototype._encodeTransactionValue = function(transaction) { - return transaction.uncheckedSerialize(); +TransactionService.prototype._encodeTransactionValue = function(transaction, height) { + var heightBuffer = new Buffer(4); + heightBuffer.writeUInt32BE(height); + return new Buffer.concat([heightBuffer, transaction.uncheckedSerialize()]); }; TransactionService.prototype._decodeTransactionValue = function(buffer) { - return new bitcore.Transaction(buffer); + return { + height: Buffer.readUInt32BE(height), + transaction: new bitcore.Transaction(buffer) + }; }; -module.exports = TransactionService; \ No newline at end of file +module.exports = TransactionService;