Skip to content

Commit

Permalink
vm: migrate test files (#3686)
Browse files Browse the repository at this point in the history
* vm: migrate test files

* vm: cspell fix

---------

Co-authored-by: Holger Drewes <[email protected]>
  • Loading branch information
gabrocheleau and holgerd77 authored Sep 19, 2024
1 parent 1413c9c commit 647a518
Show file tree
Hide file tree
Showing 16 changed files with 713 additions and 670 deletions.
4 changes: 2 additions & 2 deletions packages/vm/test/api/EIPs/eip-2565-modexp-gas-cost.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { assert, describe, it } from 'vitest'
import { createVM } from '../../../src/index.js'

// See https://github.com/holiman/go-ethereum/blob/2c99023b68c573ba24a5b01db13e000bd9b82417/core/vm/testdata/precompiles/modexp_eip2565.json
import testData from '../testdata/eip-2565.json'
import { eip2565Data } from '../testdata/eip-2565.js'

describe('EIP-2565 ModExp gas cost tests', () => {
it('Test return data, gas cost and execution status against testdata', async () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Byzantium, eips: [2565] })
const vm = await createVM({ common })

for (const test of testData) {
for (const test of eip2565Data) {
const testName = test.Name
const to = new Address(hexToBytes('0x0000000000000000000000000000000000000005'))
const result = await vm.evm.runCall({
Expand Down
9 changes: 4 additions & 5 deletions packages/vm/test/api/customChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import { assert, describe, it } from 'vitest'

import { createVM, runTx } from '../../src/index.js'

import * as testChain from './testdata/testnet.json'
import * as testnetMerge from './testdata/testnetMerge.json'
import { testnetData } from './testdata/testnet.js'
import { testnetMergeData } from './testdata/testnetMerge.js'

import type { ChainConfig } from '@ethereumjs/common'
import type { AccountState, GenesisState, PrefixedHexString } from '@ethereumjs/util'

const storage: Array<[PrefixedHexString, PrefixedHexString]> = [
Expand Down Expand Up @@ -49,7 +48,7 @@ const genesisState: GenesisState = {
[contractAddress]: accountState,
}

const common = createCustomCommon(testChain.default as ChainConfig, Mainnet, {
const common = createCustomCommon(testnetData, Mainnet, {
hardfork: Hardfork.Chainstart,
})
const block = createBlock(
Expand Down Expand Up @@ -115,7 +114,7 @@ describe('VM initialized with custom state', () => {
})

it('setHardfork', async () => {
const common = createCustomCommon(testnetMerge.default as ChainConfig, Mainnet, {
const common = createCustomCommon(testnetMergeData, Mainnet, {
hardfork: Hardfork.Istanbul,
})

Expand Down
5 changes: 2 additions & 3 deletions packages/vm/test/api/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { assert, describe, it } from 'vitest'

import { type VMOpts, createVM, paramsVM } from '../../src/index.js'

import * as testnetMerge from './testdata/testnetMerge.json'
import { testnetMergeData } from './testdata/testnetMerge.js'
import { setupVM } from './utils.js'

import type { ChainConfig } from '@ethereumjs/common'
import type { MerkleStateManager } from '@ethereumjs/statemanager'

/**
Expand Down Expand Up @@ -217,7 +216,7 @@ describe('VM -> common (chain, HFs, EIPs)', () => {

describe('VM -> setHardfork, blockchain', () => {
it('setHardfork', async () => {
const common = createCustomCommon(testnetMerge.default as ChainConfig, Mainnet, {
const common = createCustomCommon(testnetMergeData, Mainnet, {
hardfork: Hardfork.Istanbul,
})

Expand Down
47 changes: 22 additions & 25 deletions packages/vm/test/api/runBlock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ import { assert, describe, it } from 'vitest'
import { createVM, runBlock } from '../../src/index.js'
import { getDAOCommon, setupPreConditions } from '../util.js'

import * as testData from './testdata/blockchain.json'
import * as testnet from './testdata/testnet.json'
import { blockchainData } from './testdata/blockchain.js'
import { testnetData } from './testdata/testnet.js'
import { createAccountWithDefaults, setBalance, setupVM } from './utils.js'

import type { VM } from '../../src/index.js'
Expand All @@ -50,7 +50,6 @@ import type {
RunBlockOpts,
} from '../../src/types.js'
import type { Block, BlockBytes } from '@ethereumjs/block'
import type { ChainConfig } from '@ethereumjs/common'
import type { MerkleStateManager } from '@ethereumjs/statemanager'
import type { AuthorizationListBytesItem, TypedTransaction } from '@ethereumjs/tx'
import type { NestedUint8Array, PrefixedHexString } from '@ethereumjs/util'
Expand All @@ -59,13 +58,13 @@ const common = new Common({ chain: Mainnet, hardfork: Hardfork.Berlin })
describe('runBlock() -> successful API parameter usage', async () => {
async function simpleRun(vm: VM) {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const genesisRlp = hexToBytes(testData.default.genesisRLP as PrefixedHexString)
const genesisRlp = hexToBytes(blockchainData.genesisRLP as PrefixedHexString)
const genesis = createBlockFromRLP(genesisRlp, { common })

const blockRlp = hexToBytes(testData.default.blocks[0].rlp as PrefixedHexString)
const blockRlp = hexToBytes(blockchainData.blocks[0].rlp as PrefixedHexString)
const block = createBlockFromRLP(blockRlp, { common })

await setupPreConditions(vm.stateManager, testData)
await setupPreConditions(vm.stateManager, blockchainData)

assert.deepEqual(
(vm.stateManager as MerkleStateManager)['_trie'].root(),
Expand All @@ -88,12 +87,12 @@ describe('runBlock() -> successful API parameter usage', async () => {
}

async function uncleRun(vm: VM) {
const testData = await import('./testdata/uncleData.json')
const { uncleData } = await import('./testdata/uncleData.js')

await setupPreConditions(vm.stateManager, testData)
await setupPreConditions(vm.stateManager, uncleData)

const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const block1Rlp = hexToBytes(testData.default.blocks[0].rlp as PrefixedHexString)
const block1Rlp = hexToBytes(uncleData.blocks[0].rlp as PrefixedHexString)
const block1 = createBlockFromRLP(block1Rlp, { common })
await runBlock(vm, {
block: block1,
Expand All @@ -102,7 +101,7 @@ describe('runBlock() -> successful API parameter usage', async () => {
skipHardForkValidation: true,
})

const block2Rlp = hexToBytes(testData.default.blocks[1].rlp as PrefixedHexString)
const block2Rlp = hexToBytes(uncleData.blocks[1].rlp as PrefixedHexString)
const block2 = createBlockFromRLP(block2Rlp, { common })
await runBlock(vm, {
block: block2,
Expand All @@ -112,7 +111,7 @@ describe('runBlock() -> successful API parameter usage', async () => {
skipHardForkValidation: true,
})

const block3Rlp = toBytes(testData.default.blocks[2].rlp as PrefixedHexString)
const block3Rlp = toBytes(uncleData.blocks[2].rlp as PrefixedHexString)
const block3 = createBlockFromRLP(block3Rlp, { common })
await runBlock(vm, {
block: block3,
Expand All @@ -128,7 +127,7 @@ describe('runBlock() -> successful API parameter usage', async () => {

assert.equal(
`0x${uncleReward}`,
testData.default.postState['0xb94f5374fce5ed0000000097c15331677e6ebf0b'].balance,
uncleData.postState['0xb94f5374fce5ed0000000097c15331677e6ebf0b'].balance,
'calculated balance should equal postState balance',
)
}
Expand All @@ -153,7 +152,7 @@ describe('runBlock() -> successful API parameter usage', async () => {
})

it('PoW block, Common custom chain (Common customChains constructor option)', async () => {
const common = createCustomCommon(testnet.default as ChainConfig, Mainnet, {
const common = createCustomCommon(testnetData, Mainnet, {
hardfork: Hardfork.Berlin,
})
const vm = await setupVM({ common })
Expand Down Expand Up @@ -227,7 +226,7 @@ describe('runBlock() -> API parameter usage/data errors', async () => {

it('should fail when runTx fails', async () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const blockRlp = hexToBytes(testData.default.blocks[0].rlp as PrefixedHexString)
const blockRlp = hexToBytes(blockchainData.blocks[0].rlp as PrefixedHexString)
const block = createBlockFromRLP(blockRlp, { common })

// The mocked VM uses a mocked runTx
Expand All @@ -254,7 +253,7 @@ describe('runBlock() -> API parameter usage/data errors', async () => {
const blockchain = await createBlockchain()
const vm = await createVM({ common, blockchain })

const blockRlp = hexToBytes(testData.default.blocks[0].rlp as PrefixedHexString)
const blockRlp = hexToBytes(blockchainData.blocks[0].rlp as PrefixedHexString)
const block = Object.create(createBlockFromRLP(blockRlp, { common }))

await runBlock(vm, { block })
Expand All @@ -269,7 +268,7 @@ describe('runBlock() -> API parameter usage/data errors', async () => {

it('should fail when no `validateHeader` method exists on blockchain class', async () => {
const vm = await createVM({ common })
const blockRlp = hexToBytes(testData.default.blocks[0].rlp as PrefixedHexString)
const blockRlp = hexToBytes(blockchainData.blocks[0].rlp as PrefixedHexString)
const block = Object.create(createBlockFromRLP(blockRlp, { common }))
;(vm.blockchain as any).validateHeader = undefined
try {
Expand All @@ -286,7 +285,7 @@ describe('runBlock() -> API parameter usage/data errors', async () => {
it('should fail when tx gas limit higher than block gas limit', async () => {
const vm = await createVM({ common })

const blockRlp = hexToBytes(testData.default.blocks[0].rlp as PrefixedHexString)
const blockRlp = hexToBytes(blockchainData.blocks[0].rlp as PrefixedHexString)
const block = Object.create(createBlockFromRLP(blockRlp, { common }))
// modify first tx's gasLimit
const { nonce, gasPrice, to, value, data, v, r, s } = block.transactions[0]
Expand All @@ -311,13 +310,11 @@ describe('runBlock() -> runtime behavior', async () => {

const vm = await setupVM({ common })

const block1 = RLP.decode(
testData.default.blocks[0].rlp as PrefixedHexString,
) as NestedUint8Array
const block1 = RLP.decode(blockchainData.blocks[0].rlp as PrefixedHexString) as NestedUint8Array
// edit extra data of this block to "dao-hard-fork"
block1[0][12] = utf8ToBytes('dao-hard-fork')
const block = createBlockFromBytesArray(block1 as BlockBytes, { common })
await setupPreConditions(vm.stateManager, testData)
await setupPreConditions(vm.stateManager, blockchainData)

// fill two original DAO child-contracts with funds and the recovery account with funds in order to verify that the balance gets summed correctly
const fundBalance1 = BigInt('0x1111')
Expand Down Expand Up @@ -451,10 +448,10 @@ async function runWithHf(hardfork: string) {
const common = new Common({ chain: Mainnet, hardfork })
const vm = await setupVM({ common })

const blockRlp = hexToBytes(testData.default.blocks[0].rlp as PrefixedHexString)
const blockRlp = hexToBytes(blockchainData.blocks[0].rlp as PrefixedHexString)
const block = createBlockFromRLP(blockRlp, { common })

await setupPreConditions(vm.stateManager, testData)
await setupPreConditions(vm.stateManager, blockchainData)

const res = await runBlock(vm, {
block,
Expand Down Expand Up @@ -487,7 +484,7 @@ describe('runBlock() -> tx types', async () => {
async function simpleRun(vm: VM, transactions: TypedTransaction[]) {
const common = vm.common

const blockRlp = hexToBytes(testData.default.blocks[0].rlp as PrefixedHexString)
const blockRlp = hexToBytes(blockchainData.blocks[0].rlp as PrefixedHexString)
const block = createBlockFromRLP(blockRlp, { common, freeze: false })

//@ts-ignore read-only property
Expand All @@ -498,7 +495,7 @@ describe('runBlock() -> tx types', async () => {
block.header.baseFeePerGas = BigInt(7)
}

await setupPreConditions(vm.stateManager, testData)
await setupPreConditions(vm.stateManager, blockchainData)

const res = await runBlock(vm, {
block,
Expand Down
119 changes: 0 additions & 119 deletions packages/vm/test/api/testdata/blockchain.json

This file was deleted.

Loading

0 comments on commit 647a518

Please sign in to comment.