Will not try to sync is already synced.

This commit is contained in:
Chris Kleeschulte 2017-02-20 13:04:29 -05:00
parent d4238225d4
commit a213ddb4fc
4 changed files with 43 additions and 14 deletions

View File

@ -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;

View File

@ -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([

View File

@ -219,7 +219,7 @@ BlockStream.prototype._process = function() {
self.syncing = false;
self.emit('synced');
break;
};
}
}
next();

View File

@ -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
});
});