1
1
import Common from 'ethereumjs-common'
2
- import * as utils from 'ethereumjs-util'
3
- import { BN } from 'ethereumjs-util'
2
+ import {
3
+ zeros ,
4
+ KECCAK256_RLP_ARRAY ,
5
+ KECCAK256_RLP ,
6
+ toBuffer ,
7
+ defineProperties ,
8
+ bufferToInt ,
9
+ rlphash ,
10
+ } from 'ethereumjs-util'
4
11
import { Blockchain , BlockHeaderData , BufferLike , ChainOptions , PrefixedHexString } from './types'
5
12
import { Buffer } from 'buffer'
6
13
import { Block } from './block'
7
14
15
+ const { BN } = require ( 'ethereumjs-util' )
16
+ import IBN = require( 'bn.js' )
17
+
8
18
/**
9
19
* An object that represents the block header
10
20
*/
@@ -55,35 +65,35 @@ export class BlockHeader {
55
65
{
56
66
name : 'parentHash' ,
57
67
length : 32 ,
58
- default : utils . zeros ( 32 ) ,
68
+ default : zeros ( 32 ) ,
59
69
} ,
60
70
{
61
71
name : 'uncleHash' ,
62
- default : utils . KECCAK256_RLP_ARRAY ,
72
+ default : KECCAK256_RLP_ARRAY ,
63
73
} ,
64
74
{
65
75
name : 'coinbase' ,
66
76
length : 20 ,
67
- default : utils . zeros ( 20 ) ,
77
+ default : zeros ( 20 ) ,
68
78
} ,
69
79
{
70
80
name : 'stateRoot' ,
71
81
length : 32 ,
72
- default : utils . zeros ( 32 ) ,
82
+ default : zeros ( 32 ) ,
73
83
} ,
74
84
{
75
85
name : 'transactionsTrie' ,
76
86
length : 32 ,
77
- default : utils . KECCAK256_RLP ,
87
+ default : KECCAK256_RLP ,
78
88
} ,
79
89
{
80
90
name : 'receiptTrie' ,
81
91
length : 32 ,
82
- default : utils . KECCAK256_RLP ,
92
+ default : KECCAK256_RLP ,
83
93
} ,
84
94
{
85
95
name : 'bloom' ,
86
- default : utils . zeros ( 256 ) ,
96
+ default : zeros ( 256 ) ,
87
97
} ,
88
98
{
89
99
name : 'difficulty' ,
@@ -92,7 +102,7 @@ export class BlockHeader {
92
102
{
93
103
name : 'number' ,
94
104
// TODO: params.homeSteadForkNumber.v left for legacy reasons, replace on future release
95
- default : utils . toBuffer ( 1150000 ) ,
105
+ default : toBuffer ( 1150000 ) ,
96
106
} ,
97
107
{
98
108
name : 'gasLimit' ,
@@ -115,23 +125,23 @@ export class BlockHeader {
115
125
} ,
116
126
{
117
127
name : 'mixHash' ,
118
- default : utils . zeros ( 32 ) ,
128
+ default : zeros ( 32 ) ,
119
129
// length: 32
120
130
} ,
121
131
{
122
132
name : 'nonce' ,
123
- default : utils . zeros ( 8 ) , // sha3(42)
133
+ default : zeros ( 8 ) , // sha3(42)
124
134
} ,
125
135
]
126
- utils . defineProperties ( this , fields , data )
136
+ defineProperties ( this , fields , data )
127
137
}
128
138
129
139
/**
130
140
* Returns the canonical difficulty for this block.
131
141
*
132
142
* @param parentBlock - the parent `Block` of this header
133
143
*/
134
- canonicalDifficulty ( parentBlock : Block ) : BN {
144
+ canonicalDifficulty ( parentBlock : Block ) : IBN {
135
145
const hardfork = this . _getHardfork ( )
136
146
const blockTs = new BN ( this . timestamp )
137
147
const parentTs = new BN ( parentBlock . header . timestamp )
@@ -143,11 +153,11 @@ export class BlockHeader {
143
153
let num = new BN ( this . number )
144
154
145
155
// We use a ! here as TS can follow this hardforks-dependent logic, but it always gets assigned
146
- let dif ! : BN
156
+ let dif ! : IBN
147
157
148
158
if ( this . _common . hardforkGteHardfork ( hardfork , 'byzantium' ) ) {
149
159
// max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99) (EIP100)
150
- const uncleAddend = parentBlock . header . uncleHash . equals ( utils . KECCAK256_RLP_ARRAY ) ? 1 : 2
160
+ const uncleAddend = parentBlock . header . uncleHash . equals ( KECCAK256_RLP_ARRAY ) ? 1 : 2
151
161
let a = blockTs
152
162
. sub ( parentTs )
153
163
. idivn ( 9 )
@@ -252,7 +262,7 @@ export class BlockHeader {
252
262
* @param blockchain - the blockchain that this block is validating against
253
263
* @param height - If this is an uncle header, this is the height of the block that is including it
254
264
*/
255
- async validate ( blockchain : Blockchain , height ?: BN ) : Promise < void > {
265
+ async validate ( blockchain : Blockchain , height ?: IBN ) : Promise < void > {
256
266
if ( this . isGenesis ( ) ) {
257
267
return
258
268
}
@@ -283,11 +293,11 @@ export class BlockHeader {
283
293
throw new Error ( 'invalid gas limit' )
284
294
}
285
295
286
- if ( utils . bufferToInt ( this . number ) - utils . bufferToInt ( parentBlock . header . number ) !== 1 ) {
296
+ if ( bufferToInt ( this . number ) - bufferToInt ( parentBlock . header . number ) !== 1 ) {
287
297
throw new Error ( 'invalid height' )
288
298
}
289
299
290
- if ( utils . bufferToInt ( this . timestamp ) <= utils . bufferToInt ( parentBlock . header . timestamp ) ) {
300
+ if ( bufferToInt ( this . timestamp ) <= bufferToInt ( parentBlock . header . timestamp ) ) {
291
301
throw new Error ( 'invalid timestamp' )
292
302
}
293
303
@@ -301,7 +311,7 @@ export class BlockHeader {
301
311
* Returns the hash of the block header.
302
312
*/
303
313
hash ( ) : Buffer {
304
- return utils . rlphash ( this . raw )
314
+ return rlphash ( this . raw )
305
315
}
306
316
307
317
/**
@@ -347,7 +357,7 @@ export class BlockHeader {
347
357
348
358
return commonHardFork !== null
349
359
? commonHardFork
350
- : this . _common . activeHardfork ( utils . bufferToInt ( this . number ) )
360
+ : this . _common . activeHardfork ( bufferToInt ( this . number ) )
351
361
}
352
362
353
363
private async _getBlockByHash ( blockchain : Blockchain , hash : Buffer ) : Promise < Block | undefined > {
0 commit comments