Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
fix(bufferutils): remove varInt functions
Browse files Browse the repository at this point in the history
Remove these exports:

* `bufferutils.readVarInt`
* `bufferutils.readVarIntBuffer`
* `bufferutils.varIntSize`
* `bufferutils.writeVarInt`

While this changes the public API, none of our projects use these
functions.

Issue: BG-16466
  • Loading branch information
OttoAllmendinger committed Jan 15, 2020
1 parent f48669e commit 84851f0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 129 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"scripts": {
"coverage-report": "nyc report --reporter=lcov",
"coverage-html": "nyc report --reporter=html",
"coverage": "BITGO_UTXO_LIB_TEST_EXPECTED_COUNT=3496 nyc --check-coverage --branches 90 --functions 90 mocha --recursive",
"coverage": "BITGO_UTXO_LIB_TEST_EXPECTED_COUNT=3436 nyc --check-coverage --branches 90 --functions 90 mocha --recursive",
"integration": "mocha test/integration/",
"standard": "standard",
"test": "npm run standard && npm run coverage",
Expand Down
24 changes: 1 addition & 23 deletions src/bufferutils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var varuint = require('varuint-bitcoin')

// https://github.com/feross/buffer/blob/master/index.js#L1127
function verifuint (value, max) {
if (typeof value !== 'number') throw new Error('cannot write a non-number as a number')
Expand Down Expand Up @@ -34,28 +32,8 @@ function writeUInt64LE (buffer, value, offset) {
return offset + 8
}

// TODO: remove in 4.0.0?
function readVarInt (buffer, offset) {
var result = varuint.decode(buffer, offset)

return {
number: result,
size: varuint.decode.bytes
}
}

// TODO: remove in 4.0.0?
function writeVarInt (buffer, number, offset) {
varuint.encode(number, buffer, offset)
return varuint.encode.bytes
}

module.exports = {
readUInt64LE: readUInt64LE,
readInt64LE: readInt64LE,
readVarInt: readVarInt,
varIntBuffer: varuint.encode,
varIntSize: varuint.encodingLength,
writeUInt64LE: writeUInt64LE,
writeVarInt: writeVarInt
writeUInt64LE: writeUInt64LE
}
2 changes: 1 addition & 1 deletion src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ Transaction.prototype.getJoinSplitByteLength = function () {
}
var joinSplitsLen = this.joinsplits.length
var byteLength = 0
byteLength += bufferutils.varIntSize(joinSplitsLen) // vJoinSplit
byteLength += varuint.encodingLength(joinSplitsLen) // vJoinSplit

if (joinSplitsLen > 0) {
// Both pre and post Sapling JoinSplits are encoded with the following data:
Expand Down
63 changes: 0 additions & 63 deletions test/bufferutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,48 +38,6 @@ describe('bufferutils', function () {
})
})

describe('readVarInt', function () {
fixtures.valid.forEach(function (f) {
it('decodes ' + f.hexVI + ' correctly', function () {
var buffer = Buffer.from(f.hexVI, 'hex')
var d = bufferutils.readVarInt(buffer, 0)

assert.strictEqual(d.number, f.dec)
assert.strictEqual(d.size, buffer.length)
})
})

fixtures.invalid.readUInt64LE.forEach(function (f) {
it('throws on ' + f.description, function () {
var buffer = Buffer.from(f.hexVI, 'hex')

assert.throws(function () {
bufferutils.readVarInt(buffer, 0)
}, new RegExp(f.exception))
})
})
})

describe('varIntBuffer', function () {
fixtures.valid.forEach(function (f) {
it('encodes ' + f.dec + ' correctly', function () {
var buffer = bufferutils.varIntBuffer(f.dec)

assert.strictEqual(buffer.toString('hex'), f.hexVI)
})
})
})

describe('varIntSize', function () {
fixtures.valid.forEach(function (f) {
it('determines the varIntSize of ' + f.dec + ' correctly', function () {
var size = bufferutils.varIntSize(f.dec)

assert.strictEqual(size, f.hexVI.length / 2)
})
})
})

describe('writeUInt64LE', function () {
fixtures.valid.forEach(function (f) {
it('encodes ' + f.dec + ' correctly', function () {
Expand All @@ -100,25 +58,4 @@ describe('bufferutils', function () {
})
})
})

describe('writeVarInt', function () {
fixtures.valid.forEach(function (f) {
it('encodes ' + f.dec + ' correctly', function () {
var buffer = Buffer.alloc(9, 0)

var n = bufferutils.writeVarInt(buffer, f.dec, 0)
assert.strictEqual(buffer.slice(0, n).toString('hex'), f.hexVI)
})
})

fixtures.invalid.readUInt64LE.forEach(function (f) {
it('throws on ' + f.description, function () {
var buffer = Buffer.alloc(9, 0)

assert.throws(function () {
bufferutils.writeVarInt(buffer, f.dec, 0)
}, new RegExp(f.exception))
})
})
})
})
55 changes: 14 additions & 41 deletions test/fixtures/bufferutils.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,59 @@
"valid": [
{
"dec": 0,
"hex64": "0000000000000000",
"hexVI": "00",
"hexPD": "00"
"hex64": "0000000000000000"
},
{
"dec": 1,
"hex64": "0100000000000000",
"hexVI": "01",
"hexPD": "01"
"hex64": "0100000000000000"
},
{
"dec": 252,
"hex64": "fc00000000000000",
"hexVI": "fc",
"hexPD": "4cfc"
"hex64": "fc00000000000000"
},
{
"dec": 253,
"hex64": "fd00000000000000",
"hexVI": "fdfd00",
"hexPD": "4cfd"
"hex64": "fd00000000000000"
},
{
"dec": 254,
"hex64": "fe00000000000000",
"hexVI": "fdfe00",
"hexPD": "4cfe"
"hex64": "fe00000000000000"
},
{
"dec": 255,
"hex64": "ff00000000000000",
"hexVI": "fdff00",
"hexPD": "4cff"
"hex64": "ff00000000000000"
},
{
"dec": 65534,
"hex64": "feff000000000000",
"hexVI": "fdfeff",
"hexPD": "4dfeff"
"hex64": "feff000000000000"
},
{
"dec": 65535,
"hex64": "ffff000000000000",
"hexVI": "fdffff",
"hexPD": "4dffff"
"hex64": "ffff000000000000"
},
{
"dec": 65536,
"hex64": "0000010000000000",
"hexVI": "fe00000100",
"hexPD": "4e00000100"
"hex64": "0000010000000000"
},
{
"dec": 65537,
"hex64": "0100010000000000",
"hexVI": "fe01000100",
"hexPD": "4e01000100"
"hex64": "0100010000000000"
},
{
"dec": 4294967295,
"hex64": "ffffffff00000000",
"hexVI": "feffffffff",
"hexPD": "4effffffff"
"hex64": "ffffffff00000000"
},
{
"dec": 4294967296,
"hex64": "0000000001000000",
"hexVI": "ff0000000001000000"
"hex64": "0000000001000000"
},
{
"dec": 4294967297,
"hex64": "0100000001000000",
"hexVI": "ff0100000001000000"
"hex64": "0100000001000000"
},
{
"dec": 9007199254740991,
"hex64": "ffffffffffff1f00",
"hexVI": "ffffffffffffff1f00"
"hex64": "ffffffffffff1f00"
}
],
"invalid": {
Expand All @@ -88,14 +63,12 @@
"description": "n === 2^53",
"exception": "RangeError: value out of range",
"hex64": "0000000000002000",
"hexVI": "ff0000000000000020",
"dec": 9007199254740992
},
{
"description": "n > 2^53",
"exception": "RangeError: value out of range",
"hex64": "0100000000002000",
"hexVI": "ff0100000000000020",
"dec": 9007199254740993
}
]
Expand Down

0 comments on commit 84851f0

Please sign in to comment.