Skip to content

Commit

Permalink
statemanager: migrate test files (#3660)
Browse files Browse the repository at this point in the history
* common: test data

* devp2p: test data

* block: convert block test data

* blockchain: convert blockchain test data

* client: convert sim json configs

* block: adjust BeaconPayloadJSON type

* block: adjust from beacon payload test

* block: adjust from rpc test

* block: adjust remaining blocks test data

* block: add chainconfig type

* block: adjust types and fix test

* blockchain: adjust blockchain test data

* blockchain: adjust blockchain test data

* client: adjust test type issues

* devp2p: fix test type issues

* tx: fix test import

* devp2p: disable cspell for testdata

* monorepo: fix spelling

* common: remove unnecessary json parsing

* vm: fix type issue in runTx

* client: fix type issue in import

* vm: fix import

* util: add numeric string type

* block: use numeric string type

* client: migrate test data from json to ts

* client: adjust tests

* monorepo: misc type issues

* vm: fix test imports

* client: fix some tests

* client: fix geth genesis

* statemanager: migrate test files

* statemanager: remove script file accidentally pushed

* vm: fix typo
  • Loading branch information
gabrocheleau committed Sep 11, 2024
1 parent 674a8a3 commit 47051db
Show file tree
Hide file tree
Showing 33 changed files with 3,286 additions and 3,239 deletions.
79 changes: 39 additions & 40 deletions packages/statemanager/test/proofStateManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import { assert, describe, it } from 'vitest'

import { MerkleStateManager } from '../src/index.js'

import * as ropsten_contractWithStorage from './testdata/ropsten_contractWithStorage.json'
import * as ropsten_nonexistentAccount from './testdata/ropsten_nonexistentAccount.json'
import * as ropsten_validAccount from './testdata/ropsten_validAccount.json'
import { ropstenContractWithStorageData } from './testdata/ropsten_contractWithStorage.js'
import { ropstenNonexistentAccountData } from './testdata/ropsten_nonexistentAccount.js'
import { ropstenValidAccountData } from './testdata/ropsten_validAccount.js'

import type { Proof } from '@ethereumjs/common'
import type { PrefixedHexString } from '@ethereumjs/util'

describe('ProofStateManager', () => {
Expand Down Expand Up @@ -120,8 +119,8 @@ describe('ProofStateManager', () => {
const stateManager = new MerkleStateManager({ trie })
// Dump all the account proof data in the DB
let stateRoot: Uint8Array | undefined
for (const proofData of ropsten_validAccount.accountProof) {
const bufferData = hexToBytes(proofData as PrefixedHexString)
for (const proofData of ropstenValidAccountData.accountProof) {
const bufferData = hexToBytes(proofData)
const key = keccak256(bufferData)
if (stateRoot === undefined) {
stateRoot = key
Expand All @@ -130,8 +129,8 @@ describe('ProofStateManager', () => {
}
trie.root(stateRoot!)
const proof = await stateManager.getProof(address)
assert.deepEqual((ropsten_validAccount as any).default, proof)
assert.ok(await stateManager.verifyProof((ropsten_validAccount as any).default))
assert.deepEqual(ropstenValidAccountData, proof)
assert.ok(await stateManager.verifyProof(ropstenValidAccountData))
})

it('should report data equal to geth output for EIP 1178 proofs - nonexistent account', async () => {
Expand All @@ -144,8 +143,8 @@ describe('ProofStateManager', () => {
const stateManager = new MerkleStateManager({ trie })
// Dump all the account proof data in the DB
let stateRoot: Uint8Array | undefined
for (const proofData of ropsten_nonexistentAccount.accountProof) {
const bufferData = hexToBytes(proofData as PrefixedHexString)
for (const proofData of ropstenNonexistentAccountData.accountProof) {
const bufferData = hexToBytes(proofData)
const key = keccak256(bufferData)
if (stateRoot === undefined) {
stateRoot = key
Expand All @@ -154,8 +153,8 @@ describe('ProofStateManager', () => {
}
trie.root(stateRoot!)
const proof = await stateManager.getProof(address)
assert.deepEqual((ropsten_nonexistentAccount as any).default, proof)
assert.ok(await stateManager.verifyProof(ropsten_nonexistentAccount as Proof))
assert.deepEqual(ropstenNonexistentAccountData, proof)
assert.ok(await stateManager.verifyProof(ropstenNonexistentAccountData))
})

it('should report data equal to geth output for EIP 1178 proofs - account with storage', async () => {
Expand All @@ -168,22 +167,22 @@ describe('ProofStateManager', () => {
const stateManager = new MerkleStateManager({ trie })
// Dump all the account proof data in the DB
let stateRoot: Uint8Array | undefined
for (const proofData of ropsten_contractWithStorage.accountProof) {
const bufferData = hexToBytes(proofData as PrefixedHexString)
for (const proofData of ropstenContractWithStorageData.accountProof) {
const bufferData = hexToBytes(proofData)
const key = keccak256(bufferData)
if (stateRoot === undefined) {
stateRoot = key
}
await trie['_db'].put(key, bufferData)
}
const storageRoot = ropsten_contractWithStorage.storageHash as PrefixedHexString
const storageRoot = ropstenContractWithStorageData.storageHash
const storageTrie = new Trie({ useKeyHashing: true })
const storageKeys: Uint8Array[] = []
for (const storageProofsData of ropsten_contractWithStorage.storageProof) {
storageKeys.push(hexToBytes(storageProofsData.key as PrefixedHexString))
for (const storageProofsData of ropstenContractWithStorageData.storageProof) {
storageKeys.push(hexToBytes(storageProofsData.key))
for (const storageProofData of storageProofsData.proof) {
const key = keccak256(hexToBytes(storageProofData as PrefixedHexString))
await storageTrie['_db'].put(key, hexToBytes(storageProofData as PrefixedHexString))
const key = keccak256(hexToBytes(storageProofData))
await storageTrie['_db'].put(key, hexToBytes(storageProofData))
}
}
storageTrie.root(hexToBytes(storageRoot))
Expand All @@ -192,8 +191,8 @@ describe('ProofStateManager', () => {
trie.root(stateRoot!)

const proof = await stateManager.getProof(address, storageKeys)
assert.deepEqual((ropsten_contractWithStorage as any).default, proof)
await stateManager.verifyProof(ropsten_contractWithStorage as Proof)
assert.deepEqual(ropstenContractWithStorageData, proof)
await stateManager.verifyProof(ropstenContractWithStorageData)
})

it(`should throw on invalid proofs - existing accounts/slots`, async () => {
Expand All @@ -206,22 +205,22 @@ describe('ProofStateManager', () => {
const stateManager = new MerkleStateManager({ trie })
// Dump all the account proof data in the DB
let stateRoot: Uint8Array | undefined
for (const proofData of ropsten_contractWithStorage.accountProof) {
const bufferData = hexToBytes(proofData as PrefixedHexString)
for (const proofData of ropstenContractWithStorageData.accountProof) {
const bufferData = hexToBytes(proofData)
const key = keccak256(bufferData)
if (stateRoot === undefined) {
stateRoot = key
}
await trie['_db'].put(key, bufferData)
}
const storageRoot = ropsten_contractWithStorage.storageHash as PrefixedHexString
const storageRoot = ropstenContractWithStorageData.storageHash
const storageTrie = new Trie({ useKeyHashing: true })
const storageKeys: Uint8Array[] = []
for (const storageProofsData of ropsten_contractWithStorage.storageProof) {
storageKeys.push(hexToBytes(storageProofsData.key as PrefixedHexString))
for (const storageProofsData of ropstenContractWithStorageData.storageProof) {
storageKeys.push(hexToBytes(storageProofsData.key))
for (const storageProofData of storageProofsData.proof) {
const key = keccak256(hexToBytes(storageProofData as PrefixedHexString))
await storageTrie['_db'].put(key, hexToBytes(storageProofData as PrefixedHexString))
const key = keccak256(hexToBytes(storageProofData))
await storageTrie['_db'].put(key, hexToBytes(storageProofData))
}
}
storageTrie.root(hexToBytes(storageRoot))
Expand All @@ -230,29 +229,29 @@ describe('ProofStateManager', () => {
trie.root(stateRoot!)

// tamper with account data
const testdata = { ...(ropsten_contractWithStorage as any) }
const testData = { ...ropstenContractWithStorageData }
for (const tamper of ['nonce', 'balance', 'codeHash', 'storageHash']) {
const original = testdata[tamper]
const original = testData[tamper as keyof typeof testData] as PrefixedHexString
try {
const newField = `0x9${original.slice(3)}`
testdata[tamper] = newField
await stateManager.verifyProof(testdata)
;(testData[tamper as keyof typeof testData] as PrefixedHexString) =
`0x9${original.slice(3)}`
await stateManager.verifyProof(testData)
// note: this implicitly means that newField !== original,
// if newField === original then the proof would be valid and test would fail
assert.fail('should throw')
} catch (e) {
assert.ok(true, 'threw on invalid proof')
} finally {
testdata[tamper] = original
;(testData[tamper as keyof typeof testData] as PrefixedHexString) = original
}
}

// tamper with storage slots
for (const slot of testdata.storageProof) {
for (const slot of testData.storageProof) {
const original = slot.value
slot.value = `0x9${original.slice(3)}`
try {
await stateManager.verifyProof(testdata)
await stateManager.verifyProof(testData)
assert.fail('should throw')
} catch {
assert.ok(true, 'threw on invalid proof')
Expand All @@ -272,23 +271,23 @@ describe('ProofStateManager', () => {
const stateManager = new MerkleStateManager({ trie })
// Dump all the account proof data in the DB
let stateRoot: Uint8Array | undefined
for (const proofData of ropsten_nonexistentAccount.accountProof) {
const bufferData = hexToBytes(proofData as PrefixedHexString)
for (const proofData of ropstenNonexistentAccountData.accountProof) {
const bufferData = hexToBytes(proofData)
const key = keccak256(bufferData)
if (stateRoot === undefined) {
stateRoot = key
}
await trie['_db'].put(key, bufferData)
}
const storageRoot = ropsten_nonexistentAccount.storageHash as PrefixedHexString
const storageRoot = ropstenNonexistentAccountData.storageHash
const storageTrie = new Trie({ useKeyHashing: true })
storageTrie.root(hexToBytes(storageRoot))
const addressHex = bytesToHex(address.bytes)
stateManager['_storageTries'][addressHex] = storageTrie
trie.root(stateRoot!)

// tamper with account data
const testdata = { ...(ropsten_nonexistentAccount as any) }
const testdata = { ...(ropstenNonexistentAccountData as any) }
for (const tamper of ['nonce', 'balance', 'codeHash', 'storageHash']) {
const original = testdata[tamper]
try {
Expand Down
12 changes: 6 additions & 6 deletions packages/statemanager/test/rpcStateManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { assert, describe, expect, it, vi } from 'vitest'
import { MerkleStateManager } from '../src/merkleStateManager.js'
import { RPCBlockChain, RPCStateManager } from '../src/rpcStateManager.js'

import * as blockData from './testdata/providerData/blocks/block0x7a120.json'
import { block as blockData } from './testdata/providerData/blocks/block0x7a120.js'
import { getValues } from './testdata/providerData/mockProvider.js'
import * as txData from './testdata/providerData/transactions/0xed1960aa7d0d7b567c946d94331dddb37a1c67f51f30bf51f256ea40db88cfb0.json'
import { tx as txData } from './testdata/providerData/transactions/0xed1960aa7d0d7b567c946d94331dddb37a1c67f51f30bf51f256ea40db88cfb0.js'

import type { JSONRPCBlock } from '@ethereumjs/block'
import type { EVMMockBlockchainInterface } from '@ethereumjs/evm'

const provider = process.env.PROVIDER ?? 'http://cheese'
// To run the tests with a live provider, set the PROVIDER environmental variable with a valid provider url
Expand All @@ -33,7 +33,7 @@ const provider = process.env.PROVIDER ?? 'http://cheese'

describe('RPC State Manager initialization tests', async () => {
vi.mock('@ethereumjs/util', async () => {
const util = (await vi.importActual('@ethereumjs/util')) as any
const util = await vi.importActual('@ethereumjs/util')
return {
...util,
fetchFromProvider: vi.fn().mockImplementation(async (url, { method, params }: any) => {
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('runBlock test', () => {
common.setHardforkBy({ blockNumber: blockTag - 1n })

const vm = await createVM({ common, stateManager: state })
const block = createBlockFromRPC(blockData.default as JSONRPCBlock, [], { common })
const block = createBlockFromRPC(blockData, [], { common })
try {
const res = await runBlock(vm, {
block,
Expand All @@ -310,7 +310,7 @@ describe('runBlock test', () => {

describe('blockchain', () =>
it('uses blockhash', async () => {
const blockchain = new RPCBlockChain(provider)
const blockchain = new RPCBlockChain(provider) as unknown as EVMMockBlockchainInterface
const blockTag = 1n
const state = new RPCStateManager({ provider, blockTag })
const evm = await createEVM({ blockchain, stateManager: state })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ describe('Verkle Tree API tests', () => {
const retrievedBigByteCode = await sm.getCode(address)
assert.deepEqual(bigByteCode, retrievedBigByteCode)
const reallyBigByteCode = hexToBytes(
(await import('./testdata/biggestContractEver.json')).default.bytecode as PrefixedHexString,
(await import('./testdata/biggestContractEver.js')).biggestContractEverData
.bytecode as PrefixedHexString,
)
// Biggest mainnet contract - 0x10C621008B210C3A5d0385e458B48af05BF4Ec88 (supposedly anyway)
await sm.putCode(address, reallyBigByteCode)
Expand Down
18 changes: 10 additions & 8 deletions packages/statemanager/test/statelessVerkleStateManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,30 @@ import { assert, beforeAll, describe, it, test } from 'vitest'

import { CacheType, Caches, StatelessVerkleStateManager } from '../src/index.js'

import * as testnetVerkleKaustinen from './testdata/testnetVerkleKaustinen.json'
import * as verkleBlockJSON from './testdata/verkleKaustinen6Block72.json'
import { testnetVerkleKaustinenData } from './testdata/testnetVerkleKaustinen.js'
import { verkleKaustinen6Block72Data } from './testdata/verkleKaustinen6Block72.js'

import type { BlockData } from '@ethereumjs/block'
import type { PrefixedHexString, VerkleCrypto } from '@ethereumjs/util'

describe('StatelessVerkleStateManager: Kaustinen Verkle Block', () => {
let verkleCrypto: VerkleCrypto
beforeAll(async () => {
verkleCrypto = await loadVerkleCrypto()
})
const common = createCommonFromGethGenesis(testnetVerkleKaustinen.default, {
const common = createCommonFromGethGenesis(testnetVerkleKaustinenData, {
chain: 'customChain',
eips: [2935, 4895, 6800],
})

const decodedTxs = verkleBlockJSON.default.transactions.map((tx) =>
const decodedTxs = verkleKaustinen6Block72Data.transactions?.map((tx) =>
createTxFromSerializedData(hexToBytes(tx as PrefixedHexString), { common }),
)
const block = createBlock({ ...verkleBlockJSON, transactions: decodedTxs } as BlockData, {
common,
})
const block = createBlock(
{ ...verkleKaustinen6Block72Data, transactions: decodedTxs },
{
common,
},
)

it('initPreState()', async () => {
const stateManager = new StatelessVerkleStateManager({ verkleCrypto })
Expand Down
3 changes: 0 additions & 3 deletions packages/statemanager/test/testdata/biggestContractEver.json

This file was deleted.

4 changes: 4 additions & 0 deletions packages/statemanager/test/testdata/biggestContractEver.ts

Large diffs are not rendered by default.

Loading

0 comments on commit 47051db

Please sign in to comment.