diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index cc36a162..99775da5 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -97,7 +97,13 @@ Peer.prototype._init = function init() { }, this._ping.interval); // Send hello - this._write(this.framer.version()); + this._write(this.framer.version({ + height: this.options.startHeight != null + ? this.options.startHeight + : 0, + relay: this.options.relay + })); + this._req('verack', function(err, payload) { if (err) return self._error(err); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index a21185c9..7a1894d7 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -111,7 +111,9 @@ Pool.prototype._addLoader = function _addLoader() { return; var peer = new bcoin.peer(this, this.createConnection, { - backoff: 750 * Math.random() + backoff: 750 * Math.random(), + startHeight: this.options.startHeight, + relay: this.options.relay }); this.peers.load = peer; @@ -248,7 +250,9 @@ Pool.prototype._addPeer = function _addPeer(backoff) { return; var peer = new bcoin.peer(this, this.createConnection, { - backoff: backoff + backoff: backoff, + startHeight: this.options.startHeight, + relay: this.options.relay }); this.peers.pending.push(peer); @@ -356,17 +360,19 @@ Pool.prototype.watch = function watch(id) { return; } - var hid = utils.toHex(id); - if (this.watchMap[hid]) - this.watchMap[hid]++; - else - this.watchMap[hid] = 1; + if (id) { + var hid = utils.toHex(id); + if (this.watchMap[hid]) + this.watchMap[hid]++; + else + this.watchMap[hid] = 1; - if (this.bloom.test(id, 'hex')) - return; + if (this.bloom.test(id, 'hex')) + return; - if (id) this.bloom.add(id, 'hex'); + } + if (this.peers.load) this.peers.load.updateWatch(); for (var i = 0; i < this.peers.block.length; i++) diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 4970e645..94ceebcd 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -97,7 +97,7 @@ Framer.prototype.version = function version(packet) { } // Start height - off += writeU32(p, packet.height, off); + off += writeU32(p, packet.height || 0, off); // Relay p[off++] = packet.relay ? 1 : 0; diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 4bceca0f..f7c8dfbb 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -123,7 +123,7 @@ Parser.prototype.parseVersion = function parseVersion(p) { var nonce = { lo: readU32(p, 72), hi: readU32(p, 76) }; // Start height - var weight = readU32(p, 81); + var height = readU32(p, 81); // Relay var relay = p.length >= 86 ? p[85] === 1 : true; @@ -133,7 +133,7 @@ Parser.prototype.parseVersion = function parseVersion(p) { services: services, ts: ts, nonce: nonce, - weight: weight, + height: height, relay: relay }; }; diff --git a/test/protocol-test.js b/test/protocol-test.js index 808cce94..96476e70 100644 --- a/test/protocol-test.js +++ b/test/protocol-test.js @@ -25,6 +25,13 @@ describe('Protocol', function() { packetTest('version', {}, function(payload) { assert.equal(payload.v, 70002); assert.equal(payload.relay, false); + assert.equal(payload.height, 0); + }); + + packetTest('version', { relay: true, height: 10 }, function(payload) { + assert.equal(payload.v, 70002); + assert.equal(payload.relay, true); + assert.equal(payload.height, 10); }); packetTest('verack', {}, function(payload) {