fix promise chaining

This commit is contained in:
Manuel Araoz 2015-04-09 17:05:29 -03:00
parent fe038ac5f8
commit 2da7e9c239
4 changed files with 27 additions and 37 deletions

View File

@ -40,11 +40,13 @@ EventBus.prototype.process = function(e) {
var whenPreviousFinishes = Promise.resolve();
if (this.previous && !this.previous.isFulfilled()) {
//console.log('setting new task with other running, lets queue');
whenPreviousFinishes = this.previous;
}
var current = whenPreviousFinishes
.then(function() {
//console.log('ok, lets go with the new block');
return processEvent(e);
})
.then(function() {

View File

@ -71,6 +71,7 @@ NetworkMonitor.prototype.requestBlocks = function(locator) {
_.isString(locator[0]), 'start must be a block hash string array');
this.peer.sendMessage(messages.GetBlocks({
starts: locator,
//stop: '000000002c05cc2e78923c34df87fd108b22221ac6076c18f3ade378a4d915e9' // TODO: remove this!!!
}));
};

View File

@ -57,15 +57,18 @@ var BitcoreNode = function(bus, networkMonitor, blockService, transactionService
self.requestFromTip();
return;
}
console.log('block', block.id, 'height', self.blockchain.height[self.blockchain.tip] + 1);
var blockchainChanges = self.blockchain.proposeNewBlock(block);
Promise.each(blockchainChanges.unconfirmed, function(hash) {
var height = self.blockchain.height[block.id];
if (height % 100 === 0) {
console.log('block', block.id, 'height', height);
}
block.height = height;
return Promise.each(blockchainChanges.unconfirmed, function(hash) {
return self.blockService.unconfirm(self.blockCache[hash]);
})
.then(function() {
return Promise.all(blockchainChanges.confirmed.map(function(hash) {
self.blockCache[hash].height = self.blockchain.height[hash];
return self.blockService.confirm(self.blockCache[hash]);
}));
})

View File

@ -247,24 +247,15 @@ BlockService.prototype.confirm = function(block) {
//console.log(0);
return Promise.try(function() {
//console.log(1);
return self._setNextBlock(ops, block.header.prevHash, block);
self._setNextBlock(ops, block.header.prevHash, block);
})
.then(function() {
//console.log(3);
self._setBlockHeight(ops, block, block.height);
return self._setBlockHeight(ops, block, block.height);
})
.then(function() {
//console.log(4);
self._setBlockByTs(ops, block);
return self._setBlockByTs(ops, block);
})
.then(function() {
//console.log(5);
return Promise.all(block.transactions.map(function(transaction) {
return self.transactionService._confirmTransaction(ops, block, transaction);
}));
@ -272,9 +263,7 @@ BlockService.prototype.confirm = function(block) {
})
.then(function() {
//console.log(6);
var p = self.database.batchAsync(ops);
return p;
return self.database.batchAsync(ops);
})
.then(function() {
//console.log(7);
@ -286,28 +275,23 @@ BlockService.prototype._setNextBlock = function(ops, prevBlockHash, block) {
if (bitcore.util.buffer.isBuffer(prevBlockHash)) {
prevBlockHash = bitcore.util.buffer.reverse(prevBlockHash).toString('hex');
}
return Promise.try(function() {
ops.push({
type: 'put',
key: Index.getNextBlock(prevBlockHash),
value: block.hash
});
ops.push({
type: 'put',
key: Index.getPreviousBlock(block.hash),
value: prevBlockHash.toString('hex')
});
ops.push({
type: 'put',
key: Index.getNextBlock(prevBlockHash),
value: block.hash
});
ops.push({
type: 'put',
key: Index.getPreviousBlock(block.hash),
value: prevBlockHash.toString('hex')
});
};
BlockService.prototype._setBlockHeight = function(ops, block, height) {
return Promise.try(function() {
ops.push({
type: 'put',
key: Index.getBlockHeight(block),
value: height
});
return ops;
ops.push({
type: 'put',
key: Index.getBlockHeight(block),
value: height
});
};