bitcoind: try to get transaction from all bitcoind nodes

This commit is contained in:
Braydon Fuller 2016-04-11 10:15:15 -04:00
parent 019626ba15
commit d0937fea55
2 changed files with 84 additions and 84 deletions

View File

@ -996,14 +996,16 @@ Bitcoin.prototype.getRawBlock = 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 buffer = new Buffer(response.result, 'hex'); }
self.rawBlockCache.set(blockhash, buffer); var buffer = new Buffer(response.result, 'hex');
callback(null, buffer); self.rawBlockCache.set(blockhash, buffer);
}); done(null, buffer);
});
}, callback);
} }
var cachedBlock = self.rawBlockCache.get(blockArg); var cachedBlock = self.rawBlockCache.get(blockArg);
@ -1013,11 +1015,17 @@ Bitcoin.prototype.getRawBlock = 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 callback(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 {
@ -1107,25 +1115,29 @@ Bitcoin.prototype.getBlockHeader = function(block, callback) {
var self = this; var self = this;
function queryHeader(blockhash) { function queryHeader(blockhash) {
self.client.getBlockHeader(blockhash, function(err, response) { self._tryAll(function(done) {
if (err && response.error.code === -5) { self.client.getBlockHeader(blockhash, function(err, response) {
return callback(null, null); if (err) {
} else if (err) { return done(self._wrapRPCError(err));
return callback(self._wrapRPCError(err)); }
} // TODO format response prevHash instead of previousblockhash, etc.
// TODO format response prevHash instead of previousblockhash, etc. done(null, response.result);
callback(null, response.result); });
}); }, callback);
} }
if (_.isNumber(block)) { if (_.isNumber(block)) {
self.client.getBlockHash(block, function(err, response) { self._tryAll(function(done) {
if (err && response.error.code === -8) { self.client.getBlockHash(block, function(err, response) {
return callback(null, null); if (err) {
} else if (err) { return callback(self._wrapRPCError(err));
return callback(self._wrapRPCError(err)); }
done(null, response.result);
});
}, function(err, blockhash) {
if (err) {
return callback(err);
} }
var blockhash = response.result;
queryHeader(blockhash); queryHeader(blockhash);
}); });
} else { } else {
@ -1192,16 +1204,16 @@ Bitcoin.prototype.getRawTransaction = function(txid, callback) {
callback(null, tx); callback(null, tx);
}); });
} else { } else {
self.client.getRawTransaction(txid, function(err, response) { self._tryAll(function(done) {
if (err && response.error.code === -5) { self.client.getRawTransaction(txid, function(err, response) {
return callback(null, null); if (err) {
} else if (err) { return done(self._wrapRPCError(err));
return callback(self._wrapRPCError(err)); }
} var buffer = new Buffer(response.result, 'hex');
var buffer = new Buffer(response.result, 'hex'); self.rawTransactionCache.set(txid, buffer);
self.rawTransactionCache.set(txid, buffer); done(null, buffer);
callback(null, buffer); });
}); }, callback);
} }
}; };
@ -1219,17 +1231,17 @@ Bitcoin.prototype.getTransaction = function(txid, callback) {
callback(null, tx); callback(null, tx);
}); });
} else { } else {
self.client.getRawTransaction(txid, function(err, response) { self._tryAll(function(done) {
if (err && response.error.code === -5) { self.client.getRawTransaction(txid, function(err, response) {
return callback(null, null); if (err) {
} else if (err) { return done(self._wrapRPCError(err));
return callback(self._wrapRPCError(err)); }
} var tx = Transaction();
var tx = Transaction(); tx.fromString(response.result);
tx.fromString(response.result); self.transactionCache.set(txid, tx);
self.transactionCache.set(txid, tx); done(null, tx);
callback(null, tx); });
}); }, callback);
} }
}; };
@ -1251,23 +1263,23 @@ Bitcoin.prototype.getTransactionWithBlockInfo = function(txid, callback) {
callback(null, tx); callback(null, tx);
}); });
} else { } else {
self.client.getRawTransaction(txid, 1, function(err, response) { self._tryAll(function(done) {
if (err && response.error.code === -5) { self.client.getRawTransaction(txid, 1, function(err, response) {
return callback(null, null); if (err) {
} else if (err) { return done(self._wrapRPCError(err));
return callback(self._wrapRPCError(err)); }
} var tx = Transaction();
var tx = Transaction(); tx.fromString(response.result.hex);
tx.fromString(response.result.hex); tx.__blockHash = response.result.blockhash;
tx.__blockHash = response.result.blockhash; tx.__height = response.result.height ? response.result.height : -1;
tx.__height = response.result.height ? response.result.height : -1; tx.__timestamp = response.result.time;
tx.__timestamp = response.result.time; var confirmations = self._getConfirmationsDetail(tx);
var confirmations = self._getConfirmationsDetail(tx); if (confirmations >= self.transactionInfoCacheConfirmations) {
if (confirmations >= self.transactionInfoCacheConfirmations) { self.transactionInfoCache.set(txid, tx);
self.transactionInfoCache.set(txid, tx); }
} done(null, tx);
callback(null, tx); });
}); }, callback);
} }
}; };

View File

@ -222,13 +222,10 @@ describe('Bitcoind Functionality', function() {
}); });
}); });
it('will return null if the transaction does not exist', function(done) { it('will return error if the transaction does not exist', function(done) {
var txid = '6226c407d0e9705bdd7158e60983e37d0f5d23529086d6672b07d9238d5aa618'; var txid = '6226c407d0e9705bdd7158e60983e37d0f5d23529086d6672b07d9238d5aa618';
bitcoind.getTransaction(txid, function(err, response) { bitcoind.getTransaction(txid, function(err, response) {
if (err) { should.exist(err);
throw err;
}
should.not.exist(response);
done(); done();
}); });
}); });
@ -250,13 +247,10 @@ describe('Bitcoind Functionality', function() {
}); });
}); });
it('will return null if the transaction does not exist', function(done) { it('will return error if the transaction does not exist', function(done) {
var txid = '6226c407d0e9705bdd7158e60983e37d0f5d23529086d6672b07d9238d5aa618'; var txid = '6226c407d0e9705bdd7158e60983e37d0f5d23529086d6672b07d9238d5aa618';
bitcoind.getRawTransaction(txid, function(err, response) { bitcoind.getRawTransaction(txid, function(err, response) {
if (err) { should.exist(err);
throw err;
}
should.not.exist(response);
done(); done();
}); });
}); });
@ -293,12 +287,9 @@ describe('Bitcoind Functionality', function() {
done(); done();
}); });
}); });
it('will get null for block not found', function(done) { it('will get error for block not found', function(done) {
bitcoind.getBlockHeader('notahash', function(err, header) { bitcoind.getBlockHeader('notahash', function(err, header) {
if(err) { should.exist(err);
return done(err);
}
should.equal(header, null);
done(); done();
}); });
}); });
@ -321,12 +312,9 @@ describe('Bitcoind Functionality', function() {
}); });
}); });
}); });
it('will get null with number greater than tip', function(done) { it('will get error with number greater than tip', function(done) {
bitcoind.getBlockHeader(100000, function(err, header) { bitcoind.getBlockHeader(100000, function(err, header) {
if (err) { should.exist(err);
return done(err);
}
should.equal(header, null);
done(); done();
}); });
}); });