Skip to content

Commit

Permalink
common: parse depositContractAddress from genesis (#3422)
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed May 14, 2024
1 parent 0786896 commit f79a6f2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface ChainConfig {
bootstrapNodes: BootstrapNodeConfig[]
dnsNetworks?: string[]
consensus: ConsensusConfig
depositContractAddress?: string
depositContractAddress?: PrefixedHexString
}

// TODO: Remove the string type and only keep PrefixedHexString
Expand Down
6 changes: 5 additions & 1 deletion packages/common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ function parseGethParams(json: any, mergeForkIdPostMerge: boolean = true) {
timestamp: string
} = json
const genesisTimestamp = Number(unparsedTimestamp)
const { chainId }: { chainId: number } = config
const {
chainId,
depositContractAddress,
}: { chainId: number; depositContractAddress: PrefixedHexString } = config

// geth is not strictly putting empty fields with a 0x prefix
const extraData: PrefixedHexString =
Expand All @@ -84,6 +87,7 @@ function parseGethParams(json: any, mergeForkIdPostMerge: boolean = true) {
name,
chainId,
networkId: chainId,
depositContractAddress,
genesis: {
timestamp,
gasLimit,
Expand Down
36 changes: 36 additions & 0 deletions packages/common/test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,42 @@ describe('[Utils/Parse]', () => {
)
assert.equal(common.hardfork(), Hardfork.Shanghai, 'should correctly infer common hardfork')
})

it('should successfully assign mainnet deposit contract address when none provided', async () => {
const common = Common.fromGethGenesis(postMergeHardforkJSON, {
chain: 'customChain',
})
const depositContractAddress =
common['_chainParams'].depositContractAddress ??
Common.getInitializedChains().mainnet.depositContractAddress

assert.equal(
depositContractAddress,
Common.getInitializedChains().mainnet.depositContractAddress,
'should assign mainnet deposit contract'
)
})

it('should correctly parse deposit contract adddress', async () => {
// clone json out to not have side effects
const customJson = JSON.parse(JSON.stringify(postMergeHardforkJSON))
Object.assign(customJson.config, {
depositContractAddress: '0x4242424242424242424242424242424242424242',
})

const common = Common.fromGethGenesis(customJson, {
chain: 'customChain',
})
const depositContractAddress =
common['_chainParams'].depositContractAddress ??
Common.getInitializedChains().mainnet.depositContractAddress

assert.equal(
depositContractAddress,
'0x4242424242424242424242424242424242424242',
'should parse correct address'
)
})
})

const kilnForkHashes: any = {
Expand Down

0 comments on commit f79a6f2

Please sign in to comment.