more framing.
This commit is contained in:
parent
3514eb7dec
commit
f4f6c79bfd
@ -23,7 +23,6 @@ function Framer(options) {
|
|||||||
this.options = options;
|
this.options = options;
|
||||||
|
|
||||||
this.agent = new Buffer(options.userAgent || constants.userAgent, 'ascii');
|
this.agent = new Buffer(options.userAgent || constants.userAgent, 'ascii');
|
||||||
this.agent = this.agent.slice(0, 0xfc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Framer.prototype.header = function header(cmd, payload) {
|
Framer.prototype.header = function header(cmd, payload) {
|
||||||
@ -104,9 +103,10 @@ Framer.prototype._addr = function addr(data, full) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Framer.prototype.version = function version(packet) {
|
Framer.prototype.version = function version(packet) {
|
||||||
var p = new Buffer(86 + this.agent.length);
|
|
||||||
var off = 0;
|
var off = 0;
|
||||||
var i, remote, local;
|
var p, i, remote, local;
|
||||||
|
|
||||||
|
p = new Buffer(85 + utils.sizeIntv(this.agent.length) + this.agent.length);
|
||||||
|
|
||||||
if (!packet)
|
if (!packet)
|
||||||
packet = {};
|
packet = {};
|
||||||
@ -131,15 +131,11 @@ Framer.prototype.version = function version(packet) {
|
|||||||
// Nonce, very dramatic
|
// Nonce, very dramatic
|
||||||
off += utils.writeU64(p, utils.nonce(), off);
|
off += utils.writeU64(p, utils.nonce(), off);
|
||||||
|
|
||||||
// User-agent
|
|
||||||
assert.equal(off, 80);
|
assert.equal(off, 80);
|
||||||
if (!this.agent) {
|
|
||||||
p[off++] = 0;
|
// User-agent
|
||||||
} else {
|
off += utils.writeIntv(p, this.agent.length, off);
|
||||||
off += utils.writeIntv(p, this.agent.length, off);
|
off += utils.copy(this.agent, p, off);
|
||||||
for (i = 0; i < this.agent.length; i++)
|
|
||||||
p[off++] = this.agent[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start height
|
// Start height
|
||||||
off += utils.writeU32(p, packet.height || 0, off);
|
off += utils.writeU32(p, packet.height || 0, off);
|
||||||
@ -157,27 +153,28 @@ Framer.prototype.verack = function verack() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Framer.prototype._inv = function _inv(command, items) {
|
Framer.prototype._inv = function _inv(command, items) {
|
||||||
var res, off, i, hash;
|
var p, i, hash;
|
||||||
|
var off = 0;
|
||||||
|
|
||||||
assert(items.length <= 50000);
|
assert(items.length <= 50000);
|
||||||
|
|
||||||
res = new Buffer(utils.sizeIntv(items.length) + items.length * 36);
|
p = new Buffer(utils.sizeIntv(items.length) + items.length * 36);
|
||||||
|
|
||||||
off = utils.writeIntv(res, items.length, 0);
|
off += utils.writeIntv(p, items.length, off);
|
||||||
|
|
||||||
for (i = 0; i < items.length; i++) {
|
for (i = 0; i < items.length; i++) {
|
||||||
// Type
|
// Type
|
||||||
off += utils.writeU32(res, constants.inv[items[i].type], off);
|
off += utils.writeU32(p, constants.inv[items[i].type], off);
|
||||||
|
|
||||||
// Hash
|
// Hash
|
||||||
hash = items[i].hash;
|
hash = items[i].hash;
|
||||||
if (typeof hash === 'string')
|
if (typeof hash === 'string')
|
||||||
hash = new Buffer(hash, 'hex');
|
hash = new Buffer(hash, 'hex');
|
||||||
assert.equal(hash.length, 32);
|
assert.equal(hash.length, 32);
|
||||||
off += utils.copy(hash, res, off);
|
off += utils.copy(hash, p, off);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.packet(command, res);
|
return this.packet(command, p);
|
||||||
};
|
};
|
||||||
|
|
||||||
Framer.prototype.inv = function inv(items) {
|
Framer.prototype.inv = function inv(items) {
|
||||||
@ -206,21 +203,22 @@ Framer.prototype.pong = function pong(data) {
|
|||||||
|
|
||||||
Framer.prototype.filterLoad = function filterLoad(bloom, update) {
|
Framer.prototype.filterLoad = function filterLoad(bloom, update) {
|
||||||
var filter = bloom.toBuffer();
|
var filter = bloom.toBuffer();
|
||||||
var before = new Buffer(utils.sizeIntv(filter.length));
|
var p = new Buffer(utils.sizeIntv(filter.length) + filter.length + 9);
|
||||||
var after = new Buffer(9);
|
var off = 0;
|
||||||
|
|
||||||
utils.writeIntv(before, filter.length, 0);
|
off += utils.writeIntv(p, filter.length, off);
|
||||||
|
off += utils.copy(filter, p, off);
|
||||||
|
|
||||||
// Number of hash functions
|
// Number of hash functions
|
||||||
utils.writeU32(after, bloom.n, 0);
|
off += utils.writeU32(p, bloom.n, off);
|
||||||
|
|
||||||
// nTweak
|
// nTweak
|
||||||
utils.writeU32(after, bloom.tweak, 4);
|
off += utils.writeU32(p, bloom.tweak, off);
|
||||||
|
|
||||||
// nFlags
|
// nFlags
|
||||||
after[8] = constants.filterFlags[update];
|
p[off++] = constants.filterFlags[update];
|
||||||
|
|
||||||
return this.packet('filterload', Buffer.concat([before, filter, after]));
|
return this.packet('filterload', p);
|
||||||
};
|
};
|
||||||
|
|
||||||
Framer.prototype.filterClear = function filterClear() {
|
Framer.prototype.filterClear = function filterClear() {
|
||||||
@ -236,16 +234,17 @@ Framer.prototype.getBlocks = function getBlocks(hashes, stop) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Framer.prototype._getBlocks = function _getBlocks(cmd, hashes, stop) {
|
Framer.prototype._getBlocks = function _getBlocks(cmd, hashes, stop) {
|
||||||
var p = new Buffer(4 + utils.sizeIntv(hashes.length) + 32 * (hashes.length + 1));
|
var p, i, hash, len;
|
||||||
var i, hash, len;
|
|
||||||
var off = 0;
|
var off = 0;
|
||||||
|
|
||||||
|
p = new Buffer(4 + utils.sizeIntv(hashes.length) + 32 * (hashes.length + 1));
|
||||||
|
|
||||||
// getheaders can have a null hash
|
// getheaders can have a null hash
|
||||||
if (cmd === 'getheaders' && !hashes)
|
if (cmd === 'getheaders' && !hashes)
|
||||||
hashes = [];
|
hashes = [];
|
||||||
|
|
||||||
off += utils.writeU32(p, constants.version, 0);
|
off += utils.writeU32(p, constants.version, off);
|
||||||
off += utils.writeIntv(p, hashes.length, 4);
|
off += utils.writeIntv(p, hashes.length, off);
|
||||||
|
|
||||||
for (i = 0; i < hashes.length; i++) {
|
for (i = 0; i < hashes.length; i++) {
|
||||||
hash = hashes[i];
|
hash = hashes[i];
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user