diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 36ac8048..0fb97ff4 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -154,23 +154,21 @@ Parser.prototype.parsePayload = function parsePayload(cmd, p) { Parser.parsePing = function parsePing(p) { p = new BufferReader(p); - - if (p.left() < 8) - throw new Error('pong packet is too small'); + p.start(); return { - nonce: p.readU64() + nonce: p.readU64(), + _size: p.end() }; }; Parser.parsePong = function parsePong(p) { p = new BufferReader(p); - - if (p.left() < 8) - throw new Error('ping packet is too small'); + p.start(); return { - nonce: p.readU64() + nonce: p.readU64(), + _size: p.end() }; }; @@ -178,29 +176,17 @@ Parser.parseVersion = function parseVersion(p) { var version, services, ts, recv, from, nonce, agent, height, relay; p = new BufferReader(p); + p.start(); - version = p.readU32(); + version = p.read32(); services = p.readU64(); - - // Timestamp ts = p.read64(); - - // Our address (recv) recv = Parser.parseAddress(p, false); - - // Their Address (from) from = Parser.parseAddress(p, false); - - // Nonce, very dramatic nonce = p.readU64(); - - // User agent length agent = p.readVarString('ascii'); + height = p.read32(); - // Start height - height = p.readU32(); - - // Relay try { relay = p.readU8() === 1; } catch (e) { @@ -216,9 +202,13 @@ Parser.parseVersion = function parseVersion(p) { try { services = services.toNumber(); } catch (e) { - services = 1; + services = 0; } + assert(version >= 0, 'Version is negative.'); + assert(ts >= 0, 'Timestamp is negative.'); + assert(height >= 0, 'Height is negative.'); + return { version: version, services: services, @@ -232,7 +222,8 @@ Parser.parseVersion = function parseVersion(p) { nonce: nonce, agent: agent, height: height, - relay: relay + relay: relay, + _size: p.end() }; }; @@ -241,6 +232,7 @@ Parser.parseInvList = function parseInvList(p) { var i, count; p = new BufferReader(p); + p.start(); count = p.readUIntv(); @@ -251,6 +243,8 @@ Parser.parseInvList = function parseInvList(p) { }); } + items._size = p.end(); + return items; }; @@ -297,6 +291,7 @@ Parser.parseHeaders = function parseHeaders(p) { var i, count; p = new BufferReader(p); + p.start(); count = p.readUIntv(); @@ -312,6 +307,8 @@ Parser.parseHeaders = function parseHeaders(p) { }); } + headers._size = p.end(); + return headers; }; @@ -346,8 +343,8 @@ Parser.parseBlock = function parseBlock(p) { bits: bits, nonce: nonce, txs: txs, - _size: p.end(), - _witnessSize: witnessSize + _witnessSize: witnessSize, + _size: p.end() }; }; @@ -413,13 +410,13 @@ Parser.parseInput = function parseInput(p) { sequence = p.readU32(); return { - _size: p.end(), prevout: { hash: hash, index: index }, script: script, - sequence: sequence + sequence: sequence, + _size: p.end() }; }; @@ -433,9 +430,9 @@ Parser.parseOutput = function parseOutput(p) { script = new bcoin.script(p.readVarBytes()); return { - _size: p.end(), value: value, - script: script + script: script, + _size: p.end() }; }; @@ -443,6 +440,7 @@ Parser.parseCoin = function parseCoin(p, extended) { var version, height, value, script, hash, index, spent; p = new BufferReader(p); + p.start(); version = p.readU32(); height = p.readU32(); @@ -471,7 +469,8 @@ Parser.parseCoin = function parseCoin(p, extended) { script: script, hash: hash, index: index, - spent: spent + spent: spent, + _size: p.end() }; }; @@ -593,8 +592,8 @@ Parser.parseWitnessTX = function parseWitnessTX(p) { locktime: locktime, // _raw: raw, // _size: raw.length, - _size: p.end(), - _witnessSize: witnessSize + 2 + _witnessSize: witnessSize + 2, + _size: p.end() }; }; @@ -611,8 +610,8 @@ Parser.parseWitness = function parseWitness(p) { witness.push(p.readVarBytes()); return { - _size: p.end(), - witness: new bcoin.script.witness(witness) + witness: new bcoin.script.witness(witness), + _size: p.end() }; }; @@ -620,6 +619,7 @@ Parser.parseReject = function parseReject(p) { var message, ccode, reason, data; p = new BufferReader(p); + p.start(); message = p.readVarString('ascii'); ccode = p.readU8(); @@ -630,7 +630,8 @@ Parser.parseReject = function parseReject(p) { message: message, ccode: constants.rejectByVal[ccode] || ccode, reason: reason, - data: data + data: data, + _size: p.end() }; }; @@ -638,6 +639,7 @@ Parser.parseAddress = function parseAddress(p, full) { var ts, services, ip, port; p = new BufferReader(p); + p.start(); if (full) { ts = p.readU32(); @@ -666,7 +668,8 @@ Parser.parseAddress = function parseAddress(p, full) { witness: (services & constants.services.witness) !== 0, ipv6: utils.array2ip(ip, 6), ipv4: utils.array2ip(ip, 4), - port: port + port: port, + _size: p.end() }; }; @@ -675,12 +678,15 @@ Parser.parseAddr = function parseAddr(p) { var i, count; p = new BufferReader(p); + p.start(); count = p.readUIntv(); for (i = 0; i < count; i++) addrs.push(Parser.parseAddress(p, true)); + addrs._size = p.end(); + return addrs; };