diff --git a/lib/net/peer.js b/lib/net/peer.js index 9df241f1..021f41b1 100644 --- a/lib/net/peer.js +++ b/lib/net/peer.js @@ -81,75 +81,67 @@ function Peer(pool) { this.pool = pool; this.options = pool.options; this.logger = pool.logger; - this.socket = null; - this.outbound = false; - this.host = null; - this.port = 0; - this.hostname = null; - this.createSocket = this.options.createSocket; this.chain = this.pool.chain; this.mempool = this.pool.mempool; this.network = this.chain.network; this.locker = new Locker(); - this.version = null; + this.createSocket = this.options.createSocket; + this.next = null; + this.prev = null; + + this.id = Peer.uid++; + this.socket = null; + this.outbound = false; + this.host = '0.0.0.0'; + this.port = 0; + this.hostname = '0.0.0.0:0'; this.destroyed = false; this.ack = false; - this.pending = true; this.connected = false; this.ts = 0; + this.lastSend = 0; + this.lastRecv = 0; + this.drainStart = 0; + this.drainSize = 0; + this.drainQueue = []; + this.banScore = 0; + + this.version = null; this.preferHeaders = false; this.haveWitness = false; this.hashContinue = null; this.spvFilter = null; this.relay = true; this.feeRate = -1; - this.addrFilter = new Bloom.Rolling(5000, 0.001); - this.invFilter = new Bloom.Rolling(50000, 0.000001); - this.lastBlock = null; - this.waiting = 0; - this.syncSent = false; - this.connectTimeout = null; + this.bip151 = null; + this.bip150 = null; this.compactMode = null; this.compactWitness = false; this.compactBlocks = {}; + this.lastMerkle = null; + this.waitingTX = 0; + this.syncSent = false; this.sentAddr = false; - this.bip151 = null; - this.bip150 = null; - this.lastSend = 0; - this.lastRecv = 0; - this.drainStart = 0; - this.drainSize = 0; - this.drainQueue = []; - - this.next = null; - this.prev = null; - this.challenge = null; this.lastPong = -1; this.lastPing = -1; this.minPing = -1; - this.banScore = 0; - + this.connectTimeout = null; this.pingTimer = null; this.pingInterval = 30000; this.stallTimer = null; this.stallInterval = 5000; + this.addrFilter = new Bloom.Rolling(5000, 0.001); + this.invFilter = new Bloom.Rolling(50000, 0.000001); + this.requestTimeout = 10000; this.requestMap = {}; this.queueBlock = new List(); this.queueTX = new List(); - this.id = Peer.uid++; - - this.setMaxListeners(10000); - - this.host = '0.0.0.0'; - this.port = 0; - this.hostname = '0.0.0.0:0'; - if (this.options.bip151) { this.bip151 = new BIP151(); if (this.options.bip150) { @@ -290,7 +282,6 @@ Peer.prototype.accept = function accept(socket) { this.ts = util.now(); this.outbound = false; this.connected = true; - this.pending = false; }; /** @@ -316,6 +307,7 @@ Peer.prototype.connect = function connect(port, host) { this.bind(socket); this.setHost(host, port); this.outbound = true; + this.connected = false; this.logger.debug('Connecting to %s.', this.hostname); @@ -1133,9 +1125,9 @@ Peer.prototype.onPacket = co(function* onPacket(packet) { this.bip150.complete(new Error('Message before auth.')); } - if (this.lastBlock) { + if (this.lastMerkle) { if (packet.type !== packetTypes.TX) - this._flushMerkle(); + this.flushMerkle(); } this.lastRecv = util.ms(); @@ -1225,11 +1217,11 @@ Peer.prototype.onPacket = co(function* onPacket(packet) { * @private */ -Peer.prototype._flushMerkle = function _flushMerkle() { - if (this.lastBlock) - this.fire('merkleblock', this.lastBlock); - this.lastBlock = null; - this.waiting = 0; +Peer.prototype.flushMerkle = function flushMerkle() { + assert(this.lastMerkle); + this.fire('merkleblock', this.lastMerkle); + this.lastMerkle = null; + this.waitingTX = 0; }; /** @@ -1303,11 +1295,11 @@ Peer.prototype.handleMerkleBlock = co(function* handleMerkleBlock(packet) { block.verifyPartial(); - this.lastBlock = block; - this.waiting = block.matches.length; + this.lastMerkle = block; + this.waitingTX = block.matches.length; - if (this.waiting === 0) - this._flushMerkle(); + if (this.waitingTX === 0) + this.flushMerkle(); }); /** @@ -2125,11 +2117,11 @@ Peer.prototype.handleBlock = co(function* handleBlock(packet) { Peer.prototype.handleTX = co(function* handleTX(packet) { var tx = packet.tx; - if (this.lastBlock) { - if (this.lastBlock.hasTX(tx)) { - this.lastBlock.addTX(tx); - if (--this.waiting === 0) - this._flushMerkle(); + if (this.lastMerkle) { + if (this.lastMerkle.hasTX(tx)) { + this.lastMerkle.addTX(tx); + if (--this.waitingTX === 0) + this.flushMerkle(); return; } } diff --git a/lib/net/pool.js b/lib/net/pool.js index 0d6e3df6..8d714a53 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -706,7 +706,7 @@ Pool.prototype.sync = function sync() { var peer; for (peer = this.peers.head(); peer; peer = peer.next) { - if (!peer.outbound || peer.pending) + if (!peer.outbound || !peer.ack) continue; peer.sync(); } @@ -721,7 +721,7 @@ Pool.prototype.forceSync = function forceSync() { var peer; for (peer = this.peers.head(); peer; peer = peer.next) { - if (!peer.outbound || peer.pending) + if (!peer.outbound || !peer.ack) continue; peer.syncSent = false; peer.sync(); @@ -747,7 +747,7 @@ Pool.prototype.stopSync = co(function* stopSync() { this.stopTimeout(); for (peer = this.peers.head(); peer; peer = peer.next) { - if (!peer.outbound || peer.pending) + if (!peer.outbound || !peer.ack) continue; peer.syncSent = false; } @@ -761,7 +761,7 @@ Pool.prototype.sendMempool = function sendMempool() { var peer; for (peer = this.peers.head(); peer; peer = peer.next) { - if (!peer.outbound || peer.pending) + if (!peer.ack) continue; peer.sendMempool(); } @@ -776,7 +776,7 @@ Pool.prototype.sendAlert = function sendAlert(alert) { var peer; for (peer = this.peers.head(); peer; peer = peer.next) { - if (peer.pending) + if (!peer.ack) continue; peer.sendAlert(alert); } @@ -1658,7 +1658,7 @@ Pool.prototype.updateWatch = function updateWatch() { this.pendingWatch = setTimeout(function() { self.pendingWatch = null; for (peer = self.peers.head(); peer; peer = peer.next) { - if (!peer.outbound || peer.pending) + if (!peer.ack) continue; peer.updateWatch(); } @@ -1900,7 +1900,7 @@ Pool.prototype.announceBlock = function announceBlock(msg) { var peer; for (peer = this.peers.head(); peer; peer = peer.next) { - if (!peer.outbound || peer.pending) + if (!peer.ack) continue; peer.announceBlock(msg); } @@ -1915,7 +1915,7 @@ Pool.prototype.announceTX = function announceTX(msg) { var peer; for (peer = this.peers.head(); peer; peer = peer.next) { - if (!peer.outbound || peer.pending) + if (!peer.ack) continue; peer.announceTX(msg); } @@ -1932,7 +1932,7 @@ Pool.prototype.setFeeRate = function setFeeRate(rate) { this.feeRate = rate; for (peer = this.peers.head(); peer; peer = peer.next) { - if (!peer.outbound || peer.pending) + if (!peer.ack) continue; peer.sendFeeRate(rate); } @@ -2096,8 +2096,7 @@ PeerList.prototype.size = function size() { PeerList.prototype.promote = function promote(peer) { assert(peer.outbound); - assert(peer.pending); - peer.pending = false; + assert(peer.ack); this.pending--; }; @@ -2107,7 +2106,7 @@ PeerList.prototype.add = function add(peer) { this.map[peer.hostname] = peer; if (peer.outbound) { this.outbound++; - if (peer.pending) + if (!peer.ack) this.pending++; } else { this.inbound++; @@ -2126,7 +2125,7 @@ PeerList.prototype.remove = function remove(peer) { if (peer.outbound) { this.outbound--; - if (peer.pending) + if (!peer.ack) this.pending--; } else { this.inbound--; diff --git a/test/block-test.js b/test/block-test.js index d584679d..1969f1ff 100644 --- a/test/block-test.js +++ b/test/block-test.js @@ -194,7 +194,7 @@ describe('Block', function() { var ret = {}; block2.hash(); block2.merkleRoot = constants.NULL_HASH; - block2.refresh(); + block2._validHeaders = null; assert(!block2.verify(ret)); assert.equal(ret.reason, 'bad-txnmrklroot'); block2.merkleRoot = block.merkleRoot;