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

Added support for private network parameters #24

Merged
merged 1 commit into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ Sets the chain

**Parameters**

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

### setHardfork

Expand Down
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Common {

/**
* Sets the chain
* @param {String|Number} chain String ('mainnet') or Number (1) chain representation
* @param {String|Number|Dictionary} chain String ('mainnet') or Number (1) chain
* representation. Or, a Dictionary of chain parameters for a private network.
*/
setChain (chain) {
if (typeof (chain) === 'number') {
Expand All @@ -37,6 +38,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 +371,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": ""
}
]
}