Skip to content

Commit 37e31ab

Browse files
authored
common: GethGenesis interface (#3973)
* common: GethGenesis interface * monorepo: apply gethGenesis interface throughout monorepo * common: add pragueTime to gethGenesis * common: fix poa adjusted test case * common: minor improvements * chore: remove non null assertion * common: add typecasting note
1 parent d1c6ea7 commit 37e31ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+347
-1211
lines changed

packages/blockchain/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ For many custom chains we might come across a genesis configuration, which can b
127127
// ./examples/gethGenesis.ts
128128

129129
import { createBlockchain } from '@ethereumjs/blockchain'
130-
import { createCommonFromGethGenesis } from '@ethereumjs/common'
131-
import { bytesToHex, parseGethGenesisState } from '@ethereumjs/util'
130+
import { createCommonFromGethGenesis, parseGethGenesisState } from '@ethereumjs/common'
131+
import { bytesToHex} from '@ethereumjs/util'
132132

133133
import { postMergeGethGenesis } from './genesisData/post-merge.ts'
134134

packages/blockchain/examples/gethGenesis.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createBlockchain } from '@ethereumjs/blockchain'
2-
import { createCommonFromGethGenesis } from '@ethereumjs/common'
2+
import { createCommonFromGethGenesis, parseGethGenesisState } from '@ethereumjs/common'
33
import { postMergeGethGenesis } from '@ethereumjs/testdata'
4-
import { bytesToHex, parseGethGenesisState } from '@ethereumjs/util'
4+
import { bytesToHex } from '@ethereumjs/util'
55

