Skip to content

Commit 456ba5a

Browse files
authored
Merge pull request #1515 from bitcoinjs/addWeightBlock
Add weight and ability to get strippedsize
2 parents 29e3195 + 10fcf3d commit 456ba5a

File tree

8 files changed

+74
-62
lines changed

8 files changed

+74
-62
lines changed

src/block.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,17 @@ class Block {
127127
hasWitness() {
128128
return anyTxHasWitness(this.transactions);
129129
}
130-
byteLength(headersOnly) {
130+
weight() {
131+
const base = this.byteLength(false, false);
132+
const total = this.byteLength(false, true);
133+
return base * 3 + total;
134+
}
135+
byteLength(headersOnly, allowWitness = true) {
131136
if (headersOnly || !this.transactions) return 80;
132137
return (
133138
80 +
134139
varuint.encodingLength(this.transactions.length) +
135-
this.transactions.reduce((a, x) => a + x.byteLength(), 0)
140+
this.transactions.reduce((a, x) => a + x.byteLength(allowWitness), 0)
136141
);
137142
}
138143
getHash() {

src/transaction.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,31 @@ class Transaction {
179179
});
180180
}
181181
weight() {
182-
const base = this.__byteLength(false);
183-
const total = this.__byteLength(true);
182+
const base = this.byteLength(false);
183+
const total = this.byteLength(true);
184184
return base * 3 + total;
185185
}
186186
virtualSize() {
187187
return Math.ceil(this.weight() / 4);
188188
}
189-
byteLength() {
190-
return this.__byteLength(true);
189+
byteLength(_ALLOW_WITNESS = true) {
190+
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
191+
return (
192+
(hasWitnesses ? 10 : 8) +
193+
varuint.encodingLength(this.ins.length) +
194+
varuint.encodingLength(this.outs.length) +
195+
this.ins.reduce((sum, input) => {
196+
return sum + 40 + varSliceSize(input.script);
197+
}, 0) +
198+
this.outs.reduce((sum, output) => {
199+
return sum + 8 + varSliceSize(output.script);
200+
}, 0) +
201+
(hasWitnesses
202+
? this.ins.reduce((sum, input) => {
203+
return sum + vectorSize(input.witness);
204+
}, 0)
205+
: 0)
206+
);
191207
}
192208
clone() {
193209
const newTx = new Transaction();
@@ -269,7 +285,7 @@ class Transaction {
269285
txTmp.ins[inIndex].script = ourScript;
270286
}
271287
// serialize and hash
272-
const buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4);
288+
const buffer = Buffer.allocUnsafe(txTmp.byteLength(false) + 4);
273289
buffer.writeInt32LE(hashType, buffer.length - 4);
274290
txTmp.__toBuffer(buffer, 0, false);
275291
return bcrypto.hash256(buffer);
@@ -386,27 +402,8 @@ class Transaction {
386402
typeforce(types.tuple(types.Number, [types.Buffer]), arguments);
387403
this.ins[index].witness = witness;
388404
}
389-
__byteLength(_ALLOW_WITNESS) {
390-
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
391-
return (
392-
(hasWitnesses ? 10 : 8) +
393-
varuint.encodingLength(this.ins.length) +
394-
varuint.encodingLength(this.outs.length) +
395-
this.ins.reduce((sum, input) => {
396-
return sum + 40 + varSliceSize(input.script);
397-
}, 0) +
398-
this.outs.reduce((sum, output) => {
399-
return sum + 8 + varSliceSize(output.script);
400-
}, 0) +
401-
(hasWitnesses
402-
? this.ins.reduce((sum, input) => {
403-
return sum + vectorSize(input.witness);
404-
}, 0)
405-
: 0)
406-
);
407-
}
408-
__toBuffer(buffer, initialOffset, _ALLOW_WITNESS) {
409-
if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength(_ALLOW_WITNESS));
405+
__toBuffer(buffer, initialOffset, _ALLOW_WITNESS = false) {
406+
if (!buffer) buffer = Buffer.allocUnsafe(this.byteLength(_ALLOW_WITNESS));
410407
let offset = initialOffset || 0;
411408
function writeSlice(slice) {
412409
offset += slice.copy(buffer, offset);

test/block.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ describe('Block', () => {
4848
assert.strictEqual(block.bits, f.bits);
4949
assert.strictEqual(block.nonce, f.nonce);
5050
assert.strictEqual(!block.transactions, f.hex.length === 160);
51+
if (f.size && f.strippedSize && f.weight) {
52+
assert.strictEqual(block.byteLength(false, true), f.size);
53+
assert.strictEqual(block.byteLength(false, false), f.strippedSize);
54+
assert.strictEqual(block.weight(), f.weight);
55+
}
5156
});
5257
});
5358

test/fixtures/block.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@
133133
"prevHash": "8980ebb11236bacc66c447d5ad961bc546c0f9cc385a08000000000000000000",
134134
"timestamp": 1537429727,
135135
"valid": true,
136-
"version": 536870912
136+
"version": 536870912,
137+
"size": 2355,
138+
"strippedSize": 2209,
139+
"weight": 8982
137140
}
138141
],
139142
"invalid": [

ts_src/block.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,19 @@ export class Block {
148148
return anyTxHasWitness(this.transactions!);
149149
}
150150

151-
byteLength(headersOnly?: boolean): number {
151+
weight(): number {
152+
const base = this.byteLength(false, false);
153+
const total = this.byteLength(false, true);
154+
return base * 3 + total;
155+
}
156+
157+
byteLength(headersOnly?: boolean, allowWitness: boolean = true): number {
152158
if (headersOnly || !this.transactions) return 80;
153159

154160
return (
155161
80 +
156162
varuint.encodingLength(this.transactions.length) +
157-
this.transactions.reduce((a, x) => a + x.byteLength(), 0)
163+
this.transactions.reduce((a, x) => a + x.byteLength(allowWitness), 0)
158164
);
159165
}
160166

ts_src/transaction.ts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,34 @@ export class Transaction {
237237
}
238238

239239
weight(): number {
240-
const base = this.__byteLength(false);
241-
const total = this.__byteLength(true);
240+
const base = this.byteLength(false);
241+
const total = this.byteLength(true);
242242
return base * 3 + total;
243243
}
244244

245245
virtualSize(): number {
246246
return Math.ceil(this.weight() / 4);
247247
}
248248

249-
byteLength(): number {
250-
return this.__byteLength(true);
249+
byteLength(_ALLOW_WITNESS: boolean = true): number {
250+
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
251+
252+
return (
253+
(hasWitnesses ? 10 : 8) +
254+
varuint.encodingLength(this.ins.length) +
255+
varuint.encodingLength(this.outs.length) +
256+
this.ins.reduce((sum, input) => {
257+
return sum + 40 + varSliceSize(input.script);
258+
}, 0) +
259+
this.outs.reduce((sum, output) => {
260+
return sum + 8 + varSliceSize(output.script);
261+
}, 0) +
262+
(hasWitnesses
263+
? this.ins.reduce((sum, input) => {
264+
return sum + vectorSize(input.witness);
265+
}, 0)
266+
: 0)
267+
);
251268
}
252269

253270
clone(): Transaction {
@@ -352,7 +369,7 @@ export class Transaction {
352369
}
353370

354371
// serialize and hash
355-
const buffer: Buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4);
372+
const buffer: Buffer = Buffer.allocUnsafe(txTmp.byteLength(false) + 4);
356373
buffer.writeInt32LE(hashType, buffer.length - 4);
357374
txTmp.__toBuffer(buffer, 0, false);
358375

@@ -506,34 +523,13 @@ export class Transaction {
506523
this.ins[index].witness = witness;
507524
}
508525

509-
private __byteLength(_ALLOW_WITNESS: boolean): number {
510-
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
511-
512-
return (
513-
(hasWitnesses ? 10 : 8) +
514-
varuint.encodingLength(this.ins.length) +
515-
varuint.encodingLength(this.outs.length) +
516-
this.ins.reduce((sum, input) => {
517-
return sum + 40 + varSliceSize(input.script);
518-
}, 0) +
519-
this.outs.reduce((sum, output) => {
520-
return sum + 8 + varSliceSize(output.script);
521-
}, 0) +
522-
(hasWitnesses
523-
? this.ins.reduce((sum, input) => {
524-
return sum + vectorSize(input.witness);
525-
}, 0)
526-
: 0)
527-
);
528-
}
529-
530526
private __toBuffer(
531527
buffer?: Buffer,
532528
initialOffset?: number,
533-
_ALLOW_WITNESS?: boolean,
529+
_ALLOW_WITNESS: boolean = false,
534530
): Buffer {
535531
if (!buffer)
536-
buffer = Buffer.allocUnsafe(this.__byteLength(_ALLOW_WITNESS!)) as Buffer;
532+
buffer = Buffer.allocUnsafe(this.byteLength(_ALLOW_WITNESS)) as Buffer;
537533

538534
let offset = initialOffset || 0;
539535

types/block.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export declare class Block {
1515
getWitnessCommit(): Buffer | null;
1616
hasWitnessCommit(): boolean;
1717
hasWitness(): boolean;
18-
byteLength(headersOnly?: boolean): number;
18+
weight(): number;
19+
byteLength(headersOnly?: boolean, allowWitness?: boolean): number;
1920
getHash(): Buffer;
2021
getId(): string;
2122
getUTCDate(): Date;

types/transaction.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export declare class Transaction {
3030
hasWitnesses(): boolean;
3131
weight(): number;
3232
virtualSize(): number;
33-
byteLength(): number;
33+
byteLength(_ALLOW_WITNESS?: boolean): number;
3434
clone(): Transaction;
3535
/**
3636
* Hash transaction for signing a specific input.
@@ -48,6 +48,5 @@ export declare class Transaction {
4848
toHex(): string;
4949
setInputScript(index: number, scriptSig: Buffer): void;
5050
setWitness(index: number, witness: Buffer[]): void;
51-
private __byteLength;
5251
private __toBuffer;
5352
}

0 commit comments

Comments
 (0)