Fixed streaming issue and slowness when using new PublicKey.

This commit is contained in:
support 2017-05-12 15:38:02 +00:00
parent 26704e52f7
commit 492895538a
5 changed files with 18 additions and 12 deletions

View File

@ -394,12 +394,12 @@ Bitcoin.prototype.getTransaction = function(txid, callback) {
var self = this; var self = this;
self._tryAllClients(function(client, done) { self._tryAllClients(function(client, done) {
//this won't work without a bitcoin node that has a tx index //this won't work without a bitcoin node that has a tx index
log.error('Txid: ' + txid + ' not found in index! Calling getRawTransaction to retrieve.');
self.client.getRawTransaction(txid.toString('hex'), 0, function(err, response) { self.client.getRawTransaction(txid.toString('hex'), 0, function(err, response) {
if (err) { if (err) {
return done(self._wrapRPCError(err)); return done(self._wrapRPCError(err));
} }
var tx = new Transaction(response.result); done(null, response.result);
done(null, tx);
}); });
}, callback); }, callback);
}; };

View File

@ -72,19 +72,21 @@ util.inherits(DB, Service);
DB.dependencies = ['bitcoind']; DB.dependencies = ['bitcoind'];
DB.prototype.pauseSync = function(callback) { DB.prototype.pauseSync = function(callback) {
this._lockTimes.push(process.hrtime()); var self = this;
if (this._sync.syncing) { self._lockTimes.push(process.hrtime());
this._sync.once('synced', function() { if (self._sync.syncing) {
this._sync.paused = true; self._sync.once('synced', function() {
self._sync.paused = true;
callback(); callback();
}); });
} else { } else {
this._sync.paused = true; self._sync.paused = true;
setImmediate(callback); setImmediate(callback);
} }
}; };
DB.prototype.resumeSync = function() { DB.prototype.resumeSync = function() {
log.debug('Attempting to resume sync');
var time = this._lockTimes.shift(); var time = this._lockTimes.shift();
if (this._lockTimes.length === 0) { if (this._lockTimes.length === 0) {
if (time) { if (time) {

View File

@ -76,6 +76,7 @@ Sync.prototype.sync = function() {
var self = this; var self = this;
if(this.syncing || this.paused) { if(this.syncing || this.paused) {
log.debug('Sync lock held, not able to sync at the moment');
return; return;
} }

View File

@ -190,6 +190,9 @@ TransactionService.prototype.getTransaction = function(txid, options, callback)
self._getMissingInputValues(tx, next); self._getMissingInputValues(tx, next);
}); });
}, function(tx, next) { }, function(tx, next) {
if (tx) {
return next(null, tx);
}
self.node.services.bitcoind.getTransaction(txid, next); self.node.services.bitcoind.getTransaction(txid, next);
}], callback); }], callback);
}; };

View File

@ -17,7 +17,7 @@ var LRU = require('lru-cache');
var Encoding = require('./encoding'); var Encoding = require('./encoding');
var Input = require('bitcore-lib').Transaction.Input; var Input = require('bitcore-lib').Transaction.Input;
var Transform = require('stream').Transform; var Transform = require('stream').Transform;
var transform = new Transform({ objectMode: true }); var transform = new Transform({ objectMode: true, highWaterMark: 1000000 });
var WalletService = function(options) { var WalletService = function(options) {
BaseService.call(this, options); BaseService.call(this, options);
@ -668,12 +668,13 @@ WalletService.prototype._endpointGetTransactions = function() {
transform._transform = function(chunk, enc, callback) { transform._transform = function(chunk, enc, callback) {
var txid = self._encoding.decodeWalletTransactionKey(chunk).txid.toString('hex'); var txid = self._encoding.decodeWalletTransactionKey(chunk).txid.toString('hex');
self._getTransactionFromDb(options, txid, function(err, tx) { self._getTransactionFromDb(options, txid, function(err, tx) {
if(err) { if(err) {
log.error(err); log.error(err);
transform.push(null); transform.unpipe();
return; return callback();
} }
var formattedTx = utils.toJSONL(self._formatTransaction(tx)); var formattedTx = utils.toJSONL(self._formatTransaction(tx));
@ -685,13 +686,12 @@ WalletService.prototype._endpointGetTransactions = function() {
}; };
transform._flush = function(callback) { transform._flush = function(callback) {
stream.unpipe(transform);
self.db.resumeSync(); self.db.resumeSync();
transform.push(null);
callback(); callback();
}; };
var encodingFn = self._encoding.encodeWalletTransactionKey.bind(self._encoding); var encodingFn = self._encoding.encodeWalletTransactionKey.bind(self._encoding);
//creates a stream of txids that match the wallet's set of addresses
var stream = self.store.createKeyStream(self._getSearchParams(encodingFn, options)); var stream = self.store.createKeyStream(self._getSearchParams(encodingFn, options));
stream.pipe(transform).pipe(res); stream.pipe(transform).pipe(res);