Skip to content

Commit 47051db

Browse files
authored
statemanager: migrate test files (#3660)
* 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
1 parent 674a8a3 commit 47051db

33 files changed

+3286
-3239
lines changed

packages/statemanager/test/proofStateManager.spec.ts

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ import { assert, describe, it } from 'vitest'
1717

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

20-
import * as ropsten_contractWithStorage from './testdata/ropsten_contractWithStorage.json'
21-
import * as ropsten_nonexistentAccount from './testdata/ropsten_nonexistentAccount.json'
22-
import * as ropsten_validAccount from './testdata/ropsten_validAccount.json'
20+
import { ropstenContractWithStorageData } from './testdata/ropsten_contractWithStorage.js'
21+
import { ropstenNonexistentAccountData } from './testdata/ropsten_nonexistentAccount.js'
22+
import { ropstenValidAccountData } from './testdata/ropsten_validAccount.js'
2323

24-
import type { Proof } from '@ethereumjs/common'
2524
import type { PrefixedHexString } from '@ethereumjs/util'
2625

2726
describe('ProofStateManager', () => {
@@ -120,8 +119,8 @@ describe('ProofStateManager', () => {
120119
const stateManager = new MerkleStateManager({ trie })
121120
// Dump all the account proof data in the DB
122121
let stateRoot: Uint8Array | undefined
123-
for (const proofData of ropsten_validAccount.accountProof) {
124-
const bufferData = hexToBytes(proofData as PrefixedHexString)
122+
for (const proofData of ropstenValidAccountData.accountProof) {
123+
const bufferData = hexToBytes(proofData)
125124
const key = keccak256(bufferData)
126125
if (stateRoot === undefined) {
127126
stateRoot = key
@@ -130,8 +129,8 @@ describe('ProofStateManager', () => {
130129
}
131130
trie.root(stateRoot!)
132131
const proof = await stateManager.getProof(address)
133-
assert.deepEqual((ropsten_validAccount as any).default, proof)
134-
assert.ok(await stateManager.verifyProof((ropsten_validAccount as any).default))
132+
assert.deepEqual(ropstenValidAccountData, proof)
133+
assert.ok(await stateManager.verifyProof(ropstenValidAccountData))
135134
})
136135

137136
it('should report data equal to geth output for EIP 1178 proofs - nonexistent account', async () => {
@@ -144,8 +143,8 @@ describe('ProofStateManager', () => {
144143
const stateManager = new MerkleStateManager({ trie })
145144
// Dump all the account proof data in the DB
146145
let stateRoot: Uint8Array | undefined
147-
for (const proofData of ropsten_nonexistentAccount.accountProof) {
148-
const bufferData = hexToBytes(proofData as PrefixedHexString)
146+
for (const proofData of ropstenNonexistentAccountData.accountProof) {
147+
const bufferData = hexToBytes(proofData)
149148
const key = keccak256(bufferData)
150149
if (stateRoot === undefined) {
151150
stateRoot = key
@@ -154,8 +153,8 @@ describe('ProofStateManager', () => {
154153
}
155154
trie.root(stateRoot!)
156155
const proof = await stateManager.getProof(address)
157-
assert.deepEqual((ropsten_nonexistentAccount as any).default, proof)
158-
assert.ok(await stateManager.verifyProof(ropsten_nonexistentAccount as Proof))
156+
assert.deepEqual(ropstenNonexistentAccountData, proof)
157+
assert.ok(await stateManager.verifyProof(ropstenNonexistentAccountData))
159158
})
160159

161160
it('should report data equal to geth output for EIP 1178 proofs - account with storage', async () => {
@@ -168,22 +167,22 @@ describe('ProofStateManager', () => {
168167
const stateManager = new MerkleStateManager({ trie })
169168
// Dump all the account proof data in the DB
170169
let stateRoot: Uint8Array | undefined
171-
for (const proofData of ropsten_contractWithStorage.accountProof) {
172-
const bufferData = hexToBytes(proofData as PrefixedHexString)
170+
for (const proofData of ropstenContractWithStorageData.accountProof) {
171+
const bufferData = hexToBytes(proofData)
173172
const key = keccak256(bufferData)
174173
if (stateRoot === undefined) {
175174
stateRoot = key
176175
}
177176
await trie['_db'].put(key, bufferData)
178177
}
179-
const storageRoot = ropsten_contractWithStorage.storageHash as PrefixedHexString
178+
const storageRoot = ropstenContractWithStorageData.storageHash
180179
const storageTrie = new Trie({ useKeyHashing: true })
181180
const storageKeys: Uint8Array[] = []
182-
for (const storageProofsData of ropsten_contractWithStorage.storageProof) {
183-
storageKeys.push(hexToBytes(storageProofsData.key as PrefixedHexString))
181+
for (const storageProofsData of ropstenContractWithStorageData.storageProof) {
182+
storageKeys.push(hexToBytes(storageProofsData.key))
184183
for (const storageProofData of storageProofsData.proof) {
185-
const key = keccak256(hexToBytes(storageProofData as PrefixedHexString))
186-
await storageTrie['_db'].put(key, hexToBytes(storageProofData as PrefixedHexString))
184+
const key = keccak256(hexToBytes(storageProofData))
185+
await storageTrie['_db'].put(key, hexToBytes(storageProofData))
187186
}
188187
}
189188
storageTrie.root(hexToBytes(storageRoot))
@@ -192,8 +191,8 @@ describe('ProofStateManager', () => {
192191
trie.root(stateRoot!)
193192

194193
const proof = await stateManager.getProof(address, storageKeys)
195-
assert.deepEqual((ropsten_contractWithStorage as any).default, proof)
196-
await stateManager.verifyProof(ropsten_contractWithStorage as Proof)
194+
assert.deepEqual(ropstenContractWithStorageData, proof)
195+
await stateManager.verifyProof(ropstenContractWithStorageData)
197196
})
198197

199198
it(`should throw on invalid proofs - existing accounts/slots`, async () => {
@@ -206,22 +205,22 @@ describe('ProofStateManager', () => {
206205
const stateManager = new MerkleStateManager({ trie })
207206
// Dump all the account proof data in the DB
208207
let stateRoot: Uint8Array | undefined
209-
for (const proofData of ropsten_contractWithStorage.accountProof) {
210-
const bufferData = hexToBytes(proofData as PrefixedHexString)
208+
for (const proofData of ropstenContractWithStorageData.accountProof) {
209+
const bufferData = hexToBytes(proofData)
211210
const key = keccak256(bufferData)
212211
if (stateRoot === undefined) {
213212
stateRoot = key
214213
}
215214
await trie['_db'].put(key, bufferData)
216215
}
217-
const storageRoot = ropsten_contractWithStorage.storageHash as PrefixedHexString
216+
const storageRoot = ropstenContractWithStorageData.storageHash
218217
const storageTrie = new Trie({ useKeyHashing: true })
219218
const storageKeys: Uint8Array[] = []
220-
for (const storageProofsData of ropsten_contractWithStorage.storageProof) {
221-
storageKeys.push(hexToBytes(storageProofsData.key as PrefixedHexString))
219+
for (const storageProofsData of ropstenContractWithStorageData.storageProof) {
220+
storageKeys.push(hexToBytes(storageProofsData.key))
222221
for (const storageProofData of storageProofsData.proof) {
223-
const key = keccak256(hexToBytes(storageProofData as PrefixedHexString))
224-
await storageTrie['_db'].put(key, hexToBytes(storageProofData as PrefixedHexString))
222+
const key = keccak256(hexToBytes(storageProofData))
223+
await storageTrie['_db'].put(key, hexToBytes(storageProofData))
225224
}
226225
}
227226
storageTrie.root(hexToBytes(storageRoot))
@@ -230,29 +229,29 @@ describe('ProofStateManager', () => {
230229
trie.root(stateRoot!)
231230

232231
// tamper with account data
233-
const testdata = { ...(ropsten_contractWithStorage as any) }
232+
const testData = { ...ropstenContractWithStorageData }
234233
for (const tamper of ['nonce', 'balance', 'codeHash', 'storageHash']) {
235-
const original = testdata[tamper]
234+
const original = testData[tamper as keyof typeof testData] as PrefixedHexString
236235
try {
237-
const newField = `0x9${original.slice(3)}`
238-
testdata[tamper] = newField
239-
await stateManager.verifyProof(testdata)
236+
;(testData[tamper as keyof typeof testData] as PrefixedHexString) =
237+
`0x9${original.slice(3)}`
238+
await stateManager.verifyProof(testData)
240239
// note: this implicitly means that newField !== original,
241240
// if newField === original then the proof would be valid and test would fail
242241
assert.fail('should throw')
243242
} catch (e) {
244243
assert.ok(true, 'threw on invalid proof')
245244
} finally {
246-
testdata[tamper] = original
245+
;(testData[tamper as keyof typeof testData] as PrefixedHexString) = original
247246
}
248247
}
249248

250249
// tamper with storage slots
251-
for (const slot of testdata.storageProof) {
250+
for (const slot of testData.storageProof) {
252251
const original = slot.value
253252
slot.value = `0x9${original.slice(3)}`
254253
try {
255-
await stateManager.verifyProof(testdata)
254+
await stateManager.verifyProof(testData)
256255
assert.fail('should throw')
257256
} catch {
258257
assert.ok(true, 'threw on invalid proof')
@@ -272,23 +271,23 @@ describe('ProofStateManager', () => {
272271
const stateManager = new MerkleStateManager({ trie })
273272
// Dump all the account proof data in the DB
274273
let stateRoot: Uint8Array | undefined
275-
for (const proofData of ropsten_nonexistentAccount.accountProof) {
276-
const bufferData = hexToBytes(proofData as PrefixedHexString)
274+
for (const proofData of ropstenNonexistentAccountData.accountProof) {
275+
const bufferData = hexToBytes(proofData)
277276
const key = keccak256(bufferData)
278277
if (stateRoot === undefined) {
279278
stateRoot = key
280279
}
281280
await trie['_db'].put(key, bufferData)
282281
}
283-
const storageRoot = ropsten_nonexistentAccount.storageHash as PrefixedHexString
282+
const storageRoot = ropstenNonexistentAccountData.storageHash
284283
const storageTrie = new Trie({ useKeyHashing: true })
285284
storageTrie.root(hexToBytes(storageRoot))
286285
const addressHex = bytesToHex(address.bytes)
287286
stateManager['_storageTries'][addressHex] = storageTrie
288287
trie.root(stateRoot!)
289288

290289
// tamper with account data
291-
const testdata = { ...(ropsten_nonexistentAccount as any) }
290+
const testdata = { ...(ropstenNonexistentAccountData as any) }
292291
for (const tamper of ['nonce', 'balance', 'codeHash', 'storageHash']) {
293292
const original = testdata[tamper]
294293
try {

packages/statemanager/test/rpcStateManager.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import { assert, describe, expect, it, vi } from 'vitest'
2020
import { MerkleStateManager } from '../src/merkleStateManager.js'
2121
import { RPCBlockChain, RPCStateManager } from '../src/rpcStateManager.js'
2222

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

27-
import type { JSONRPCBlock } from '@ethereumjs/block'
27+
import type { EVMMockBlockchainInterface } from '@ethereumjs/evm'
2828

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

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

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

311311
describe('blockchain', () =>
312312
it('uses blockhash', async () => {
313-
const blockchain = new RPCBlockChain(provider)
313+
const blockchain = new RPCBlockChain(provider) as unknown as EVMMockBlockchainInterface
314314
const blockTag = 1n
315315
const state = new RPCStateManager({ provider, blockTag })
316316
const evm = await createEVM({ blockchain, stateManager: state })

packages/statemanager/test/statefulVerkleStateManager.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ describe('Verkle Tree API tests', () => {
5151
const retrievedBigByteCode = await sm.getCode(address)
5252
assert.deepEqual(bigByteCode, retrievedBigByteCode)
5353
const reallyBigByteCode = hexToBytes(
54-
(await import('./testdata/biggestContractEver.json')).default.bytecode as PrefixedHexString,
54+
(await import('./testdata/biggestContractEver.js')).biggestContractEverData
55+
.bytecode as PrefixedHexString,
5556
)
5657
// Biggest mainnet contract - 0x10C621008B210C3A5d0385e458B48af05BF4Ec88 (supposedly anyway)
5758
await sm.putCode(address, reallyBigByteCode)

packages/statemanager/test/statelessVerkleStateManager.spec.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,30 @@ import { assert, beforeAll, describe, it, test } from 'vitest'
1818

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

21-
import * as testnetVerkleKaustinen from './testdata/testnetVerkleKaustinen.json'
22-
import * as verkleBlockJSON from './testdata/verkleKaustinen6Block72.json'
21+
import { testnetVerkleKaustinenData } from './testdata/testnetVerkleKaustinen.js'
22+
import { verkleKaustinen6Block72Data } from './testdata/verkleKaustinen6Block72.js'
2323

24-
import type { BlockData } from '@ethereumjs/block'
2524
import type { PrefixedHexString, VerkleCrypto } from '@ethereumjs/util'
2625

2726
describe('StatelessVerkleStateManager: Kaustinen Verkle Block', () => {
2827
let verkleCrypto: VerkleCrypto
2928
beforeAll(async () => {
3029
verkleCrypto = await loadVerkleCrypto()
3130
})
32-
const common = createCommonFromGethGenesis(testnetVerkleKaustinen.default, {
31+
const common = createCommonFromGethGenesis(testnetVerkleKaustinenData, {
3332
chain: 'customChain',
3433
eips: [2935, 4895, 6800],
3534
})
3635

37-
const decodedTxs = verkleBlockJSON.default.transactions.map((tx) =>
36+
const decodedTxs = verkleKaustinen6Block72Data.transactions?.map((tx) =>
3837
createTxFromSerializedData(hexToBytes(tx as PrefixedHexString), { common }),
3938
)
40-
const block = createBlock({ ...verkleBlockJSON, transactions: decodedTxs } as BlockData, {
41-
common,
42-
})
39+
const block = createBlock(
40+
{ ...verkleKaustinen6Block72Data, transactions: decodedTxs },
41+
{
42+
common,
43+
},
44+
)
4345

4446
it('initPreState()', async () => {
4547
const stateManager = new StatelessVerkleStateManager({ verkleCrypto })

packages/statemanager/test/testdata/biggestContractEver.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/statemanager/test/testdata/biggestContractEver.ts

Lines changed: 4 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)