Add BufferReader tests
This commit is contained in:
parent
06674b19fe
commit
c8fdfae955
4 changed files with 486 additions and 340 deletions
src
73
src/block.js
73
src/block.js
|
@ -26,43 +26,24 @@ class Block {
|
|||
}
|
||||
static fromBuffer(buffer) {
|
||||
if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)');
|
||||
let offset = 0;
|
||||
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 bufferReader = new bufferutils_1.BufferReader(buffer);
|
||||
const block = new Block();
|
||||
block.version = readInt32();
|
||||
block.prevHash = readSlice(32);
|
||||
block.merkleRoot = readSlice(32);
|
||||
block.timestamp = readUInt32();
|
||||
block.bits = readUInt32();
|
||||
block.nonce = readUInt32();
|
||||
block.version = bufferReader.readInt32();
|
||||
block.prevHash = bufferReader.readSlice(32);
|
||||
block.merkleRoot = bufferReader.readSlice(32);
|
||||
block.timestamp = bufferReader.readUInt32();
|
||||
block.bits = bufferReader.readUInt32();
|
||||
block.nonce = bufferReader.readUInt32();
|
||||
if (buffer.length === 80) return block;
|
||||
const readVarInt = () => {
|
||||
const vi = varuint.decode(buffer, offset);
|
||||
offset += varuint.decode.bytes;
|
||||
return vi;
|
||||
};
|
||||
const readTransaction = () => {
|
||||
const tx = transaction_1.Transaction.fromBuffer(
|
||||
buffer.slice(offset),
|
||||
bufferReader.buffer.slice(bufferReader.offset),
|
||||
true,
|
||||
);
|
||||
offset += tx.byteLength();
|
||||
bufferReader.offset += tx.byteLength();
|
||||
return tx;
|
||||
};
|
||||
const nTransactions = readVarInt();
|
||||
const nTransactions = bufferReader.readVarInt();
|
||||
block.transactions = [];
|
||||
for (let i = 0; i < nTransactions; ++i) {
|
||||
const tx = readTransaction();
|
||||
|
@ -154,32 +135,20 @@ class Block {
|
|||
// TODO: buffer, offset compatibility
|
||||
toBuffer(headersOnly) {
|
||||
const buffer = Buffer.allocUnsafe(this.byteLength(headersOnly));
|
||||
let offset = 0;
|
||||
const writeSlice = slice => {
|
||||
slice.copy(buffer, offset);
|
||||
offset += slice.length;
|
||||
};
|
||||
const writeInt32 = i => {
|
||||
buffer.writeInt32LE(i, offset);
|
||||
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);
|
||||
const bufferWriter = new bufferutils_1.BufferWriter(buffer);
|
||||
bufferWriter.writeInt32(this.version);
|
||||
bufferWriter.writeSlice(this.prevHash);
|
||||
bufferWriter.writeSlice(this.merkleRoot);
|
||||
bufferWriter.writeUInt32(this.timestamp);
|
||||
bufferWriter.writeUInt32(this.bits);
|
||||
bufferWriter.writeUInt32(this.nonce);
|
||||
if (headersOnly || !this.transactions) return buffer;
|
||||
varuint.encode(this.transactions.length, buffer, offset);
|
||||
offset += varuint.encode.bytes;
|
||||
varuint.encode(this.transactions.length, buffer, bufferWriter.offset);
|
||||
bufferWriter.offset += varuint.encode.bytes;
|
||||
this.transactions.forEach(tx => {
|
||||
const txSize = tx.byteLength(); // TODO: extract from toBuffer?
|
||||
tx.toBuffer(buffer, offset);
|
||||
offset += txSize;
|
||||
tx.toBuffer(buffer, bufferWriter.offset);
|
||||
bufferWriter.offset += txSize;
|
||||
});
|
||||
return buffer;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue