diff --git a/lib/http/rpc.js b/lib/http/rpc.js index cecf97da..9806cf5d 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -1027,7 +1027,7 @@ RPC.prototype._submitWork = async function _submitWork(data) { if (data.length !== 128) throw new RPCError(errs.INVALID_PARAMETER, 'Invalid work size.'); - header = Headers.fromAbbr(data); + header = Headers.fromHead(data); data = data.slice(0, 80); data = swap32(data); diff --git a/lib/net/bip152.js b/lib/net/bip152.js index 35036baf..94a176f9 100644 --- a/lib/net/bip152.js +++ b/lib/net/bip152.js @@ -257,7 +257,7 @@ CompactBlock.prototype.getSize = function getSize(witness) { */ CompactBlock.prototype.writeRaw = function writeRaw(bw, witness) { - this.writeAbbr(bw); + this.writeHead(bw); bw.writeBytes(this.keyNonce); @@ -397,7 +397,7 @@ CompactBlock.prototype.hasIndex = function hasIndex(index) { */ CompactBlock.prototype.getKey = function getKey() { - let data = Buffer.concat([this.abbr(), this.keyNonce]); + let data = Buffer.concat([this.toHead(), this.keyNonce]); let hash = digest.sha256(data); return hash.slice(0, 16); }; diff --git a/lib/primitives/abstractblock.js b/lib/primitives/abstractblock.js index c7a0c1eb..dd4f97b9 100644 --- a/lib/primitives/abstractblock.js +++ b/lib/primitives/abstractblock.js @@ -155,7 +155,7 @@ AbstractBlock.prototype.hash = function _hash(enc) { let hash = this._hash; if (!hash) { - hash = digest.hash256(this.abbr()); + hash = digest.hash256(this.toHead()); if (!this.mutable) this._hash = hash; } @@ -178,8 +178,8 @@ AbstractBlock.prototype.hash = function _hash(enc) { * @returns {Buffer} */ -AbstractBlock.prototype.abbr = function abbr() { - return this.writeAbbr(new StaticWriter(80)).render(); +AbstractBlock.prototype.toHead = function toHead() { + return this.writeHead(new StaticWriter(80)).render(); }; /** @@ -187,7 +187,7 @@ AbstractBlock.prototype.abbr = function abbr() { * @param {BufferWriter} bw */ -AbstractBlock.prototype.writeAbbr = function writeAbbr(bw) { +AbstractBlock.prototype.writeHead = function writeHead(bw) { bw.writeU32(this.version); bw.writeHash(this.prevBlock); bw.writeHash(this.merkleRoot); @@ -202,7 +202,7 @@ AbstractBlock.prototype.writeAbbr = function writeAbbr(bw) { * @param {BufferReader} br */ -AbstractBlock.prototype.parseAbbr = function parseAbbr(br) { +AbstractBlock.prototype.parseHead = function parseHead(br) { this.version = br.readU32(); this.prevBlock = br.readHash('hex'); this.merkleRoot = br.readHash('hex'); diff --git a/lib/primitives/block.js b/lib/primitives/block.js index 5eb72153..08ec67c7 100644 --- a/lib/primitives/block.js +++ b/lib/primitives/block.js @@ -644,7 +644,7 @@ Block.prototype.fromReader = function fromReader(br) { br.start(); - this.parseAbbr(br); + this.parseHead(br); count = br.readVarint(); @@ -718,14 +718,12 @@ Block.prototype.toMerkle = function toMerkle(filter) { */ Block.prototype.writeNormal = function writeNormal(bw) { - this.writeAbbr(bw); + this.writeHead(bw); bw.writeVarint(this.txs.length); - for (let i = 0; i < this.txs.length; i++) { - let tx = this.txs[i]; + for (let tx of this.txs) tx.toNormalWriter(bw); - } return bw; }; @@ -739,14 +737,12 @@ Block.prototype.writeNormal = function writeNormal(bw) { */ Block.prototype.writeWitness = function writeWitness(bw) { - this.writeAbbr(bw); + this.writeHead(bw); bw.writeVarint(this.txs.length); - for (let i = 0; i < this.txs.length; i++) { - let tx = this.txs[i]; + for (let tx of this.txs) tx.toWriter(bw); - } return bw; }; @@ -802,10 +798,8 @@ Block.prototype.getNormalSizes = function getNormalSizes() { size += 80; size += encoding.sizeVarint(this.txs.length); - for (let i = 0; i < this.txs.length; i++) { - let tx = this.txs[i]; + for (let tx of this.txs) size += tx.getBaseSize(); - } return new RawBlock(size, 0); }; @@ -822,8 +816,7 @@ Block.prototype.getWitnessSizes = function getWitnessSizes() { size += 80; size += encoding.sizeVarint(this.txs.length); - for (let i = 0; i < this.txs.length; i++) { - let tx = this.txs[i]; + for (let tx of this.txs) { let sizes = tx.getSizes(); size += sizes.total; witness += sizes.witness; diff --git a/lib/primitives/headers.js b/lib/primitives/headers.js index 7599ad91..96cd92c1 100644 --- a/lib/primitives/headers.js +++ b/lib/primitives/headers.js @@ -58,7 +58,7 @@ Headers.prototype.getSize = function getSize() { */ Headers.prototype.toWriter = function toWriter(bw) { - this.writeAbbr(bw); + this.writeHead(bw); bw.writeVarint(0); return bw; }; @@ -80,7 +80,7 @@ Headers.prototype.toRaw = function toRaw() { */ Headers.prototype.fromReader = function fromReader(br) { - this.parseAbbr(br); + this.parseHead(br); br.readVarint(); return this; }; @@ -118,34 +118,15 @@ Headers.fromRaw = function fromRaw(data, enc) { return new Headers().fromRaw(data); }; -/** - * Inject properties from buffer reader. - * @private - * @param {BufferReader} br - */ - -Headers.prototype.fromAbbrReader = function fromAbbrReader(br) { - return this.parseAbbr(br); -}; - /** * Inject properties from serialized data. + * TODO: MOVE. * @private * @param {Buffer} data */ -Headers.prototype.fromAbbr = function fromAbbr(data) { - return this.fromAbbrReader(new BufferReader(data)); -}; - -/** - * Instantiate headers from buffer reader. - * @param {BufferReader} br - * @returns {Headers} - */ - -Headers.fromAbbrReader = function fromAbbrReader(br) { - return new Headers().fromAbbrReader(br); +Headers.prototype.fromHead = function fromHead(data) { + return this.parseHead(new BufferReader(data)); }; /** @@ -155,10 +136,10 @@ Headers.fromAbbrReader = function fromAbbrReader(br) { * @returns {Headers} */ -Headers.fromAbbr = function fromAbbr(data, enc) { +Headers.fromHead = function fromHead(data, enc) { if (typeof data === 'string') data = Buffer.from(data, enc); - return new Headers().fromAbbr(data); + return new Headers().fromHead(data); }; /** @@ -291,7 +272,7 @@ Headers.prototype.format = function format(view, height) { Headers.isHeaders = function isHeaders(obj) { return obj && !obj.txs - && typeof obj.abbr === 'function' + && typeof obj.toHead === 'function' && typeof obj.toBlock !== 'function'; }; diff --git a/lib/primitives/memblock.js b/lib/primitives/memblock.js index 5209b30b..475237c5 100644 --- a/lib/primitives/memblock.js +++ b/lib/primitives/memblock.js @@ -59,7 +59,7 @@ MemBlock.prototype.memory = true; * @returns {Buffer} */ -MemBlock.prototype.abbr = function abbr() { +MemBlock.prototype.toHead = function toHead() { return this._raw.slice(0, 80); }; @@ -144,7 +144,7 @@ MemBlock.prototype.parseCoinbaseHeight = function parseCoinbaseHeight() { MemBlock.prototype.fromRaw = function fromRaw(data) { let br = new BufferReader(data, true); - this.parseAbbr(br); + this.parseHead(br); this._raw = br.data; diff --git a/lib/primitives/merkleblock.js b/lib/primitives/merkleblock.js index 5ac28db6..42f2d03d 100644 --- a/lib/primitives/merkleblock.js +++ b/lib/primitives/merkleblock.js @@ -346,7 +346,7 @@ MerkleBlock.prototype.getSize = function getSize() { */ MerkleBlock.prototype.toWriter = function toWriter(bw) { - this.writeAbbr(bw); + this.writeHead(bw); bw.writeU32(this.totalTX); @@ -380,7 +380,7 @@ MerkleBlock.prototype.toRaw = function toRaw() { MerkleBlock.prototype.fromReader = function fromReader(br) { let count; - this.parseAbbr(br); + this.parseHead(br); this.totalTX = br.readU32(); diff --git a/lib/wallet/client.js b/lib/wallet/client.js index f3f0ef54..67ab31a1 100644 --- a/lib/wallet/client.js +++ b/lib/wallet/client.js @@ -314,7 +314,7 @@ function parseEntry(data, enc) { if (typeof data === 'string') data = Buffer.from(data, 'hex'); - block = Headers.fromAbbr(data); + block = Headers.fromHead(data); br = new BufferReader(data); br.seek(80);