bip152: use regular varints for lengths.

This commit is contained in:
Christopher Jeffrey 2016-08-25 20:40:47 -07:00
parent fa366e56fd
commit 4fa6136ec4
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 32 additions and 14 deletions

View File

@ -96,19 +96,19 @@ CompactBlock.prototype.fromRaw = function fromRaw(data) {
this.initKey();
count = p.readVarint2();
count = p.readVarint();
this.totalTX += count;
for (i = 0; i < count; i++)
this.ids.push(p.readU32() + p.readU16() * 0x100000000);
count = p.readVarint2();
count = p.readVarint();
this.totalTX += count;
for (i = 0; i < count; i++) {
index = p.readVarint2();
index = p.readVarint();
assert(index <= 0xffff);
assert(index < this.totalTX);
tx = bcoin.tx.fromRaw(p);
@ -139,7 +139,7 @@ CompactBlock.prototype.toRaw = function toRaw(witness, writer) {
p.writeBytes(this.keyNonce);
p.writeVarint2(this.ids.length);
p.writeVarint(this.ids.length);
for (i = 0; i < this.ids.length; i++) {
id = this.ids[i];
@ -150,11 +150,11 @@ CompactBlock.prototype.toRaw = function toRaw(witness, writer) {
p.writeU16(hi);
}
p.writeVarint2(this.ptx.length);
p.writeVarint(this.ptx.length);
for (i = 0; i < this.ptx.length; i++) {
ptx = this.ptx[i];
p.writeVarint2(ptx[0]);
p.writeVarint(ptx[0]);
if (witness)
ptx[1].toRaw(p);
else
@ -415,10 +415,10 @@ TXRequest.prototype.fromRaw = function fromRaw(data) {
this.hash = p.readHash('hex');
count = p.readVarint2();
count = p.readVarint();
for (i = 0; i < count; i++) {
index = p.readVarint2();
index = p.readVarint();
assert(index <= 0xffff);
this.indexes.push(index);
}
@ -446,11 +446,11 @@ TXRequest.prototype.toRaw = function toRaw(writer) {
p.writeHash(this.hash);
p.writeVarint2(this.indexes.length);
p.writeVarint(this.indexes.length);
for (i = 0; i < this.indexes.length; i++) {
index = this.indexes[i] - (i === 0 ? 0 : this.indexes[i - 1] + 1);
p.writeVarint2(index);
p.writeVarint(index);
}
if (!writer)
@ -495,7 +495,7 @@ TXResponse.prototype.fromRaw = function fromRaw(data) {
this.hash = p.readHash('hex');
count = p.readVarint2();
count = p.readVarint();
for (i = 0; i < count; i++)
this.txs.push(bcoin.tx.fromRaw(p));
@ -532,7 +532,7 @@ TXResponse.prototype.toRaw = function toRaw(witness, writer) {
p.writeHash(this.hash);
p.writeVarint2(this.txs.length);
p.writeVarint(this.txs.length);
for (i = 0; i < this.txs.length; i++) {
tx = this.txs[i];

View File

@ -125,19 +125,37 @@ describe('Utils', function() {
assert.equal(utils.readVarint2(b, 0).value, 255);
assert.deepEqual(b, [0x80, 0x7f]);
var b = new Buffer(2);
b.fill(0x00);
utils.writeVarint2(b, 16383, 0);
assert.equal(utils.readVarint2(b, 0).value, 16383);
assert.deepEqual(b, [0xfe, 0x7f]);
var b = new Buffer(2);
b.fill(0x00);
utils.writeVarint2(b, 16384, 0);
assert.equal(utils.readVarint2(b, 0).value, 16384);
assert.deepEqual(b, [0xff, 0x00]);
var b = new Buffer(3);
b.fill(0x00);
utils.writeVarint2(b, 16511, 0);
assert.equal(utils.readVarint2(b, 0).value, 16511);
//assert.deepEqual(b, [0x80, 0xff, 0x7f]);
// assert.deepEqual(b, [0x80, 0xff, 0x7f]);
assert.deepEqual(b, [0xff, 0x7f, 0x00]);
var b = new Buffer(3);
b.fill(0x00);
utils.writeVarint2(b, 65535, 0);
assert.equal(utils.readVarint2(b, 0).value, 65535);
//assert.deepEqual(b, [0x82, 0xfd, 0x7f]);
// assert.deepEqual(b, [0x82, 0xfd, 0x7f]);
assert.deepEqual(b, [0x82, 0xfe, 0x7f]);
var b = new Buffer(5);
b.fill(0x00);
utils.writeVarint2(b, Math.pow(2, 32), 0);
assert.equal(utils.readVarint2(b, 0).value, Math.pow(2, 32));
assert.deepEqual(b, [0x8e, 0xfe, 0xfe, 0xff, 0x00]);
});
var unsigned = [