From ac774ba9e89f352dddacb17682e7f5f51d884eaa Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Fri, 24 Jul 2015 12:34:33 -0600 Subject: [PATCH] find transactions which spend from the address --- lib/modules/address.js | 95 +++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 43 deletions(-) diff --git a/lib/modules/address.js b/lib/modules/address.js index 69da0c19..4148aa97 100644 --- a/lib/modules/address.js +++ b/lib/modules/address.js @@ -5,6 +5,7 @@ var inherits = require('util').inherits; var async = require('async'); var chainlib = require('chainlib'); var log = chainlib.log; +var levelup = chainlib.deps.levelup; var errors = chainlib.errors; var bitcore = require('bitcore'); var $ = bitcore.util.preconditions; @@ -23,7 +24,8 @@ var AddressModule = function(options) { inherits(AddressModule, BaseModule); AddressModule.PREFIXES = { - OUTPUTS: 'outs' + OUTPUTS: 'outs', + SPENTS: 'sp' }; AddressModule.prototype.getAPIMethods = function() { @@ -117,6 +119,13 @@ AddressModule.prototype.blockHandler = function(block, addOutput, callback) { continue; } + for(var j = 0; j < inputs.length; j++) { + operations.push({ + type: action, + key: [AddressModule.PREFIXES.SPENTS, inputs[j].prevTxId, inputs[j].outputIndex].join('-'), + value: txid + }); + } } setImmediate(function() { @@ -282,55 +291,55 @@ AddressModule.prototype.isSpent = function(output, queryMempool, callback) { }); }; +AddressModule.prototype.getSpendTxForOutput = function(txid, outputIndex, queryMempool, callback) { + var self = this; + + var key = [AddressModule.PREFIXES.SPENTS, txid, outputIndex].join('-'); + this.db.store.get(key, function(err, spentTxId) { + if(err) { + return callback(err); + } + + self.db.getTransaction(spentTxId, queryMempool, callback); + }); +}; + AddressModule.prototype.getTransactionsForAddress = function(address, queryMempool, callback) { var self = this; - var txids = []; - var key = [AddressModule.PREFIXES.OUTPUTS, address].join('-'); - - var stream = this.db.store.createReadStream({ - start: key, - end: key + '~' - }); - - stream.on('data', function(data) { - var key = data.key.split('-'); - txids.push(key[3]); - }); - - var error; - - stream.on('error', function(streamError) { - if (streamError) { - error = streamError; - } - }); - - stream.on('close', function() { - if (error) { - return callback(error); + this.getOutputs(address, queryMempool, function(err, outputs) { + if(err) { + return callback(err); } - async.map( - txids, - function(txid, next) { - self.db.getTransaction(txid, next); + var transactions = []; + + async.eachSeries( + outputs, + function(output, next) { + self.db.getTransaction(output.txid, queryMempool, function(err, tx) { + if(err) { + return next(err); + } + + transactions.push(tx); + + self.db.getSpendTxForOutput(output.txid, output.outputIndex, queryMempool, function(err, tx) { + if(err instanceof levelup.errors.NotFoundError) { + return next(); + } else if(err) { + return next(err); + } + + transactions.push(tx); + next(); + }); + }); }, - function(err, transactions) { - if(err) { - return callback(err); - } - - if(queryMempool) { - // TODO - } - - callback(null, transactions); - }); + function(err) { + callback(err, transactions); + }); }); - - return stream; - }; module.exports = AddressModule; \ No newline at end of file