Reorgs and sync

This commit is contained in:
eordano 2015-04-07 16:08:46 -03:00
parent 1cd51ef944
commit acfbef8a33
2 changed files with 42 additions and 20 deletions

View File

@ -35,7 +35,29 @@ var BitcoreNode = function(bus, networkMonitor, blockService, transactionService
this.transactionService = transactionService;
this.blockService = blockService;
this.bus.register(bitcore.Block, this.blockService.onBlock.bind(this.blockService));
this.blockCache = {};
this.bus.register(bitcore.Block, function(block) {
var prevHash = bitcore.util.buffer.reverse(block.header.prevHash).toString('hex');
self.blockCache[block.hash] = block;
if (!self.blockchain.hasData(prevHash)) {
self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator());
return;
}
var blockchainChanges = self.blockchain.proposeNewBlock(block);
Promise.series(blockchainChanges.unconfirmed.map(function(hash) {
return self.blockService.unconfirm(self.blockCache[hash]);
}))
.then(Promise.series(blockchainChanges.confirmed.map(function(hash) {
return self.blockService.confirm(self.blockCache[hash]);
})))
.then(function() {
self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator());
});
});
this.bus.onAny(function(value) {
self.emit(this.event, value);
@ -82,8 +104,18 @@ BitcoreNode.create = function(opts) {
};
BitcoreNode.prototype.start = function() {
this.sync();
this.networkMonitor.start();
var self = this;
this.blockService.getBlockchain().then(function(blockchain) {
if (!blockchain) {
self.blockchain = new BlockChain();
self.blockchain.setTip(genesis);
}
self.sync();
self.networkMonitor.start();
});
this.networkMonitor.on('stop', function() {
self.blockService.saveBlockchain(self.blockchain);
});
};
BitcoreNode.prototype.stop = function() {
@ -92,24 +124,15 @@ BitcoreNode.prototype.stop = function() {
BitcoreNode.prototype.sync = function() {
var genesis = bitcore.Block.fromBuffer(genesisBlocks[bitcore.Networks.defaultNetwork.name]);
var self = this;
this.networkMonitor.on('ready', function() {
self.blockService.getBlockchain().then(function(blockchain) {
if (blockchain) {
self.blockchain = blockchain;
} else {
self.blockchain = new BlockChain();
self.blockchain.setTip(genesis);
}
self.networkMonitor.requestBlocks(self.blockchain.tip);
})
.catch(function(err) {
self.networkMonitor.stop();
throw err;
});
self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator());
}).catch(function(err) {
self.networkMonitor.stop();
throw err;
});
});
};
module.exports = BitcoreNode;

View File

@ -226,9 +226,8 @@ BlockService.prototype.onBlock = function(block) {
* @param {bitcore.Block} block
* @return {Promise<Block>} a promise of the same block, for chaining
*/
BlockService.prototype.save = function(block) {
BlockService.prototype.unconfirm = function(block) {
// TODO: unconfirm previous tip, confirm new tip.
return this._confirmBlock(block);
};
@ -238,7 +237,7 @@ BlockService.prototype.save = function(block) {
* @param {bitcore.Block} block
* @return {Promise<Block>} a promise of the same block, for chaining
*/
BlockService.prototype._confirmBlock = function(block) {
BlockService.prototype.confirm = function(block) {
$.checkArgument(block instanceof bitcore.Block);
var self = this;