Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common/Monorepo: Remove NetworkId #3513

Merged
merged 8 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 0 additions & 1 deletion packages/block/test/testdata/testnetMerge.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "testnetMerge",
"chainId": 55555,
"networkId": 55555,
"defaultHardfork": "istanbul",
"consensus": {
"type": "poa",
Expand Down
1 change: 0 additions & 1 deletion packages/blockchain/test/testdata/testnet.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "mainnet",
"chainId": 1,
"networkId": 1,
"defaultHardfork": "london",
"consensus": {
"type": "pow",
Expand Down
13 changes: 10 additions & 3 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,14 @@ const args: ClientOpts = yargs
choices: networks.map((n) => n[1]).filter((el) => isNaN(parseInt(el))),
default: 'mainnet',
})
.option('chainId', {
describe: 'Chain ID',
choices: networks.map((n) => parseInt(n[0])).filter((el) => !isNaN(el)),
default: undefined,
conflicts: ['customChain', 'customGenesisState', 'gethGenesis'], // Disallows custom chain data and chainId
})
.option('networkId', {
describe: 'Network ID',
describe: 'Network ID (deprecated, use chainId instead)',
holgerd77 marked this conversation as resolved.
Show resolved Hide resolved
choices: networks.map((n) => parseInt(n[0])).filter((el) => !isNaN(el)),
default: undefined,
conflicts: ['customChain', 'customGenesisState', 'gethGenesis'], // Disallows custom chain data and networkId
Expand Down Expand Up @@ -922,8 +928,9 @@ async function run() {

// TODO sharding: Just initialize kzg library now, in future it can be optimized to be
// loaded and initialized on the sharding hardfork activation
// Give network id precedence over network name
const chain = args.networkId ?? args.network ?? Chain.Mainnet
// Give chainId priority over networkId
// Give networkId precedence over network name
const chain = args.chainId ?? args.networkId ?? args.network ?? Chain.Mainnet
const cryptoFunctions: CustomCrypto = {}
const kzg = await loadKZG()

Expand Down
2 changes: 1 addition & 1 deletion packages/client/examples/private-geth-network.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Second, get geth configured to use the genesis parameters file just updated.

Now, let's run geth and ensure that its sealing blocks. Note, geth will prompt you for a password to unlock your signer account.

`geth --datadir data --nat extip:[your local ip address here] --networkid 15470 --unlock [the signer account you created] --mine --nodiscover`
`geth --datadir data --nat extip:[your local ip address here] --chainId 15470 --unlock [the signer account you created] --mine --nodiscover`

You should start seeing logs like below:

Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/blockchain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ export class Chain {
}

/**
* Network ID
* Chain ID
*/
get networkId(): bigint {
return this.config.chainCommon.networkId()
get chainId(): bigint {
return this.config.chainCommon.chainId()
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/net/protocol/ethprotocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ export class EthProtocol extends Protocol {
*/
encodeStatus(): any {
return {
networkId: bigIntToUnpaddedBytes(this.chain.networkId),
chainId: bigIntToUnpaddedBytes(this.chain.chainId),
td: bigIntToUnpaddedBytes(this.chain.blocks.td),
bestHash: this.chain.blocks.latest!.hash(),
genesisHash: this.chain.genesis.hash(),
Expand All @@ -417,7 +417,7 @@ export class EthProtocol extends Protocol {
*/
decodeStatus(status: any): any {
return {
networkId: bytesToBigInt(status.networkId),
chainId: bytesToBigInt(status.chainId),
td: bytesToBigInt(status.td),
bestHash: status.bestHash,
genesisHash: status.genesisHash,
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/net/protocol/lesprotocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class LesProtocol extends Protocol {
const forkID = [hexToBytes(forkHash), bigIntToUnpaddedBytes(nextFork ?? 0n)]

return {
networkId: bigIntToUnpaddedBytes(this.chain.networkId),
chainId: bigIntToUnpaddedBytes(this.chain.chainId),
headTd: bigIntToUnpaddedBytes(this.chain.headers.td),
headHash: this.chain.headers.latest?.hash(),
headNum: bigIntToUnpaddedBytes(this.chain.headers.height),
Expand Down Expand Up @@ -223,7 +223,7 @@ export class LesProtocol extends Protocol {
}
}
return {
networkId: bytesToBigInt(status.networkId),
chainId: bytesToBigInt(status.chainId),
headTd: bytesToBigInt(status.headTd),
headHash: status.headHash,
headNum: bytesToBigInt(status.headNum),
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/rpc/modules/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Admin {
const difficulty = latestHeader.difficulty.toString()
const genesis = bytesToHex(this._chain.genesis.hash())
const head = bytesToHex(latestHeader.mixHash)
const network = this._chain.networkId.toString()
const network = this._chain.chainId.toString()

const nodeInfo = {
name: clientName,
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export type DnsNetwork = string

export interface ClientOpts {
network?: string
chainId?: number
// Deprecated, use chainId instead
networkId?: number
sync?: SyncMode
lightServe?: boolean
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/blockchain/chain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('[Chain]', () => {
it('should retrieve chain properties', async () => {
const chain = await Chain.create({ config })
await chain.open()
assert.equal(chain.networkId, BigInt(1), 'get chain.networkId')
assert.equal(chain.chainId, BigInt(1), 'get chain.chainId')
assert.equal(chain.blocks.td.toString(10), '17179869184', 'get chain.blocks.td')
assert.equal(chain.blocks.height.toString(10), '0', 'get chain.blocks.height')
assert.equal(
Expand Down
7 changes: 3 additions & 4 deletions packages/client/test/cli/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function clientRunHelper(
describe('[CLI]', () => {
// chain network tests
it('should successfully start client with a custom network and network id', async () => {
const cliArgs = ['--network=sepolia', '--networkId=11155111']
const cliArgs = ['--network=sepolia', '--chainId=11155111']
const onData = (message: string, child: ChildProcessWithoutNullStreams, resolve: Function) => {
if (message.includes('Initializing Ethereumjs client')) {
assert.ok(
Expand Down Expand Up @@ -648,7 +648,6 @@ describe('[CLI]', () => {
const customChainJson = `{
"name": "customChain",
"chainId": 11155111,
"networkId": 11155111,
"defaultHardfork": "shanghai",
"consensus": {
"type": "pow",
Expand Down Expand Up @@ -782,13 +781,13 @@ describe('[CLI]', () => {
await clientRunHelper(cliArgs, onData, true)
}, 5000)
it('should not start client with conflicting parameters', async () => {
const cliArgs = ['--networkId', '--gethGenesis']
const cliArgs = ['--chainId', '--gethGenesis']
const onData = async (
message: string,
child: ChildProcessWithoutNullStreams,
resolve: Function
) => {
if (message.includes('Arguments networkId and gethGenesis are mutually exclusive')) {
if (message.includes('Arguments chainId and gethGenesis are mutually exclusive')) {
assert.ok(true, 'correctly errors on conflicting arguments')
}
child.kill(15)
Expand Down
8 changes: 4 additions & 4 deletions packages/client/test/net/protocol/ethprotocol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('[EthProtocol]', () => {
const config = new Config({ accountCache: 10000, storageCache: 1000 })
const chain = await Chain.create({ config })
const p = new EthProtocol({ config, chain })
Object.defineProperty(chain, 'networkId', {
Object.defineProperty(chain, 'chainId', {
get: () => {
return BigInt(1)
},
Expand All @@ -53,7 +53,7 @@ describe('[EthProtocol]', () => {
assert.deepEqual(
p.encodeStatus(),
{
networkId: hexToBytes('0x01'),
chainId: hexToBytes('0x01'),
td: hexToBytes('0x64'),
bestHash: '0xaa',
genesisHash: '0xbb',
Expand All @@ -62,13 +62,13 @@ describe('[EthProtocol]', () => {
'encode status'
)
const status = p.decodeStatus({
networkId: [0x01],
chainId: [0x01],
td: hexToBytes('0x64'),
bestHash: '0xaa',
genesisHash: '0xbb',
})
assert.ok(
status.networkId === BigInt(1) &&
status.chainId === BigInt(1) &&
status.td === BigInt(100) &&
status.bestHash === '0xaa' &&
status.genesisHash === '0xbb',
Expand Down
8 changes: 4 additions & 4 deletions packages/client/test/net/protocol/lesprotocol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('[LesProtocol]', () => {
mrc: { GetBlockHeaders: { base: 10, req: 10 } },
})
const p = new LesProtocol({ config, chain, flow })
Object.defineProperty(chain, 'networkId', {
Object.defineProperty(chain, 'chainId', {
get: () => {
return BigInt(1)
},
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('[LesProtocol]', () => {
})
let status = p.encodeStatus()
assert.ok(
bytesToHex(status.networkId) === '0x01' &&
bytesToHex(status.chainId) === '0x01' &&
bytesToHex(status.headTd) === '0x64' &&
status.headHash === '0xaa' &&
bytesToHex(status.headNum) === '0x64' &&
Expand All @@ -84,10 +84,10 @@ describe('[LesProtocol]', () => {
bytesToHex(status['flowControl/MRC'][0][2]) === '0x0a',
'encode status'
)
status = { ...status, networkId: [0x01] }
status = { ...status, chainId: [0x01] }
status = p.decodeStatus(status)
assert.ok(
status.networkId === BigInt(1) &&
status.chainId === BigInt(1) &&
status.headTd === BigInt(100) &&
status.headHash === '0xaa' &&
status.headNum === BigInt(100) &&
Expand Down
1 change: 0 additions & 1 deletion packages/client/test/rpc/eth/getProof.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const expectedProof = {
const testnetData = {
name: 'testnet2',
chainId: 12345,
networkId: 12345,
defaultHardfork: 'istanbul',
consensus: {
type: 'pow',
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/sim/beaconsync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('simple mainnet test run', async () => {
}

// Better add it as a option in startnetwork
process.env.NETWORKID = `${common.networkId()}`
process.env.NETWORKID = `${common.chainId()}`
const { teardownCallBack, result } = await startNetwork(network, client, {
filterKeywords,
filterOutWords,
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/sim/snapsync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('simple mainnet test run', async () => {
process.env.EXTRA_CL_PARAMS = '--params.CAPELLA_FORK_EPOCH 0'
}
// Better add it as a option in startnetwork
process.env.NETWORKID = `${common.networkId()}`
process.env.NETWORKID = `${common.chainId()}`
const { teardownCallBack, result } = await startNetwork(network, client, {
filterKeywords,
filterOutWords,
Expand Down
1 change: 0 additions & 1 deletion packages/client/test/testdata/common/testnet.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "testnet",
"chainId": 12345,
"networkId": 12345,
"defaultHardfork": "byzantium",
"consensus": {
"type": "pow",
Expand Down
1 change: 0 additions & 1 deletion packages/common/examples/genesisData/testnet.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "testnet1",
"chainId": 22222,
"networkId": 22222,
"defaultHardfork": "istanbul",
"consensus": {
"type": "poa",
Expand Down
1 change: 0 additions & 1 deletion packages/common/examples/genesisData/testnet2.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "testnet2",
"chainId": 33333,
"networkId": 33333,
"defaultHardfork": "istanbul",
"consensus": {
"type": "poa",
Expand Down
5 changes: 0 additions & 5 deletions packages/common/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const chains: ChainsDict = {
mainnet: {
name: 'mainnet',
chainId: 1,
networkId: 1,
defaultHardfork: 'shanghai',
consensus: {
type: 'pow',
Expand Down Expand Up @@ -162,7 +161,6 @@ export const chains: ChainsDict = {
goerli: {
name: 'goerli',
chainId: 5,
networkId: 5,
defaultHardfork: 'shanghai',
consensus: {
type: 'poa',
Expand Down Expand Up @@ -324,7 +322,6 @@ export const chains: ChainsDict = {
sepolia: {
name: 'sepolia',
chainId: 11155111,
networkId: 11155111,
defaultHardfork: 'shanghai',
consensus: {
type: 'pow',
Expand Down Expand Up @@ -459,7 +456,6 @@ export const chains: ChainsDict = {
holesky: {
name: 'holesky',
chainId: 17000,
networkId: 17000,
defaultHardfork: 'paris',
consensus: {
type: 'pos',
Expand Down Expand Up @@ -578,7 +574,6 @@ export const chains: ChainsDict = {
kaustinen6: {
name: 'kaustinen6',
chainId: 69420,
networkId: 69420,
defaultHardfork: 'osaka',
consensus: {
type: 'pos',
Expand Down
10 changes: 1 addition & 9 deletions packages/common/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class Common {
'Chain must be a string, number, or bigint when initialized with customChains passed in'
)
}
const required = ['networkId', 'genesis', 'hardforks', 'bootstrapNodes']
const required = ['chainId', 'genesis', 'hardforks', 'bootstrapNodes']
for (const param of required) {
if (!(param in chain)) {
throw new Error(`Missing required chain parameter: ${param}`)
Expand Down Expand Up @@ -847,14 +847,6 @@ export class Common {
return this._chainParams.name
}

/**
* Returns the Id of current network
* @returns network Id
*/
networkId(): bigint {
return BigInt(this._chainParams.networkId)
}

/**
* Returns the additionally activated EIPs
* (by using the `eips` constructor option)
Expand Down
6 changes: 0 additions & 6 deletions packages/common/src/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export function createCustomCommon(
{
name: CustomChain.PolygonMainnet,
chainId: 137,
networkId: 137,
},
opts
)
Expand All @@ -58,7 +57,6 @@ export function createCustomCommon(
{
name: CustomChain.PolygonMumbai,
chainId: 80001,
networkId: 80001,
},
opts
)
Expand All @@ -68,7 +66,6 @@ export function createCustomCommon(
{
name: CustomChain.ArbitrumOne,
chainId: 42161,
networkId: 42161,
},
opts
)
Expand All @@ -78,7 +75,6 @@ export function createCustomCommon(
{
name: CustomChain.xDaiChain,
chainId: 100,
networkId: 100,
},
opts
)
Expand All @@ -89,7 +85,6 @@ export function createCustomCommon(
{
name: CustomChain.OptimisticKovan,
chainId: 69,
networkId: 69,
},
opts
)
Expand All @@ -100,7 +95,6 @@ export function createCustomCommon(
{
name: CustomChain.OptimisticEthereum,
chainId: 10,
networkId: 10,
},
// Optimism has not implemented the London hardfork yet (targeting Q1.22)
{ hardfork: Hardfork.Berlin, ...opts }
Expand Down
1 change: 0 additions & 1 deletion packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type ConsensusConfig = {
export interface ChainConfig {
name: string
chainId: number | bigint
networkId: number | bigint
defaultHardfork?: string
comment?: string
url?: string
Expand Down
Loading
Loading