From c00ca5b23fd5d18a6652780a9c43047f9c2ef324 Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Thu, 16 Jul 2015 14:09:10 -0400 Subject: [PATCH] query bitcoind for spents and transactions --- lib/db.js | 113 +++++++--------------------------------------------- lib/node.js | 1 + 2 files changed, 16 insertions(+), 98 deletions(-) diff --git a/lib/db.js b/lib/db.js index bdac5058..ed24ab50 100644 --- a/lib/db.js +++ b/lib/db.js @@ -29,7 +29,6 @@ function DB(options) { util.inherits(DB, BaseDB); DB.PREFIXES = { - TX: 'tx', SPENTS: 'sp', OUTPUTS: 'outs' }; @@ -53,9 +52,15 @@ DB.prototype.putBlock = function(block, callback) { this._updatePrevHashIndex(block, callback); }; -/*DB.prototype.getTransaction = function(txid, queryMempool, callback) { +DB.prototype.getTransaction = function(txid, queryMempool, callback) { + this.bitcoind.getTransaction(txid, function(err, txBuffer) { + if(err) { + return callback(err); + } -};*/ + callback(null, Transaction().fromBuffer(txBuffer)); + }); +}; DB.prototype.validateBlockData = function(block, callback) { // bitcoind does the validation @@ -216,54 +221,6 @@ DB.prototype._updateOutputs = function(block, addOutput, callback) { }); }; -DB.prototype._updateTransactions = function(block, addTransaction, callback) { - var self = this; - - DB.super_.prototype._updateTransactions.call(self, block, addTransaction, function(err, operations) { - if(err || !addTransaction) { - return callback(err, operations); - } - - // Remove transactions from mempool with inputs that were spent - var mempoolTransactions = self.mempool.getTransactions(); - var blockTransactions = self.getTransactionsFromBlock(block); - var newMempoolTransactions = []; - - for(var i = 0; i < mempoolTransactions.length; i++) { - var txHasInputsInBlock = false; - for(var j = 0; j < mempoolTransactions[i].inputs.length; j++) { - for(var k = 0; k < blockTransactions.length; k++) { - for(var l = 0; l < blockTransactions[k].inputs.length; l++) { - var mempoolInput = mempoolTransactions[i].inputs[j]; - var blockInput = blockTransactions[k].inputs[l]; - if(mempoolInput.prevTxId === blockInput.prevTxId && - mempoolInput.outputIndex === blockInput.outputIndex) { - txHasInputsInBlock = true; - break; - } - } - - if(txHasInputsInBlock) { - break; - } - } - - if(txHasInputsInBlock) { - break; - } - } - - if(!txHasInputsInBlock) { - newMempoolTransactions.push(mempoolTransactions[i]); - } - } - - self.mempool.transactions = newMempoolTransactions; - - callback(null, operations); - }); -}; - DB.prototype._onChainAddBlock = function(block, callback) { var self = this; @@ -275,7 +232,6 @@ DB.prototype._onChainAddBlock = function(block, callback) { async.series([ this._updateOutputs.bind(this, block, true), // add outputs - this._updateTransactions.bind(this, block, true) // add transactions ], function(err, results) { if (err) { @@ -302,7 +258,6 @@ DB.prototype._onChainRemoveBlock = function(block, callback) { async.series([ this._updateOutputs.bind(this, block, false), // remove outputs - this._updateTransactions.bind(this, block, false) // remove transactions ], function(err, results) { if (err) { @@ -390,11 +345,11 @@ DB.prototype.getOutputs = function(address, queryMempool, callback) { return callback(error); } - if(queryMempool) { + /*if(queryMempool) { var mempoolOutputs = self._getMempoolOutputs(address); outputs = outputs.concat(self._getMempoolOutputs(address)); - } + }*/ callback(null, outputs); }); @@ -403,35 +358,6 @@ DB.prototype.getOutputs = function(address, queryMempool, callback) { }; -DB.prototype._getMempoolOutputs = function(address) { - var outputs = []; - - var transactions = this.mempool.getTransactions(); - transactions.forEach(function(tx) { - // add additional info to outputs - var outputObjects = []; - for(var i = 0; i < tx.outputs.length; i++) { - var output = {}; - output.script = tx.outputs[i].script.toString(); - output.satoshis = tx.outputs[i].satoshis; - output.txid = tx.hash; - output.outputIndex = i; - output.address = tx.outputs[i].script.toAddress().toString(); - outputObjects.push(output); - } - - var filtered = outputObjects.filter(function(output) { - return address.toString() === output.address; - }); - - if(filtered.length) { - outputs = outputs.concat(filtered); - } - }); - - return outputs; -}; - DB.prototype.getUnspentOutputs = function(address, queryMempool, callback) { var self = this; @@ -460,21 +386,12 @@ DB.prototype.isUnspent = function(output, queryMempool, callback) { }; DB.prototype.isSpent = function(output, queryMempool, callback) { - if(queryMempool && this._isSpentMempool(output)) { - return callback(true); - } + var self = this; + var txid = output.prevTxId ? output.prevTxId.toString('hex') : output.txid; - this.isSpentDB(output, callback); -}; - -DB.prototype.isSpentDB = function(output, callback) { - // Query bitcoind - return callback(null, true); -}; - -DB.prototype._isSpentMempool = function(output) { - // Query bitcoind - return true; + setImmediate(function() { + callback(self.bitcoind.isSpent(txid, output.outputIndex)); + }); }; module.exports = DB; diff --git a/lib/node.js b/lib/node.js index 264be92a..23e94fba 100644 --- a/lib/node.js +++ b/lib/node.js @@ -111,6 +111,7 @@ Node.prototype._loadDB = function(config) { // Other modules can inherit from our DB and replace it with their own DB = config.DB; } + config.db.network = this.network; this.db = new DB(config.db); };