Skip to content

Commit

Permalink
Common: method deprecations (new) (#1698)
Browse files Browse the repository at this point in the history
* common: delete deprecated hardforkBlock method

* common: adapt tests to use hardforkBlockBN

* common: delete deprecated nextHardforkBlock method

* common/tests: rename nextHardforkBlock() calls
to nextHardforkBlockBN()

* common: adapt tests to use BN.eqn(number)

* common: delete deprecated networkId() method

* common: delete tests for networkId()

* common: delete deprecated chainId() method

* common: delete tests for .chainId()

* delete deprecated forCustomChain meethod

* common: delete forCustomChain() test

* blockchain: switch forCustomChain to Common.custom

* client: switch fromCustomChain to Common.custom

* tx: switch forCustomChain to Common.custom

* VM: switch forCustomChain to Common.custom

* common: internalize _getInitializedChains() method

* common: delete deprecaed chains/index.ts file

* client: switch to Common._getInitializedChains

* common: remove genesisStates/index.ts

* common: remove test for genesisStates
  • Loading branch information
ScottyPoi authored and holgerd77 committed Apr 7, 2022
1 parent 55c157c commit f28206a
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 273 deletions.
16 changes: 10 additions & 6 deletions packages/blockchain/test/clique.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,7 @@ tape('Clique: Initialization', (t) => {
t.test(
'Clique Voting: Epoch transitions reset all votes to allow chain checkpointing',
async (st) => {
const common = Common.forCustomChain(
'rinkeby',
const common = Common.custom(
{
consensus: {
type: ConsensusType.ProofOfAuthority,
Expand All @@ -546,7 +545,10 @@ tape('Clique: Initialization', (t) => {
},
},
},
'chainstart'
{
baseChain: Chain.Rinkeby,
hardfork: Hardfork.Chainstart,
}
)
const { blocks, blockchain } = await initWithSigners([A, B], common)
await addNextBlock(blockchain, blocks, A, [C, true], undefined, common)
Expand Down Expand Up @@ -597,8 +599,7 @@ tape('Clique: Initialization', (t) => {
t.test(
'Clique Voting: Recent signatures should not reset on checkpoint blocks imported in a batch',
async (st) => {
const common = Common.forCustomChain(
'rinkeby',
const common = Common.custom(
{
consensus: {
type: ConsensusType.ProofOfAuthority,
Expand All @@ -609,7 +610,10 @@ tape('Clique: Initialization', (t) => {
},
},
},
'chainstart'
{
baseChain: Chain.Rinkeby,
hardfork: Hardfork.Chainstart,
}
)
const { blocks, blockchain } = await initWithSigners([A, B, C], common)
await addNextBlock(blockchain, blocks, A, undefined, undefined, common)
Expand Down
4 changes: 2 additions & 2 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { existsSync } from 'fs'
import { ensureDirSync, readFileSync, removeSync } from 'fs-extra'
import Common, { Chain, Hardfork } from '@ethereumjs/common'
import { _getInitializedChains } from '@ethereumjs/common/dist/chains'
import { Address, toBuffer, BN } from 'ethereumjs-util'
import { Address, toBuffer } from 'ethereumjs-util'
import { parseMultiaddrs, parseGenesisState, parseCustomParams } from '../lib/util'
import EthereumClient from '../lib/client'
import { Config, DataDirectory } from '../lib/config'
Expand All @@ -21,7 +21,7 @@ const { hideBin } = require('yargs/helpers')

type Account = [address: Address, privateKey: Buffer]

const networks = Object.entries(_getInitializedChains().names)
const networks = Object.entries(Common._getInitializedChains().names)

let logger: Logger

Expand Down
5 changes: 4 additions & 1 deletion packages/client/test/miner/miner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ tape('[Miner]', async (t) => {
t.test('assembleBlocks() -> should not include tx under the baseFee', async (t) => {
t.plan(1)
const customChainParams = { hardforks: [{ name: 'london', block: 0 }] }
const common = Common.forCustomChain(CommonChain.Rinkeby, customChainParams, Hardfork.London)
const common = Common.custom(customChainParams, {
baseChain: CommonChain.Rinkeby,
hardfork: Hardfork.London,
})
const config = new Config({ transports: [], accounts, mine: true, common })
const pool = new PeerPool() as any
const chain = new FakeChain() as any
Expand Down
45 changes: 0 additions & 45 deletions packages/common/src/chains/index.ts

This file was deleted.

40 changes: 0 additions & 40 deletions packages/common/src/genesisStates/index.ts

This file was deleted.

107 changes: 37 additions & 70 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { EventEmitter } from 'events'
import { buf as crc32Buffer } from 'crc-32'
import { BN, BNLike, toType, TypeOutput, intToBuffer } from 'ethereumjs-util'
import { _getInitializedChains } from './chains'
import { hardforks as HARDFORK_CHANGES } from './hardforks'
import { EIPs } from './eips'
import {
Expand All @@ -11,6 +10,12 @@ import {
GenesisState,
Hardfork as HardforkParams,
} from './types'
import mainnet from './chains/mainnet.json'
import ropsten from './chains/ropsten.json'
import rinkeby from './chains/rinkeby.json'
import kovan from './chains/kovan.json'
import goerli from './chains/goerli.json'
import sepolia from './chains/sepolia.json'

export enum CustomChain {
/**
Expand Down Expand Up @@ -290,48 +295,21 @@ export default class Common extends EventEmitter {
}
}

/**
* Creates a {@link Common} object for a custom chain, based on a standard one. It uses all the `Chain`
* params from {@link baseChain} except the ones overridden in {@link customChainParams}.
*
* @deprecated Use {@link Common.custom} instead
*
* @param baseChain The name (`mainnet`) or id (`1`) of a standard chain used to base the custom
* chain params on.
* @param customChainParams The custom parameters of the chain.
* @param hardfork String identifier ('byzantium') for hardfork (optional)
*/
static forCustomChain(
baseChain: string | number | Chain,
customChainParams: Partial<IChain>,
hardfork?: string | Hardfork
): Common {
const standardChainParams = Common._getChainParams(baseChain)

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

/**
* Static method to determine if a {@link chainId} is supported as a standard chain
* @param chainId BN id (`1`) of a standard chain
* @returns boolean
*/
static isSupportedChainId(chainId: BN): boolean {
const initializedChains: any = _getInitializedChains()
const initializedChains: any = this._getInitializedChains()
return Boolean(initializedChains['names'][chainId.toString()])
}

private static _getChainParams(
chain: string | number | Chain | BN,
customChains?: IChain[]
): IChain {
const initializedChains: any = _getInitializedChains(customChains)
const initializedChains: any = this._getInitializedChains(customChains)
if (typeof chain === 'number' || BN.isBN(chain)) {
chain = chain.toString()

Expand Down Expand Up @@ -765,17 +743,6 @@ export default class Common extends EventEmitter {
}
}

/**
* Returns the hardfork change block for hardfork provided or set
* @param hardfork Hardfork name, optional if HF set
* @returns Block number or null if unscheduled
* @deprecated Please use {@link Common.hardforkBlockBN} for large number support
*/
hardforkBlock(hardfork?: string | Hardfork): number | null {
const block = this.hardforkBlockBN(hardfork)
return toType(block, TypeOutput.Number)
}

/**
* Returns the hardfork change block for hardfork provided or set
* @param hardfork Hardfork name, optional if HF set
Expand Down Expand Up @@ -817,17 +784,6 @@ export default class Common extends EventEmitter {
return block ? block.eq(blockNumber) : false
}

/**
* Returns the change block for the next hardfork after the hardfork provided or set
* @param hardfork Hardfork name, optional if HF set
* @returns Block number or null if not available
* @deprecated Please use {@link Common.nextHardforkBlockBN} for large number support
*/
nextHardforkBlock(hardfork?: string | Hardfork): number | null {
const block = this.nextHardforkBlockBN(hardfork)
return toType(block, TypeOutput.Number)
}

/**
* Returns the change block for the next hardfork after the hardfork provided or set
* @param hardfork Hardfork name, optional if HF set
Expand Down Expand Up @@ -1005,15 +961,6 @@ export default class Common extends EventEmitter {
return this._hardfork
}

/**
* Returns the Id of current chain
* @returns chain Id
* @deprecated Please use {@link Common.chainIdBN} for large number support
*/
chainId(): number {
return toType(this.chainIdBN(), TypeOutput.Number)
}

/**
* Returns the Id of current chain
* @returns chain Id
Expand All @@ -1030,15 +977,6 @@ export default class Common extends EventEmitter {
return this._chainParams['name']
}

/**
* Returns the Id of current network
* @returns network Id
* @deprecated Please use {@link Common.networkIdBN} for large number support
*/
networkId(): number {
return toType(this.networkIdBN(), TypeOutput.Number)
}

/**
* Returns the Id of current network
* @returns network Id
Expand Down Expand Up @@ -1142,4 +1080,33 @@ export default class Common extends EventEmitter {
copy.removeAllListeners()
return copy
}

static _getInitializedChains(customChains?: IChain[]) {
const names: any = {
'1': 'mainnet',
'3': 'ropsten',
'4': 'rinkeby',
'42': 'kovan',
'5': 'goerli',
'11155111': 'sepolia',
}
const chains: any = {
mainnet,
ropsten,
rinkeby,
kovan,
goerli,
sepolia,
}
if (customChains) {
for (const chain of customChains) {
const name = chain.name
names[chain.chainId.toString()] = name
chains[name] = chain
}
}

chains['names'] = names
return chains
}
}
4 changes: 0 additions & 4 deletions packages/common/tests/chains.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ tape('[Common/Chains]: Initialization / Chain params', function (t: tape.Test) {
t.test('Should initialize with chain provided', function (st: tape.Test) {
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.ok(c.chainIdBN().eqn(1), 'should return correct chain Id')
st.equal(c.networkId(), 1, 'should return correct network Id')
st.ok(c.networkIdBN().eqn(1), 'should return correct network Id')
st.equal(c.hardfork(), 'istanbul', 'should set hardfork to current default hardfork')
st.equal(
Expand All @@ -26,9 +24,7 @@ tape('[Common/Chains]: Initialization / Chain params', function (t: tape.Test) {
t.test('Should initialize with chain provided by Chain enum', function (st: tape.Test) {
const c = new Common({ chain: Chain.Mainnet })
st.equal(c.chainName(), 'mainnet', 'should initialize with chain name')
st.equal(c.chainId(), 1, 'should return correct chain Id')
st.ok(c.chainIdBN().eqn(1), 'should return correct chain Id')
st.equal(c.networkId(), 1, 'should return correct network Id')
st.ok(c.networkIdBN().eqn(1), 'should return correct network Id')
st.equal(c.hardfork(), 'istanbul', 'should set hardfork to current default hardfork')
st.equal(
Expand Down
Loading

0 comments on commit f28206a

Please sign in to comment.