Skip to content

Commit 48bf08c

Browse files
committed
Add weight and ability to get strippedsize
1 parent 29e3195 commit 48bf08c

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

src/block.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,18 @@ 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+
// @ts-ignore using the __byteLength private method on Transaction
141+
this.transactions.reduce((a, x) => a + x.__byteLength(allowWitness), 0)
136142
);
137143
}
138144
getHash() {

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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,20 @@ 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+
// @ts-ignore using the __byteLength private method on Transaction
164+
this.transactions.reduce((a, x) => a + x.__byteLength(allowWitness), 0)
158165
);
159166
}
160167

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;

0 commit comments

Comments
 (0)