Add BufferReader tests

This commit is contained in:
junderw 2020-01-15 11:25:53 +09:00
parent 06674b19fe
commit c8fdfae955
No known key found for this signature in database
GPG key ID: B256185D3A971908
4 changed files with 486 additions and 340 deletions

View file

@ -26,43 +26,24 @@ class Block {
} }
static fromBuffer(buffer) { static fromBuffer(buffer) {
if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)'); if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)');
let offset = 0; const bufferReader = new bufferutils_1.BufferReader(buffer);
const readSlice = n => {
offset += n;
return buffer.slice(offset - n, offset);
};
const readUInt32 = () => {
const i = buffer.readUInt32LE(offset);
offset += 4;
return i;
};
const readInt32 = () => {
const i = buffer.readInt32LE(offset);
offset += 4;
return i;
};
const block = new Block(); const block = new Block();
block.version = readInt32(); block.version = bufferReader.readInt32();
block.prevHash = readSlice(32); block.prevHash = bufferReader.readSlice(32);
block.merkleRoot = readSlice(32); block.merkleRoot = bufferReader.readSlice(32);
block.timestamp = readUInt32(); block.timestamp = bufferReader.readUInt32();
block.bits = readUInt32(); block.bits = bufferReader.readUInt32();
block.nonce = readUInt32(); block.nonce = bufferReader.readUInt32();
if (buffer.length === 80) return block; if (buffer.length === 80) return block;
const readVarInt = () => {
const vi = varuint.decode(buffer, offset);
offset += varuint.decode.bytes;
return vi;
};
const readTransaction = () => { const readTransaction = () => {
const tx = transaction_1.Transaction.fromBuffer( const tx = transaction_1.Transaction.fromBuffer(
buffer.slice(offset), bufferReader.buffer.slice(bufferReader.offset),
true, true,
); );
offset += tx.byteLength(); bufferReader.offset += tx.byteLength();
return tx; return tx;
}; };
const nTransactions = readVarInt(); const nTransactions = bufferReader.readVarInt();
block.transactions = []; block.transactions = [];
for (let i = 0; i < nTransactions; ++i) { for (let i = 0; i < nTransactions; ++i) {
const tx = readTransaction(); const tx = readTransaction();
@ -154,32 +135,20 @@ class Block {
// TODO: buffer, offset compatibility // TODO: buffer, offset compatibility
toBuffer(headersOnly) { toBuffer(headersOnly) {
const buffer = Buffer.allocUnsafe(this.byteLength(headersOnly)); const buffer = Buffer.allocUnsafe(this.byteLength(headersOnly));
let offset = 0; const bufferWriter = new bufferutils_1.BufferWriter(buffer);
const writeSlice = slice => { bufferWriter.writeInt32(this.version);
slice.copy(buffer, offset); bufferWriter.writeSlice(this.prevHash);
offset += slice.length; bufferWriter.writeSlice(this.merkleRoot);
}; bufferWriter.writeUInt32(this.timestamp);
const writeInt32 = i => { bufferWriter.writeUInt32(this.bits);
buffer.writeInt32LE(i, offset); bufferWriter.writeUInt32(this.nonce);
offset += 4;
};
const writeUInt32 = i => {
buffer.writeUInt32LE(i, offset);
offset += 4;
};
writeInt32(this.version);
writeSlice(this.prevHash);
writeSlice(this.merkleRoot);
writeUInt32(this.timestamp);
writeUInt32(this.bits);
writeUInt32(this.nonce);
if (headersOnly || !this.transactions) return buffer; if (headersOnly || !this.transactions) return buffer;
varuint.encode(this.transactions.length, buffer, offset); varuint.encode(this.transactions.length, buffer, bufferWriter.offset);
offset += varuint.encode.bytes; bufferWriter.offset += varuint.encode.bytes;
this.transactions.forEach(tx => { this.transactions.forEach(tx => {
const txSize = tx.byteLength(); // TODO: extract from toBuffer? const txSize = tx.byteLength(); // TODO: extract from toBuffer?
tx.toBuffer(buffer, offset); tx.toBuffer(buffer, bufferWriter.offset);
offset += txSize; bufferWriter.offset += txSize;
}); });
return buffer; return buffer;
} }

View file

@ -1,231 +0,0 @@
import * as assert from 'assert';
import { describe, it } from 'mocha';
import { BufferWriter } from '../src/bufferutils';
const varuint = require('varuint-bitcoin');
describe('BufferWriter', () => {
function concatToBuffer(values: number[][]): Buffer {
return Buffer.concat(values.map(data => Buffer.from(data)));
}
function testBuffer(
bufferWriter: BufferWriter,
expectedBuffer: Buffer,
expectedOffset: number = expectedBuffer.length,
): void {
assert.strictEqual(bufferWriter.offset, expectedOffset);
assert.deepStrictEqual(
bufferWriter.buffer.slice(0, expectedOffset),
expectedBuffer.slice(0, expectedOffset),
);
}
it('writeUint8', () => {
const values = [0, 1, 254, 255];
const expectedBuffer = Buffer.from([0, 1, 0xfe, 0xff]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((v: number) => {
const expectedOffset = bufferWriter.offset + 1;
bufferWriter.writeUInt8(v);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeInt32', () => {
const values = [
0,
1,
Math.pow(2, 31) - 2,
Math.pow(2, 31) - 1,
-1,
-Math.pow(2, 31),
];
const expectedBuffer = concatToBuffer([
[0, 0, 0, 0],
[1, 0, 0, 0],
[0xfe, 0xff, 0xff, 0x7f],
[0xff, 0xff, 0xff, 0x7f],
[0xff, 0xff, 0xff, 0xff],
[0x00, 0x00, 0x00, 0x80],
]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((value: number) => {
const expectedOffset = bufferWriter.offset + 4;
bufferWriter.writeInt32(value);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeUInt32', () => {
const maxUInt32 = Math.pow(2, 32) - 1;
const values = [0, 1, Math.pow(2, 16), maxUInt32];
const expectedBuffer = concatToBuffer([
[0, 0, 0, 0],
[1, 0, 0, 0],
[0, 0, 1, 0],
[0xff, 0xff, 0xff, 0xff],
]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((value: number) => {
const expectedOffset = bufferWriter.offset + 4;
bufferWriter.writeUInt32(value);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeUInt64', () => {
const values = [
0,
1,
Math.pow(2, 32),
Number.MAX_SAFE_INTEGER /* 2^53 - 1 */,
];
const expectedBuffer = concatToBuffer([
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00],
]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((value: number) => {
const expectedOffset = bufferWriter.offset + 8;
bufferWriter.writeUInt64(value);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeVarInt', () => {
const values = [
0,
1,
252,
253,
254,
255,
256,
Math.pow(2, 16) - 2,
Math.pow(2, 16) - 1,
Math.pow(2, 16),
Math.pow(2, 32) - 2,
Math.pow(2, 32) - 1,
Math.pow(2, 32),
Number.MAX_SAFE_INTEGER,
];
const expectedBuffer = concatToBuffer([
[0x00],
[0x01],
[0xfc],
[0xfd, 0xfd, 0x00],
[0xfd, 0xfe, 0x00],
[0xfd, 0xff, 0x00],
[0xfd, 0x00, 0x01],
[0xfd, 0xfe, 0xff],
[0xfd, 0xff, 0xff],
[0xfe, 0x00, 0x00, 0x01, 0x00],
[0xfe, 0xfe, 0xff, 0xff, 0xff],
[0xfe, 0xff, 0xff, 0xff, 0xff],
[0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00],
[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00],
]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((value: number) => {
const expectedOffset =
bufferWriter.offset + varuint.encodingLength(value);
bufferWriter.writeVarInt(value);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeSlice', () => {
const values = [[], [1], [1, 2, 3, 4], [254, 255]];
const expectedBuffer = concatToBuffer(values);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((v: number[]) => {
const expectedOffset = bufferWriter.offset + v.length;
bufferWriter.writeSlice(Buffer.from(v));
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeVarSlice', () => {
const values = [
Buffer.alloc(1, 1),
Buffer.alloc(252, 2),
Buffer.alloc(253, 3),
];
const expectedBuffer = Buffer.concat([
Buffer.from([0x01, 0x01]),
Buffer.from([0xfc]),
Buffer.alloc(252, 0x02),
Buffer.from([0xfd, 0xfd, 0x00]),
Buffer.alloc(253, 0x03),
]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((value: Buffer) => {
const expectedOffset =
bufferWriter.offset +
varuint.encodingLength(value.length) +
value.length;
bufferWriter.writeVarSlice(value);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeVector', () => {
const values = [
[Buffer.alloc(1, 4), Buffer.alloc(253, 5)],
Array(253).fill(Buffer.alloc(1, 6)),
];
const expectedBuffer = Buffer.concat([
Buffer.from([0x02]),
Buffer.from([0x01, 0x04]),
Buffer.from([0xfd, 0xfd, 0x00]),
Buffer.alloc(253, 5),
Buffer.from([0xfd, 0xfd, 0x00]),
Buffer.concat(
Array(253)
.fill(0)
.map(() => Buffer.from([0x01, 0x06])),
),
]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((value: Buffer[]) => {
const expectedOffset =
bufferWriter.offset +
varuint.encodingLength(value.length) +
value.reduce(
(sum: number, v) => sum + varuint.encodingLength(v.length) + v.length,
0,
);
bufferWriter.writeVector(value);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
});

View file

@ -1,10 +1,16 @@
import * as assert from 'assert'; import * as assert from 'assert';
import { describe, it } from 'mocha'; import { describe, it } from 'mocha';
import * as bufferutils from '../src/bufferutils'; import * as bufferutils from '../src/bufferutils';
import { BufferReader, BufferWriter } from '../src/bufferutils';
import * as fixtures from './fixtures/bufferutils.json'; import * as fixtures from './fixtures/bufferutils.json';
const varuint = require('varuint-bitcoin');
describe('bufferutils', () => { describe('bufferutils', () => {
function concatToBuffer(values: number[][]): Buffer {
return Buffer.concat(values.map(data => Buffer.from(data)));
}
describe('readUInt64LE', () => { describe('readUInt64LE', () => {
fixtures.valid.forEach(f => { fixtures.valid.forEach(f => {
it('decodes ' + f.hex, () => { it('decodes ' + f.hex, () => {
@ -46,4 +52,438 @@ describe('bufferutils', () => {
}); });
}); });
}); });
describe('BufferWriter', () => {
function testBuffer(
bufferWriter: BufferWriter,
expectedBuffer: Buffer,
expectedOffset: number = expectedBuffer.length,
): void {
assert.strictEqual(bufferWriter.offset, expectedOffset);
assert.deepStrictEqual(
bufferWriter.buffer.slice(0, expectedOffset),
expectedBuffer.slice(0, expectedOffset),
);
}
it('writeUint8', () => {
const values = [0, 1, 254, 255];
const expectedBuffer = Buffer.from([0, 1, 0xfe, 0xff]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((v: number) => {
const expectedOffset = bufferWriter.offset + 1;
bufferWriter.writeUInt8(v);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeInt32', () => {
const values = [
0,
1,
Math.pow(2, 31) - 2,
Math.pow(2, 31) - 1,
-1,
-Math.pow(2, 31),
];
const expectedBuffer = concatToBuffer([
[0, 0, 0, 0],
[1, 0, 0, 0],
[0xfe, 0xff, 0xff, 0x7f],
[0xff, 0xff, 0xff, 0x7f],
[0xff, 0xff, 0xff, 0xff],
[0x00, 0x00, 0x00, 0x80],
]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((value: number) => {
const expectedOffset = bufferWriter.offset + 4;
bufferWriter.writeInt32(value);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeUInt32', () => {
const maxUInt32 = Math.pow(2, 32) - 1;
const values = [0, 1, Math.pow(2, 16), maxUInt32];
const expectedBuffer = concatToBuffer([
[0, 0, 0, 0],
[1, 0, 0, 0],
[0, 0, 1, 0],
[0xff, 0xff, 0xff, 0xff],
]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((value: number) => {
const expectedOffset = bufferWriter.offset + 4;
bufferWriter.writeUInt32(value);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeUInt64', () => {
const values = [
0,
1,
Math.pow(2, 32),
Number.MAX_SAFE_INTEGER /* 2^53 - 1 */,
];
const expectedBuffer = concatToBuffer([
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00],
]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((value: number) => {
const expectedOffset = bufferWriter.offset + 8;
bufferWriter.writeUInt64(value);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeVarInt', () => {
const values = [
0,
1,
252,
253,
254,
255,
256,
Math.pow(2, 16) - 2,
Math.pow(2, 16) - 1,
Math.pow(2, 16),
Math.pow(2, 32) - 2,
Math.pow(2, 32) - 1,
Math.pow(2, 32),
Number.MAX_SAFE_INTEGER,
];
const expectedBuffer = concatToBuffer([
[0x00],
[0x01],
[0xfc],
[0xfd, 0xfd, 0x00],
[0xfd, 0xfe, 0x00],
[0xfd, 0xff, 0x00],
[0xfd, 0x00, 0x01],
[0xfd, 0xfe, 0xff],
[0xfd, 0xff, 0xff],
[0xfe, 0x00, 0x00, 0x01, 0x00],
[0xfe, 0xfe, 0xff, 0xff, 0xff],
[0xfe, 0xff, 0xff, 0xff, 0xff],
[0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00],
[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00],
]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((value: number) => {
const expectedOffset =
bufferWriter.offset + varuint.encodingLength(value);
bufferWriter.writeVarInt(value);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeSlice', () => {
const values = [[], [1], [1, 2, 3, 4], [254, 255]];
const expectedBuffer = concatToBuffer(values);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((v: number[]) => {
const expectedOffset = bufferWriter.offset + v.length;
bufferWriter.writeSlice(Buffer.from(v));
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeVarSlice', () => {
const values = [
Buffer.alloc(1, 1),
Buffer.alloc(252, 2),
Buffer.alloc(253, 3),
];
const expectedBuffer = Buffer.concat([
Buffer.from([0x01, 0x01]),
Buffer.from([0xfc]),
Buffer.alloc(252, 0x02),
Buffer.from([0xfd, 0xfd, 0x00]),
Buffer.alloc(253, 0x03),
]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((value: Buffer) => {
const expectedOffset =
bufferWriter.offset +
varuint.encodingLength(value.length) +
value.length;
bufferWriter.writeVarSlice(value);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
it('writeVector', () => {
const values = [
[Buffer.alloc(1, 4), Buffer.alloc(253, 5)],
Array(253).fill(Buffer.alloc(1, 6)),
];
const expectedBuffer = Buffer.concat([
Buffer.from([0x02]),
Buffer.from([0x01, 0x04]),
Buffer.from([0xfd, 0xfd, 0x00]),
Buffer.alloc(253, 5),
Buffer.from([0xfd, 0xfd, 0x00]),
Buffer.concat(
Array(253)
.fill(0)
.map(() => Buffer.from([0x01, 0x06])),
),
]);
const bufferWriter = new BufferWriter(
Buffer.allocUnsafe(expectedBuffer.length),
);
values.forEach((value: Buffer[]) => {
const expectedOffset =
bufferWriter.offset +
varuint.encodingLength(value.length) +
value.reduce(
(sum: number, v) =>
sum + varuint.encodingLength(v.length) + v.length,
0,
);
bufferWriter.writeVector(value);
testBuffer(bufferWriter, expectedBuffer, expectedOffset);
});
testBuffer(bufferWriter, expectedBuffer);
});
});
describe('BufferReader', () => {
function testValue(
bufferReader: BufferReader,
value: Buffer | number,
expectedValue: Buffer | number,
expectedOffset: number = Buffer.isBuffer(expectedValue)
? expectedValue.length
: 0,
): void {
assert.strictEqual(bufferReader.offset, expectedOffset);
if (Buffer.isBuffer(expectedValue)) {
assert.deepStrictEqual(
(value as Buffer).slice(0, expectedOffset),
expectedValue.slice(0, expectedOffset),
);
} else {
assert.strictEqual(value as number, expectedValue);
}
}
it('readUint8', () => {
const values = [0, 1, 0xfe, 0xff];
const buffer = Buffer.from([0, 1, 0xfe, 0xff]);
const bufferReader = new BufferReader(buffer);
values.forEach((v: number) => {
const expectedOffset = bufferReader.offset + 1;
const val = bufferReader.readUInt8();
testValue(bufferReader, val, v, expectedOffset);
});
});
it('readInt32', () => {
const values = [
0,
1,
Math.pow(2, 31) - 2,
Math.pow(2, 31) - 1,
-1,
-Math.pow(2, 31),
];
const buffer = concatToBuffer([
[0, 0, 0, 0],
[1, 0, 0, 0],
[0xfe, 0xff, 0xff, 0x7f],
[0xff, 0xff, 0xff, 0x7f],
[0xff, 0xff, 0xff, 0xff],
[0x00, 0x00, 0x00, 0x80],
]);
const bufferReader = new BufferReader(buffer);
values.forEach((value: number) => {
const expectedOffset = bufferReader.offset + 4;
const val = bufferReader.readInt32();
testValue(bufferReader, val, value, expectedOffset);
});
});
it('readUInt32', () => {
const maxUInt32 = Math.pow(2, 32) - 1;
const values = [0, 1, Math.pow(2, 16), maxUInt32];
const buffer = concatToBuffer([
[0, 0, 0, 0],
[1, 0, 0, 0],
[0, 0, 1, 0],
[0xff, 0xff, 0xff, 0xff],
]);
const bufferReader = new BufferReader(buffer);
values.forEach((value: number) => {
const expectedOffset = bufferReader.offset + 4;
const val = bufferReader.readUInt32();
testValue(bufferReader, val, value, expectedOffset);
});
});
it('readUInt64', () => {
const values = [
0,
1,
Math.pow(2, 32),
Number.MAX_SAFE_INTEGER /* 2^53 - 1 */,
];
const buffer = concatToBuffer([
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00],
]);
const bufferReader = new BufferReader(buffer);
values.forEach((value: number) => {
const expectedOffset = bufferReader.offset + 8;
const val = bufferReader.readUInt64();
testValue(bufferReader, val, value, expectedOffset);
});
});
it('readVarInt', () => {
const values = [
0,
1,
252,
253,
254,
255,
256,
Math.pow(2, 16) - 2,
Math.pow(2, 16) - 1,
Math.pow(2, 16),
Math.pow(2, 32) - 2,
Math.pow(2, 32) - 1,
Math.pow(2, 32),
Number.MAX_SAFE_INTEGER,
];
const buffer = concatToBuffer([
[0x00],
[0x01],
[0xfc],
[0xfd, 0xfd, 0x00],
[0xfd, 0xfe, 0x00],
[0xfd, 0xff, 0x00],
[0xfd, 0x00, 0x01],
[0xfd, 0xfe, 0xff],
[0xfd, 0xff, 0xff],
[0xfe, 0x00, 0x00, 0x01, 0x00],
[0xfe, 0xfe, 0xff, 0xff, 0xff],
[0xfe, 0xff, 0xff, 0xff, 0xff],
[0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00],
[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00],
]);
const bufferReader = new BufferReader(buffer);
values.forEach((value: number) => {
const expectedOffset =
bufferReader.offset + varuint.encodingLength(value);
const val = bufferReader.readVarInt();
testValue(bufferReader, val, value, expectedOffset);
});
});
it('readSlice', () => {
const values = [[1], [1, 2, 3, 4], [254, 255]];
const buffer = concatToBuffer(values);
const bufferReader = new BufferReader(buffer);
values.forEach((v: number[]) => {
const expectedOffset = bufferReader.offset + v.length;
const val = bufferReader.readSlice(v.length);
testValue(bufferReader, val, Buffer.from(v), expectedOffset);
});
});
it('readVarSlice', () => {
const values = [
Buffer.alloc(1, 1),
Buffer.alloc(252, 2),
Buffer.alloc(253, 3),
];
const buffer = Buffer.concat([
Buffer.from([0x01, 0x01]),
Buffer.from([0xfc]),
Buffer.alloc(252, 0x02),
Buffer.from([0xfd, 0xfd, 0x00]),
Buffer.alloc(253, 0x03),
]);
const bufferReader = new BufferReader(buffer);
values.forEach((value: Buffer) => {
const expectedOffset =
bufferReader.offset +
varuint.encodingLength(value.length) +
value.length;
const val = bufferReader.readVarSlice();
testValue(bufferReader, val, value, expectedOffset);
});
});
it('readVector', () => {
const values = [
[Buffer.alloc(1, 4), Buffer.alloc(253, 5)],
Array(253).fill(Buffer.alloc(1, 6)),
];
const buffer = Buffer.concat([
Buffer.from([0x02]),
Buffer.from([0x01, 0x04]),
Buffer.from([0xfd, 0xfd, 0x00]),
Buffer.alloc(253, 5),
Buffer.from([0xfd, 0xfd, 0x00]),
Buffer.concat(
Array(253)
.fill(0)
.map(() => Buffer.from([0x01, 0x06])),
),
]);
const bufferReader = new BufferReader(buffer);
values.forEach((value: Buffer[]) => {
const expectedOffset =
bufferReader.offset +
varuint.encodingLength(value.length) +
value.reduce(
(sum: number, v) =>
sum + varuint.encodingLength(v.length) + v.length,
0,
);
const val = bufferReader.readVector();
testValue(
bufferReader,
Buffer.concat(val),
Buffer.concat(value),
expectedOffset,
);
});
});
});
}); });

View file

@ -1,4 +1,4 @@
import { reverseBuffer } from './bufferutils'; import { BufferReader, BufferWriter, reverseBuffer } from './bufferutils';
import * as bcrypto from './crypto'; import * as bcrypto from './crypto';
import { Transaction } from './transaction'; import { Transaction } from './transaction';
import * as types from './types'; import * as types from './types';
@ -18,47 +18,28 @@ export class Block {
static fromBuffer(buffer: Buffer): Block { static fromBuffer(buffer: Buffer): Block {
if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)'); if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)');
let offset: number = 0; const bufferReader = new BufferReader(buffer);
const readSlice = (n: number): Buffer => {
offset += n;
return buffer.slice(offset - n, offset);
};
const readUInt32 = (): number => {
const i = buffer.readUInt32LE(offset);
offset += 4;
return i;
};
const readInt32 = (): number => {
const i = buffer.readInt32LE(offset);
offset += 4;
return i;
};
const block = new Block(); const block = new Block();
block.version = readInt32(); block.version = bufferReader.readInt32();
block.prevHash = readSlice(32); block.prevHash = bufferReader.readSlice(32);
block.merkleRoot = readSlice(32); block.merkleRoot = bufferReader.readSlice(32);
block.timestamp = readUInt32(); block.timestamp = bufferReader.readUInt32();
block.bits = readUInt32(); block.bits = bufferReader.readUInt32();
block.nonce = readUInt32(); block.nonce = bufferReader.readUInt32();
if (buffer.length === 80) return block; if (buffer.length === 80) return block;
const readVarInt = (): number => {
const vi = varuint.decode(buffer, offset);
offset += varuint.decode.bytes;
return vi;
};
const readTransaction = (): any => { const readTransaction = (): any => {
const tx = Transaction.fromBuffer(buffer.slice(offset), true); const tx = Transaction.fromBuffer(
offset += tx.byteLength(); bufferReader.buffer.slice(bufferReader.offset),
true,
);
bufferReader.offset += tx.byteLength();
return tx; return tx;
}; };
const nTransactions = readVarInt(); const nTransactions = bufferReader.readVarInt();
block.transactions = []; block.transactions = [];
for (let i = 0; i < nTransactions; ++i) { for (let i = 0; i < nTransactions; ++i) {
@ -183,37 +164,24 @@ export class Block {
toBuffer(headersOnly?: boolean): Buffer { toBuffer(headersOnly?: boolean): Buffer {
const buffer: Buffer = Buffer.allocUnsafe(this.byteLength(headersOnly)); const buffer: Buffer = Buffer.allocUnsafe(this.byteLength(headersOnly));
let offset: number = 0; const bufferWriter = new BufferWriter(buffer);
const writeSlice = (slice: Buffer): void => {
slice.copy(buffer, offset);
offset += slice.length;
};
const writeInt32 = (i: number): void => { bufferWriter.writeInt32(this.version);
buffer.writeInt32LE(i, offset); bufferWriter.writeSlice(this.prevHash!);
offset += 4; bufferWriter.writeSlice(this.merkleRoot!);
}; bufferWriter.writeUInt32(this.timestamp);
const writeUInt32 = (i: number): void => { bufferWriter.writeUInt32(this.bits);
buffer.writeUInt32LE(i, offset); bufferWriter.writeUInt32(this.nonce);
offset += 4;
};
writeInt32(this.version);
writeSlice(this.prevHash!);
writeSlice(this.merkleRoot!);
writeUInt32(this.timestamp);
writeUInt32(this.bits);
writeUInt32(this.nonce);
if (headersOnly || !this.transactions) return buffer; if (headersOnly || !this.transactions) return buffer;
varuint.encode(this.transactions.length, buffer, offset); varuint.encode(this.transactions.length, buffer, bufferWriter.offset);
offset += varuint.encode.bytes; bufferWriter.offset += varuint.encode.bytes;
this.transactions.forEach(tx => { this.transactions.forEach(tx => {
const txSize = tx.byteLength(); // TODO: extract from toBuffer? const txSize = tx.byteLength(); // TODO: extract from toBuffer?
tx.toBuffer(buffer, offset); tx.toBuffer(buffer, bufferWriter.offset);
offset += txSize; bufferWriter.offset += txSize;
}); });
return buffer; return buffer;