Skip to content
This repository was archived by the owner on Apr 6, 2020. It is now read-only.

Commit f426d93

Browse files
authored
Merge pull request #24 from ethereumjs/private-networks
Added support for private network parameters
2 parents 029fec0 + a1e9efe commit f426d93

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ Sets the chain
4545

4646
**Parameters**
4747

48-
- `chain` **([String][27] \| [Number][28])** String ('mainnet') or Number (1) chain representation
48+
- `chain` **([String][27] \| [Number][28] | Dictionary)** String ('mainnet') or Number (1) chain
49+
representation. Or, a Dictionary of chain parameters for a private network.
4950

5051
### setHardfork
5152

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class Common {
2222

2323
/**
2424
* Sets the chain
25-
* @param {String|Number} chain String ('mainnet') or Number (1) chain representation
25+
* @param {String|Number|Dictionary} chain String ('mainnet') or Number (1) chain
26+
* representation. Or, a Dictionary of chain parameters for a private network.
2627
*/
2728
setChain (chain) {
2829
if (typeof (chain) === 'number') {
@@ -37,6 +38,14 @@ class Common {
3738
} else {
3839
throw new Error(`Chain with name ${chain} not supported`)
3940
}
41+
} else if (typeof (chain) === 'object') {
42+
const required = ['networkId', 'genesis', 'hardforks', 'bootstrapNodes']
43+
for (let param of required) {
44+
if (chain[param] === undefined) {
45+
throw new Error(`Missing required chain parameter: ${param}`)
46+
}
47+
}
48+
this._chainParams = chain
4049
} else {
4150
throw new Error('Wrong input format')
4251
}
@@ -362,7 +371,7 @@ class Common {
362371
* @returns {String} chain name (lower case)
363372
*/
364373
chainName () {
365-
return chainParams['names'][this.chainId()]
374+
return chainParams['names'][this.chainId()] || this._chainParams['name']
366375
}
367376

368377
/**

tests/chains.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,25 @@ tape('[Common]: Initialization / Chain params', function (t) {
6060

6161
st.end()
6262
})
63+
64+
t.test('Should provide correct access to private network chain parameters', function (st) {
65+
let chainParams = require('./testnet.json')
66+
let c = new Common(chainParams, 'byzantium')
67+
st.equal(c.chainName(), 'testnet', 'should initialize with chain name')
68+
st.equal(c.chainId(), 12345, 'should return correct chain Id')
69+
st.equal(c.networkId(), 12345, 'should return correct network Id')
70+
st.equal(c.genesis().hash, '0xaa00000000000000000000000000000000000000000000000000000000000000', 'should return correct genesis hash')
71+
st.equal(c.hardforks()[3]['block'], 3, 'should return correct hardfork data')
72+
st.equal(c.bootstrapNodes()[1].ip, '10.0.0.2', 'should return a bootstrap node array')
73+
74+
st.end()
75+
})
76+
77+
t.test('Should handle custom chain parameters with missing field', function (st) {
78+
let chainParams = require('./testnet.json')
79+
delete chainParams['hardforks']
80+
st.throws(function () { new Common(chainParams) }, /Missing required/, 'should throw an exception on missing parameter') // eslint-disable-line no-new
81+
82+
st.end()
83+
})
6384
})

tests/testnet.json

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"name": "testnet",
3+
"chainId": 12345,
4+
"networkId": 12345,
5+
"comment": "Private test network",
6+
"genesis": {
7+
"hash": "0xaa00000000000000000000000000000000000000000000000000000000000000",
8+
"timestamp": null,
9+
"gasLimit": 1000000,
10+
"difficulty": 1,
11+
"nonce": "0xbb00000000000000",
12+
"extraData": "0xcc000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
13+
"stateRoot": "0xdd00000000000000000000000000000000000000000000000000000000000000"
14+
},
15+
"hardforks": [
16+
{
17+
"name": "chainstart",
18+
"block": 0,
19+
"consensus": "poa",
20+
"finality": null
21+
},
22+
{
23+
"name": "homestead",
24+
"block": 1,
25+
"consensus": "poa",
26+
"finality": null
27+
},
28+
{
29+
"name": "tangerineWhistle",
30+
"block": 2,
31+
"consensus": "poa",
32+
"finality": null
33+
},
34+
{
35+
"name": "spuriousDragon",
36+
"block": 3,
37+
"consensus": "poa",
38+
"finality": null
39+
},
40+
{
41+
"name": "byzantium",
42+
"block": 4,
43+
"consensus": "poa",
44+
"finality": null
45+
}
46+
],
47+
"bootstrapNodes": [
48+
{
49+
"ip": "10.0.0.1",
50+
"port": 30303,
51+
"id": "11000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
52+
"location": "",
53+
"comment": ""
54+
},
55+
{
56+
"ip": "10.0.0.2",
57+
"port": 30303,
58+
"id": "22000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
59+
"location": "",
60+
"comment": ""
61+
}
62+
]
63+
}

0 commit comments

Comments
 (0)