Skip to content

Commit 0614e2e

Browse files
committed
common -> custom chains: added customChains functionality, added tests
1 parent e7ec70b commit 0614e2e

File tree

7 files changed

+218
-29
lines changed

7 files changed

+218
-29
lines changed

packages/common/src/chains/index.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { chainsType } from './../types'
1+
import { Chain, chainsType } from './../types'
22
import mainnet from './mainnet.json'
33
import ropsten from './ropsten.json'
44
import rinkeby from './rinkeby.json'
@@ -8,21 +8,30 @@ import goerli from './goerli.json'
88
/**
99
* @hidden
1010
*/
11-
export function _getInitializedChains() {
12-
const chains = {
13-
names: {
14-
'1': 'mainnet',
15-
'3': 'ropsten',
16-
'4': 'rinkeby',
17-
'42': 'kovan',
18-
'5': 'goerli',
19-
},
11+
export function _getInitializedChains(customChains?: Chain[]) {
12+
const names: any = {
13+
'1': 'mainnet',
14+
'3': 'ropsten',
15+
'4': 'rinkeby',
16+
'42': 'kovan',
17+
'5': 'goerli',
18+
}
19+
const chains: any = {
2020
mainnet,
2121
ropsten,
2222
rinkeby,
2323
kovan,
2424
goerli,
2525
}
26+
if (customChains) {
27+
for (const chain of customChains) {
28+
const name = chain.name
29+
names[chain.chainId.toString()] = name
30+
chains[name] = chain
31+
}
32+
}
33+
34+
chains['names'] = names
2635
return chains
2736
}
2837

packages/common/src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { BootstrapNode, Chain, GenesisBlock, Hardfork } from './types'
99
*/
1010
export interface CommonOpts {
1111
/**
12-
* String ('mainnet') or Number (1) chain
12+
* Chain name ('mainnet') or id (1), either from a chain directly supported
13+
* or a custom chain passed in via `customChains`
1314
*/
1415
chain: string | number | object
1516
/**
@@ -92,8 +93,8 @@ export default class Common {
9293
})
9394
}
9495

95-
private static _getChainParams(chain: string | number): Chain {
96-
const initializedChains: any = _getInitializedChains()
96+
private static _getChainParams(chain: string | number, customChains?: Chain[]): Chain {
97+
const initializedChains: any = _getInitializedChains(customChains)
9798
if (typeof chain === 'number') {
9899
if (initializedChains['names'][chain]) {
99100
const name: string = initializedChains['names'][chain]
@@ -136,7 +137,7 @@ export default class Common {
136137
*/
137138
setChain(chain: string | number | object): any {
138139
if (typeof chain === 'number' || typeof chain === 'string') {
139-
this._chainParams = Common._getChainParams(chain)
140+
this._chainParams = Common._getChainParams(chain, this._customChains)
140141
} else if (typeof chain === 'object') {
141142
if (this._customChains.length > 0) {
142143
throw new Error(
@@ -564,7 +565,7 @@ export default class Common {
564565
* Returns an eth/64 compliant fork hash (EIP-2124)
565566
* @param hardfork Hardfork name, optional if HF set
566567
*/
567-
forkHash(hardfork?: string) {
568+
forkHash(hardfork?: string): string | null {
568569
hardfork = this._chooseHardfork(hardfork, false)
569570
const data = this._getHardfork(hardfork)
570571
if (data['block'] === null) {

packages/common/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface GenesisBlock {
4444
export interface Hardfork {
4545
name: string
4646
block: number | null
47-
forkHash: string | null
47+
forkHash?: string | null
4848
}
4949

5050
export interface BootstrapNode {

packages/common/tests/customChains.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import tape from 'tape'
22
import Common from '../src/'
33
import testnet from './data/testnet.json'
4+
import energyWebChain from './data/energyWebChain.json'
5+
import volta from './data/volta.json'
46

57
tape('[Common]: Custom chains', function (t: tape.Test) {
68
t.test(
@@ -35,23 +37,39 @@ tape('[Common]: Custom chains', function (t: tape.Test) {
3537
'should throw an exception on missing parameter'
3638
) // eslint-disable-line no-new
3739

38-
st.comment('-----------------------------------------------------------------')
3940
st.end()
4041
}
4142
)
4243

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')
44+
t.test('customChains parameter: initialization exception', (st) => {
45+
st.throws(
46+
function () {
47+
new Common({ chain: testnet, customChains: [testnet] })
48+
},
49+
/Chain must be a string or number when initialized with customChains passed in/,
50+
'should throw an exception on wrong initialization'
51+
) // eslint-disable-line no-new
52+
53+
st.end()
54+
})
55+
56+
t.test('customChains parameter: initialization', (st) => {
57+
let c = new Common({ chain: 'mainnet', hardfork: 'byzantium', customChains: [testnet] })
58+
st.equal(c.chainName(), 'mainnet', 'customChains, chain set to supported chain')
59+
st.equal(c.hardforkBlock(), 4370000, 'customChains, chain set to supported chain')
60+
61+
c.setChain('testnet')
62+
st.equal(c.chainName(), 'testnet', 'customChains, chain switched to custom chain')
63+
st.equal(c.hardforkBlock(), 4, 'customChains, chain switched to custom chain')
64+
65+
c = new Common({ chain: 'testnet', hardfork: 'byzantium', customChains: [testnet] })
66+
st.equal(c.chainName(), 'testnet', 'customChains, chain initialized with custom chain')
67+
st.equal(c.hardforkBlock(), 4, 'customChains, chain initialized with custom chain')
68+
69+
const customChains = [testnet, energyWebChain, volta]
70+
c = new Common({ chain: 'energyWebChain', hardfork: 'istanbul', customChains })
71+
st.equal(c.chainName(), 'energyWebChain', 'customChains, chain initialized with custom chain')
72+
st.equal(c.hardforkBlock(), 4922294, 'customChains, chain initialized with custom chain')
5573

5674
st.end()
5775
})
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"name": "energyWebChain",
3+
"chainId": 246,
4+
"networkId": 246,
5+
"consensus": {
6+
"type": "pow",
7+
"algorithm": "ethash"
8+
},
9+
"comment": "Energy Web Chain network",
10+
"url": "https://explorer.energyweb.org/",
11+
"genesis": {
12+
"hash": "0x0b6d3e680af2fc525392c720666cce58e3d8e6fe75ba4b48cb36bcc69039229b",
13+
"timestamp": null,
14+
"gasLimit": 6000000,
15+
"difficulty": 131072,
16+
"nonce": "0x0000000000000000",
17+
"extraData": "",
18+
"stateRoot": ""
19+
},
20+
"hardforks": [
21+
{
22+
"name": "chainstart",
23+
"block": 0,
24+
"consensus": "poa",
25+
"finality": null
26+
},
27+
{
28+
"name": "istanbul",
29+
"block": 4922294,
30+
"consensus": "poa",
31+
"finality": null
32+
}
33+
],
34+
"bootstrapNodes": [
35+
{
36+
"ip": "35.181.30.169",
37+
"port": 30303,
38+
"id": "cc67008e850c4b64702f1664f18704dd23980fb574138588904270739dca3a9c417ac3f97077b0f4e4872a5db109195f7137c156314dbcc724eabd809044553e",
39+
"location": "",
40+
"comment": ""
41+
},
42+
{
43+
"ip": "35.181.104.81",
44+
"port": 30303,
45+
"id": "34f97743b9c07fac84c2db921337f006b5932e14c29df496951bdcfe3fce71e3d1504c25d66156dae01065623849b4fe940168c7e3f21c49b538af0cabb5805f",
46+
"location": "",
47+
"comment": ""
48+
},
49+
{
50+
"ip": "3.1.233.174",
51+
"port": 30303,
52+
"id": "80e49825bb6bdfbf69576fd4a5f7bb173db98c08439bd7a019e2d42548afbb1f70232b74ede42dbd99cc0de3990a0101d49543c1b299027d2fd6f5a2c81eff75",
53+
"location": "",
54+
"comment": ""
55+
},
56+
{
57+
"ip": "52.74.191.91",
58+
"port": 30303,
59+
"id": "1cd00f54ddaa41793d32500f0cb123d43e95f5ed18220c367d5a4db7d111e5fb59f250c397e40977562881875827f022d88a51e51171daaebf916beaa60e2139",
60+
"location": "",
61+
"comment": ""
62+
},
63+
{
64+
"ip": "3.130.32.228",
65+
"port": 30303,
66+
"id": "3a8588bd883ea3bce606b08e7ca71d55717077cb3d25d3c00421ac371fbae286b94f58ba2e739b17b64c4d0d3e19f1b0ae2e47962703b42d7f36f1b9a448d4ea",
67+
"location": "",
68+
"comment": ""
69+
},
70+
{
71+
"ip": "3.130.9.1",
72+
"port": 30303,
73+
"id": "69749dbe7e6f5fceaeb0a16f60353b3ec04825a12400e6d581fe980275e96fecb464276e61d79fc1628e448f1f74da2985d778ea54e0e8e7c64d980f2b3b6a94",
74+
"location": "",
75+
"comment": ""
76+
}
77+
]
78+
}

packages/common/tests/data/testnet.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
"name": "testnet",
33
"chainId": 12345,
44
"networkId": 12345,
5+
"consensus": {
6+
"type": "pow",
7+
"algorithm": "ethash"
8+
},
59
"comment": "Private test network",
10+
"url": "[TESTNET_URL]",
611
"genesis": {
712
"hash": "0xaa00000000000000000000000000000000000000000000000000000000000000",
813
"timestamp": null,
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"name": "volta",
3+
"chainId": 73799,
4+
"networkId": 73799,
5+
"consensus": {
6+
"type": "pow",
7+
"algorithm": "ethash"
8+
},
9+
"comment": "Volta test network",
10+
"url": "http://volta-explorer.energyweb.org/",
11+
"genesis": {
12+
"hash": "0xebd8b413ca7b7f84a8dd20d17519ce2b01954c74d94a0a739a3e416abe0e43e5",
13+
"timestamp": null,
14+
"gasLimit": 6000000,
15+
"difficulty": 131072,
16+
"nonce": "0x0000000000000000",
17+
"extraData": "",
18+
"stateRoot": ""
19+
},
20+
"hardforks": [
21+
{
22+
"name": "chainstart",
23+
"block": 0,
24+
"consensus": "poa",
25+
"finality": null
26+
},
27+
{
28+
"name": "istanbul",
29+
"block": 5240580,
30+
"consensus": "poa",
31+
"finality": null
32+
}
33+
],
34+
"bootstrapNodes": [
35+
{
36+
"ip": "54.70.158.106",
37+
"port": 30303,
38+
"id": "59c9250cb805409e84c9cd0038e97d8e5e4605b928663675869ebdfd4c251d80ccad76267a5eb2f4362ddceb5ec671f7595463adfc0a12e9f68dbf233072db41",
39+
"location": "",
40+
"comment": ""
41+
},
42+
{
43+
"ip": "99.81.92.124",
44+
"port": 30303,
45+
"id": "e487ebacbdad3418905d2ed7f009fa5dbd17d73880854884acc604c0afc1a60a396aa90cb2741278c555a4e30ffc6ffc1c29e83840aa22009ec92fe53f81ec04",
46+
"location": "",
47+
"comment": ""
48+
},
49+
{
50+
"ip": "54.201.62.74",
51+
"port": 30303,
52+
"id": "563f12602a117201b39ebeea108185abb15d9286830c074640c9fccbaaaabcc7fe2c95682cc43f95b95059f6d0dc4c9becbc1b2bd78e0c5ef5fddff07d85ba0e",
53+
"location": "",
54+
"comment": ""
55+
},
56+
{
57+
"ip": "3.121.165.10",
58+
"port": 30303,
59+
"id": "5903b3acebdc4a34800f6923e5f3aec3ca7e5d1285bec4adb9f20ebb0f87a3bebdd748b1849ca1108a9f1e37ff9ced0b475292b8effc29e95d49ec438f244b02",
60+
"location": "",
61+
"comment": ""
62+
},
63+
{
64+
"ip": "54.93.159.98",
65+
"port": 30303,
66+
"id": "8f8e35a6dcacfee946f46447b4703c84f4e485e478143997f86b1834e1b0bb78dab363d700dff3147442b9d3e2a1c521f79340c436eb7245a97c7fe385b89a5d",
67+
"location": "",
68+
"comment": ""
69+
},
70+
{
71+
"ip": "52.31.129.130",
72+
"port": 30303,
73+
"id": "bd228aa03cf4a88491c81c5f3ab4a1437df3b463081cc93943c4d3ab37f1e4f8081c6995eca076f717d4fdf9a277c750bd0289477ac151f1e2b024747dcd1747",
74+
"location": "",
75+
"comment": ""
76+
}
77+
]
78+
}

0 commit comments

Comments
 (0)