66
const main = async () => {
77
// Load geth genesis file

packages/blockchain/src/blockchain.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,8 @@ import { DBManager } from './db/manager.ts'
3030
import { DBTarget } from './db/operation.ts'
3131

3232
import type { HeaderData } from '@ethereumjs/block'
33-
import type { CliqueConfig } from '@ethereumjs/common'
34-
import type {
35-
BigIntLike,
36-
DB,
37-
DBObject,
38-
GenesisState,
39-
VerkleExecutionWitness,
40-
} from '@ethereumjs/util'
33+
import type { CliqueConfig, GenesisState } from '@ethereumjs/common'
34+
import type { BigIntLike, DB, DBObject, VerkleExecutionWitness } from '@ethereumjs/util'
4135
import type { Debugger } from 'debug'
4236
import type {
4337
BlockchainEvent,

packages/blockchain/src/helpers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { ChainGenesis } from '@ethereumjs/common'
22
import { genesisMPTStateRoot } from '@ethereumjs/mpt'
3-
import { type GenesisState } from '@ethereumjs/util'
43

5-
import type { Chain, Common } from '@ethereumjs/common'
4+
import type { Chain, Common, GenesisState } from '@ethereumjs/common'
65

76
/**
87
* Safe creation of a new Blockchain object awaiting the initialization function,

packages/blockchain/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Block, BlockHeader } from '@ethereumjs/block'
2-
import type { Common, ConsensusAlgorithm } from '@ethereumjs/common'
3-
import type { DB, DBObject, GenesisState } from '@ethereumjs/util'
2+
import type { Common, ConsensusAlgorithm, GenesisState } from '@ethereumjs/common'
3+
import type { DB, DBObject } from '@ethereumjs/util'
44
import type { EventEmitter } from 'eventemitter3'
55
import type { Blockchain } from './index.ts'
66

packages/blockchain/test/utils.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
import { createCommonFromGethGenesis } from '@ethereumjs/common'
1+
import {
2+
type GethGenesis,
3+
createCommonFromGethGenesis,
4+
parseGethGenesisState,
5+
} from '@ethereumjs/common'
26
import { genesisMPTStateRoot } from '@ethereumjs/mpt'
37
import { postMergeGethGenesis } from '@ethereumjs/testdata'
4-
import { bytesToHex, parseGethGenesisState } from '@ethereumjs/util'
8+
import { bytesToHex } from '@ethereumjs/util'
59
import { assert, describe, it } from 'vitest'
610

711
import { createBlockchain } from '../src/index.ts'
812

913
import type { Blockchain } from '../src/blockchain.ts'
1014

11-
async function getBlockchain(gethGenesis: any): Promise<Blockchain> {
15+
async function getBlockchain(gethGenesis: GethGenesis): Promise<Blockchain> {
1216
const common = createCommonFromGethGenesis(gethGenesis, { chain: 'kiln' })
1317
const genesisState = parseGethGenesisState(gethGenesis)
1418
const blockchain = await createBlockchain({

packages/client/bin/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { generateClientConfig, getArgs } from './utils.ts'
1919
import type * as http from 'http'
2020
import type { Block, BlockBytes } from '@ethereumjs/block'
2121
import type { ConsensusDict } from '@ethereumjs/blockchain'
22-
import type { GenesisState } from '@ethereumjs/util'
22+
import type { GenesisState } from '@ethereumjs/common'
2323
import type { AbstractLevel } from 'abstract-level'
2424
import type { Server as RPCServer } from 'jayson/promise/index.js'
2525
import type { Config } from '../src/config.ts'

packages/client/bin/repl.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { createInlineClient } from '../src/util/index.ts'
66
import { startRPCServers } from './startRPC.ts'
77
import { generateClientConfig, getArgs } from './utils.ts'
88

9-
import type { Common } from '@ethereumjs/common'
10-
import type { GenesisState } from '@ethereumjs/util'
9+
import type { Common, GenesisState } from '@ethereumjs/common'
1110
import type { Config } from '../src/config.ts'
1211
import type { EthereumClient } from '../src/index.ts'
1312
import type { ClientOpts } from '../src/types.ts'

packages/client/bin/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
createCommonFromGethGenesis,
1414
createCustomCommon,
1515
getPresetChainConfig,
16+
parseGethGenesisState,
1617
} from '@ethereumjs/common'
1718
import {
1819
EthereumJSErrorWithoutCode,
@@ -24,7 +25,6 @@ import {
2425
createAddressFromString,
2526
ecrecover,
2627
hexToBytes,
27-
parseGethGenesisState,
2828
randomBytes,
2929
setLengthLeft,
3030
} from '@ethereumjs/util'
@@ -52,8 +52,8 @@ import { Event } from '../src/types.ts'
5252
import { parseMultiaddrs } from '../src/util/index.ts'
5353
import { setupMetrics } from '../src/util/metrics.ts'
5454

55-
import type { CustomCrypto } from '@ethereumjs/common'
56-
import type { Address, GenesisState, PrefixedHexString } from '@ethereumjs/util'
55+
import type { CustomCrypto, GenesisState } from '@ethereumjs/common'
56+
import type { Address, PrefixedHexString } from '@ethereumjs/util'
5757
import type { Logger } from '../src/logging.ts'
5858
import type { ClientOpts } from '../src/types.ts'
5959

packages/client/src/blockchain/chain.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { Event } from '../types.ts'
88

99
import type { Block, BlockHeader } from '@ethereumjs/block'
1010
import type { Blockchain, ConsensusDict } from '@ethereumjs/blockchain'
11-
import type { DB, DBObject, GenesisState } from '@ethereumjs/util'
11+
import type { GenesisState } from '@ethereumjs/common'
12+
import type { DB, DBObject } from '@ethereumjs/util'
1213
import type { AbstractLevel } from 'abstract-level'
1314
import type { Config } from '../config.ts'
1415

packages/client/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Event } from './types.ts'
44
import { getPackageJSON } from './util/index.ts'
55

66
import type { Blockchain } from '@ethereumjs/blockchain'
7-
import type { GenesisState } from '@ethereumjs/util'
7+
import type { GenesisState } from '@ethereumjs/common'
88
import type { AbstractLevel } from 'abstract-level'
99
import type { Config } from './config.ts'
1010
import type { MultiaddrLike } from './types.ts'

packages/client/src/util/inclineClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CliqueConsensus, createBlockchain } from '@ethereumjs/blockchain'
2-
import { type Common, ConsensusAlgorithm } from '@ethereumjs/common'
2+
import { ConsensusAlgorithm } from '@ethereumjs/common'
33
import { Level } from 'level'
44
import { MemoryLevel } from 'memory-level'
55

@@ -8,7 +8,7 @@ import { Config } from '../config.ts'
88
import { LevelDB } from '../execution/level.ts'
99

1010
import type { ConsensusDict } from '@ethereumjs/blockchain'
11-
import type { GenesisState } from '@ethereumjs/util'
11+
import type { Common, GenesisState } from '@ethereumjs/common'
1212
import type { AbstractLevel } from 'abstract-level'
1313

1414
export async function createInlineClient(

packages/client/src/util/vkt.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
setLengthLeft,
1010
} from '@ethereumjs/util'
1111

12-
import type { Common } from '@ethereumjs/common'
13-
import type { GenesisState, PrefixedHexString, StoragePair } from '@ethereumjs/util'
12+
import type { Common, GenesisState, StoragePair } from '@ethereumjs/common'
13+
import type { PrefixedHexString } from '@ethereumjs/util'
1414

1515
export async function generateVKTStateRoot(genesisState: GenesisState, common: Common) {
1616
const state = new StatefulVerkleStateManager({ common })
@@ -19,7 +19,7 @@ export async function generateVKTStateRoot(genesisState: GenesisState, common: C
1919
for (const addressStr of Object.keys(genesisState)) {
2020
const addrState = genesisState[addressStr]
2121
let nonce, balance, code
22-
let storage: StoragePair[] = []
22+
let storage: StoragePair[] | undefined = []
2323
if (Array.isArray(addrState)) {
2424
;[balance, code, storage, nonce] = addrState
2525
} else {
@@ -29,7 +29,7 @@ export async function generateVKTStateRoot(genesisState: GenesisState, common: C
2929
}
3030
const address = createAddressFromString(addressStr)
3131
await state.putAccount(address, new Account())
32-
const codeBuf = hexToBytes((code as string) ?? '0x')
32+
const codeBuf = hexToBytes(code ?? '0x')
3333
if (common.customCrypto?.keccak256 === undefined) {
3434
throw Error('keccak256 required')
3535
}

packages/client/test/execution/vmexecution.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Mainnet,
77
createCommonFromGethGenesis,
88
createCustomCommon,
9+
parseGethGenesisState,
910
} from '@ethereumjs/common'
1011
import {
1112
customChainConfig,
@@ -14,7 +15,6 @@ import {
1415
mainnetBlocks,
1516
withdrawalsGethGenesis,
1617
} from '@ethereumjs/testdata'
17-
import { bytesToHex, parseGethGenesisState } from '@ethereumjs/util'
1818
import { createVM } from '@ethereumjs/vm'
1919
import { assert, describe, it } from 'vitest'
2020

@@ -25,6 +25,7 @@ import { closeRPC, setupChain, testSetup } from '../rpc/helpers.ts'
2525

2626
import type { ExecutionPayload } from '@ethereumjs/block'
2727
import type { Blockchain } from '@ethereumjs/blockchain'
28+
import { bytesToHex } from '@ethereumjs/util'
2829

2930
const shanghaiPayload: ExecutionPayload = {
3031
blockNumber: '0x1',

packages/client/test/integration/miner.spec.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import { Hardfork, createCommonFromGethGenesis } from '@ethereumjs/common'
2-
import {
3-
Address,
4-
bytesToHex,
5-
concatBytes,
6-
hexToBytes,
7-
parseGethGenesisState,
8-
} from '@ethereumjs/util'
1+
import { Hardfork, createCommonFromGethGenesis, parseGethGenesisState } from '@ethereumjs/common'
2+
import { Address, bytesToHex, concatBytes, hexToBytes } from '@ethereumjs/util'
93
import { assert, describe, it } from 'vitest'
104

115
import { Config } from '../../src/config.ts'

packages/client/test/integration/pow.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { rmSync } from 'fs'
2-
import { Hardfork, createCommonFromGethGenesis } from '@ethereumjs/common'
3-
import { createAddressFromPrivateKey, hexToBytes, parseGethGenesisState } from '@ethereumjs/util'
2+
import { Hardfork, createCommonFromGethGenesis, parseGethGenesisState } from '@ethereumjs/common'
43
import { assert, describe, it } from 'vitest'
54

65
import { Config } from '../../src/index.ts'
76
import { createInlineClient } from '../../src/util/index.ts'
87

9-
import type { Address } from '@ethereumjs/util'
8+
import { type Address, createAddressFromPrivateKey, hexToBytes } from '@ethereumjs/util'
109

1110
const pk = hexToBytes('0x95a602ff1ae30a2243f400dcf002561b9743b2ae9827b1008e3714a5cc1c0cfe')
1211
const minerAddress = createAddressFromPrivateKey(pk)

packages/client/test/rpc/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Mainnet,
77
createCommonFromGethGenesis,
88
parseGethGenesis,
9+
parseGethGenesisState,
910
} from '@ethereumjs/common'
1011
import { getGenesis } from '@ethereumjs/genesis'
1112
import {
@@ -14,7 +15,6 @@ import {
1415
KECCAK256_RLP,
1516
createAddressFromString,
1617
hexToBytes,
17-
parseGethGenesisState,
1818
} from '@ethereumjs/util'
1919
import { buildBlock } from '@ethereumjs/vm'
2020
import { Client, Server as RPCServer } from 'jayson/promise/index.js'
@@ -36,8 +36,8 @@ import { mockBlockchain } from './mockBlockchain.ts'
3636

3737
import type { AddressInfo } from 'node:net'
3838
import type { Blockchain } from '@ethereumjs/blockchain'
39+
import type { GenesisState } from '@ethereumjs/common'
3940
import type { TypedTransaction } from '@ethereumjs/tx'
40-
import type { GenesisState } from '@ethereumjs/util'
4141
import type { IncomingMessage } from 'connect'
4242
import type { HttpClient, HttpServer } from 'jayson/promise/index.js'
4343
import type { EthereumClient } from '../../src/client.ts'

packages/client/test/sim/beaconsync.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { createCommonFromGethGenesis } from '@ethereumjs/common'
2-
import { bytesToHex, hexToBytes, parseGethGenesisState, privateToAddress } from '@ethereumjs/util'
1+
import { createCommonFromGethGenesis, parseGethGenesisState } from '@ethereumjs/common'
2+
import { bytesToHex, hexToBytes, privateToAddress } from '@ethereumjs/util'
33
import debug from 'debug'
44
import { Client } from 'jayson/promise/index.js'
55
import { assert, describe, it } from 'vitest'

packages/client/test/sim/configs/eof.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ export const eofConfig = {
892892
extraData: '',
893893
gasLimit: '0x400000',
894894
nonce: '0x1234',
895-
mixhash: '0x0000000000000000000000000000000000000000000000000000000000000000',
895+
mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
896896
parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
897897
timestamp: '0',
898898
}

packages/client/test/sim/configs/mainnet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ export const mainnetConfig = {
892892
extraData: '',
893893
gasLimit: '0x400000',
894894
nonce: '0x1234',
895-
mixhash: '0x0000000000000000000000000000000000000000000000000000000000000000',
895+
mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
896896
parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
897897
timestamp: '0',
898898
}

packages/client/test/sim/snapsync.spec.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import { createCommonFromGethGenesis } from '@ethereumjs/common'
2-
import {
3-
bytesToHex,
4-
createAddressFromString,
5-
hexToBytes,
6-
parseGethGenesisState,
7-
privateToAddress,
8-
} from '@ethereumjs/util'
1+
import { createCommonFromGethGenesis, parseGethGenesisState } from '@ethereumjs/common'
2+
import { bytesToHex, createAddressFromString, hexToBytes, privateToAddress } from '@ethereumjs/util'
93
import debug from 'debug'
104
import { Client } from 'jayson/promise/index.js'
115
import { assert, describe, it } from 'vitest'

packages/client/test/testdata/geth-genesis/kaustinen2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ export const kaustinen2Data = {
881881
extraData: '',
882882
gasLimit: '0x17D7840',
883883
nonce: '0x1234',
884-
mixhash: '0x0000000000000000000000000000000000000000000000000000000000000000',
884+
mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
885885
parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
886886
timestamp: '1700825700',
887887
}

packages/client/test/testdata/geth-genesis/kaustinen6.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ export const kaustinen6Data = {
926926
extraData: '',
927927
gasLimit: '0x17D7840',
928928
nonce: '0x1234',
929-
mixhash: '0x0000000000000000000000000000000000000000000000000000000000000000',
929+
mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
930930
parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
931931
timestamp: '1712918460',
932932
}

packages/common/src/constructors.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Common, parseGethGenesis } from './index.ts'
22

3+
import type { GethGenesis } from './gethGenesis.ts'
34
import type { BaseOpts, ChainConfig, GethConfigOpts } from './index.ts'
45

56
/**
@@ -33,18 +34,21 @@ export function createCustomCommon(
3334
}
3435

3536
/**
36-
* Static method to load and set common from a geth genesis JSON
37-
* @param genesisJSON JSON of geth configuration
37+
* Static method to load and set common from a geth genesis object
38+
* @param gethGenesis GethGenesis object
3839
* @param opts additional {@link GethConfigOpts} for configuring common
3940
* @returns Common
4041
*/
4142
export function createCommonFromGethGenesis(
42-
genesisJSON: any,
43+
genesisJSON: GethGenesis,
4344
{ chain, eips, genesisHash, hardfork, params, customCrypto }: GethConfigOpts,
4445
): Common {
4546
const genesisParams = parseGethGenesis(genesisJSON, chain)
4647
const common = new Common({
47-
chain: genesisParams,
48+
chain: {
49+
...genesisParams,
50+
name: genesisParams.name ?? 'Custom chain',
51+
} as ChainConfig, // Typecasting because of `string` -> `PrefixedHexString` mismatches
4852
eips,
4953
params,
5054
hardfork: hardfork ?? genesisParams.hardfork,

0 commit comments

Comments
 (0)