Skip to content

Commit e3c4939

Browse files
committed
common -> custom chains: replaced static chains collection in chains/index.ts with function
1 parent e06b66e commit e3c4939

File tree

3 files changed

+54
-22
lines changed

3 files changed

+54
-22
lines changed
Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
import { chainsType } from './../types'
22

3-
export const chains: chainsType = {
4-
names: {
5-
'1': 'mainnet',
6-
'3': 'ropsten',
7-
'4': 'rinkeby',
8-
'42': 'kovan',
9-
'5': 'goerli',
10-
},
11-
mainnet: require('./mainnet.json'),
12-
ropsten: require('./ropsten.json'),
13-
rinkeby: require('./rinkeby.json'),
14-
kovan: require('./kovan.json'),
15-
goerli: require('./goerli.json'),
3+
/**
4+
* @hidden
5+
*/
6+
export function _getInitializedChains() {
7+
const chains = {
8+
names: {
9+
'1': 'mainnet',
10+
'3': 'ropsten',
11+
'4': 'rinkeby',
12+
'42': 'kovan',
13+
'5': 'goerli',
14+
},
15+
mainnet: require('./mainnet.json'),
16+
ropsten: require('./ropsten.json'),
17+
rinkeby: require('./rinkeby.json'),
18+
kovan: require('./kovan.json'),
19+
goerli: require('./goerli.json'),
20+
}
21+
return chains
1622
}
23+
24+
/**
25+
* @deprecated this constant will be internalized (removed)
26+
* on next major version update
27+
*/
28+
export const chains: chainsType = _getInitializedChains()

packages/common/src/index.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { buf as crc32Buffer } from 'crc-32'
2-
import { chains as chainParams } from './chains'
2+
import { _getInitializedChains } from './chains'
33
import { hardforks as HARDFORK_CHANGES } from './hardforks'
44
import { EIPs } from './eips'
5-
import { Chain } from './types'
5+
import { BootstrapNode, Chain, GenesisBlock, Hardfork } from './types'
66

77
/**
88
* Options for instantiating a [[Common]] instance.
@@ -31,6 +31,19 @@ export interface CommonOpts {
3131
* - [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537) - BLS12-381 precompiles
3232
*/
3333
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[]
3447
}
3548

3649
interface hardforkOptions {
@@ -80,16 +93,18 @@ export default class Common {
8093
}
8194

8295
private static _getChainParams(chain: string | number): Chain {
96+
const initializedChains: any = _getInitializedChains()
8397
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]
86101
}
87102

88103
throw new Error(`Chain with ID ${chain} not supported`)
89104
}
90105

91-
if (chainParams[chain]) {
92-
return chainParams[chain]
106+
if (initializedChains[chain]) {
107+
return initializedChains[chain]
93108
}
94109

95110
throw new Error(`Chain with name ${chain} not supported`)
@@ -491,7 +506,7 @@ export default class Common {
491506
// Logic: if accumulator is still null and on the first occurence of
492507
// a block greater than the current hfBlock set the accumulator,
493508
// 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) => {
495510
return hf.block > hfBlock && acc === null ? hf.block : acc
496511
}, null)
497512
return nextHfBlock
@@ -517,7 +532,7 @@ export default class Common {
517532
const genesis = Buffer.from(this.genesis().hash.substr(2), 'hex')
518533

519534
let hfBuffer = Buffer.alloc(0)
520-
let prevBlock = 0
535+
let prevBlock: number | null = 0
521536
for (const hf of this.hardforks()) {
522537
const block = hf.block
523538

@@ -613,7 +628,7 @@ export default class Common {
613628
* @returns chain name (lower case)
614629
*/
615630
chainName(): string {
616-
return chainParams['names'][this.chainId()] || (<any>this._chainParams)['name']
631+
return (<any>this._chainParams)['name']
617632
}
618633

619634
/**

packages/common/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export interface Chain {
2121
genesis: GenesisBlock
2222
hardforks: Hardfork[]
2323
bootstrapNodes: BootstrapNode[]
24+
consensus: {
25+
type: string
26+
algorithm: string
27+
}
2428
}
2529

2630
export interface eipsType {
@@ -40,6 +44,7 @@ export interface GenesisBlock {
4044
export interface Hardfork {
4145
name: string
4246
block: number | null
47+
forkHash: string | null
4348
}
4449

4550
export interface BootstrapNode {

0 commit comments

Comments
 (0)