Skip to content

Commit

Permalink
common, all consumers: switch to an option dictionary for instantiati…
Browse files Browse the repository at this point in the history
…on as preparation for new eips option and future non-breaking option additions
  • Loading branch information
holgerd77 committed Sep 7, 2020
1 parent 567bd82 commit 14bc686
Show file tree
Hide file tree
Showing 35 changed files with 156 additions and 116 deletions.
2 changes: 1 addition & 1 deletion packages/block/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Block {
// TODO: Compute the hardfork based on this block's number. It can be implemented right now
// because the block number is not immutable, so the Common can get out of sync.
const hardfork = chainOptions.hardfork ? chainOptions.hardfork : null
this._common = new Common(chain, hardfork)
this._common = new Common({ chain, hardfork })
}

let rawTransactions
Expand Down
2 changes: 1 addition & 1 deletion packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class BlockHeader {
} else {
const chain = opts.chain ? opts.chain : 'mainnet'
const hardfork = opts.hardfork ? opts.hardfork : null
this._common = new Common(chain, hardfork)
this._common = new Common({ chain, hardfork })
}

const fields = [
Expand Down
8 changes: 4 additions & 4 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Block } from '../src/block'
tape('[Block]: block functions', function (t) {
t.test('should test block initialization', function (st) {
const block1 = new Block(undefined, { chain: 'ropsten' })
const common = new Common('ropsten')
const common = new Common({ chain: 'ropsten' })
const block2 = new Block(undefined, { common: common })
block1.setGenesisParams()
block2.setGenesisParams()
Expand Down Expand Up @@ -103,7 +103,7 @@ tape('[Block]: block functions', function (t) {
})

t.test('should test genesis hashes (ropsten)', function (st) {
const common = new Common('ropsten')
const common = new Common({ chain: 'ropsten' })
const genesisBlock = new Block(undefined, { common: common })
genesisBlock.setGenesisParams()
st.strictEqual(
Expand All @@ -115,7 +115,7 @@ tape('[Block]: block functions', function (t) {
})

t.test('should test genesis hashes (rinkeby)', function (st) {
const common = new Common('rinkeby')
const common = new Common({ chain: 'rinkeby' })
const genesisBlock = new Block(undefined, { common: common })
genesisBlock.setGenesisParams()
st.strictEqual(
Expand Down Expand Up @@ -150,7 +150,7 @@ tape('[Block]: block functions', function (t) {
// Set block number from test block to mainnet DAO fork block 1920000
blockData[0][8] = Buffer.from('1D4C00', 'hex')

const common = new Common('mainnet', 'dao')
const common = new Common({ chain: 'mainnet', hardfork: 'dao' })
st.throws(
function () {
new Block(blockData, { common: common })
Expand Down
2 changes: 1 addition & 1 deletion packages/block/test/header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tape('[Block]: Header functions', function (t) {

t.test('should test header initialization', function (st) {
const header1 = new BlockHeader(undefined, { chain: 'ropsten' })
const common = new Common('ropsten')
const common = new Common({ chain: 'ropsten' })
const header2 = new BlockHeader(undefined, { common: common })
header1.setGenesisParams()
header2.setGenesisParams()
Expand Down
2 changes: 1 addition & 1 deletion packages/blockchain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default class Blockchain implements BlockchainInterface {
} else {
const chain = opts.chain ? opts.chain : 'mainnet'
const hardfork = opts.hardfork ? opts.hardfork : null
this._common = new Common(chain, hardfork)
this._common = new Common({ chain, hardfork })
}

this._validatePow = opts.validatePow !== undefined ? opts.validatePow : true
Expand Down
4 changes: 2 additions & 2 deletions packages/blockchain/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('blockchain test', (t) => {
})

t.test('should throw on initialization with chain and common parameter', async (st) => {
const common = new Common('ropsten')
const common = new Common({ chain: 'ropsten' })

st.throws(() => {
new Blockchain({ chain: 'ropsten', common })
Expand Down Expand Up @@ -586,7 +586,7 @@ test('blockchain test', (t) => {
})

t.test('mismatched chains', async (st) => {
const common = new Common('mainnet')
const common = new Common({ chain: 'mainnet' })
const blockchain = new Blockchain({ common: common, validateBlocks: true, validatePow: false })
const blocks = [
new Block(undefined, { common: common }),
Expand Down
9 changes: 6 additions & 3 deletions packages/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ Here are some simple usage examples:
const Common = require('@ethereumjs/common')

// Instantiate with only the chain
let c = new Common('ropsten')
let c = new Common({ chain: 'ropsten' })
c.param('gasPrices', 'ecAddGas', 'byzantium') // 500

// Chain and hardfork provided
c = new Common('ropsten', 'byzantium')
c = new Common({ chain: 'ropsten', hardfork: 'byzantium' })
c.param('pow', 'minerReward') // 3000000000000000000

// Access genesis data for Ropsten network
Expand All @@ -46,7 +46,10 @@ It is encouraged to also explicitly set the `supportedHardforks` if the initiali
only supports a certain range of `hardforks`:

```javascript
let c = new Common('ropsten', null, ['byzantium', 'constantinople', 'petersburg'])
let c = new Common({
chain: 'ropsten',
supportedHardforks: ['byzantium', 'constantinople', 'petersburg'],
})
```

This will e.g. throw an error when a param is requested for an unsupported hardfork and
Expand Down
45 changes: 28 additions & 17 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ import { hardforks as hardforkChanges } from './hardforks'
import { EIPs } from './eips'
import { Chain } from './types'

/**
* Options for instantiating a [[Common]] instance.
*/
export interface CommonOpts {
/**
* String ('mainnet') or Number (1) chain
*/
chain: string | number | object
/**
* String identifier ('byzantium') for hardfork
*/
hardfork?: string | null
/**
* Limit parameter returns to the given hardforks
*/
supportedHardforks?: Array<string>
}

interface hardforkOptions {
/** optional, only allow supported HFs (default: false) */
onlySupported?: boolean
Expand Down Expand Up @@ -37,14 +55,14 @@ export default class Common {
): Common {
const standardChainParams = Common._getChainParams(baseChain)

return new Common(
{
return new Common({
chain: {
...standardChainParams,
...customChainParams,
},
hardfork,
supportedHardforks,
)
hardfork: hardfork,
supportedHardforks: supportedHardforks,
})
}

private static _getChainParams(chain: string | number): Chain {
Expand All @@ -65,20 +83,13 @@ export default class Common {

/**
* @constructor
* @param chain String ('mainnet') or Number (1) chain
* @param hardfork String identifier ('byzantium') for hardfork (optional)
* @param supportedHardforks Limit parameter returns to the given hardforks (optional)
*/
constructor(
chain: string | number | object,
hardfork?: string | null,
supportedHardforks?: Array<string>,
) {
this._chainParams = this.setChain(chain)
constructor(opts: CommonOpts) {
this._chainParams = this.setChain(opts.chain)
this._hardfork = null
this._supportedHardforks = supportedHardforks === undefined ? [] : supportedHardforks
if (hardfork) {
this.setHardfork(hardfork)
this._supportedHardforks = opts.supportedHardforks === undefined ? [] : opts.supportedHardforks
if (opts.hardfork) {
this.setHardfork(opts.hardfork)
}
}

Expand Down
32 changes: 20 additions & 12 deletions packages/common/tests/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@ import Common from '../src/'

tape('[Common]: Initialization / Chain params', function (t: tape.Test) {
t.test('Should initialize with chain provided', function (st: tape.Test) {
let c = new Common('mainnet')
let c = new Common({ chain: 'mainnet' })
st.equal(c.chainName(), 'mainnet', 'should initialize with chain name')
st.equal(c.chainId(), 1, 'should return correct chain Id')
st.equal(c.networkId(), 1, 'should return correct network Id')
st.equal(c.hardfork(), null, 'should set hardfork to null')
st.equal(c._isSupportedHardfork('constantinople'), true, 'should not restrict supported HFs')

c = new Common(1)
c = new Common({ chain: 1 })
st.equal(c.chainName(), 'mainnet', 'should initialize with chain Id')

st.end()
})

t.test('Should initialize with chain and hardfork provided', function (st: tape.Test) {
const c = new Common('mainnet', 'byzantium')
const c = new Common({ chain: 'mainnet', hardfork: 'byzantium' })
st.equal(c.hardfork(), 'byzantium', 'should return correct hardfork name')

st.end()
})

t.test('Should initialize with supportedHardforks provided', function (st: tape.Test) {
const c = new Common('mainnet', 'byzantium', ['byzantium', 'constantinople'])
const c = new Common({
chain: 'mainnet',
hardfork: 'byzantium',
supportedHardforks: ['byzantium', 'constantinople'],
})
st.equal(c._isSupportedHardfork('byzantium'), true, 'should return true for supported HF')
const msg = 'should return false for unsupported HF'
st.equal(c._isSupportedHardfork('spuriousDragon'), false, msg)
Expand All @@ -34,19 +38,23 @@ tape('[Common]: Initialization / Chain params', function (t: tape.Test) {

t.test('Should handle initialization errors', function (st: tape.Test) {
let f = function () {
new Common('chainnotexisting')
new Common({ chain: 'chainnotexisting' })
}
let msg = 'should throw an exception on non-existing chain'
st.throws(f, /not supported$/, msg) // eslint-disable-line no-new

f = function () {
new Common('mainnet', 'hardforknotexisting')
new Common({ chain: 'mainnet', hardfork: 'hardforknotexisting' })
}
msg = 'should throw an exception on non-existing hardfork'
st.throws(f, /not supported$/, msg) // eslint-disable-line no-new

f = function () {
new Common('mainnet', 'spuriousDragon', ['byzantium', 'constantinople'])
new Common({
chain: 'mainnet',
hardfork: 'spuriousDragon',
supportedHardforks: ['byzantium', 'constantinople'],
})
}
msg = 'should throw an exception on conflicting active/supported HF params'
st.throws(f, /supportedHardforks$/, msg) // eslint-disable-line no-new
Expand All @@ -55,7 +63,7 @@ tape('[Common]: Initialization / Chain params', function (t: tape.Test) {
})

t.test('Should provide correct access to chain parameters', function (st: tape.Test) {
const c = new Common('mainnet')
const c = new Common({ chain: 'mainnet' })
const hash = '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3'
st.equal(c.genesis().hash, hash, 'should return correct genesis hash')
st.equal(c.hardforks()[3]['block'], 2463000, 'should return correct hardfork data')
Expand All @@ -66,7 +74,7 @@ tape('[Common]: Initialization / Chain params', function (t: tape.Test) {
t.test('Should provide the bootnode information in a uniform way', function (st: tape.Test) {
const configs = ['mainnet', 'ropsten', 'rinkeby', 'goerli']
for (const network of configs) {
const c = new Common(network)
const c = new Common({ chain: network })
const bootnode = c.bootstrapNodes()[0]
st.equal(typeof bootnode.ip, 'string', 'returns the ip as string')
st.equal(typeof bootnode.port, 'number', 'returns the port as number')
Expand All @@ -86,7 +94,7 @@ tape('[Common]: Initialization / Chain params', function (t: tape.Test) {
})

t.test('Should be able to access data for all chains provided', function (st: tape.Test) {
const c = new Common('mainnet')
const c = new Common({ chain: 'mainnet' })
let hash = '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3'
st.equal(c.genesis().hash, hash, 'mainnet')
c.setChain('ropsten')
Expand All @@ -109,7 +117,7 @@ tape('[Common]: Initialization / Chain params', function (t: tape.Test) {
st: tape.Test,
) {
const chainParams = require('./testnet.json')
const c = new Common(chainParams, 'byzantium')
const c = new Common({ chain: chainParams, hardfork: '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')
Expand All @@ -129,7 +137,7 @@ tape('[Common]: Initialization / Chain params', function (t: tape.Test) {
delete chainParams['hardforks']
st.throws(
function () {
new Common(chainParams)
new Common({ chain: chainParams })
},
/Missing required/,
'should throw an exception on missing parameter',
Expand Down
Loading

1 comment on commit 14bc686

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 14bc686 Previous: 567bd82 Ratio
Block 9422905 1980 ops/sec (±4.61%) 2628 ops/sec (±4.54%) 1.33
Block 9422906 1972 ops/sec (±6.88%) 2698 ops/sec (±1.36%) 1.37
Block 9422907 1866 ops/sec (±6.95%) 2289 ops/sec (±11.51%) 1.23
Block 9422908 2051 ops/sec (±1.53%) 2573 ops/sec (±1.63%) 1.25
Block 9422909 2006 ops/sec (±1.49%) 2510 ops/sec (±1.82%) 1.25
Block 9422910 1977 ops/sec (±1.21%) 2559 ops/sec (±1.69%) 1.29
Block 9422911 1500 ops/sec (±13.95%) 2517 ops/sec (±1.89%) 1.68
Block 9422912 1886 ops/sec (±1.53%) 2494 ops/sec (±1.71%) 1.32
Block 9422913 1891 ops/sec (±1.43%) 2408 ops/sec (±1.85%) 1.27
Block 9422914 1892 ops/sec (±2.86%) 1884 ops/sec (±15.55%) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.