diff --git a/lib/services/bitcoind/index.js b/lib/services/bitcoind/index.js index 65623bea..6ff46a8f 100644 --- a/lib/services/bitcoind/index.js +++ b/lib/services/bitcoind/index.js @@ -389,6 +389,10 @@ Bitcoin.prototype.start = function(callback) { }; +Bitcoin.prototype.stop = function(callback) { + callback(); +}; + Bitcoin.prototype._maybeGetBlockHash = function(blockArg, callback) { var self = this; if (_.isNumber(blockArg) || (blockArg.length < 40 && /^[0-9]+$/.test(blockArg))) { @@ -448,8 +452,28 @@ Bitcoin.prototype.getBlock = function(blockArg, callback) { self._maybeGetBlockHash(blockArg, queryBlock); }; -Bitcoin.prototype.stop = function(callback) { - callback(); +Bitcoin.prototype.isSynced = function(callback) { + this.syncPercentage(function(err, percentage) { + if (err) { + return callback(err); + } + if (Math.round(percentage) >= 100) { + callback(null, true); + } else { + callback(null, false); + } + }); +}; + +Bitcoin.prototype.syncPercentage = function(callback) { + var self = this; + self.client.getBlockchainInfo(function(err, response) { + if (err) { + return callback(self._wrapRPCError(err)); + } + var percentSynced = response.result.verificationprogress * 100; + callback(null, percentSynced); + }); }; module.exports = Bitcoin; diff --git a/lib/services/db/index.js b/lib/services/db/index.js index be814780..b01a9340 100644 --- a/lib/services/db/index.js +++ b/lib/services/db/index.js @@ -123,18 +123,19 @@ DB.prototype._setVersion = function(callback) { DB.prototype.start = function(callback) { var self = this; - if (!fs.existsSync(this.dataPath)) { + + if (!fs.existsSync(self.dataPath)) { mkdirp.sync(this.dataPath); } - this.genesis = Block.fromBuffer(this.node.services.bitcoind.genesisBuffer); - this.store = levelup(this.dataPath, { db: this.levelupStore, keyEncoding: 'binary', valueEncoding: 'binary'}); + self.genesis = Block.fromBuffer(self.node.services.bitcoind.genesisBuffer); + self.store = levelup(self.dataPath, { db: self.levelupStore, keyEncoding: 'binary', valueEncoding: 'binary'}); - this._sync.on('error', function(err) { + self._sync.on('error', function(err) { log.error(err); }); - this._sync.on('reorg', function(block) { + self._sync.on('reorg', function(block) { log.warn('Reorg detected! Tip: ' + self.tip.hash + ' Concurrent tip: ' + self.concurrentTip.hash + ' Bitcoind tip: ' + self.node.services.bitcoind.tiphash); @@ -158,22 +159,26 @@ DB.prototype.start = function(callback) { }); }); - this._sync.on('synced', function() { + self._sync.on('synced', function() { log.permitWrites = true; log.info('Initial sync complete'); }); - this.node.on('stopping', function() { + self.node.on('stopping', function() { self._sync.stop(); }); - this.node.once('ready', function() { + self.node.once('ready', function() { log.permitWrites = false; - self._sync.initialSync(); + if (!(self.tip.__height === self.node.services.bitcoind.height && + self.tip === self.node.services.bitcoind.tiphash)) { + self._sync.initialSync(); + } self.node.services.bitcoind.on('tip', function() { self._sync.sync(); }); + }); async.series([ diff --git a/lib/services/db/sync.js b/lib/services/db/sync.js index bedfe74a..fccd0310 100644 --- a/lib/services/db/sync.js +++ b/lib/services/db/sync.js @@ -219,7 +219,7 @@ BlockStream.prototype._process = function() { self.syncing = false; self.emit('synced'); break; - }; + } } next(); diff --git a/lib/services/wallet-api/index.js b/lib/services/wallet-api/index.js index fc74efcb..101f5ab2 100644 --- a/lib/services/wallet-api/index.js +++ b/lib/services/wallet-api/index.js @@ -641,10 +641,10 @@ WalletService.prototype._getUtxos = function(walletId, options, callback) { utxos.push({ txid: key.txid, - outputIndex: key.outputIndex, + vout: key.outputIndex, height: value.height, satoshis: value.satoshis, - scriptPubKey: value.script.toHex() + scriptPubKey: value.script._scriptBuffer }); });