From a5f20114e881ec42d96c75e2fa01cb9a2db6948b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 20 May 2014 04:06:19 -0500 Subject: [PATCH 1/3] framer: version packet options, relay and height. --- lib/bcoin/peer.js | 9 ++++++++- lib/bcoin/pool.js | 8 ++++++-- lib/bcoin/protocol/framer.js | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index e6770e75..7e032792 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -97,7 +97,14 @@ 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, + //: this.pool.chain.index.heights[this.pool.chain.index.heights.length-1], + 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 471eafe0..56bafde3 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -105,7 +105,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; @@ -242,7 +244,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); diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index d75b80c0..10b8e3f5 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; From 24d9b2b2d8bf46d58dbc19bf1a182880cb37968e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 20 May 2014 04:08:57 -0500 Subject: [PATCH 2/3] pool: allow sending filterload to peers without an id to add to the bloom filter. --- lib/bcoin/pool.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 56bafde3..8a8e2baa 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -348,17 +348,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++) From 9dc0063ea9939d5e426aa4b38e5e83a160b26393 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 24 May 2014 02:09:49 -0500 Subject: [PATCH 3/3] test/parser: add version test for height and relay / fix typo. --- lib/bcoin/peer.js | 1 - lib/bcoin/protocol/parser.js | 4 ++-- test/protocol-test.js | 7 +++++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 7e032792..94bbfdad 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -101,7 +101,6 @@ Peer.prototype._init = function init() { height: this.options.startHeight != null ? this.options.startHeight : 0, - //: this.pool.chain.index.heights[this.pool.chain.index.heights.length-1], relay: this.options.relay })); diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 8238b436..9d6d6bf6 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) {