From b3ad83b9eee90fd4afdb116adca5c18c9cf576d2 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 7 Apr 2015 16:34:54 -0300 Subject: [PATCH] fix work bug --- lib/blockchain.js | 2 +- lib/node.js | 28 +++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/blockchain.js b/lib/blockchain.js index 7cadd778..33dae810 100644 --- a/lib/blockchain.js +++ b/lib/blockchain.js @@ -40,7 +40,7 @@ BlockChain.prototype.addData = function(block) { var prevHash = bitcore.util.buffer.reverse(block.header.prevHash).toString('hex'); - this.work[block.hash] = this.work[prevHash].work + getWork(block.header.bits); + this.work[block.hash] = this.work[prevHash] + getWork(block.header.bits); this.prev[block.hash] = prevHash; }; diff --git a/lib/node.js b/lib/node.js index be57b068..61f5b2d9 100644 --- a/lib/node.js +++ b/lib/node.js @@ -39,6 +39,7 @@ var BitcoreNode = function(bus, networkMonitor, blockService, transactionService this.bus.register(bitcore.Block, function(block) { + console.log('Block', block.id); var prevHash = bitcore.util.buffer.reverse(block.header.prevHash).toString('hex'); self.blockCache[block.hash] = block; @@ -48,15 +49,21 @@ var BitcoreNode = function(bus, networkMonitor, blockService, transactionService } var blockchainChanges = self.blockchain.proposeNewBlock(block); + console.log('changes', blockchainChanges); Promise.each(blockchainChanges.unconfirmed, function(hash) { - return self.blockService.unconfirm(self.blockCache[hash]); - }) - .then(Promise.each(blockchainChanges.confirmed, function(hash) { - return self.blockService.confirm(self.blockCache[hash]); - })) - .then(function() { - self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator()); - }); + return self.blockService.unconfirm(self.blockCache[hash]); + }) + .then(function() { + return Promise.all(blockchainChanges.confirmed.map(function(hash) { + return self.blockService.confirm(self.blockCache[hash]); + })); + }) + .then(function() { + self.networkMonitor.requestBlocks(self.blockchain.getBlockLocator()); + }) + .catch(function(error) { + self.stop(error); + }); }); this.bus.onAny(function(value) { @@ -79,7 +86,6 @@ BitcoreNode.create = function(opts) { var networkMonitor = NetworkMonitor.create(bus, opts.NetworkMonitor); - console.log(opts.LevelUp); var database = Promise.promisifyAll( new LevelUp(opts.LevelUp || config.get('LevelUp')) ); @@ -119,8 +125,8 @@ BitcoreNode.prototype.start = function() { }); }; -BitcoreNode.prototype.stop = function() { - this.networkMonitor.stop(); +BitcoreNode.prototype.stop = function(reason) { + this.networkMonitor.stop(reason); };