change explorers to an object

This commit is contained in:
Matias Alejo Garcia 2016-12-08 12:14:15 -03:00
parent 7c5ad7bdfa
commit 86b9435a12
No known key found for this signature in database
GPG Key ID: 02470DB551277AB3
2 changed files with 50 additions and 10 deletions

View File

@ -25,7 +25,7 @@ BlockchainMonitor.prototype.start = function(opts, cb) {
async.parallel([
function(done) {
self.explorers = _.map(['livenet', 'testnet'], function(network) {
self.explorers = _.object(_.map(['livenet', 'testnet'], function(network) {
var explorer;
if (opts.blockchainExplorers) {
explorer = opts.blockchainExplorers[network];
@ -42,9 +42,9 @@ BlockchainMonitor.prototype.start = function(opts, cb) {
});
}
$.checkState(explorer);
self._initExplorer(explorer);
return explorer;
});
self._initExplorer(explorer, network);
return [network, explorer];
}));
done();
},
function(done) {
@ -72,7 +72,7 @@ BlockchainMonitor.prototype.start = function(opts, cb) {
});
};
BlockchainMonitor.prototype._initExplorer = function(explorer) {
BlockchainMonitor.prototype._initExplorer = function(explorer, network) {
var self = this;
var socket = explorer.initSocket();
@ -85,7 +85,7 @@ BlockchainMonitor.prototype._initExplorer = function(explorer) {
log.error('Error connecting to ' + explorer.getConnectionInfo());
});
socket.on('tx', _.bind(self._handleIncomingTx, self));
socket.on('block', _.bind(self._handleNewBlock, self, explorer.network));
socket.on('block', _.bind(self._handleNewBlock, self, network));
};
BlockchainMonitor.prototype._handleTxId = function(data, processIt) {
@ -180,6 +180,21 @@ BlockchainMonitor.prototype._handleTxOuts = function(data) {
});
};
BlockchainMonitor.prototype._processBlock = function(network, hash , cb) {
console.log('[blockchainmonitor.js.184:network:]',network); //TODO
//1. check block is not reorg
//2. doProcessBlock
this.explorers[network].getBlock(hash, function(err, data) {
if (err) return cb(err);
log.debug('Processing block ' + network + ' ' + hash);
console.log('[blockchainmonitor.js.190]', data); //TODO
});
};
BlockchainMonitor.prototype._handleIncomingTx = function(data) {
this._handleTxId(data);
this._handleTxOuts(data);
@ -198,11 +213,13 @@ BlockchainMonitor.prototype._handleNewBlock = function(network, hash) {
},
});
self.storage.softResetAllTxHistoryCache(function() {
self._storeAndBroadcastNotification(notification, function(err) {
return;
self._processBlock(network, hash, function() {
self.storage.softResetAllTxHistoryCache(function() {
self._storeAndBroadcastNotification(notification, function(err) {
return;
});
});
});
})
};
BlockchainMonitor.prototype._storeAndBroadcastNotification = function(notification, cb) {

View File

@ -38,6 +38,7 @@ describe('Blockchain monitor', function() {
storage = res.storage;
blockchainExplorer = res.blockchainExplorer;
blockchainExplorer.initSocket = sinon.stub().returns(socket);
blockchainExplorer.getBlock = sinon.stub().yields(null);
helpers.createAndJoinWallet(2, 3, function(s, w) {
server = s;
@ -131,4 +132,26 @@ describe('Blockchain monitor', function() {
});
});
});
it.only('should process incoming blocks', function(done) {
var incoming = {
hash: '123',
};
var address;
server.createAddress({}, function(err, a) {
should.not.exist(err);
address = a.address;
// TODO add address to block data
blockchainExplorer.getBlock = sinon.stub().yields(null, 'xxx');
socket.handlers['block'](incoming);
setTimeout(function() {
done();
}, 50);
});
});
});