Merge pull request #16 from chjj/version-packet

Version packet
This commit is contained in:
Fedor Indutny 2014-05-26 22:12:36 +04:00
commit 013a85c6a5
5 changed files with 33 additions and 14 deletions

View File

@ -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);

View File

@ -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++)

View File

@ -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;

View File

@ -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
};
};

View File

@ -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) {