Skip to content

Commit

Permalink
Common: allow overriding params (#3506)
Browse files Browse the repository at this point in the history
* common: expand custom chain type to support param overrides

* vm: update 2935 test to override history address param

* common: update types

* common: correctly build params cache with all available topics

* common: do not duplicate types

* common: remove unused params

* common: add override param test
  • Loading branch information
jochem-brouwer authored Jul 15, 2024
1 parent 4d8ad71 commit 80fcde6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
9 changes: 9 additions & 0 deletions packages/common/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,15 @@ export class Common {
} else {
this._mergeWithParamsCache(hfChanges[1])
}
if (
hfChanges[1].vm ||
hfChanges[1].gasConfig ||
hfChanges[1].gasPrices ||
hfChanges[1].pow ||
hfChanges[1].sharding
) {
this._mergeWithParamsCache(hfChanges[1])
}
if (hfChanges[0] === hardfork) break
}
// Iterate through all additionally activated EIPs
Expand Down
37 changes: 37 additions & 0 deletions packages/common/test/customChains.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BIGINT_0 } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import { Status } from '../src/hardforks.js'
Expand Down Expand Up @@ -229,6 +230,42 @@ describe('[Common]: Custom chains', () => {
assert.equal(c.hardfork(), 'testEIP2935Hardfork')
assert.ok(c.isActivatedEIP(2935))
})

it('customHardforks: override params', () => {
const c = createCustomCommon({
customHardforks: {
stop10Gas: {
name: 'stop10Gas',
comment: 'Hardfork which changes the gas of STOP from 0 to 10',
url: '',
status: Status.Final,
eips: [2935],
vm: {
stop: BigInt(10),
},
},
},
hardforks: [
{
name: 'chainstart',
block: 0,
},
{
name: 'stop10Gas',
block: null,
timestamp: 1000,
},
],
})
c.setHardfork(Hardfork.Chainstart)
assert.equal(c.param('vm', 'stop'), BIGINT_0)
c.setHardforkBy({
blockNumber: 1,
timestamp: 1000,
})
assert.equal(c.hardfork(), 'stop10Gas')
assert.equal(c.param('vm', 'stop'), BigInt(10))
})
})

describe('custom chain setup with hardforks with undefined/null block numbers', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { VM } from '../../../src/vm.js'

import type { Block } from '@ethereumjs/block'

function eip2935ActiveAtCommon(timestamp: number) {
function eip2935ActiveAtCommon(timestamp: number, address: bigint) {
const hfs = [
Hardfork.Chainstart,
Hardfork.Homestead,
Expand Down Expand Up @@ -64,6 +64,9 @@ function eip2935ActiveAtCommon(timestamp: number) {
url: '',
status: 'final',
eips: [2935, 7709],
vm: {
historyStorageAddress: address,
},
},
},
hardforks,
Expand Down Expand Up @@ -112,6 +115,7 @@ describe('EIP 2935: historical block hashes', () => {
] = deploymentConfig

const historyAddress = Address.fromString(deployedToAddress)
const historyAddressBigInt = bytesToBigInt(historyAddress.bytes)
const contract2935Code = hexToBytes(contract2935CodeHex)

// eslint-disable-next-line no-inner-declarations
Expand Down Expand Up @@ -164,7 +168,7 @@ describe('EIP 2935: historical block hashes', () => {
})

it('should save genesis block hash to the history block hash contract', async () => {
const commonGenesis = eip2935ActiveAtCommon(1)
const commonGenesis = eip2935ActiveAtCommon(1, historyAddressBigInt)
const blockchain = await createBlockchain({
common: commonGenesis,
validateBlocks: false,
Expand All @@ -174,10 +178,6 @@ describe('EIP 2935: historical block hashes', () => {
commonGenesis.setHardforkBy({
timestamp: 1,
})
commonGenesis['_paramsCache']['vm']['historyStorageAddress'] = bytesToBigInt(
historyAddress.bytes
)

const genesis = await vm.blockchain.getBlock(0)
const block = await (
await vm.buildBlock({
Expand All @@ -201,9 +201,9 @@ describe('EIP 2935: historical block hashes', () => {
const blocksActivation = 256 // This ensures that only block 255 gets stored into the hash contract
// More than blocks activation to build, so we can ensure that we can also retrieve block 0 or block 1 hash at block 300
const blocksToBuild = 500
const commonGetHistoryServeWindow = eip2935ActiveAtCommon(0)
const commonGetHistoryServeWindow = eip2935ActiveAtCommon(0, historyAddressBigInt)
commonGetHistoryServeWindow.setEIPs([2935])
const common = eip2935ActiveAtCommon(blocksActivation)
const common = eip2935ActiveAtCommon(blocksActivation, historyAddressBigInt)
const historyServeWindow = commonGetHistoryServeWindow.param('vm', 'historyServeWindow')

const blockchain = await createBlockchain({
Expand Down

0 comments on commit 80fcde6

Please sign in to comment.