File tree Expand file tree Collapse file tree 5 files changed +28
-6
lines changed Expand file tree Collapse file tree 5 files changed +28
-6
lines changed Original file line number Diff line number Diff line change @@ -127,12 +127,18 @@ class Block {
127
127
hasWitness ( ) {
128
128
return anyTxHasWitness ( this . transactions ) ;
129
129
}
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 ) {
131
136
if ( headersOnly || ! this . transactions ) return 80 ;
132
137
return (
133
138
80 +
134
139
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 )
136
142
) ;
137
143
}
138
144
getHash ( ) {
Original file line number Diff line number Diff line change @@ -48,6 +48,11 @@ describe('Block', () => {
48
48
assert . strictEqual ( block . bits , f . bits ) ;
49
49
assert . strictEqual ( block . nonce , f . nonce ) ;
50
50
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
+ }
51
56
} ) ;
52
57
} ) ;
53
58
Original file line number Diff line number Diff line change 133
133
"prevHash" : " 8980ebb11236bacc66c447d5ad961bc546c0f9cc385a08000000000000000000" ,
134
134
"timestamp" : 1537429727 ,
135
135
"valid" : true ,
136
- "version" : 536870912
136
+ "version" : 536870912 ,
137
+ "size" : 2355 ,
138
+ "strippedSize" : 2209 ,
139
+ "weight" : 8982
137
140
}
138
141
],
139
142
"invalid" : [
Original file line number Diff line number Diff line change @@ -148,13 +148,20 @@ export class Block {
148
148
return anyTxHasWitness ( this . transactions ! ) ;
149
149
}
150
150
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 {
152
158
if ( headersOnly || ! this . transactions ) return 80 ;
153
159
154
160
return (
155
161
80 +
156
162
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 )
158
165
) ;
159
166
}
160
167
Original file line number Diff line number Diff line change @@ -15,7 +15,8 @@ export declare class Block {
15
15
getWitnessCommit ( ) : Buffer | null ;
16
16
hasWitnessCommit ( ) : boolean ;
17
17
hasWitness ( ) : boolean ;
18
- byteLength ( headersOnly ?: boolean ) : number ;
18
+ weight ( ) : number ;
19
+ byteLength ( headersOnly ?: boolean , allowWitness ?: boolean ) : number ;
19
20
getHash ( ) : Buffer ;
20
21
getId ( ) : string ;
21
22
getUTCDate ( ) : Date ;
You can’t perform that action at this time.
0 commit comments