more framing.

This commit is contained in:
Christopher Jeffrey 2016-02-23 03:25:18 -08:00
parent 3514eb7dec
commit f4f6c79bfd

View File

@ -23,7 +23,6 @@ function Framer(options) {
this.options = options;
this.agent = new Buffer(options.userAgent || constants.userAgent, 'ascii');
this.agent = this.agent.slice(0, 0xfc);
}
Framer.prototype.header = function header(cmd, payload) {
@ -104,9 +103,10 @@ Framer.prototype._addr = function addr(data, full) {
};
Framer.prototype.version = function version(packet) {
var p = new Buffer(86 + this.agent.length);
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)
packet = {};
@ -131,15 +131,11 @@ Framer.prototype.version = function version(packet) {
// Nonce, very dramatic
off += utils.writeU64(p, utils.nonce(), off);
// User-agent
assert.equal(off, 80);
if (!this.agent) {
p[off++] = 0;
} else {
off += utils.writeIntv(p, this.agent.length, off);
for (i = 0; i < this.agent.length; i++)
p[off++] = this.agent[i];
}
// User-agent
off += utils.writeIntv(p, this.agent.length, off);
off += utils.copy(this.agent, p, off);
// Start height
off += utils.writeU32(p, packet.height || 0, off);
@ -157,27 +153,28 @@ Framer.prototype.verack = function verack() {
};
Framer.prototype._inv = function _inv(command, items) {
var res, off, i, hash;
var p, i, hash;
var off = 0;
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++) {
// Type
off += utils.writeU32(res, constants.inv[items[i].type], off);
off += utils.writeU32(p, constants.inv[items[i].type], off);
// Hash
hash = items[i].hash;
if (typeof hash === 'string')
hash = new Buffer(hash, 'hex');
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) {
@ -206,21 +203,22 @@ Framer.prototype.pong = function pong(data) {
Framer.prototype.filterLoad = function filterLoad(bloom, update) {
var filter = bloom.toBuffer();
var before = new Buffer(utils.sizeIntv(filter.length));
var after = new Buffer(9);
var p = new Buffer(utils.sizeIntv(filter.length) + filter.length + 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
utils.writeU32(after, bloom.n, 0);
off += utils.writeU32(p, bloom.n, off);
// nTweak
utils.writeU32(after, bloom.tweak, 4);
off += utils.writeU32(p, bloom.tweak, off);
// 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() {
@ -236,16 +234,17 @@ Framer.prototype.getBlocks = function getBlocks(hashes, stop) {
};
Framer.prototype._getBlocks = function _getBlocks(cmd, hashes, stop) {
var p = new Buffer(4 + utils.sizeIntv(hashes.length) + 32 * (hashes.length + 1));
var i, hash, len;
var p, i, hash, len;
var off = 0;
p = new Buffer(4 + utils.sizeIntv(hashes.length) + 32 * (hashes.length + 1));
// getheaders can have a null hash
if (cmd === 'getheaders' && !hashes)
hashes = [];
off += utils.writeU32(p, constants.version, 0);
off += utils.writeIntv(p, hashes.length, 4);
off += utils.writeU32(p, constants.version, off);
off += utils.writeIntv(p, hashes.length, off);
for (i = 0; i < hashes.length; i++) {
hash = hashes[i];