Skip to content

Commit e7ec70b

Browse files
committed
common -> custom chains: API adoption, preparations for dedicated test file for custom chain creation
1 parent e3c4939 commit e7ec70b

File tree

5 files changed

+83
-49
lines changed

5 files changed

+83
-49
lines changed

packages/common/src/chains/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { chainsType } from './../types'
2+
import mainnet from './mainnet.json'
3+
import ropsten from './ropsten.json'
4+
import rinkeby from './rinkeby.json'
5+
import kovan from './kovan.json'
6+
import goerli from './goerli.json'
27

38
/**
49
* @hidden
@@ -12,11 +17,11 @@ export function _getInitializedChains() {
1217
'42': 'kovan',
1318
'5': 'goerli',
1419
},
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+
mainnet,
21+
ropsten,
22+
rinkeby,
23+
kovan,
24+
goerli,
2025
}
2126
return chains
2227
}

packages/common/src/index.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,18 @@ 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
3834
/**
3935
* Initialize (in addition to the supported chains) with the selected
40-
* custom chains from customChainDir
36+
* custom chains
37+
*
38+
* Usage (directly with the respective chain intialization via the `chain` option):
4139
*
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'
40+
* ```javascript
41+
* import myCustomChain1 from '[PATH_TO_MY_CHAINS]/myCustomChain1.json'
42+
* const common = new Common({ chain: 'myCustomChain1', customChains: [ myCustomChain1 ]})
43+
* ```
4544
*/
46-
initCustomChains?: string[]
45+
customChains?: Chain[]
4746
}
4847

4948
interface hardforkOptions {
@@ -63,6 +62,7 @@ export default class Common {
6362
private _hardfork: string
6463
private _supportedHardforks: Array<string> = []
6564
private _eips: number[] = []
65+
private _customChains: Chain[]
6666

6767
/**
6868
* Creates a Common object for a custom chain, based on a standard one. It uses all the [[Chain]]
@@ -114,6 +114,7 @@ export default class Common {
114114
* @constructor
115115
*/
116116
constructor(opts: CommonOpts) {
117+
this._customChains = opts.customChains ?? []
117118
this._chainParams = this.setChain(opts.chain)
118119
this._hardfork = this.DEFAULT_HARDFORK
119120
if (opts.supportedHardforks) {
@@ -137,6 +138,11 @@ export default class Common {
137138
if (typeof chain === 'number' || typeof chain === 'string') {
138139
this._chainParams = Common._getChainParams(chain)
139140
} else if (typeof chain === 'object') {
141+
if (this._customChains.length > 0) {
142+
throw new Error(
143+
'Chain must be a string or number when initialized with customChains passed in'
144+
)
145+
}
140146
const required = ['networkId', 'genesis', 'hardforks', 'bootstrapNodes']
141147
for (const param of required) {
142148
if ((<any>chain)[param] === undefined) {

packages/common/tests/chains.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -119,39 +119,4 @@ tape('[Common]: Initialization / Chain params', function (t: tape.Test) {
119119

120120
st.end()
121121
})
122-
123-
t.test(
124-
'Should provide correct access to private network chain parameters',
125-
function (st: tape.Test) {
126-
const chainParams = require('./testnet.json')
127-
const c = new Common({ chain: chainParams, hardfork: 'byzantium' })
128-
st.equal(c.chainName(), 'testnet', 'should initialize with chain name')
129-
st.equal(c.chainId(), 12345, 'should return correct chain Id')
130-
st.equal(c.networkId(), 12345, 'should return correct network Id')
131-
st.equal(
132-
c.genesis().hash,
133-
'0xaa00000000000000000000000000000000000000000000000000000000000000',
134-
'should return correct genesis hash'
135-
)
136-
st.equal(c.hardforks()[3]['block'], 3, 'should return correct hardfork data')
137-
st.equal(c.bootstrapNodes()[1].ip, '10.0.0.2', 'should return a bootstrap node array')
138-
139-
st.end()
140-
}
141-
)
142-
143-
t.test('Should handle custom chain parameters with missing field', function (st: tape.Test) {
144-
const chainParams = require('./testnet.json')
145-
delete chainParams['hardforks']
146-
st.throws(
147-
function () {
148-
new Common({ chain: chainParams })
149-
},
150-
/Missing required/,
151-
'should throw an exception on missing parameter'
152-
) // eslint-disable-line no-new
153-
154-
st.comment('-----------------------------------------------------------------')
155-
st.end()
156-
})
157122
})
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import tape from 'tape'
2+
import Common from '../src/'
3+
import testnet from './data/testnet.json'
4+
5+
tape('[Common]: Custom chains', function (t: tape.Test) {
6+
t.test(
7+
'chain -> object: should provide correct access to private network chain parameters',
8+
function (st: tape.Test) {
9+
const c = new Common({ chain: testnet, hardfork: 'byzantium' })
10+
st.equal(c.chainName(), 'testnet', 'should initialize with chain name')
11+
st.equal(c.chainId(), 12345, 'should return correct chain Id')
12+
st.equal(c.networkId(), 12345, 'should return correct network Id')
13+
st.equal(
14+
c.genesis().hash,
15+
'0xaa00000000000000000000000000000000000000000000000000000000000000',
16+
'should return correct genesis hash'
17+
)
18+
st.equal(c.hardforks()[3]['block'], 3, 'should return correct hardfork data')
19+
st.equal(c.bootstrapNodes()[1].ip, '10.0.0.2', 'should return a bootstrap node array')
20+
21+
st.end()
22+
}
23+
)
24+
25+
t.test(
26+
'chain -> object: should handle custom chain parameters with missing field',
27+
function (st: tape.Test) {
28+
const chainParams = Object.assign({}, testnet)
29+
delete (chainParams as any)['hardforks']
30+
st.throws(
31+
function () {
32+
new Common({ chain: chainParams })
33+
},
34+
/Missing required/,
35+
'should throw an exception on missing parameter'
36+
) // eslint-disable-line no-new
37+
38+
st.comment('-----------------------------------------------------------------')
39+
st.end()
40+
}
41+
)
42+
43+
t.test('customChains parameter: ', (st) => {
44+
const c = new Common({ chain: testnet, hardfork: 'byzantium' })
45+
st.equal(c.chainName(), 'testnet', 'should initialize with chain name')
46+
st.equal(c.chainId(), 12345, 'should return correct chain Id')
47+
st.equal(c.networkId(), 12345, 'should return correct network Id')
48+
st.equal(
49+
c.genesis().hash,
50+
'0xaa00000000000000000000000000000000000000000000000000000000000000',
51+
'should return correct genesis hash'
52+
)
53+
st.equal(c.hardforks()[3]['block'], 3, 'should return correct hardfork data')
54+
st.equal(c.bootstrapNodes()[1].ip, '10.0.0.2', 'should return a bootstrap node array')
55+
56+
st.end()
57+
})
58+
})
File renamed without changes.

0 commit comments

Comments
 (0)