1
1
import { buf as crc32Buffer } from 'crc-32'
2
- import { chains as chainParams } from './chains'
2
+ import { _getInitializedChains } from './chains'
3
3
import { hardforks as HARDFORK_CHANGES } from './hardforks'
4
4
import { EIPs } from './eips'
5
- import { Chain } from './types'
5
+ import { BootstrapNode , Chain , GenesisBlock , Hardfork } from './types'
6
6
7
7
/**
8
8
* Options for instantiating a [[Common]] instance.
@@ -31,6 +31,19 @@ export interface CommonOpts {
31
31
* - [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537) - BLS12-381 precompiles
32
32
*/
33
33
eips ?: number [ ]
34
+ /**
35
+ * Directory to look for custom chains
36
+ */
37
+ customChainDir ?: string
38
+ /**
39
+ * Initialize (in addition to the supported chains) with the selected
40
+ * custom chains from customChainDir
41
+ *
42
+ * Please provide a list of strings matching a corresponding .json file
43
+ * in customChainDir named by the chain name, e.g.`['myCustomChain']
44
+ * to read the configuration parameters from a file named 'myCustomChain.json'
45
+ */
46
+ initCustomChains ?: string [ ]
34
47
}
35
48
36
49
interface hardforkOptions {
@@ -80,16 +93,18 @@ export default class Common {
80
93
}
81
94
82
95
private static _getChainParams ( chain : string | number ) : Chain {
96
+ const initializedChains : any = _getInitializedChains ( )
83
97
if ( typeof chain === 'number' ) {
84
- if ( chainParams [ 'names' ] [ chain ] ) {
85
- return chainParams [ chainParams [ 'names' ] [ chain ] ]
98
+ if ( initializedChains [ 'names' ] [ chain ] ) {
99
+ const name : string = initializedChains [ 'names' ] [ chain ]
100
+ return initializedChains [ name ]
86
101
}
87
102
88
103
throw new Error ( `Chain with ID ${ chain } not supported` )
89
104
}
90
105
91
- if ( chainParams [ chain ] ) {
92
- return chainParams [ chain ]
106
+ if ( initializedChains [ chain ] ) {
107
+ return initializedChains [ chain ]
93
108
}
94
109
95
110
throw new Error ( `Chain with name ${ chain } not supported` )
@@ -491,7 +506,7 @@ export default class Common {
491
506
// Logic: if accumulator is still null and on the first occurence of
492
507
// a block greater than the current hfBlock set the accumulator,
493
508
// pass on the accumulator as the final result from this time on
494
- const nextHfBlock = this . hardforks ( ) . reduce ( ( acc : number , hf : any ) => {
509
+ const nextHfBlock = ( this . hardforks ( ) as any ) . reduce ( ( acc : number , hf : any ) => {
495
510
return hf . block > hfBlock && acc === null ? hf . block : acc
496
511
} , null )
497
512
return nextHfBlock
@@ -517,7 +532,7 @@ export default class Common {
517
532
const genesis = Buffer . from ( this . genesis ( ) . hash . substr ( 2 ) , 'hex' )
518
533
519
534
let hfBuffer = Buffer . alloc ( 0 )
520
- let prevBlock = 0
535
+ let prevBlock : number | null = 0
521
536
for ( const hf of this . hardforks ( ) ) {
522
537
const block = hf . block
523
538
@@ -613,7 +628,7 @@ export default class Common {
613
628
* @returns chain name (lower case)
614
629
*/
615
630
chainName ( ) : string {
616
- return chainParams [ 'names' ] [ this . chainId ( ) ] || ( < any > this . _chainParams ) [ 'name' ]
631
+ return ( < any > this . _chainParams ) [ 'name' ]
617
632
}
618
633
619
634
/**
0 commit comments