Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statemanager: migrate test files #3660

Merged
merged 39 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
9602773
common: test data
gabrocheleau Sep 6, 2024
5b716c8
devp2p: test data
gabrocheleau Sep 6, 2024
84d5264
block: convert block test data
gabrocheleau Sep 7, 2024
5db8f8f
blockchain: convert blockchain test data
gabrocheleau Sep 7, 2024
bcbd1bc
client: convert sim json configs
gabrocheleau Sep 7, 2024
10225a7
block: adjust BeaconPayloadJSON type
gabrocheleau Sep 7, 2024
3a92009
block: adjust from beacon payload test
gabrocheleau Sep 7, 2024
7631a64
block: adjust from rpc test
gabrocheleau Sep 7, 2024
ed6d438
block: adjust remaining blocks test data
gabrocheleau Sep 7, 2024
55e1486
block: add chainconfig type
gabrocheleau Sep 7, 2024
fbae7ae
chore: merge with master
gabrocheleau Sep 8, 2024
092c05e
Merge branch 'master' into monorepo/typescript-test-files
gabrocheleau Sep 8, 2024
befc8ac
block: adjust types and fix test
gabrocheleau Sep 8, 2024
1e98496
Merge branch 'monorepo/typescript-test-files' of https://github.com/e…
gabrocheleau Sep 8, 2024
b268563
blockchain: adjust blockchain test data
gabrocheleau Sep 8, 2024
efdd235
blockchain: adjust blockchain test data
gabrocheleau Sep 8, 2024
360131b
client: adjust test type issues
gabrocheleau Sep 9, 2024
5c37723
devp2p: fix test type issues
gabrocheleau Sep 9, 2024
1379cd2
tx: fix test import
gabrocheleau Sep 9, 2024
1a25fbe
devp2p: disable cspell for testdata
gabrocheleau Sep 9, 2024
85a3910
monorepo: fix spelling
gabrocheleau Sep 9, 2024
83836bb
common: remove unnecessary json parsing
gabrocheleau Sep 9, 2024
d44fdc7
vm: fix type issue in runTx
gabrocheleau Sep 9, 2024
a24e543
client: fix type issue in import
gabrocheleau Sep 9, 2024
80ef607
vm: fix import
gabrocheleau Sep 9, 2024
b9e9a5d
util: add numeric string type
gabrocheleau Sep 9, 2024
ac28a0d
block: use numeric string type
gabrocheleau Sep 9, 2024
7ef44bd
chore: fix merge conflicts
gabrocheleau Sep 10, 2024
f6e0246
client: migrate test data from json to ts
gabrocheleau Sep 10, 2024
52d3c3e
client: adjust tests
gabrocheleau Sep 10, 2024
2b5c002
monorepo: misc type issues
gabrocheleau Sep 10, 2024
564175b
chore: merge with master
gabrocheleau Sep 10, 2024
a170cb4
vm: fix test imports
gabrocheleau Sep 10, 2024
ded8d99
client: fix some tests
gabrocheleau Sep 10, 2024
ef4fee1
client: fix geth genesis
gabrocheleau Sep 10, 2024
7f06f4c
chore: merge with master
gabrocheleau Sep 11, 2024
6a84107
statemanager: migrate test files
gabrocheleau Sep 11, 2024
4092b36
statemanager: remove script file accidentally pushed
gabrocheleau Sep 11, 2024
5f0dc66
vm: fix typo
gabrocheleau Sep 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading