database improvements. record blockhash.

This commit is contained in:
Christopher Jeffrey 2014-12-08 11:30:06 -08:00
parent 6347910195
commit fab1f4512b
2 changed files with 41 additions and 17 deletions

View File

@ -10,6 +10,7 @@ var bitcoindjs = require('../build/Release/bitcoindjs.node');
var util = require('util'); var util = require('util');
var fs = require('fs'); var fs = require('fs');
var mkdirp = require('mkdirp'); var mkdirp = require('mkdirp');
var tiny = require('tiny').json;
// Compatibility with old node versions: // Compatibility with old node versions:
var setImmediate = global.setImmediate || process.nextTick.bind(process); var setImmediate = global.setImmediate || process.nextTick.bind(process);
@ -145,6 +146,17 @@ Bitcoin.prototype.__defineGetter__('global', function() {
return Bitcoin.global; 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) { Bitcoin.prototype.start = function(options, callback) {
var self = this; var self = this;
@ -382,10 +394,32 @@ Bitcoin.prototype.getTx = function(txHash, blockHash, callback) {
blockHash = ''; blockHash = '';
} }
} }
return bitcoindjs.getTransaction(txHash, blockHash, function(err, tx) { return bitcoinjs.getTransaction(txHash, blockHash, function(err, tx) {
if (err) return callback(err); if (err) return callback(err);
bitcoin.db.set('tx-block/' + txHash, { hash: tx.blockhash }, utils.NOOP);
return callback(null, bitcoin.tx(tx)); 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() { Bitcoin.prototype.getInfo = function() {
@ -417,19 +451,7 @@ Bitcoin.prototype.getMiningInfo = function() {
}; };
Bitcoin.prototype.getAddrTransactions = function(address, callback) { Bitcoin.prototype.getAddrTransactions = function(address, callback) {
if (!bitcoin.db) { return bitcoin.db.get('addr-tx/' + address, function(err, records) {
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) {
var options = { var options = {
address: address, address: address,
blockheight: (records || []).reduce(function(out, record) { blockheight: (records || []).reduce(function(out, record) {
@ -447,7 +469,7 @@ Bitcoin.prototype.getAddrTransactions = function(address, callback) {
if (err) return callback(err); if (err) return callback(err);
addr = bitcoin.addr(addr); addr = bitcoin.addr(addr);
if (addr.tx[0] && !addr.tx[0].vout[0]) { if (addr.tx[0] && !addr.tx[0].vout[0]) {
return bitcoin.db.set(address, [{ return bitcoin.db.set('addr-tx/' + address, [{
txid: null, txid: null,
blockhash: null, blockhash: null,
blockheight: null, blockheight: null,
@ -471,7 +493,7 @@ Bitcoin.prototype.getAddrTransactions = function(address, callback) {
blocktime: tx.blocktime blocktime: tx.blocktime
}); });
}); });
return bitcoin.db.set(address, set, function() { return bitcoin.db.set('addr-tx/' + address, set, function() {
return callback(null, addr); return callback(null, addr);
}); });
}); });

View File

@ -1160,6 +1160,7 @@ async_get_tx(uv_work_t *req) {
if (get_tx(hash, block_hash, ctx)) { if (get_tx(hash, block_hash, ctx)) {
data->ctx = ctx; data->ctx = ctx;
data->blockHash = block_hash;
} else { } else {
data->err_msg = std::string("get_tx(): failed."); data->err_msg = std::string("get_tx(): failed.");
} }
@ -5855,7 +5856,7 @@ cblock_to_jsblock(const CBlock& cblock, CBlockIndex* cblock_index, Local<Object>
} }
static int static int
get_tx(uint256 txid, uint256 blockhash, CTransaction& ctx) { get_tx(uint256 txid, uint256& blockhash, CTransaction& ctx) {
if (GetTransaction(txid, ctx, blockhash, true)) { if (GetTransaction(txid, ctx, blockhash, true)) {
return 1; return 1;
} else if (blockhash != 0) { } else if (blockhash != 0) {
@ -5865,6 +5866,7 @@ get_tx(uint256 txid, uint256 blockhash, CTransaction& ctx) {
BOOST_FOREACH(const CTransaction& tx, block.vtx) { BOOST_FOREACH(const CTransaction& tx, block.vtx) {
if (tx.GetHash() == txid) { if (tx.GetHash() == txid) {
ctx = tx; ctx = tx;
blockhash = block.GetHash();
return -1; return -1;
} }
} }