From 62a7dba55619040f597c74a51d903b2e0a4f48ca Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 25 Mar 2016 15:31:43 -0700 Subject: [PATCH] rename intv. --- lib/bcoin/block.js | 4 +-- lib/bcoin/coins.js | 6 ++--- lib/bcoin/mtx.js | 6 ++--- lib/bcoin/protocol/framer.js | 22 +++++++-------- lib/bcoin/protocol/parser.js | 26 +++++++++--------- lib/bcoin/reader.js | 15 ++++------- lib/bcoin/tx.js | 6 ++--- lib/bcoin/utils.js | 52 +++--------------------------------- lib/bcoin/workers.js | 10 +++---- lib/bcoin/writer.js | 14 ++++------ 10 files changed, 53 insertions(+), 108 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 68211182..5b701d3a 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -379,7 +379,7 @@ Block.prototype.toCompact = function toCompact() { p.writeBytes(this.abbr()); p.writeU32(height); - p.writeIntv(this.txs.length); + p.writeVarint(this.txs.length); this.txs.forEach(function(tx) { p.writeBytes(tx.hash()); @@ -398,7 +398,7 @@ Block.fromCompact = function fromCompact(buf) { var bits = p.readU32(); var nonce = p.readU32(); var height = p.readU32(); - var txCount = p.readIntv(); + var txCount = p.readVarint(); var i; for (i = 0; i < txCount; i++) diff --git a/lib/bcoin/coins.js b/lib/bcoin/coins.js index f9d26cc7..668e6f34 100644 --- a/lib/bcoin/coins.js +++ b/lib/bcoin/coins.js @@ -147,11 +147,11 @@ Coins.toRaw = function toRaw(tx) { p.writeU32(tx.version); p.writeU32(height); p.writeU8(tx.coinbase ? 1 : 0); - p.writeUIntv(tx.outputs.length); + p.writeVarint(tx.outputs.length); tx.outputs.forEach(function(output) { if (!output) { - p.writeUIntv(0); + p.writeVarint(0); return; } p.writeVarBytes(bcoin.protocol.framer.output(output)); @@ -172,7 +172,7 @@ Coins._fromRaw = function _fromRaw(buf) { if (tx.height === 0x7fffffff) tx.height = -1; - coinCount = p.readUIntv(); + coinCount = p.readVarint(); for (i = 0; i < coinCount; i++) { coin = p.readVarBytes(); if (coin.length === 0) { diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 13a8cb90..4855d2be 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -709,7 +709,7 @@ MTX.prototype.maxSize = function maxSize(maxM, maxN) { // the isMultisig clause. // OP_PUSHDATA2 [redeem] prev = this.inputs[i].script.getRedeem(); - size += utils.sizeIntv(prev.getSize()) + prev.getSize(); + size += utils.sizeVarint(prev.getSize()) + prev.getSize(); } if (prev.isWitnessProgram()) { @@ -720,7 +720,7 @@ MTX.prototype.maxSize = function maxSize(maxM, maxN) { size *= 4; if (this.inputs[i].witness.items.length && prev.isWitnessScripthash()) { prev = this.inputs[i].witness.getRedeem(); - size += utils.sizeIntv(prev.getSize()) + prev.getSize(); + size += utils.sizeVarint(prev.getSize()) + prev.getSize(); } else if (prev.isWitnessPubkeyhash()) { prev = Script.createPubkeyhash(prev.code[1]); } @@ -781,7 +781,7 @@ MTX.prototype.maxSize = function maxSize(maxM, maxN) { } // Byte for varint size of input script. - size += utils.sizeIntv(size); + size += utils.sizeVarint(size); // Calculate vsize if we're a witness program. if (witness) diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 26dc428c..3a2b2900 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -233,7 +233,7 @@ Framer._inv = function _inv(items, writer) { assert(items.length <= 50000); - p.writeUIntv(items.length); + p.writeVarint(items.length); for (i = 0; i < items.length; i++) { p.writeU32(constants.inv[items[i].type]); @@ -305,7 +305,7 @@ Framer._getBlocks = function _getBlocks(hashes, stop, writer) { var i; p.writeU32(constants.version); - p.writeUIntv(hashes.length); + p.writeVarint(hashes.length); for (i = 0; i < hashes.length; i++) p.writeHash(hashes[i]); @@ -370,11 +370,11 @@ Framer.tx = function _tx(tx, writer) { p.write32(tx.version); - p.writeUIntv(tx.inputs.length); + p.writeVarint(tx.inputs.length); for (i = 0; i < tx.inputs.length; i++) Framer.input(tx.inputs[i], p); - p.writeUIntv(tx.outputs.length); + p.writeVarint(tx.outputs.length); for (i = 0; i < tx.outputs.length; i++) Framer.output(tx.outputs[i], p); @@ -425,12 +425,12 @@ Framer.witnessTX = function _witnessTX(tx, writer) { p.writeU8(0); p.writeU8(tx.flag || 1); - p.writeUIntv(tx.inputs.length); + p.writeVarint(tx.inputs.length); for (i = 0; i < tx.inputs.length; i++) Framer.input(tx.inputs[i], p); - p.writeUIntv(tx.outputs.length); + p.writeVarint(tx.outputs.length); for (i = 0; i < tx.outputs.length; i++) Framer.output(tx.outputs[i], p); @@ -463,7 +463,7 @@ Framer.witness = function _witness(witness, writer) { var p = new BufferWriter(writer); var i; - p.writeUIntv(witness.items.length); + p.writeVarint(witness.items.length); for (i = 0; i < witness.items.length; i++) p.writeVarBytes(witness.items[i]); @@ -521,7 +521,7 @@ Framer._block = function _block(block, useWitness, writer) { p.writeU32(block.ts); p.writeU32(block.bits); p.writeU32(block.nonce); - p.writeUIntv(block.txs.length); + p.writeVarint(block.txs.length); for (i = 0; i < block.txs.length; i++) { Framer.renderTX(block.txs[i], useWitness, p); @@ -548,7 +548,7 @@ Framer.merkleBlock = function _merkleBlock(block, writer) { p.writeU32(block.nonce); p.writeU32(block.totalTX); - p.writeUIntv(block.hashes.length); + p.writeVarint(block.hashes.length); for (i = 0; i < block.hashes.length; i++) p.writeHash(block.hashes[i]); @@ -570,7 +570,7 @@ Framer.headers = function _headers(block, writer) { p.writeU32(block.ts); p.writeU32(block.bits); p.writeU32(block.nonce); - p.writeUIntv(block.totalTX); + p.writeVarint(block.totalTX); if (!writer) p = p.render(); @@ -597,7 +597,7 @@ Framer.addr = function addr(peers, writer) { var p = new BufferWriter(writer); var i, peer; - p.writeUIntv(peers.length); + p.writeVarint(peers.length); for (i = 0; i < peers.length; i++) { peer = peers[i]; diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index ac2a1c90..b9690a59 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -234,7 +234,7 @@ Parser.parseInvList = function parseInvList(p) { p = new BufferReader(p); p.start(); - count = p.readUIntv(); + count = p.readVarint(); for (i = 0; i < count; i++) { items.push({ @@ -263,7 +263,7 @@ Parser.parseMerkleBlock = function parseMerkleBlock(p) { nonce = p.readU32(); totalTX = p.readU32(); - hashCount = p.readUIntv(); + hashCount = p.readVarint(); hashes = new Array(hashCount); @@ -293,7 +293,7 @@ Parser.parseHeaders = function parseHeaders(p) { p = new BufferReader(p); p.start(); - count = p.readUIntv(); + count = p.readVarint(); for (i = 0; i < count; i++) { headers.push({ @@ -303,7 +303,7 @@ Parser.parseHeaders = function parseHeaders(p) { ts: p.readU32(), bits: p.readU32(), nonce: p.readU32(), - totalTX: p.readUIntv() + totalTX: p.readVarint() }); } @@ -327,7 +327,7 @@ Parser.parseBlock = function parseBlock(p) { ts = p.readU32(); bits = p.readU32(); nonce = p.readU32(); - totalTX = p.readUIntv(); + totalTX = p.readVarint(); for (i = 0; i < totalTX; i++) { tx = Parser.parseTX(p); @@ -364,11 +364,11 @@ Parser.parseBlockCompact = function parseBlockCompact(p) { bits = p.readU32(); nonce = p.readU32(); - totalTX = p.readUIntv(); + totalTX = p.readVarint(); if (version > 1 && totalTX > 0) { p.read32(); - inCount = p.readUIntv(); + inCount = p.readVarint(); if (inCount > 0) input = Parser.parseInput(p); @@ -504,7 +504,7 @@ Parser.parseTX = function parseTX(p) { return Parser.parseWitnessTX(p); version = p.readU32(); - inCount = p.readUIntv(); + inCount = p.readVarint(); txIn = new Array(inCount); for (i = 0; i < inCount; i++) { @@ -514,7 +514,7 @@ Parser.parseTX = function parseTX(p) { txIn[i].witness = new bcoin.script.witness([]); } - outCount = p.readUIntv(); + outCount = p.readVarint(); txOut = new Array(outCount); for (i = 0; i < outCount; i++) { tx = Parser.parseOutput(p); @@ -573,7 +573,7 @@ Parser.parseWitnessTX = function parseWitnessTX(p) { if (flag === 0) throw new Error('Invalid witness tx (flag == 0)'); - inCount = p.readUIntv(); + inCount = p.readVarint(); txIn = new Array(inCount); for (i = 0; i < inCount; i++) { @@ -581,7 +581,7 @@ Parser.parseWitnessTX = function parseWitnessTX(p) { txIn[i] = tx; } - outCount = p.readUIntv(); + outCount = p.readVarint(); txOut = new Array(outCount); for (i = 0; i < outCount; i++) { @@ -619,7 +619,7 @@ Parser.parseWitness = function parseWitness(p) { p = new BufferReader(p); p.start(); - chunkCount = p.readUIntv(); + chunkCount = p.readVarint(); for (i = 0; i < chunkCount; i++) witness.push(p.readVarBytes()); @@ -699,7 +699,7 @@ Parser.parseAddr = function parseAddr(p) { p = new BufferReader(p); p.start(); - count = p.readUIntv(); + count = p.readVarint(); for (i = 0; i < count; i++) addrs.push(Parser.parseAddress(p, true)); diff --git a/lib/bcoin/reader.js b/lib/bcoin/reader.js index 8f78631b..9669b4a8 100644 --- a/lib/bcoin/reader.js +++ b/lib/bcoin/reader.js @@ -216,11 +216,11 @@ BufferReader.prototype.readHash = function readHash(enc) { }; BufferReader.prototype.readVarString = function readVarString(enc) { - return this.readString(enc, this.readUIntv()); + return this.readString(enc, this.readVarint()); }; BufferReader.prototype.readVarBytes = function readVarBytes() { - return this.readBytes(this.readUIntv()); + return this.readBytes(this.readVarint()); }; BufferReader.prototype.readNullString = function readNullString(enc) { @@ -240,20 +240,15 @@ BufferReader.prototype.left = function left() { return this.data.length - this.offset; }; -BufferReader.prototype.readIntv = function readIntv() { +BufferReader.prototype.readVarint = function readVarint() { assert(this.offset + 1 <= this.data.length); - var result = utils.readIntv(this.data, this.offset); + var result = utils.readVarint(this.data, this.offset); assert(result.off <= this.data.length); + assert(result.r >= 0); this.offset = result.off; return result.r; }; -BufferReader.prototype.readUIntv = function readUIntv() { - var result = this.readIntv(); - assert(result >= 0); - return result; -}; - /** * Expose */ diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 592307c7..741b1249 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1191,10 +1191,10 @@ TX.prototype.toExtended = function toExtended(saveCoins) { // p.writeU32(changeIndex); if (saveCoins) { - p.writeUIntv(this.inputs.length); + p.writeVarint(this.inputs.length); this.inputs.forEach(function(input) { if (!input.output) { - p.writeUIntv(0); + p.writeVarint(0); return; } @@ -1233,7 +1233,7 @@ TX._fromExtended = function _fromExtended(buf, saveCoins) { tx.changeIndex = -1; if (saveCoins) { - coinCount = p.readUIntv(); + coinCount = p.readVarint(); for (i = 0; i < coinCount; i++) { coin = p.readVarBytes(); if (coin.length === 0) diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index a2fcd3c9..923e743e 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -1326,52 +1326,6 @@ utils.read64NBE = function read64NBE(dst, off) { return (hi * 0x100000000) + lo; }; -utils.writeU256 = function writeU256(dst, num, off) { - var i; - - if (!bn.isBN(num)) - num = new bn(+num); - - off = off >>> 0; - - // We shouldn't think of - // this as negative. - if (num.isNeg()) - num = num.neg(); - - num = num.toArray('le', 32); - - assert.equal(num.length, 32); - - for (i = 0; i < num.length; i++) - dst[off++] = num[i] & 0xff; - - return 32; -}; - -utils.writeU256BE = function writeU256BE(dst, num, off) { - var i; - - if (!bn.isBN(num)) - num = new bn(+num); - - off = off >>> 0; - - // We shouldn't think of - // this as negative. - if (num.isNeg()) - num = num.neg(); - - num = num.toArray('be', 32); - - assert.equal(num.length, 32); - - for (i = 0; i < num.length; i++) - dst[off++] = num[i] & 0xff; - - return 32; -}; - utils.write8 = function write8(dst, num, off) { num = +num; off = off >>> 0; @@ -1481,7 +1435,7 @@ utils.write64BE = function write64BE(dst, num, off) { return 8; }; -utils.readIntv = function readIntv(arr, off) { +utils.readVarint = function readVarint(arr, off) { var r, bytes; off = off >>> 0; @@ -1511,7 +1465,7 @@ utils.readIntv = function readIntv(arr, off) { return { off: off + bytes, r: r }; }; -utils.writeIntv = function writeIntv(dst, num, off) { +utils.writeVarint = function writeVarint(dst, num, off) { off = off >>> 0; if (bn.isBN(num)) { @@ -1551,7 +1505,7 @@ utils.writeIntv = function writeIntv(dst, num, off) { return 9; }; -utils.sizeIntv = function sizeIntv(num) { +utils.sizeVarint = function sizeVarint(num) { if (bn.isBN(num)) { if (num.cmpn(0xffffffff) > 0) return 9; diff --git a/lib/bcoin/workers.js b/lib/bcoin/workers.js index 248e008d..3743e179 100644 --- a/lib/bcoin/workers.js +++ b/lib/bcoin/workers.js @@ -175,7 +175,7 @@ function createBody(name, items) { if (name) p.writeVarString(name, 'ascii'); else - p.writeUIntv(0); + p.writeVarint(0); frameItem(items, p); @@ -222,13 +222,13 @@ function frameItem(item, p) { p.writeVarBytes(item); } else if (Array.isArray(item)) { p.writeU8(5); - p.writeUIntv(item.length); + p.writeVarint(item.length); for (i = 0; i < item.length; i++) frameItem(item[i], p); } else { keys = Object.keys(item); p.writeU8(6); - p.writeUIntv(keys.length); + p.writeVarint(keys.length); for (i = 0; i < keys.length; i++) { p.writeVarString(keys[i], 'utf8'); frameItem(item[keys[i]], p); @@ -276,13 +276,13 @@ function parseItem(p) { return p.readVarBytes(); case 5: items = []; - count = p.readUIntv(); + count = p.readVarint(); for (i = 0; i < count; i++) items.push(parseItem(p)); return items; case 6: items = {}; - count = p.readUIntv(); + count = p.readVarint(); for (i = 0; i < count; i++) items[p.readVarString('utf8')] = parseItem(p); return items; diff --git a/lib/bcoin/writer.js b/lib/bcoin/writer.js index 92e06e7b..73016f4f 100644 --- a/lib/bcoin/writer.js +++ b/lib/bcoin/writer.js @@ -44,7 +44,7 @@ BufferWriter.prototype.render = function render(keep) { case '32be': off += utils.write32BE(data, item[1], off); break; case '64': off += utils.write64(data, item[1], off); break; case '64be': off += utils.write64BE(data, item[1], off); break; - case 'varint': off += utils.writeIntv(data, item[1], off); break; + case 'varint': off += utils.writeVarint(data, item[1], off); break; case 'bytes': off += utils.copy(item[1], data, off); break; } } @@ -159,7 +159,7 @@ BufferWriter.prototype.writeVarString = function writeVarString(value, enc) { }; BufferWriter.prototype.writeVarBytes = function writeVarBytes(value) { - this.written += utils.sizeIntv(value.length); + this.written += utils.sizeVarint(value.length); this.written += value.length; this.data.push(['varint', value.length]); this.data.push(['bytes', value]); @@ -170,13 +170,9 @@ BufferWriter.prototype.writeNullString = function writeNullString(value, enc) { this.writeU8(0); }; -BufferWriter.prototype.writeIntv = function writeIntv(value) { - this.written += utils.sizeIntv(value); - this.data.push(['varint', value]); -}; - -BufferWriter.prototype.writeUIntv = function writeUIntv(value) { - this.written += utils.sizeIntv(value); +BufferWriter.prototype.writeVarint = function writeVarint(value) { + assert(value >= 0); + this.written += utils.sizeVarint(value); this.data.push(['varint', value]); };