bitcoind: subscribe to zmq events without interval if already synced
This commit is contained in:
parent
2b38f08175
commit
b092adcc21
@ -435,11 +435,12 @@ Bitcoin.prototype._zmqTransactionHandler = function(node, message) {
|
|||||||
Bitcoin.prototype._checkSyncedAndSubscribeZmqEvents = function(node) {
|
Bitcoin.prototype._checkSyncedAndSubscribeZmqEvents = function(node) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var interval;
|
var interval;
|
||||||
interval = setInterval(function() {
|
|
||||||
|
function checkAndSubscribe(callback) {
|
||||||
// update tip
|
// update tip
|
||||||
node.client.getBestBlockHash(function(err, response) {
|
node.client.getBestBlockHash(function(err, response) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return log.error(self._wrapRPCError(err));
|
return callback(self._wrapRPCError(err));
|
||||||
}
|
}
|
||||||
var blockhash = new Buffer(response.result, 'hex');
|
var blockhash = new Buffer(response.result, 'hex');
|
||||||
self._updateTip(node, blockhash);
|
self._updateTip(node, blockhash);
|
||||||
@ -447,17 +448,36 @@ Bitcoin.prototype._checkSyncedAndSubscribeZmqEvents = function(node) {
|
|||||||
// check if synced
|
// check if synced
|
||||||
node.client.getBlockchainInfo(function(err, response) {
|
node.client.getBlockchainInfo(function(err, response) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return log.error(self._wrapRPCError(err));
|
return callback(self._wrapRPCError(err));
|
||||||
}
|
}
|
||||||
var percentSynced = response.result.verificationprogress * 100;
|
var percentSynced = response.result.verificationprogress * 100;
|
||||||
if (Math.round(percentSynced) >= 99) {
|
if (Math.round(percentSynced) >= 99) {
|
||||||
// subscribe to events for further updates
|
// subscribe to events for further updates
|
||||||
self._subscribeZmqEvents(node);
|
self._subscribeZmqEvents(node);
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
|
callback(null, true);
|
||||||
|
} else {
|
||||||
|
callback(null, false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}, node._tipUpdateInterval || Bitcoin.DEFAULT_TIP_UPDATE_INTERVAL).unref();
|
}
|
||||||
|
|
||||||
|
checkAndSubscribe(function(err, synced) {
|
||||||
|
if (err) {
|
||||||
|
log.error(err);
|
||||||
|
}
|
||||||
|
if (!synced) {
|
||||||
|
interval = setInterval(function() {
|
||||||
|
checkAndSubscribe(function(err) {
|
||||||
|
if (err) {
|
||||||
|
log.error(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, node._tipUpdateInterval || Bitcoin.DEFAULT_TIP_UPDATE_INTERVAL).unref();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Bitcoin.prototype._subscribeZmqEvents = function(node) {
|
Bitcoin.prototype._subscribeZmqEvents = function(node) {
|
||||||
|
|||||||
@ -685,11 +685,20 @@ describe('Bitcoin Service', function() {
|
|||||||
result: '00000000000000001bb82a7f5973618cfd3185ba1ded04dd852a653f92a27c45'
|
result: '00000000000000001bb82a7f5973618cfd3185ba1ded04dd852a653f92a27c45'
|
||||||
});
|
});
|
||||||
getBestBlockHash.onCall(0).callsArgWith(0, {code: -1 , message: 'Test error'});
|
getBestBlockHash.onCall(0).callsArgWith(0, {code: -1 , message: 'Test error'});
|
||||||
var getBlockchainInfo = sinon.stub().callsArgWith(0, null, {
|
var progress = 0.90;
|
||||||
result: {
|
function getProgress() {
|
||||||
verificationprogress: 0.99
|
progress = progress + 0.01;
|
||||||
|
return progress;
|
||||||
|
}
|
||||||
|
var info = {};
|
||||||
|
Object.defineProperty(info, 'result', {
|
||||||
|
get: function() {
|
||||||
|
return {
|
||||||
|
verificationprogress: getProgress()
|
||||||
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
var getBlockchainInfo = sinon.stub().callsArgWith(0, null, info);
|
||||||
getBlockchainInfo.onCall(0).callsArgWith(0, {code: -1, message: 'Test error'});
|
getBlockchainInfo.onCall(0).callsArgWith(0, {code: -1, message: 'Test error'});
|
||||||
var node = {
|
var node = {
|
||||||
_reindex: true,
|
_reindex: true,
|
||||||
@ -703,10 +712,10 @@ describe('Bitcoin Service', function() {
|
|||||||
bitcoind._checkSyncedAndSubscribeZmqEvents(node);
|
bitcoind._checkSyncedAndSubscribeZmqEvents(node);
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
log.error.callCount.should.equal(2);
|
log.error.callCount.should.equal(2);
|
||||||
bitcoind._updateTip.callCount.should.equal(2);
|
bitcoind._updateTip.callCount.should.equal(10);
|
||||||
bitcoind._subscribeZmqEvents.callCount.should.equal(1);
|
bitcoind._subscribeZmqEvents.callCount.should.equal(1);
|
||||||
done();
|
done();
|
||||||
}, 10);
|
}, 20);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user