diff --git a/lib/primitives/tx.js b/lib/primitives/tx.js index c0841175..88628b2d 100644 --- a/lib/primitives/tx.js +++ b/lib/primitives/tx.js @@ -2442,12 +2442,12 @@ TX.prototype.writeWitness = function writeWitness(bw) { for (const output of this.outputs) output.toWriter(bw); - let witness = bw.written; + const start = bw.offset; for (const input of this.inputs) input.witness.toWriter(bw); - witness = bw.written - witness; + const witness = bw.offset - start; bw.writeU32(this.locktime); diff --git a/lib/script/script.js b/lib/script/script.js index e36340e1..434fc4f5 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -3426,7 +3426,7 @@ Script.verifyMast = function verifyMast(program, stack, output, flags, tx, index for (let j = 0; j < subscripts; j++) { const script = stack.get(-(4 + j)); if (version === 0) { - if ((scripts.written + script.length) > consensus.MAX_SCRIPT_SIZE) + if ((scripts.offset + script.length) > consensus.MAX_SCRIPT_SIZE) throw new ScriptError('SCRIPT_SIZE'); } scriptRoot.writeBytes(digest.hash256(script)); diff --git a/lib/utils/reader.js b/lib/utils/reader.js index ee8a7d5e..4323c17b 100644 --- a/lib/utils/reader.js +++ b/lib/utils/reader.js @@ -10,6 +10,7 @@ const assert = require('assert'); const encoding = require('./encoding'); const digest = require('../crypto/digest'); +const EMPTY_BUFFER = Buffer.alloc(0); /** * An object that allows reading of buffers in a sane manner. @@ -144,9 +145,9 @@ BufferReader.prototype.endData = function endData(zeroCopy) { */ BufferReader.prototype.destroy = function destroy() { - this.offset = null; - this.stack = null; - this.data = null; + this.data = EMPTY_BUFFER; + this.offset = 0; + this.stack.length = 0; }; /** diff --git a/lib/utils/staticwriter.js b/lib/utils/staticwriter.js index 1c0c53d0..c90ad799 100644 --- a/lib/utils/staticwriter.js +++ b/lib/utils/staticwriter.js @@ -9,6 +9,7 @@ const assert = require('assert'); const encoding = require('./encoding'); const digest = require('../crypto/digest'); +const EMPTY_BUFFER = Buffer.alloc(0); /** * Statically allocated buffer writer. @@ -22,7 +23,7 @@ function StaticWriter(size) { return new StaticWriter(size); this.data = Buffer.allocUnsafe(size); - this.written = 0; + this.offset = 0; } /** @@ -34,7 +35,7 @@ function StaticWriter(size) { StaticWriter.prototype.render = function render(keep) { const data = this.data; - assert(this.written === data.length); + assert(this.offset === data.length); if (!keep) this.destroy(); @@ -48,7 +49,7 @@ StaticWriter.prototype.render = function render(keep) { */ StaticWriter.prototype.getSize = function getSize() { - return this.written; + return this.offset; }; /** @@ -57,7 +58,7 @@ StaticWriter.prototype.getSize = function getSize() { */ StaticWriter.prototype.seek = function seek(offset) { - this.written += offset; + this.offset += offset; }; /** @@ -65,8 +66,8 @@ StaticWriter.prototype.seek = function seek(offset) { */ StaticWriter.prototype.destroy = function destroy() { - this.data = null; - this.written = null; + this.data = EMPTY_BUFFER; + this.offset = 0; }; /** @@ -75,7 +76,7 @@ StaticWriter.prototype.destroy = function destroy() { */ StaticWriter.prototype.writeU8 = function writeU8(value) { - this.written = this.data.writeUInt8(value, this.written, true); + this.offset = this.data.writeUInt8(value, this.offset, true); }; /** @@ -84,7 +85,7 @@ StaticWriter.prototype.writeU8 = function writeU8(value) { */ StaticWriter.prototype.writeU16 = function writeU16(value) { - this.written = this.data.writeUInt16LE(value, this.written, true); + this.offset = this.data.writeUInt16LE(value, this.offset, true); }; /** @@ -93,7 +94,7 @@ StaticWriter.prototype.writeU16 = function writeU16(value) { */ StaticWriter.prototype.writeU16BE = function writeU16BE(value) { - this.written = this.data.writeUInt16BE(value, this.written, true); + this.offset = this.data.writeUInt16BE(value, this.offset, true); }; /** @@ -102,7 +103,7 @@ StaticWriter.prototype.writeU16BE = function writeU16BE(value) { */ StaticWriter.prototype.writeU32 = function writeU32(value) { - this.written = this.data.writeUInt32LE(value, this.written, true); + this.offset = this.data.writeUInt32LE(value, this.offset, true); }; /** @@ -111,7 +112,7 @@ StaticWriter.prototype.writeU32 = function writeU32(value) { */ StaticWriter.prototype.writeU32BE = function writeU32BE(value) { - this.written = this.data.writeUInt32BE(value, this.written, true); + this.offset = this.data.writeUInt32BE(value, this.offset, true); }; /** @@ -120,7 +121,7 @@ StaticWriter.prototype.writeU32BE = function writeU32BE(value) { */ StaticWriter.prototype.writeU64 = function writeU64(value) { - this.written = encoding.writeU64(this.data, value, this.written); + this.offset = encoding.writeU64(this.data, value, this.offset); }; /** @@ -129,7 +130,7 @@ StaticWriter.prototype.writeU64 = function writeU64(value) { */ StaticWriter.prototype.writeU64BE = function writeU64BE(value) { - this.written = encoding.writeU64BE(this.data, value, this.written); + this.offset = encoding.writeU64BE(this.data, value, this.offset); }; /** @@ -138,7 +139,7 @@ StaticWriter.prototype.writeU64BE = function writeU64BE(value) { */ StaticWriter.prototype.writeU64N = function writeU64N(value) { - this.written = encoding.writeU64N(this.data, value, this.written); + this.offset = encoding.writeU64N(this.data, value, this.offset); }; /** @@ -147,7 +148,7 @@ StaticWriter.prototype.writeU64N = function writeU64N(value) { */ StaticWriter.prototype.writeU64BEN = function writeU64BEN(value) { - this.written = encoding.writeU64BEN(this.data, value, this.written); + this.offset = encoding.writeU64BEN(this.data, value, this.offset); }; /** @@ -156,7 +157,7 @@ StaticWriter.prototype.writeU64BEN = function writeU64BEN(value) { */ StaticWriter.prototype.writeI8 = function writeI8(value) { - this.written = this.data.writeInt8(value, this.written, true); + this.offset = this.data.writeInt8(value, this.offset, true); }; /** @@ -165,7 +166,7 @@ StaticWriter.prototype.writeI8 = function writeI8(value) { */ StaticWriter.prototype.writeI16 = function writeI16(value) { - this.written = this.data.writeInt16LE(value, this.written, true); + this.offset = this.data.writeInt16LE(value, this.offset, true); }; /** @@ -174,7 +175,7 @@ StaticWriter.prototype.writeI16 = function writeI16(value) { */ StaticWriter.prototype.writeI16BE = function writeI16BE(value) { - this.written = this.data.writeInt16BE(value, this.written, true); + this.offset = this.data.writeInt16BE(value, this.offset, true); }; /** @@ -183,7 +184,7 @@ StaticWriter.prototype.writeI16BE = function writeI16BE(value) { */ StaticWriter.prototype.writeI32 = function writeI32(value) { - this.written = this.data.writeInt32LE(value, this.written, true); + this.offset = this.data.writeInt32LE(value, this.offset, true); }; /** @@ -192,7 +193,7 @@ StaticWriter.prototype.writeI32 = function writeI32(value) { */ StaticWriter.prototype.writeI32BE = function writeI32BE(value) { - this.written = this.data.writeInt32BE(value, this.written, true); + this.offset = this.data.writeInt32BE(value, this.offset, true); }; /** @@ -201,7 +202,7 @@ StaticWriter.prototype.writeI32BE = function writeI32BE(value) { */ StaticWriter.prototype.writeI64 = function writeI64(value) { - this.written = encoding.writeI64(this.data, value, this.written); + this.offset = encoding.writeI64(this.data, value, this.offset); }; /** @@ -210,7 +211,7 @@ StaticWriter.prototype.writeI64 = function writeI64(value) { */ StaticWriter.prototype.writeI64BE = function writeI64BE(value) { - this.written = encoding.writeI64BE(this.data, value, this.written); + this.offset = encoding.writeI64BE(this.data, value, this.offset); }; /** @@ -219,7 +220,7 @@ StaticWriter.prototype.writeI64BE = function writeI64BE(value) { */ StaticWriter.prototype.writeI64N = function writeI64N(value) { - this.written = encoding.writeI64N(this.data, value, this.written); + this.offset = encoding.writeI64N(this.data, value, this.offset); }; /** @@ -228,7 +229,7 @@ StaticWriter.prototype.writeI64N = function writeI64N(value) { */ StaticWriter.prototype.writeI64BEN = function writeI64BEN(value) { - this.written = encoding.writeI64BEN(this.data, value, this.written); + this.offset = encoding.writeI64BEN(this.data, value, this.offset); }; /** @@ -237,7 +238,7 @@ StaticWriter.prototype.writeI64BEN = function writeI64BEN(value) { */ StaticWriter.prototype.writeFloat = function writeFloat(value) { - this.written = this.data.writeFloatLE(value, this.written, true); + this.offset = this.data.writeFloatLE(value, this.offset, true); }; /** @@ -246,7 +247,7 @@ StaticWriter.prototype.writeFloat = function writeFloat(value) { */ StaticWriter.prototype.writeFloatBE = function writeFloatBE(value) { - this.written = this.data.writeFloatBE(value, this.written, true); + this.offset = this.data.writeFloatBE(value, this.offset, true); }; /** @@ -255,7 +256,7 @@ StaticWriter.prototype.writeFloatBE = function writeFloatBE(value) { */ StaticWriter.prototype.writeDouble = function writeDouble(value) { - this.written = this.data.writeDoubleLE(value, this.written, true); + this.offset = this.data.writeDoubleLE(value, this.offset, true); }; /** @@ -264,7 +265,7 @@ StaticWriter.prototype.writeDouble = function writeDouble(value) { */ StaticWriter.prototype.writeDoubleBE = function writeDoubleBE(value) { - this.written = this.data.writeDoubleBE(value, this.written, true); + this.offset = this.data.writeDoubleBE(value, this.offset, true); }; /** @@ -273,7 +274,7 @@ StaticWriter.prototype.writeDoubleBE = function writeDoubleBE(value) { */ StaticWriter.prototype.writeVarint = function writeVarint(value) { - this.written = encoding.writeVarint(this.data, value, this.written); + this.offset = encoding.writeVarint(this.data, value, this.offset); }; /** @@ -282,7 +283,7 @@ StaticWriter.prototype.writeVarint = function writeVarint(value) { */ StaticWriter.prototype.writeVarintN = function writeVarintN(value) { - this.written = encoding.writeVarintN(this.data, value, this.written); + this.offset = encoding.writeVarintN(this.data, value, this.offset); }; /** @@ -291,7 +292,7 @@ StaticWriter.prototype.writeVarintN = function writeVarintN(value) { */ StaticWriter.prototype.writeVarint2 = function writeVarint2(value) { - this.written = encoding.writeVarint2(this.data, value, this.written); + this.offset = encoding.writeVarint2(this.data, value, this.offset); }; /** @@ -300,7 +301,7 @@ StaticWriter.prototype.writeVarint2 = function writeVarint2(value) { */ StaticWriter.prototype.writeVarint2N = function writeVarint2N(value) { - this.written = encoding.writeVarint2N(this.data, value, this.written); + this.offset = encoding.writeVarint2N(this.data, value, this.offset); }; /** @@ -312,9 +313,9 @@ StaticWriter.prototype.writeBytes = function writeBytes(value) { if (value.length === 0) return; - value.copy(this.data, this.written); + value.copy(this.data, this.offset); - this.written += value.length; + this.offset += value.length; }; /** @@ -340,8 +341,8 @@ StaticWriter.prototype.copy = function copy(value, start, end) { if (len === 0) return; - value.copy(this.data, this.written, start, end); - this.written += len; + value.copy(this.data, this.offset, start, end); + this.offset += len; }; /** @@ -356,9 +357,9 @@ StaticWriter.prototype.writeString = function writeString(value, enc) { const size = Buffer.byteLength(value, enc); - this.data.write(value, this.written, enc); + this.data.write(value, this.offset, enc); - this.written += size; + this.offset += size; }; /** @@ -373,8 +374,8 @@ StaticWriter.prototype.writeHash = function writeHash(value) { return; } assert(value.length === 64); - this.data.write(value, this.written, 'hex'); - this.written += 32; + this.data.write(value, this.offset, 'hex'); + this.offset += 32; }; /** @@ -392,9 +393,9 @@ StaticWriter.prototype.writeVarString = function writeVarString(value, enc) { const size = Buffer.byteLength(value, enc); this.writeVarint(size); - this.data.write(value, this.written, enc); + this.data.write(value, this.offset, enc); - this.written += size; + this.offset += size; }; /** @@ -413,10 +414,10 @@ StaticWriter.prototype.writeNullString = function writeNullString(value, enc) { */ StaticWriter.prototype.writeChecksum = function writeChecksum() { - const data = this.data.slice(0, this.written); + const data = this.data.slice(0, this.offset); const hash = digest.hash256(data); - hash.copy(this.data, this.written, 0, 4); - this.written += 4; + hash.copy(this.data, this.offset, 0, 4); + this.offset += 4; }; /** @@ -431,8 +432,8 @@ StaticWriter.prototype.fill = function fill(value, size) { if (size === 0) return; - this.data.fill(value, this.written, this.written + size); - this.written += size; + this.data.fill(value, this.offset, this.offset + size); + this.offset += size; }; /* diff --git a/lib/utils/writer.js b/lib/utils/writer.js index 3047a53e..96da456e 100644 --- a/lib/utils/writer.js +++ b/lib/utils/writer.js @@ -66,7 +66,7 @@ function BufferWriter() { return new BufferWriter(); this.ops = []; - this.written = 0; + this.offset = 0; } /** @@ -76,7 +76,7 @@ function BufferWriter() { */ BufferWriter.prototype.render = function render(keep) { - const data = Buffer.allocUnsafe(this.written); + const data = Buffer.allocUnsafe(this.offset); let off = 0; for (const op of this.ops) { @@ -195,7 +195,7 @@ BufferWriter.prototype.render = function render(keep) { */ BufferWriter.prototype.getSize = function getSize() { - return this.written; + return this.offset; }; /** @@ -204,7 +204,7 @@ BufferWriter.prototype.getSize = function getSize() { */ BufferWriter.prototype.seek = function seek(offset) { - this.written += offset; + this.offset += offset; this.ops.push(new WriteOp(SEEK, offset)); }; @@ -214,8 +214,7 @@ BufferWriter.prototype.seek = function seek(offset) { BufferWriter.prototype.destroy = function destroy() { this.ops.length = 0; - this.ops = null; - this.written = null; + this.offset = 0; }; /** @@ -224,7 +223,7 @@ BufferWriter.prototype.destroy = function destroy() { */ BufferWriter.prototype.writeU8 = function writeU8(value) { - this.written += 1; + this.offset += 1; this.ops.push(new WriteOp(UI8, value)); }; @@ -234,7 +233,7 @@ BufferWriter.prototype.writeU8 = function writeU8(value) { */ BufferWriter.prototype.writeU16 = function writeU16(value) { - this.written += 2; + this.offset += 2; this.ops.push(new WriteOp(UI16, value)); }; @@ -244,7 +243,7 @@ BufferWriter.prototype.writeU16 = function writeU16(value) { */ BufferWriter.prototype.writeU16BE = function writeU16BE(value) { - this.written += 2; + this.offset += 2; this.ops.push(new WriteOp(UI16BE, value)); }; @@ -254,7 +253,7 @@ BufferWriter.prototype.writeU16BE = function writeU16BE(value) { */ BufferWriter.prototype.writeU32 = function writeU32(value) { - this.written += 4; + this.offset += 4; this.ops.push(new WriteOp(UI32, value)); }; @@ -264,7 +263,7 @@ BufferWriter.prototype.writeU32 = function writeU32(value) { */ BufferWriter.prototype.writeU32BE = function writeU32BE(value) { - this.written += 4; + this.offset += 4; this.ops.push(new WriteOp(UI32BE, value)); }; @@ -274,7 +273,7 @@ BufferWriter.prototype.writeU32BE = function writeU32BE(value) { */ BufferWriter.prototype.writeU64 = function writeU64(value) { - this.written += 8; + this.offset += 8; this.ops.push(new WriteOp(UI64, value)); }; @@ -284,7 +283,7 @@ BufferWriter.prototype.writeU64 = function writeU64(value) { */ BufferWriter.prototype.writeU64BE = function writeU64BE(value) { - this.written += 8; + this.offset += 8; this.ops.push(new WriteOp(UI64BE, value)); }; @@ -294,7 +293,7 @@ BufferWriter.prototype.writeU64BE = function writeU64BE(value) { */ BufferWriter.prototype.writeU64N = function writeU64N(value) { - this.written += 8; + this.offset += 8; this.ops.push(new WriteOp(UI64N, value)); }; @@ -304,7 +303,7 @@ BufferWriter.prototype.writeU64N = function writeU64N(value) { */ BufferWriter.prototype.writeU64BEN = function writeU64BEN(value) { - this.written += 8; + this.offset += 8; this.ops.push(new WriteOp(UI64BEN, value)); }; @@ -314,7 +313,7 @@ BufferWriter.prototype.writeU64BEN = function writeU64BEN(value) { */ BufferWriter.prototype.writeI8 = function writeI8(value) { - this.written += 1; + this.offset += 1; this.ops.push(new WriteOp(I8, value)); }; @@ -324,7 +323,7 @@ BufferWriter.prototype.writeI8 = function writeI8(value) { */ BufferWriter.prototype.writeI16 = function writeI16(value) { - this.written += 2; + this.offset += 2; this.ops.push(new WriteOp(I16, value)); }; @@ -334,7 +333,7 @@ BufferWriter.prototype.writeI16 = function writeI16(value) { */ BufferWriter.prototype.writeI16BE = function writeI16BE(value) { - this.written += 2; + this.offset += 2; this.ops.push(new WriteOp(I16BE, value)); }; @@ -344,7 +343,7 @@ BufferWriter.prototype.writeI16BE = function writeI16BE(value) { */ BufferWriter.prototype.writeI32 = function writeI32(value) { - this.written += 4; + this.offset += 4; this.ops.push(new WriteOp(I32, value)); }; @@ -354,7 +353,7 @@ BufferWriter.prototype.writeI32 = function writeI32(value) { */ BufferWriter.prototype.writeI32BE = function writeI32BE(value) { - this.written += 4; + this.offset += 4; this.ops.push(new WriteOp(I32BE, value)); }; @@ -364,7 +363,7 @@ BufferWriter.prototype.writeI32BE = function writeI32BE(value) { */ BufferWriter.prototype.writeI64 = function writeI64(value) { - this.written += 8; + this.offset += 8; this.ops.push(new WriteOp(I64, value)); }; @@ -374,7 +373,7 @@ BufferWriter.prototype.writeI64 = function writeI64(value) { */ BufferWriter.prototype.writeI64BE = function writeI64BE(value) { - this.written += 8; + this.offset += 8; this.ops.push(new WriteOp(I64BE, value)); }; @@ -384,7 +383,7 @@ BufferWriter.prototype.writeI64BE = function writeI64BE(value) { */ BufferWriter.prototype.writeI64N = function writeI64N(value) { - this.written += 8; + this.offset += 8; this.ops.push(new WriteOp(I64N, value)); }; @@ -394,7 +393,7 @@ BufferWriter.prototype.writeI64N = function writeI64N(value) { */ BufferWriter.prototype.writeI64BEN = function writeI64BEN(value) { - this.written += 8; + this.offset += 8; this.ops.push(new WriteOp(I64BEN, value)); }; @@ -404,7 +403,7 @@ BufferWriter.prototype.writeI64BEN = function writeI64BEN(value) { */ BufferWriter.prototype.writeFloat = function writeFloat(value) { - this.written += 4; + this.offset += 4; this.ops.push(new WriteOp(FL, value)); }; @@ -414,7 +413,7 @@ BufferWriter.prototype.writeFloat = function writeFloat(value) { */ BufferWriter.prototype.writeFloatBE = function writeFloatBE(value) { - this.written += 4; + this.offset += 4; this.ops.push(new WriteOp(FLBE, value)); }; @@ -424,7 +423,7 @@ BufferWriter.prototype.writeFloatBE = function writeFloatBE(value) { */ BufferWriter.prototype.writeDouble = function writeDouble(value) { - this.written += 8; + this.offset += 8; this.ops.push(new WriteOp(DBL, value)); }; @@ -434,7 +433,7 @@ BufferWriter.prototype.writeDouble = function writeDouble(value) { */ BufferWriter.prototype.writeDoubleBE = function writeDoubleBE(value) { - this.written += 8; + this.offset += 8; this.ops.push(new WriteOp(DBLBE, value)); }; @@ -444,7 +443,7 @@ BufferWriter.prototype.writeDoubleBE = function writeDoubleBE(value) { */ BufferWriter.prototype.writeVarint = function writeVarint(value) { - this.written += encoding.sizeVarint(value); + this.offset += encoding.sizeVarint(value); this.ops.push(new WriteOp(VARINT, value)); }; @@ -454,7 +453,7 @@ BufferWriter.prototype.writeVarint = function writeVarint(value) { */ BufferWriter.prototype.writeVarintN = function writeVarintN(value) { - this.written += encoding.sizeVarintN(value); + this.offset += encoding.sizeVarintN(value); this.ops.push(new WriteOp(VARINTN, value)); }; @@ -464,7 +463,7 @@ BufferWriter.prototype.writeVarintN = function writeVarintN(value) { */ BufferWriter.prototype.writeVarint2 = function writeVarint2(value) { - this.written += encoding.sizeVarint2(value); + this.offset += encoding.sizeVarint2(value); this.ops.push(new WriteOp(VARINT2, value)); }; @@ -474,7 +473,7 @@ BufferWriter.prototype.writeVarint2 = function writeVarint2(value) { */ BufferWriter.prototype.writeVarint2N = function writeVarint2N(value) { - this.written += encoding.sizeVarint2N(value); + this.offset += encoding.sizeVarint2N(value); this.ops.push(new WriteOp(VARINT2N, value)); }; @@ -487,7 +486,7 @@ BufferWriter.prototype.writeBytes = function writeBytes(value) { if (value.length === 0) return; - this.written += value.length; + this.offset += value.length; this.ops.push(new WriteOp(BYTES, value)); }; @@ -497,13 +496,13 @@ BufferWriter.prototype.writeBytes = function writeBytes(value) { */ BufferWriter.prototype.writeVarBytes = function writeVarBytes(value) { - this.written += encoding.sizeVarint(value.length); + this.offset += encoding.sizeVarint(value.length); this.ops.push(new WriteOp(VARINT, value.length)); if (value.length === 0) return; - this.written += value.length; + this.offset += value.length; this.ops.push(new WriteOp(BYTES, value)); }; @@ -530,7 +529,7 @@ BufferWriter.prototype.writeString = function writeString(value, enc) { if (value.length === 0) return; - this.written += Buffer.byteLength(value, enc); + this.offset += Buffer.byteLength(value, enc); this.ops.push(new WriteOp(STR, value, enc)); }; @@ -563,8 +562,8 @@ BufferWriter.prototype.writeVarString = function writeVarString(value, enc) { const size = Buffer.byteLength(value, enc); - this.written += encoding.sizeVarint(size); - this.written += size; + this.offset += encoding.sizeVarint(size); + this.offset += size; this.ops.push(new WriteOp(VARINT, size)); @@ -587,7 +586,7 @@ BufferWriter.prototype.writeNullString = function writeNullString(value, enc) { */ BufferWriter.prototype.writeChecksum = function writeChecksum() { - this.written += 4; + this.offset += 4; this.ops.push(new WriteOp(CHECKSUM)); }; @@ -603,7 +602,7 @@ BufferWriter.prototype.fill = function fill(value, size) { if (size === 0) return; - this.written += size; + this.offset += size; this.ops.push(new WriteOp(FILL, value, null, size)); }; diff --git a/migrate/coins-old.js b/migrate/coins-old.js index 991cc5a7..6eda0119 100644 --- a/migrate/coins-old.js +++ b/migrate/coins-old.js @@ -257,7 +257,7 @@ Coins.prototype.toRaw = function toRaw() { // allocating a buffer. We mark the spents // after rendering the final buffer. bw.writeVarint(len); - const start = bw.written; + const start = bw.offset; bw.fill(0, len); // Write the compressed outputs.