util: refactor encoding.
This commit is contained in:
parent
dbed720d20
commit
c190dd2aad
@ -2211,7 +2211,7 @@ FeeFilterPacket.prototype.toRaw = function toRaw(writer) {
|
||||
|
||||
FeeFilterPacket.prototype.fromRaw = function fromRaw(data) {
|
||||
var br = BufferReader(data);
|
||||
this.rate = br.read64N();
|
||||
this.rate = br.read64();
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
@ -229,7 +229,7 @@ Coin.prototype.fromRaw = function fromRaw(data) {
|
||||
|
||||
this.version = br.readU32();
|
||||
this.height = br.readU32();
|
||||
this.value = br.read64N();
|
||||
this.value = br.read64();
|
||||
this.script.fromRaw(br.readVarBytes());
|
||||
this.coinbase = br.readU8() === 1;
|
||||
|
||||
|
||||
@ -244,7 +244,7 @@ Output.prototype.toRaw = function toRaw(writer) {
|
||||
Output.prototype.fromRaw = function fromRaw(data) {
|
||||
var br = BufferReader(data);
|
||||
|
||||
this.value = br.read64N();
|
||||
this.value = br.read64();
|
||||
this.script.fromRaw(br.readVarBytes());
|
||||
|
||||
return this;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -105,7 +105,7 @@ ProtoReader.prototype.readField = function readField(tag, opt) {
|
||||
value = this.readVarint();
|
||||
break;
|
||||
case wireType.FIXED64:
|
||||
value = this.readU64N();
|
||||
value = this.readU64();
|
||||
break;
|
||||
case wireType.DELIMITED:
|
||||
data = this.readVarBytes();
|
||||
|
||||
@ -200,8 +200,9 @@ BufferReader.prototype.readU32BE = function readU32BE() {
|
||||
};
|
||||
|
||||
/**
|
||||
* Read uint64le.
|
||||
* @returns {BN}
|
||||
* Read uint64le as a js number.
|
||||
* @returns {Number}
|
||||
* @throws on num > MAX_SAFE_INTEGER
|
||||
*/
|
||||
|
||||
BufferReader.prototype.readU64 = function readU64() {
|
||||
@ -213,8 +214,9 @@ BufferReader.prototype.readU64 = function readU64() {
|
||||
};
|
||||
|
||||
/**
|
||||
* Read uint64be.
|
||||
* @returns {BN}
|
||||
* Read uint64be as a js number.
|
||||
* @returns {Number}
|
||||
* @throws on num > MAX_SAFE_INTEGER
|
||||
*/
|
||||
|
||||
BufferReader.prototype.readU64BE = function readU64BE() {
|
||||
@ -225,34 +227,6 @@ BufferReader.prototype.readU64BE = function readU64BE() {
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read uint64le as a js number.
|
||||
* @returns {Number}
|
||||
* @throws on num > MAX_SAFE_INTEGER
|
||||
*/
|
||||
|
||||
BufferReader.prototype.readU64N = function readU64N(force53) {
|
||||
var ret;
|
||||
assert(this.offset + 8 <= this.data.length);
|
||||
ret = encoding.readU64N(this.data, this.offset, force53);
|
||||
this.offset += 8;
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read uint64be as a js number.
|
||||
* @returns {Number}
|
||||
* @throws on num > MAX_SAFE_INTEGER
|
||||
*/
|
||||
|
||||
BufferReader.prototype.readU64NBE = function readU64NBE(force53) {
|
||||
var ret;
|
||||
assert(this.offset + 8 <= this.data.length);
|
||||
ret = encoding.readU64NBE(this.data, this.offset, force53);
|
||||
this.offset += 8;
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read first least significant 53 bits of
|
||||
* a uint64le as a js number. Maintain the sign.
|
||||
@ -260,7 +234,11 @@ BufferReader.prototype.readU64NBE = function readU64NBE(force53) {
|
||||
*/
|
||||
|
||||
BufferReader.prototype.readU53 = function readU53() {
|
||||
return this.readU64N(true);
|
||||
var ret;
|
||||
assert(this.offset + 8 <= this.data.length);
|
||||
ret = encoding.readU53(this.data, this.offset);
|
||||
this.offset += 8;
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -270,7 +248,11 @@ BufferReader.prototype.readU53 = function readU53() {
|
||||
*/
|
||||
|
||||
BufferReader.prototype.readU53BE = function readU53BE() {
|
||||
return this.readU64NBE(true);
|
||||
var ret;
|
||||
assert(this.offset + 8 <= this.data.length);
|
||||
ret = encoding.readU53BE(this.data, this.offset);
|
||||
this.offset += 8;
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -339,8 +321,9 @@ BufferReader.prototype.read32BE = function read32BE() {
|
||||
};
|
||||
|
||||
/**
|
||||
* Read int64le.
|
||||
* @returns {BN}
|
||||
* Read int64le as a js number.
|
||||
* @returns {Number}
|
||||
* @throws on num > MAX_SAFE_INTEGER
|
||||
*/
|
||||
|
||||
BufferReader.prototype.read64 = function read64() {
|
||||
@ -352,8 +335,9 @@ BufferReader.prototype.read64 = function read64() {
|
||||
};
|
||||
|
||||
/**
|
||||
* Read int64be.
|
||||
* @returns {BN}
|
||||
* Read int64be as a js number.
|
||||
* @returns {Number}
|
||||
* @throws on num > MAX_SAFE_INTEGER
|
||||
*/
|
||||
|
||||
BufferReader.prototype.read64BE = function read64BE() {
|
||||
@ -364,34 +348,6 @@ BufferReader.prototype.read64BE = function read64BE() {
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read int64le as a js number.
|
||||
* @returns {Number}
|
||||
* @throws on num > MAX_SAFE_INTEGER
|
||||
*/
|
||||
|
||||
BufferReader.prototype.read64N = function read64N(force53) {
|
||||
var ret;
|
||||
assert(this.offset + 8 <= this.data.length);
|
||||
ret = encoding.read64N(this.data, this.offset, force53);
|
||||
this.offset += 8;
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read int64be as a js number.
|
||||
* @returns {Number}
|
||||
* @throws on num > MAX_SAFE_INTEGER
|
||||
*/
|
||||
|
||||
BufferReader.prototype.read64NBE = function read64NBE(force53) {
|
||||
var ret;
|
||||
assert(this.offset + 8 <= this.data.length);
|
||||
ret = encoding.read64NBE(this.data, this.offset, force53);
|
||||
this.offset += 8;
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read first least significant 53 bits of
|
||||
* a int64le as a js number. Maintain the sign.
|
||||
@ -399,7 +355,11 @@ BufferReader.prototype.read64NBE = function read64NBE(force53) {
|
||||
*/
|
||||
|
||||
BufferReader.prototype.read53 = function read53() {
|
||||
return this.read64N(true);
|
||||
var ret;
|
||||
assert(this.offset + 8 <= this.data.length);
|
||||
ret = encoding.read53(this.data, this.offset);
|
||||
this.offset += 8;
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -409,7 +369,63 @@ BufferReader.prototype.read53 = function read53() {
|
||||
*/
|
||||
|
||||
BufferReader.prototype.read53BE = function read53BE() {
|
||||
return this.read64NBE(true);
|
||||
var ret;
|
||||
assert(this.offset + 8 <= this.data.length);
|
||||
ret = encoding.read53BE(this.data, this.offset);
|
||||
this.offset += 8;
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read uint64le.
|
||||
* @returns {BN}
|
||||
*/
|
||||
|
||||
BufferReader.prototype.readU64BN = function readU64BN() {
|
||||
var ret;
|
||||
assert(this.offset + 8 <= this.data.length);
|
||||
ret = encoding.readU64BN(this.data, this.offset);
|
||||
this.offset += 8;
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read uint64be.
|
||||
* @returns {BN}
|
||||
*/
|
||||
|
||||
BufferReader.prototype.readU64BEBN = function readU64BEBN() {
|
||||
var ret;
|
||||
assert(this.offset + 8 <= this.data.length);
|
||||
ret = encoding.readU64BEBN(this.data, this.offset);
|
||||
this.offset += 8;
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read int64le.
|
||||
* @returns {BN}
|
||||
*/
|
||||
|
||||
BufferReader.prototype.read64BN = function read64BN() {
|
||||
var ret;
|
||||
assert(this.offset + 8 <= this.data.length);
|
||||
ret = encoding.read64BN(this.data, this.offset);
|
||||
this.offset += 8;
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read int64be.
|
||||
* @returns {BN}
|
||||
*/
|
||||
|
||||
BufferReader.prototype.read64BEBN = function read64BEBN() {
|
||||
var ret;
|
||||
assert(this.offset + 8 <= this.data.length);
|
||||
ret = encoding.read64BEBN(this.data, this.offset);
|
||||
this.offset += 8;
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -466,12 +482,11 @@ BufferReader.prototype.readDoubleBE = function readDoubleBE() {
|
||||
|
||||
/**
|
||||
* Read a varint.
|
||||
* @param {Boolean?} big - Whether to read as a big number.
|
||||
* @returns {Number}
|
||||
*/
|
||||
|
||||
BufferReader.prototype.readVarint = function readVarint(big) {
|
||||
var result = encoding.readVarint(this.data, this.offset, big);
|
||||
BufferReader.prototype.readVarint = function readVarint() {
|
||||
var result = encoding.readVarint(this.data, this.offset);
|
||||
this.offset += result.size;
|
||||
return result.value;
|
||||
};
|
||||
@ -487,14 +502,46 @@ BufferReader.prototype.skipVarint = function skipVarint() {
|
||||
this.offset += size;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read a varint.
|
||||
* @returns {BN}
|
||||
*/
|
||||
|
||||
BufferReader.prototype.readVarintBN = function readVarintBN() {
|
||||
var result = encoding.readVarintBN(this.data, this.offset);
|
||||
this.offset += result.size;
|
||||
return result.value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read a varint (type 2).
|
||||
* @param {Boolean?} big - Whether to read as a big number.
|
||||
* @returns {Number}
|
||||
*/
|
||||
|
||||
BufferReader.prototype.readVarint2 = function readVarint2(big) {
|
||||
var result = encoding.readVarint2(this.data, this.offset, big);
|
||||
BufferReader.prototype.readVarint2 = function readVarint2() {
|
||||
var result = encoding.readVarint2(this.data, this.offset);
|
||||
this.offset += result.size;
|
||||
return result.value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Skip past a varint (type 2).
|
||||
* @returns {Number}
|
||||
*/
|
||||
|
||||
BufferReader.prototype.skipVarint2 = function skipVarint2() {
|
||||
var size = encoding.skipVarint2(this.data, this.offset);
|
||||
assert(this.offset + size <= this.data.length);
|
||||
this.offset += size;
|
||||
};
|
||||
|
||||
/**
|
||||
* Read a varint (type 2).
|
||||
* @returns {BN}
|
||||
*/
|
||||
|
||||
BufferReader.prototype.readVarint2BN = function readVarint2BN() {
|
||||
var result = encoding.readVarint2BN(this.data, this.offset);
|
||||
this.offset += result.size;
|
||||
return result.value;
|
||||
};
|
||||
|
||||
@ -206,7 +206,7 @@ BufferWriter.prototype.writeU32BE = function writeU32BE(value) {
|
||||
|
||||
/**
|
||||
* Write uint64le.
|
||||
* @param {BN|Number} value
|
||||
* @param {Number} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.writeU64 = function writeU64(value) {
|
||||
@ -216,7 +216,7 @@ BufferWriter.prototype.writeU64 = function writeU64(value) {
|
||||
|
||||
/**
|
||||
* Write uint64be.
|
||||
* @param {BN|Number} value
|
||||
* @param {Number} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.writeU64BE = function writeU64BE(value) {
|
||||
@ -224,6 +224,24 @@ BufferWriter.prototype.writeU64BE = function writeU64BE(value) {
|
||||
this.ops.push(new WriteOp(UI64BE, value));
|
||||
};
|
||||
|
||||
/**
|
||||
* Write uint64le.
|
||||
* @param {BN} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.writeU64BN = function writeU64BN(value) {
|
||||
assert(false, 'Not implemented.');
|
||||
};
|
||||
|
||||
/**
|
||||
* Write uint64be.
|
||||
* @param {BN} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.writeU64BEBN = function writeU64BEBN(value) {
|
||||
assert(false, 'Not implemented.');
|
||||
};
|
||||
|
||||
/**
|
||||
* Write int8.
|
||||
* @param {Number} value
|
||||
@ -276,7 +294,7 @@ BufferWriter.prototype.write32BE = function write32BE(value) {
|
||||
|
||||
/**
|
||||
* Write int64le.
|
||||
* @param {BN|Number} value
|
||||
* @param {Number} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.write64 = function write64(value) {
|
||||
@ -286,7 +304,7 @@ BufferWriter.prototype.write64 = function write64(value) {
|
||||
|
||||
/**
|
||||
* Write int64be.
|
||||
* @param {BN|Number} value
|
||||
* @param {Number} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.write64BE = function write64BE(value) {
|
||||
@ -294,6 +312,24 @@ BufferWriter.prototype.write64BE = function write64BE(value) {
|
||||
this.ops.push(new WriteOp(I64BE, value));
|
||||
};
|
||||
|
||||
/**
|
||||
* Write int64le.
|
||||
* @param {BN} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.write64BN = function write64BN(value) {
|
||||
assert(false, 'Not implemented.');
|
||||
};
|
||||
|
||||
/**
|
||||
* Write int64be.
|
||||
* @param {BN} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.write64BEBN = function write64BEBN(value) {
|
||||
assert(false, 'Not implemented.');
|
||||
};
|
||||
|
||||
/**
|
||||
* Write float le.
|
||||
* @param {Number} value
|
||||
@ -336,34 +372,42 @@ BufferWriter.prototype.writeDoubleBE = function writeDoubleBE(value) {
|
||||
|
||||
/**
|
||||
* Write a varint.
|
||||
* @param {BN|Number} value
|
||||
* @param {Number} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.writeVarint = function writeVarint(value) {
|
||||
if (typeof value === 'number')
|
||||
assert(value >= 0);
|
||||
else
|
||||
assert(!value.isNeg());
|
||||
|
||||
this.written += encoding.sizeVarint(value);
|
||||
this.ops.push(new WriteOp(VARINT, value));
|
||||
};
|
||||
|
||||
/**
|
||||
* Write a varint.
|
||||
* @param {BN} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.writeVarintBN = function writeVarintBN(value) {
|
||||
assert(false, 'Not implemented.');
|
||||
};
|
||||
|
||||
/**
|
||||
* Write a varint (type 2).
|
||||
* @param {BN|Number} value
|
||||
* @param {Number} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.writeVarint2 = function writeVarint2(value) {
|
||||
if (typeof value === 'number')
|
||||
assert(value >= 0);
|
||||
else
|
||||
assert(!value.isNeg());
|
||||
|
||||
this.written += encoding.sizeVarint2(value);
|
||||
this.ops.push(new WriteOp(VARINT2, value));
|
||||
};
|
||||
|
||||
/**
|
||||
* Write a varint (type 2).
|
||||
* @param {BN} value
|
||||
*/
|
||||
|
||||
BufferWriter.prototype.writeVarint2BN = function writeVarint2BN(value) {
|
||||
assert(false, 'Not implemented.');
|
||||
};
|
||||
|
||||
/**
|
||||
* Write bytes.
|
||||
* @param {Buffer} value
|
||||
|
||||
@ -15,7 +15,7 @@ describe('Chain', function() {
|
||||
var chain, wallet, node, miner, walletdb;
|
||||
var tip1, tip2, cb1, cb2, mineBlock;
|
||||
|
||||
this.timeout(5000);
|
||||
this.timeout(8000);
|
||||
|
||||
node = new bcoin.fullnode({ db: 'memory', apiKey: 'foo' });
|
||||
// node.walletdb.client = new Client({ apiKey: 'foo', network: 'regtest' });
|
||||
|
||||
@ -4,6 +4,7 @@ var BN = require('bn.js');
|
||||
var bcoin = require('../').set('main');
|
||||
var assert = require('assert');
|
||||
var util = bcoin.util;
|
||||
var encoding = require('../lib/utils/encoding');
|
||||
var crypto = require('../lib/crypto/crypto');
|
||||
var constants = bcoin.constants;
|
||||
var opcodes = bcoin.constants.opcodes;
|
||||
@ -492,22 +493,23 @@ describe('TX', function() {
|
||||
],
|
||||
outputs: [{
|
||||
script: [],
|
||||
value: 0
|
||||
value: 0xdeadbeef
|
||||
}],
|
||||
locktime: 0
|
||||
});
|
||||
tx.outputs[0].value = new BN('00ffffffffffffff', 'hex');
|
||||
assert(tx.outputs[0].value.bitLength() === 56);
|
||||
var raw = tx.toRaw()
|
||||
var raw = tx.toRaw();
|
||||
assert(encoding.readU64(raw, 47) === 0xdeadbeef);
|
||||
raw[54] = 0x7f;
|
||||
assert.throws(function() {
|
||||
bcoin.tx.fromRaw(raw);
|
||||
console.log(bcoin.tx.fromRaw(raw));
|
||||
});
|
||||
delete tx._raw;
|
||||
tx.outputs[0].value = new BN('00ffffffffffffff', 'hex').ineg();
|
||||
assert(tx.outputs[0].value.bitLength() === 56);
|
||||
var raw = tx.toRaw()
|
||||
tx._raw = null;
|
||||
tx.outputs[0].value = 0;
|
||||
var raw = tx.toRaw();
|
||||
assert(encoding.readU64(raw, 47) === 0x00);
|
||||
raw[54] = 0x80;
|
||||
assert.throws(function() {
|
||||
bcoin.tx.fromRaw(raw);
|
||||
console.log(bcoin.tx.fromRaw(raw));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -192,11 +192,11 @@ describe('Utils', function() {
|
||||
var buf2 = new Buffer(8);
|
||||
var msg = 'should write+read a ' + num.bitLength() + ' bit unsigned int';
|
||||
it(msg, function() {
|
||||
encoding.writeU64(buf1, num, 0);
|
||||
encoding.writeU64N(buf2, num.toNumber(), 0);
|
||||
encoding.writeU64BN(buf1, num, 0);
|
||||
encoding.writeU64(buf2, num.toNumber(), 0);
|
||||
assert.deepEqual(buf1, buf2);
|
||||
var n1 = encoding.readU64(buf1, 0);
|
||||
var n2 = encoding.readU64N(buf2, 0);
|
||||
var n1 = encoding.readU64BN(buf1, 0);
|
||||
var n2 = encoding.readU64(buf2, 0);
|
||||
assert.equal(n1.toNumber(), n2);
|
||||
});
|
||||
});
|
||||
@ -207,26 +207,26 @@ describe('Utils', function() {
|
||||
var msg = 'should write+read a ' + num.bitLength()
|
||||
+ ' bit ' + (num.isNeg() ? 'negative' : 'positive') + ' int';
|
||||
it(msg, function() {
|
||||
encoding.write64(buf1, num, 0);
|
||||
encoding.write64N(buf2, num.toNumber(), 0);
|
||||
encoding.write64BN(buf1, num, 0);
|
||||
encoding.write64(buf2, num.toNumber(), 0);
|
||||
assert.deepEqual(buf1, buf2);
|
||||
var n1 = encoding.read64(buf1, 0);
|
||||
var n2 = encoding.read64N(buf2, 0);
|
||||
var n1 = encoding.read64BN(buf1, 0);
|
||||
var n2 = encoding.read64(buf2, 0);
|
||||
assert.equal(n1.toNumber(), n2);
|
||||
});
|
||||
var msg = 'should write+read a ' + num.bitLength()
|
||||
+ ' bit ' + (num.isNeg() ? 'negative' : 'positive') + ' int as unsigned';
|
||||
it(msg, function() {
|
||||
encoding.writeU64(buf1, num, 0);
|
||||
encoding.writeU64N(buf2, num.toNumber(), 0);
|
||||
encoding.writeU64BN(buf1, num, 0);
|
||||
encoding.writeU64(buf2, num.toNumber(), 0);
|
||||
assert.deepEqual(buf1, buf2);
|
||||
var n1 = encoding.readU64(buf1, 0);
|
||||
var n1 = encoding.readU64BN(buf1, 0);
|
||||
if (num.isNeg()) {
|
||||
assert.throws(function() {
|
||||
encoding.readU64N(buf2, 0);
|
||||
encoding.readU64(buf2, 0);
|
||||
});
|
||||
} else {
|
||||
var n2 = encoding.readU64N(buf2, 0);
|
||||
var n2 = encoding.readU64(buf2, 0);
|
||||
assert.equal(n1.toNumber(), n2);
|
||||
}
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user