Skip to content

Commit 4f39d6f

Browse files
ethash: migrate test data to js (#3680)
* ethash: migrate test data to js * ethash: cspell fix --------- Co-authored-by: Holger Drewes <[email protected]>
1 parent 7a76c49 commit 4f39d6f

File tree

8 files changed

+108
-101
lines changed

8 files changed

+108
-101
lines changed

packages/ethash/test/block.spec.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { createBlock, createBlockFromBytesArray, createBlockFromRLP } from '@ethereumjs/block'
22
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
33
import { RLP } from '@ethereumjs/rlp'
4-
import { MapDB, hexToBytes, toBytes } from '@ethereumjs/util'
4+
import { MapDB, hexToBytes } from '@ethereumjs/util'
55
import { assert, describe, it } from 'vitest'
66

77
import { Ethash } from '../src/index.js'
88

9+
import { blockTestsData } from './block_tests_data.js'
10+
import { invalidBlockRLP, validBlockRLP } from './ethash_block_rlp_tests.js'
11+
912
import type { BlockBytes } from '@ethereumjs/block'
13+
import type { PrefixedHexString } from '@ethereumjs/util'
1014

1115
const cacheDB = new MapDB()
1216

13-
const { validBlockRlp, invalidBlockRlp } = require('./ethash_block_rlp_tests.json')
14-
1517
describe('Verify POW for valid and invalid blocks', () => {
1618
it('should work', async () => {
1719
const e = new Ethash(cacheDB as any)
@@ -22,21 +24,20 @@ describe('Verify POW for valid and invalid blocks', () => {
2224
const genesisResult = await e.verifyPOW(genesis)
2325
assert.ok(genesisResult, 'genesis block should be valid')
2426

25-
const validRlp = hexToBytes(`0x${validBlockRlp}`)
27+
const validRlp = hexToBytes(validBlockRLP)
2628
const validBlock = createBlockFromRLP(validRlp, { common })
2729
const validBlockResult = await e.verifyPOW(validBlock)
2830
assert.ok(validBlockResult, 'should be valid')
2931

30-
const invalidRlp = hexToBytes(`0x${invalidBlockRlp}`)
32+
const invalidRlp = hexToBytes(invalidBlockRLP)
3133
// Put correct amount of extraData in block extraData field so block can be deserialized
3234
const values = RLP.decode(Uint8Array.from(invalidRlp)) as BlockBytes
3335
values[0][12] = new Uint8Array(32)
3436
const invalidBlock = createBlockFromBytesArray(values, { common })
3537
const invalidBlockResult = await e.verifyPOW(invalidBlock)
3638
assert.ok(!invalidBlockResult, 'should be invalid')
3739

38-
const testData = require('./block_tests_data.json')
39-
const blockRlp = toBytes(testData.blocks[0].rlp)
40+
const blockRlp = hexToBytes(blockTestsData.blocks[0].rlp as PrefixedHexString)
4041
const block = createBlockFromRLP(blockRlp, { common })
4142
const uncleBlockResult = await e.verifyPOW(block)
4243
assert.ok(uncleBlockResult, 'should be valid')

packages/ethash/test/block_tests_data.json

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// cspell:disable
2+
export const blockTestsData = {
3+
_info: {
4+
comment: '',
5+
filledwith: 'cpp-1.3.0+commit.26123543.Linux.g++',
6+
source: '/src/BlockchainTestsFiller/bcUncleHeaderValiditiy/correctFiller.json',
7+
},
8+
blocks: [
9+
{
10+
blockHeader: {
11+
bloom:
12+
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
13+
coinbase: '0x8888f1f195afa192cfee860698584c030f4c9db1',
14+
difficulty: '0x020080',
15+
extraData: '',
16+
gasLimit: '0x2fefba',
17+
gasUsed: '0x5208',
18+
hash: '0xc6208f30be1fb9053b073c49cc16795001bd07c6d2650b28d2e4a37a5eb2dde2',
19+
mixHash: '0x16bd3db367a3b218565e6744de193fb601587af40ba093e8e3cf9b29f0aa4ff1',
20+
nonce: '0xf5c0d237b1a07faa',
21+
number: '0x03',
22+
parentHash: '0x2b530c31b2556d8ad5e12311658f0ec47e35a4ceffecd83d06e7cd918d3a85f1',
23+
receiptTrie: '0x4ede0225773c7a517b91994aca65ade45124e7ef4b8be1e6097c9773a11920af',
24+
stateRoot: '0x77f96f4c766c10cd0207e2672b1b747c741ed75bc94e7be7abacb71cdca3c8fb',
25+
timestamp: '0x5982d2d1',
26+
transactionsTrie: '0x1722b8a91bfc4f5614ce36ee77c7cce6620ab4af36d3c54baa66d7dbeb7bce1a',
27+
uncleHash: '0xbeb175854a56183e630cd77e1c6dcd50a8bab221f81f2376919c649b33c500e0',
28+
},
29+
rlp: '0xf9045df901f9a02b530c31b2556d8ad5e12311658f0ec47e35a4ceffecd83d06e7cd918d3a85f1a0beb175854a56183e630cd77e1c6dcd50a8bab221f81f2376919c649b33c500e0948888f1f195afa192cfee860698584c030f4c9db1a077f96f4c766c10cd0207e2672b1b747c741ed75bc94e7be7abacb71cdca3c8fba01722b8a91bfc4f5614ce36ee77c7cce6620ab4af36d3c54baa66d7dbeb7bce1aa04ede0225773c7a517b91994aca65ade45124e7ef4b8be1e6097c9773a11920afb90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba825208845982d2d180a016bd3db367a3b218565e6744de193fb601587af40ba093e8e3cf9b29f0aa4ff188f5c0d237b1a07faaf862f86002018304cb2f94095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca015eb1cc916728b9799e55c489857727669afb2986433d5f54cde11faaed9f0eea05d36f6d06c34aae8d0a2a5895c8ba4a17ad46a5fa59f361cb3e7e01a23030e38f901faf901f7a0ca028b1318795714d130a99d8023bd7463cf8084f31d2f95f1a2d9eb342e9caca01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a0cb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302004002832fefba80845982d2cf80a0b5488407bc8b147a9b3c4811864ebfc5bdb568fc8f91dcf9232ed6b7429c52f8882b9b47250942c14e',
30+
transactions: [
31+
{
32+
data: '',
33+
gasLimit: '0x04cb2f',
34+
gasPrice: '0x01',
35+
nonce: '0x02',
36+
r: '0x15eb1cc916728b9799e55c489857727669afb2986433d5f54cde11faaed9f0ee',
37+
s: '0x5d36f6d06c34aae8d0a2a5895c8ba4a17ad46a5fa59f361cb3e7e01a23030e38',
38+
to: '0x095e7baea6a6c7c4c2dfeb977efac326af552d87',
39+
v: '0x1c',
40+
value: '0x0a',
41+
},
42+
],
43+
uncleHeaders: [
44+
{
45+
bloom:
46+
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
47+
coinbase: '0x0000000000000000000000000000000000000000',
48+
difficulty: '0x020040',
49+
extraData: '',
50+
gasLimit: '0x2fefba',
51+
gasUsed: '0x00',
52+
hash: '0xcac5903348d2b4ca370227f7bd24bc3101b327a05172a3d7d3106a11d2019c16',
53+
mixHash: '0xb5488407bc8b147a9b3c4811864ebfc5bdb568fc8f91dcf9232ed6b7429c52f8',
54+
nonce: '0x2b9b47250942c14e',
55+
number: '0x02',
56+
parentHash: '0xca028b1318795714d130a99d8023bd7463cf8084f31d2f95f1a2d9eb342e9cac',
57+
receiptTrie: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
58+
stateRoot: '0xcb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878',
59+
timestamp: '0x5982d2cf',
60+
transactionsTrie: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
61+
uncleHash: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
62+
},
63+
],
64+
},
65+
],
66+
}

packages/ethash/test/ethash.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import { assert, describe, it } from 'vitest'
66
import { Ethash } from '../src/index.js'
77
import { getCacheSize, getEpoc, getFullSize } from '../src/util.js'
88

9-
const powTests = require('./ethash_tests.json')
9+
import { ethashTests } from './ethash_tests.js'
1010

1111
const ethash = new Ethash()
12-
const tests = Object.keys(powTests)
12+
const tests = Object.keys(ethashTests) as (keyof typeof ethashTests)[]
1313
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
1414

1515
describe('POW tests', () => {
1616
it('should work', async () => {
1717
for (const key of tests) {
18-
const test = powTests[key]
18+
const test = ethashTests[key]
1919
const header = createBlockHeaderFromRLP(hexToBytes(`0x${test.header}`), { common })
2020

2121
const headerHash = ethash.headerHash(header.raw())

packages/ethash/test/ethash_block_rlp_tests.json

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

0 commit comments

Comments
 (0)