Skip to content

Commit

Permalink
vm/tests: ensure tests run with EIPs activated (#2108)
Browse files Browse the repository at this point in the history
* vm/tests: ensure tests run with EIPs activated

* vm/tests: fix common

* vm: add test runner tests

* vm: testloader tests; add extra check

* vm/tests: cleanup

* vm: lint

Co-authored-by: Brian Faust <[email protected]>
  • Loading branch information
jochem-brouwer and faustbrian authored Aug 19, 2022
1 parent d210bda commit f0516de
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
58 changes: 58 additions & 0 deletions packages/vm/tests/api/tester/tester.config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Hardfork } from '@ethereumjs/common'
import * as tape from 'tape'

import { getCommon } from '../../tester/config'

tape('bloom', (t: tape.Test) => {
t.test('should initialize common with the right hardfork', (st) => {
const common = getCommon('byzantium')
st.ok(common.hardfork() === Hardfork.Byzantium)
st.end()
})
t.test('should initialize common with the right hardfork uppercased', (st) => {
let common = getCommon('Byzantium')
st.ok(common.hardfork() === Hardfork.Byzantium)
common = getCommon('BYZANTIUM')
st.ok(common.hardfork() === Hardfork.Byzantium)
st.end()
})
t.test('should always activate EIP 3607', (st) => {
let common = getCommon('byzantium')
st.ok(common.isActivatedEIP(3607))
common = getCommon('ArrowGlacierToMergeAtDiffC0000')
st.ok(common.isActivatedEIP(3607))
common = getCommon('ByzantiumToConstantinopleFixAt5')
st.ok(common.isActivatedEIP(3607))
st.end()
})
t.test('should be able to activate hardforks with EIPs enabled', (st) => {
let common = getCommon('byzantium+2537')
st.ok(common.isActivatedEIP(2537))
common = getCommon('byzantium+2537+2929')
st.ok(common.isActivatedEIP(2537))
st.ok(common.isActivatedEIP(2929))
st.end()
})
t.test('should be able to activate transition forks', (st) => {
st.doesNotThrow(() => getCommon('ByzantiumToConstantinopleFixAt5'))
st.end()
})
t.test('should be able to activate merge transition fork with the correct TTD set', (st) => {
const forks = [
{ hf: 'arrowGlacier', TTD: 20000 },
{ hf: 'london', TTD: 20000 },
{ hf: 'arrowGlacier', TTD: 40000 },
]
forks.map((testCase) => {
const str = testCase.hf + 'ToMergeAtDiff' + testCase.TTD.toString(16)
const common = getCommon(str)
st.ok(common.hardfork() === testCase.hf)
st.ok(common.hardforkTTD('merge') === BigInt(testCase.TTD))
})
st.end()
})
t.test('should throw on a non-existing fork', (st) => {
st.throws(() => getCommon('NonExistingFork'))
st.end()
})
})
29 changes: 19 additions & 10 deletions packages/vm/tests/tester/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,18 @@ export function getTestDirs(network: string, testType: string) {
}
/**
* Setups the common with networks
* @param targetNetwork Network target
* @param targetNetwork Network target (this can include EIPs, such as Byzantium+2537+2929)
* @param ttd If set: total terminal difficulty to switch to merge
* @returns
*/
function setupCommonWithNetworks(targetNetwork: string, ttd?: number) {
const networkLowercase = targetNetwork.toLowerCase()
function setupCommonWithNetworks(network: string, ttd?: number) {
let networkLowercase: string // This only consists of the target hardfork, so without the EIPs
if (network.includes('+')) {
const index = network.indexOf('+')
networkLowercase = network.slice(0, index).toLowerCase()
} else {
networkLowercase = network.toLowerCase()
}
// normal hard fork, return the common with this hard fork
// find the right upper/lowercased version
const hfName = normalHardforks.reduce((previousValue, currentValue) =>
Expand Down Expand Up @@ -308,7 +314,8 @@ function setupCommonWithNetworks(targetNetwork: string, ttd?: number) {
},
{ eips: [3607] }
)
const eips = targetNetwork.match(/(?<=\+)(.\d+)/g)
// Activate EIPs
const eips = network.match(/(?<=\+)(.\d+)/g)
if (eips) {
common.setEIPs(eips.map((e: string) => parseInt(e)))
}
Expand All @@ -324,24 +331,25 @@ function setupCommonWithNetworks(targetNetwork: string, ttd?: number) {
* For instance, "London+3855+3860" will also activate EIP-3855 and EIP-3860.
* @returns {Common} the Common which should be used
*/
export function getCommon(targetNetwork: string) {
let network = targetNetwork
const networkLowercase = network.toLowerCase()
export function getCommon(network: string) {
let networkLowercase = network.toLowerCase()
if (network.includes('+')) {
const index = network.indexOf('+')
network = network.slice(0, index)
networkLowercase = network.slice(0, index).toLowerCase()
}
if (normalHardforks.map((str) => str.toLowerCase()).includes(networkLowercase)) {
return setupCommonWithNetworks(targetNetwork)
// Case 1: normal network, such as "London" or "Byzantium" (without any EIPs enabled, and it is not a transition network)
return setupCommonWithNetworks(network)
} else if (networkLowercase.match('tomergeatdiff')) {
// Case 2: special case of a transition network, this setups the right common with the right Merge properties (TTD)
// This is a HF -> Merge transition
const start = networkLowercase.match('tomergeatdiff')!.index!
const end = start + 'tomergeatdiff'.length
const startNetwork = network.substring(0, start) // HF before the merge
const TTD = Number('0x' + network.substring(end)) // Total difficulty to transition to PoS
return setupCommonWithNetworks(startNetwork, TTD)
} else {
// this is not a "default fork" network, but it is a "transition" network. we will test the VM if it transitions the right way
// Case 3: this is not a "default fork" network, but it is a "transition" network. Test the VM if it transitions the right way
const transitionForks = isTruthy(transitionNetworks[network])
? transitionNetworks[network]
: transitionNetworks[network.substring(0, 1).toUpperCase() + network.substr(1)]
Expand Down Expand Up @@ -379,6 +387,7 @@ export function getCommon(targetNetwork: string) {
{
baseChain: 'mainnet',
hardfork: transitionForks.startFork,
eips: [3607],
}
)
}
Expand Down

0 comments on commit f0516de

Please sign in to comment.