fix promise chaining
This commit is contained in:
parent
fe038ac5f8
commit
2da7e9c239
@ -40,11 +40,13 @@ EventBus.prototype.process = function(e) {
|
|||||||
|
|
||||||
var whenPreviousFinishes = Promise.resolve();
|
var whenPreviousFinishes = Promise.resolve();
|
||||||
if (this.previous && !this.previous.isFulfilled()) {
|
if (this.previous && !this.previous.isFulfilled()) {
|
||||||
|
//console.log('setting new task with other running, lets queue');
|
||||||
whenPreviousFinishes = this.previous;
|
whenPreviousFinishes = this.previous;
|
||||||
}
|
}
|
||||||
|
|
||||||
var current = whenPreviousFinishes
|
var current = whenPreviousFinishes
|
||||||
.then(function() {
|
.then(function() {
|
||||||
|
//console.log('ok, lets go with the new block');
|
||||||
return processEvent(e);
|
return processEvent(e);
|
||||||
})
|
})
|
||||||
.then(function() {
|
.then(function() {
|
||||||
|
|||||||
@ -71,6 +71,7 @@ NetworkMonitor.prototype.requestBlocks = function(locator) {
|
|||||||
_.isString(locator[0]), 'start must be a block hash string array');
|
_.isString(locator[0]), 'start must be a block hash string array');
|
||||||
this.peer.sendMessage(messages.GetBlocks({
|
this.peer.sendMessage(messages.GetBlocks({
|
||||||
starts: locator,
|
starts: locator,
|
||||||
|
//stop: '000000002c05cc2e78923c34df87fd108b22221ac6076c18f3ade378a4d915e9' // TODO: remove this!!!
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
11
lib/node.js
11
lib/node.js
@ -57,15 +57,18 @@ var BitcoreNode = function(bus, networkMonitor, blockService, transactionService
|
|||||||
self.requestFromTip();
|
self.requestFromTip();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log('block', block.id, 'height', self.blockchain.height[self.blockchain.tip] + 1);
|
|
||||||
|
|
||||||
var blockchainChanges = self.blockchain.proposeNewBlock(block);
|
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]);
|
return self.blockService.unconfirm(self.blockCache[hash]);
|
||||||
})
|
})
|
||||||
.then(function() {
|
.then(function() {
|
||||||
return Promise.all(blockchainChanges.confirmed.map(function(hash) {
|
return Promise.all(blockchainChanges.confirmed.map(function(hash) {
|
||||||
self.blockCache[hash].height = self.blockchain.height[hash];
|
|
||||||
return self.blockService.confirm(self.blockCache[hash]);
|
return self.blockService.confirm(self.blockCache[hash]);
|
||||||
}));
|
}));
|
||||||
})
|
})
|
||||||
|
|||||||
@ -247,24 +247,15 @@ BlockService.prototype.confirm = function(block) {
|
|||||||
//console.log(0);
|
//console.log(0);
|
||||||
return Promise.try(function() {
|
return Promise.try(function() {
|
||||||
//console.log(1);
|
//console.log(1);
|
||||||
return self._setNextBlock(ops, block.header.prevHash, block);
|
self._setNextBlock(ops, block.header.prevHash, block);
|
||||||
|
|
||||||
})
|
|
||||||
.then(function() {
|
|
||||||
//console.log(3);
|
//console.log(3);
|
||||||
|
self._setBlockHeight(ops, block, block.height);
|
||||||
|
|
||||||
return self._setBlockHeight(ops, block, block.height);
|
|
||||||
|
|
||||||
})
|
|
||||||
.then(function() {
|
|
||||||
//console.log(4);
|
//console.log(4);
|
||||||
|
self._setBlockByTs(ops, block);
|
||||||
|
|
||||||
return self._setBlockByTs(ops, block);
|
|
||||||
|
|
||||||
})
|
|
||||||
.then(function() {
|
|
||||||
//console.log(5);
|
//console.log(5);
|
||||||
|
|
||||||
return Promise.all(block.transactions.map(function(transaction) {
|
return Promise.all(block.transactions.map(function(transaction) {
|
||||||
return self.transactionService._confirmTransaction(ops, block, transaction);
|
return self.transactionService._confirmTransaction(ops, block, transaction);
|
||||||
}));
|
}));
|
||||||
@ -272,9 +263,7 @@ BlockService.prototype.confirm = function(block) {
|
|||||||
})
|
})
|
||||||
.then(function() {
|
.then(function() {
|
||||||
//console.log(6);
|
//console.log(6);
|
||||||
var p = self.database.batchAsync(ops);
|
return self.database.batchAsync(ops);
|
||||||
return p;
|
|
||||||
|
|
||||||
})
|
})
|
||||||
.then(function() {
|
.then(function() {
|
||||||
//console.log(7);
|
//console.log(7);
|
||||||
@ -286,28 +275,23 @@ BlockService.prototype._setNextBlock = function(ops, prevBlockHash, block) {
|
|||||||
if (bitcore.util.buffer.isBuffer(prevBlockHash)) {
|
if (bitcore.util.buffer.isBuffer(prevBlockHash)) {
|
||||||
prevBlockHash = bitcore.util.buffer.reverse(prevBlockHash).toString('hex');
|
prevBlockHash = bitcore.util.buffer.reverse(prevBlockHash).toString('hex');
|
||||||
}
|
}
|
||||||
return Promise.try(function() {
|
ops.push({
|
||||||
ops.push({
|
type: 'put',
|
||||||
type: 'put',
|
key: Index.getNextBlock(prevBlockHash),
|
||||||
key: Index.getNextBlock(prevBlockHash),
|
value: block.hash
|
||||||
value: block.hash
|
});
|
||||||
});
|
ops.push({
|
||||||
ops.push({
|
type: 'put',
|
||||||
type: 'put',
|
key: Index.getPreviousBlock(block.hash),
|
||||||
key: Index.getPreviousBlock(block.hash),
|
value: prevBlockHash.toString('hex')
|
||||||
value: prevBlockHash.toString('hex')
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockService.prototype._setBlockHeight = function(ops, block, height) {
|
BlockService.prototype._setBlockHeight = function(ops, block, height) {
|
||||||
return Promise.try(function() {
|
ops.push({
|
||||||
ops.push({
|
type: 'put',
|
||||||
type: 'put',
|
key: Index.getBlockHeight(block),
|
||||||
key: Index.getBlockHeight(block),
|
value: height
|
||||||
value: height
|
|
||||||
});
|
|
||||||
return ops;
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user