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

Commit

Permalink
Added support for private network parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
vpulim committed Aug 22, 2018
1 parent 029fec0 commit 9bc9b1a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class Common {
} else {
throw new Error(`Chain with name ${chain} not supported`)
}
} else if (typeof (chain) === 'object') {
const required = ['networkId', 'genesis', 'hardforks', 'bootstrapNodes']
for (let param of required) {
if (chain[param] === undefined) {
throw new Error(`Missing required chain parameter: ${param}`)
}
}
this._chainParams = chain
} else {
throw new Error('Wrong input format')
}
Expand Down Expand Up @@ -362,7 +370,7 @@ class Common {
* @returns {String} chain name (lower case)
*/
chainName () {
return chainParams['names'][this.chainId()]
return chainParams['names'][this.chainId()] || this._chainParams['name']
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,25 @@ tape('[Common]: Initialization / Chain params', function (t) {

st.end()
})

t.test('Should provide correct access to private network chain parameters', function (st) {
let chainParams = require('./testnet.json')
let c = new Common(chainParams, 'byzantium')
st.equal(c.chainName(), 'testnet', 'should initialize with chain name')
st.equal(c.chainId(), 12345, 'should return correct chain Id')
st.equal(c.networkId(), 12345, 'should return correct network Id')
st.equal(c.genesis().hash, '0xaa00000000000000000000000000000000000000000000000000000000000000', 'should return correct genesis hash')
st.equal(c.hardforks()[3]['block'], 3, 'should return correct hardfork data')
st.equal(c.bootstrapNodes()[1].ip, '10.0.0.2', 'should return a bootstrap node array')

st.end()
})

t.test('Should handle custom chain parameters with missing field', function (st) {
let chainParams = require('./testnet.json')
delete chainParams['hardforks']
st.throws(function () { new Common(chainParams) }, /Missing required/, 'should throw an exception on missing parameter') // eslint-disable-line no-new

st.end()
})
})
63 changes: 63 additions & 0 deletions tests/testnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "testnet",
"chainId": 12345,
"networkId": 12345,
"comment": "Private test network",
"genesis": {
"hash": "0xaa00000000000000000000000000000000000000000000000000000000000000",
"timestamp": null,
"gasLimit": 1000000,
"difficulty": 1,
"nonce": "0xbb00000000000000",
"extraData": "0xcc000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"stateRoot": "0xdd00000000000000000000000000000000000000000000000000000000000000"
},
"hardforks": [
{
"name": "chainstart",
"block": 0,
"consensus": "poa",
"finality": null
},
{
"name": "homestead",
"block": 1,
"consensus": "poa",
"finality": null
},
{
"name": "tangerineWhistle",
"block": 2,
"consensus": "poa",
"finality": null
},
{
"name": "spuriousDragon",
"block": 3,
"consensus": "poa",
"finality": null
},
{
"name": "byzantium",
"block": 4,
"consensus": "poa",
"finality": null
}
],
"bootstrapNodes": [
{
"ip": "10.0.0.1",
"port": 30303,
"id": "11000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"location": "",
"comment": ""
},
{
"ip": "10.0.0.2",
"port": 30303,
"id": "22000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"location": "",
"comment": ""
}
]
}

0 comments on commit 9bc9b1a

Please sign in to comment.