From 492895538a68a6df19fd2ab3424315d04546e3f9 Mon Sep 17 00:00:00 2001 From: support Date: Fri, 12 May 2017 15:38:02 +0000 Subject: [PATCH] Fixed streaming issue and slowness when using new PublicKey. --- lib/services/bitcoind/index.js | 4 ++-- lib/services/db/index.js | 12 +++++++----- lib/services/db/sync.js | 1 + lib/services/transaction/index.js | 3 +++ lib/services/wallet-api/index.js | 10 +++++----- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/services/bitcoind/index.js b/lib/services/bitcoind/index.js index ed1c260a..0207e663 100644 --- a/lib/services/bitcoind/index.js +++ b/lib/services/bitcoind/index.js @@ -394,12 +394,12 @@ Bitcoin.prototype.getTransaction = function(txid, callback) { var self = this; self._tryAllClients(function(client, done) { //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) { if (err) { return done(self._wrapRPCError(err)); } - var tx = new Transaction(response.result); - done(null, tx); + done(null, response.result); }); }, callback); }; diff --git a/lib/services/db/index.js b/lib/services/db/index.js index 54f01a5a..fd0ed177 100644 --- a/lib/services/db/index.js +++ b/lib/services/db/index.js @@ -72,19 +72,21 @@ util.inherits(DB, Service); DB.dependencies = ['bitcoind']; DB.prototype.pauseSync = function(callback) { - this._lockTimes.push(process.hrtime()); - if (this._sync.syncing) { - this._sync.once('synced', function() { - this._sync.paused = true; + var self = this; + self._lockTimes.push(process.hrtime()); + if (self._sync.syncing) { + self._sync.once('synced', function() { + self._sync.paused = true; callback(); }); } else { - this._sync.paused = true; + self._sync.paused = true; setImmediate(callback); } }; DB.prototype.resumeSync = function() { + log.debug('Attempting to resume sync'); var time = this._lockTimes.shift(); if (this._lockTimes.length === 0) { if (time) { diff --git a/lib/services/db/sync.js b/lib/services/db/sync.js index 8b55b4ef..c470fa07 100644 --- a/lib/services/db/sync.js +++ b/lib/services/db/sync.js @@ -76,6 +76,7 @@ Sync.prototype.sync = function() { var self = this; if(this.syncing || this.paused) { + log.debug('Sync lock held, not able to sync at the moment'); return; } diff --git a/lib/services/transaction/index.js b/lib/services/transaction/index.js index 6a0c2d13..064e8187 100644 --- a/lib/services/transaction/index.js +++ b/lib/services/transaction/index.js @@ -190,6 +190,9 @@ TransactionService.prototype.getTransaction = function(txid, options, callback) self._getMissingInputValues(tx, next); }); }, function(tx, next) { + if (tx) { + return next(null, tx); + } self.node.services.bitcoind.getTransaction(txid, next); }], callback); }; diff --git a/lib/services/wallet-api/index.js b/lib/services/wallet-api/index.js index ecf9f1ae..19595310 100644 --- a/lib/services/wallet-api/index.js +++ b/lib/services/wallet-api/index.js @@ -17,7 +17,7 @@ var LRU = require('lru-cache'); var Encoding = require('./encoding'); var Input = require('bitcore-lib').Transaction.Input; var Transform = require('stream').Transform; -var transform = new Transform({ objectMode: true }); +var transform = new Transform({ objectMode: true, highWaterMark: 1000000 }); var WalletService = function(options) { BaseService.call(this, options); @@ -668,12 +668,13 @@ WalletService.prototype._endpointGetTransactions = function() { transform._transform = function(chunk, enc, callback) { var txid = self._encoding.decodeWalletTransactionKey(chunk).txid.toString('hex'); + self._getTransactionFromDb(options, txid, function(err, tx) { if(err) { log.error(err); - transform.push(null); - return; + transform.unpipe(); + return callback(); } var formattedTx = utils.toJSONL(self._formatTransaction(tx)); @@ -685,13 +686,12 @@ WalletService.prototype._endpointGetTransactions = function() { }; transform._flush = function(callback) { + stream.unpipe(transform); self.db.resumeSync(); - transform.push(null); callback(); }; 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)); stream.pipe(transform).pipe(res);