From a1e9efe849a3cb22912b8912de75f0f6a81b7568 Mon Sep 17 00:00:00 2001 From: Vinay Pulim Date: Wed, 22 Aug 2018 00:29:21 -0400 Subject: [PATCH] Added support for private network parameters --- docs/index.md | 3 ++- index.js | 13 ++++++++-- tests/chains.js | 21 ++++++++++++++++ tests/testnet.json | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 tests/testnet.json diff --git a/docs/index.md b/docs/index.md index ff3a823..f8385bc 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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 diff --git a/index.js b/index.js index c5b7509..4bc8282 100644 --- a/index.js +++ b/index.js @@ -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') { @@ -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') } @@ -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'] } /** diff --git a/tests/chains.js b/tests/chains.js index d35ffc6..59bc9e7 100644 --- a/tests/chains.js +++ b/tests/chains.js @@ -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() + }) }) diff --git a/tests/testnet.json b/tests/testnet.json new file mode 100644 index 0000000..84b8f99 --- /dev/null +++ b/tests/testnet.json @@ -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": "" + } + ] +}