From 4a8c4dee57205d7bb194c2a3d15409cdc0d660b0 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 14 Dec 2016 12:03:47 -0800 Subject: [PATCH] node: fix event race conditions. --- lib/http/rpc.js | 4 ++-- lib/node/fullnode.js | 44 ++++++++++++++++++++++++---------------- lib/protocol/networks.js | 4 ++-- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 7427d6b8..d17b2ad6 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -1735,7 +1735,7 @@ RPC.prototype._bindChain = function _bindChain() { this._boundChain = true; - this.chain.on('connect', function() { + this.node.on('connect', function() { if (!self.attempt) return; @@ -1745,7 +1745,7 @@ RPC.prototype._bindChain = function _bindChain() { if (!this.mempool) return; - this.mempool.on('tx', function() { + this.node.on('tx', function() { if (!self.attempt) return; diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index c43a3b42..3f6e0ecb 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -196,33 +196,41 @@ FullNode.prototype._init = function _init() { }); this.mempool.on('tx', function(tx) { - self.emit('tx', tx); self.miner.notifyEntry(); + self.emit('tx', tx); }); - this.chain.on('block', function(block) { + this.chain.on('connect', co(function* (entry, block) { + if (self.chain.synced) { + try { + yield self.mempool.addBlock(entry, block.txs); + } catch (e) { + self._error(e); + } + } self.emit('block', block); - }); - - this.chain.on('connect', function(entry, block) { self.emit('connect', entry, block); + })); - if (self.chain.synced) - self.mempool.addBlock(entry, block.txs).catch(onError); - }); - - this.chain.on('disconnect', function(entry, block) { + this.chain.on('disconnect', co(function* (entry, block) { + if (self.chain.synced) { + try { + yield self.mempool.removeBlock(entry, block.txs); + } catch (e) { + self._error(e); + } + } self.emit('disconnect', entry, block); + })); - if (self.chain.synced) - self.mempool.removeBlock(entry, block.txs).catch(onError); - }); - - this.chain.on('reset', function(tip) { + this.chain.on('reset', co(function* (tip) { + try { + yield self.mempool.reset(); + } catch (e) { + self._error(e); + } self.emit('reset', tip); - - self.mempool.reset().catch(onError); - }); + })); this.miner.on('block', function(block) { self.broadcast(block.toInv()).catch(onError); diff --git a/lib/protocol/networks.js b/lib/protocol/networks.js index 05cd142d..c596b358 100644 --- a/lib/protocol/networks.js +++ b/lib/protocol/networks.js @@ -664,7 +664,7 @@ regtest.seeds = [ regtest.magic = 0xdab5bffa; -regtest.port = 18444; +regtest.port = 48444; regtest.alertPrivateKey = new Buffer( 'b866c595a088e2d9ea87ff4df173dd5990b1331fa9acff6aa82cc04162a63f91', @@ -791,7 +791,7 @@ regtest.addressPrefix = { regtest.requireStandard = false; -regtest.rpcPort = 18332; +regtest.rpcPort = 48332; regtest.minRelay = 1000;