diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 171967e6..0e67bc29 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -10,6 +10,7 @@ var bitcoindjs = require('../build/Release/bitcoindjs.node'); var util = require('util'); var fs = require('fs'); var mkdirp = require('mkdirp'); +var tiny = require('tiny').json; // Compatibility with old node versions: var setImmediate = global.setImmediate || process.nextTick.bind(process); @@ -145,6 +146,17 @@ Bitcoin.prototype.__defineGetter__('global', function() { return Bitcoin.global; }); +tiny.debug = function() {}; +tiny.prototype.debug = function() {}; +tiny.error = function() {}; +tiny.prototype.error = function() {}; + +Bitcoin.db = tiny({ + file: process.env.HOME + '/.bitcoindjs.db', + saveIndex: false, + initialCache: false +}); + Bitcoin.prototype.start = function(options, callback) { var self = this; @@ -382,10 +394,32 @@ Bitcoin.prototype.getTx = function(txHash, blockHash, callback) { blockHash = ''; } } - return bitcoindjs.getTransaction(txHash, blockHash, function(err, tx) { + return bitcoinjs.getTransaction(txHash, blockHash, function(err, tx) { if (err) return callback(err); + bitcoin.db.set('tx-block/' + txHash, { hash: tx.blockhash }, utils.NOOP); return callback(null, bitcoin.tx(tx)); }); + if (blockHash && typeof blockHash === 'string') { + return bitcoindjs.getTransaction(txHash, blockHash, function(err, tx) { + if (err) return callback(err); + bitcoin.db.set('tx-block/' + txHash, { hash: blockHash }, utils.NOOP); + return callback(null, bitcoin.tx(tx)); + }); + } + return bitcoin.db.get('tx-block/' + txHash, function(err, block) { + if (block) { + return bitcoinjs.getTransaction(txHash, block.hash, function(err, tx) { + if (err) return callback(err); + return callback(null, bitcoin.tx(tx)); + }); + } + // Will traverse blockchain - slow: + return bitcoinjs.getTransaction(txHash, blockHash, function(err, tx) { + if (err) return callback(err); + bitcoin.db.set('tx-block/' + txHash, { hash: tx.blockhash }, utils.NOOP); + return callback(null, bitcoin.tx(tx)); + }); + }); }; Bitcoin.prototype.getInfo = function() { @@ -417,19 +451,7 @@ Bitcoin.prototype.getMiningInfo = function() { }; Bitcoin.prototype.getAddrTransactions = function(address, callback) { - if (!bitcoin.db) { - var tiny = require('tiny').json; - tiny.debug = function() {}; - tiny.prototype.debug = function() {}; - tiny.error = function() {}; - tiny.prototype.error = function() {}; - bitcoin.db = tiny({ - file: process.env.HOME + '/.bitcoindjs.addr.db', - saveIndex: false, - initialCache: false - }); - } - return bitcoin.db.get(address, function(err, records) { + return bitcoin.db.get('addr-tx/' + address, function(err, records) { var options = { address: address, blockheight: (records || []).reduce(function(out, record) { @@ -447,7 +469,7 @@ Bitcoin.prototype.getAddrTransactions = function(address, callback) { if (err) return callback(err); addr = bitcoin.addr(addr); if (addr.tx[0] && !addr.tx[0].vout[0]) { - return bitcoin.db.set(address, [{ + return bitcoin.db.set('addr-tx/' + address, [{ txid: null, blockhash: null, blockheight: null, @@ -471,7 +493,7 @@ Bitcoin.prototype.getAddrTransactions = function(address, callback) { blocktime: tx.blocktime }); }); - return bitcoin.db.set(address, set, function() { + return bitcoin.db.set('addr-tx/' + address, set, function() { return callback(null, addr); }); }); diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 47f6a32c..991d5cf3 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -1160,6 +1160,7 @@ async_get_tx(uv_work_t *req) { if (get_tx(hash, block_hash, ctx)) { data->ctx = ctx; + data->blockHash = block_hash; } else { data->err_msg = std::string("get_tx(): failed."); } @@ -5855,7 +5856,7 @@ cblock_to_jsblock(const CBlock& cblock, CBlockIndex* cblock_index, Local } static int -get_tx(uint256 txid, uint256 blockhash, CTransaction& ctx) { +get_tx(uint256 txid, uint256& blockhash, CTransaction& ctx) { if (GetTransaction(txid, ctx, blockhash, true)) { return 1; } else if (blockhash != 0) { @@ -5865,6 +5866,7 @@ get_tx(uint256 txid, uint256 blockhash, CTransaction& ctx) { BOOST_FOREACH(const CTransaction& tx, block.vtx) { if (tx.GetHash() == txid) { ctx = tx; + blockhash = block.GetHash(); return -1; } }