updateTip

This commit is contained in:
Matias Alejo Garcia 2016-12-08 20:27:12 -03:00
parent 86b9435a12
commit e13068e7eb
No known key found for this signature in database
GPG Key ID: 02470DB551277AB3
6 changed files with 73 additions and 18 deletions

View File

@ -79,6 +79,23 @@ Insight.prototype.broadcast = function(rawTx, cb) {
});
};
Insight.prototype.getBlock = function(hash, cb) {
var args = {
method: 'GET',
path: this.apiPrefix + '/rawblock/' + hash,
json: true,
};
this._doRequest(args, function(err, res, data) {
if (res && res.statusCode == 404) return cb();
if (err || res.statusCode !== 200)
return cb(_parseErr(err, res));
return cb(null, data);
});
};
Insight.prototype.getTransaction = function(txid, cb) {
var args = {
method: 'GET',
@ -182,6 +199,7 @@ Insight.prototype.getBlockchainHeight = function(cb) {
});
};
Insight.prototype.initSocket = function() {
// sockets always use the first server on the pull

View File

@ -10,6 +10,7 @@ var BlockchainExplorer = require('./blockchainexplorer');
var Storage = require('./storage');
var MessageBroker = require('./messagebroker');
var Lock = require('./lock');
var Bitcore = require('bitcore-lib');
var Notification = require('./model/notification');
@ -180,9 +181,10 @@ BlockchainMonitor.prototype._handleTxOuts = function(data) {
});
};
BlockchainMonitor.prototype._processBlock = function(network, hash, cb) {
BlockchainMonitor.prototype._processBlock = function(network, hash , cb) {
console.log('[blockchainmonitor.js.184:network:]',network); //TODO
var self = this;
console.log('[blockchainmonitor.js.184:network:]', network); //TODO
//1. check block is not reorg
//2. doProcessBlock
@ -190,8 +192,27 @@ console.log('[blockchainmonitor.js.184:network:]',network); //TODO
if (err) return cb(err);
log.debug('Processing block ' + network + ' ' + hash);
console.log('[blockchainmonitor.js.195:data:]',data); //TODO
var block = new Bitcore.Block(new Buffer(data.rawblock, 'hex'));
var txs = block.toObject().transactions;
console.log('[blockchainmonitor.js.190]', data); //TODO
var allAddresses = {};
_.each(txs, function(tx) {
_.each(tx.outputs, function(o) {
if (o.script) {
var s = new Bitcore.Script(new Buffer(o.script, 'hex'));
var a = s.toAddress(network);
if (a) {
allAddresses[a] = true;
}
}
});
});
allAddresses = _.keys(allAddresses);
async.eachLimit(allAddresses, 10, function(address, i_cb) {
self.storage.updateLastUsedOn(address, i_cb);
}, cb);
});
};
@ -214,9 +235,11 @@ BlockchainMonitor.prototype._handleNewBlock = function(network, hash) {
});
self._processBlock(network, hash, function() {
self.storage.softResetAllTxHistoryCache(function() {
self._storeAndBroadcastNotification(notification, function(err) {
return;
self.storage.updateTip(function() {
self.storage.softResetAllTxHistoryCache(function() {
self._storeAndBroadcastNotification(notification, function(err) {
return;
});
});
});
})

View File

@ -623,26 +623,35 @@ Storage.prototype.getTxHistoryCache = function(walletId, from, to, cb) {
})
};
Storage.prototype.softResetAllTxHistoryCache = function(cb) {
Storage.prototype.updateTip = function(hash, cb) {
this.db.collection(collections.CACHE).update({
type: 'blochainTip',
}, {
$set: {
hash:hash,
}
}, {
upsert: true,
}, cb);
};
Storage.prototype.softResetAllTxHistoryCache = function(cb) {
this.db.collection(collections.CACHE).remove({
type: 'historyCacheStatus',
}, {
isUpdated: false,
}, {
}, {
multi: true,
}, cb);
};
Storage.prototype.softResetTxHistoryCache = function(walletId, cb) {
this.db.collection(collections.CACHE).update({
this.db.collection(collections.CACHE).remove({
walletId: walletId,
type: 'historyCacheStatus',
key: null
}, {
isUpdated: false,
}, {
w: 1,
upsert: true,
}, cb);
};

View File

@ -38,7 +38,7 @@ describe('Blockchain monitor', function() {
storage = res.storage;
blockchainExplorer = res.blockchainExplorer;
blockchainExplorer.initSocket = sinon.stub().returns(socket);
blockchainExplorer.getBlock = sinon.stub().yields(null);
blockchainExplorer.getBlock = sinon.stub().yields('error');
helpers.createAndJoinWallet(2, 3, function(s, w) {
server = s;
@ -133,7 +133,7 @@ describe('Blockchain monitor', function() {
});
});
it.only('should process incoming blocks', function(done) {
it('should process incoming blocks', function(done) {
var incoming = {
hash: '123',
@ -145,7 +145,9 @@ describe('Blockchain monitor', function() {
// TODO add address to block data
blockchainExplorer.getBlock = sinon.stub().yields(null, 'xxx');
blockchainExplorer.getBlock = sinon.stub().yields(null, {
rawblock: TestData.block
});
socket.handlers['block'](incoming);

View File

@ -6129,7 +6129,7 @@ describe('Wallet service', function() {
});
});
describe('Downloading history', function() {
describe.only('Downloading history', function() {
var h;
beforeEach(function(done) {
blockchainExplorer.getBlockchainHeight = sinon.stub().callsArgWith(0, null, 1000);

File diff suppressed because one or more lines are too long