bitcoind: try querying all bitcoind nodes

This commit is contained in:
Braydon Fuller 2016-04-11 10:14:36 -04:00
parent d7f49cc192
commit 5bea36edc6
2 changed files with 24 additions and 14 deletions

View File

@ -235,6 +235,10 @@ Bitcoin.prototype._resetCaches = function() {
this.summaryCache.reset(); this.summaryCache.reset();
}; };
Bitcoin.prototype._tryAll = function(func, callback) {
async.retry({times: this.nodes.length, interval: 1000}, func, callback);
};
Bitcoin.prototype._wrapRPCError = function(errObj) { Bitcoin.prototype._wrapRPCError = function(errObj) {
var err = new Error(errObj.message); var err = new Error(errObj.message);
err.code = errObj.code; err.code = errObj.code;
@ -1036,14 +1040,16 @@ Bitcoin.prototype.getBlock = function(blockArg, callback) {
var self = this; var self = this;
function queryBlock(blockhash) { function queryBlock(blockhash) {
self.client.getBlock(blockhash, false, function(err, response) { self._tryAll(function(done) {
if (err) { self.client.getBlock(blockhash, false, function(err, response) {
return callback(self._wrapRPCError(err)); if (err) {
} return done(self._wrapRPCError(err));
var blockObj = bitcore.Block.fromString(response.result); }
self.blockCache.set(blockhash, blockObj); var blockObj = bitcore.Block.fromString(response.result);
callback(null, blockObj); self.blockCache.set(blockhash, blockObj);
}); done(null, blockObj);
});
}, callback);
} }
var cachedBlock = self.blockCache.get(blockArg); var cachedBlock = self.blockCache.get(blockArg);
@ -1053,11 +1059,17 @@ Bitcoin.prototype.getBlock = function(blockArg, callback) {
}); });
} else { } else {
if (_.isNumber(blockArg)) { if (_.isNumber(blockArg)) {
self.client.getBlockHash(blockArg, function(err, response) { self._tryAll(function(done) {
self.client.getBlockHash(blockArg, function(err, response) {
if (err) {
return done(self._wrapRPCError(err));
}
done(null, response.result);
});
}, function(err, blockhash) {
if (err) { if (err) {
return callback(self._wrapRPCError(err)); return callback(err);
} }
var blockhash = response.result;
queryBlock(blockhash); queryBlock(blockhash);
}); });
} else { } else {

View File

@ -157,9 +157,7 @@ describe('Bitcoin Cluster', function() {
it('step 2: receive block events', function(done) { it('step 2: receive block events', function(done) {
this.timeout(10000); this.timeout(10000);
node.services.bitcoind.once('tip', function() { node.services.bitcoind.once('tip', function() {
setTimeout(function() { done();
done();
}, 1000);
}); });
node.generateBlock(1, function(err, hashes) { node.generateBlock(1, function(err, hashes) {
if (err) { if (err) {