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

ethash: migrate test data to js #3680

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 8 additions & 7 deletions packages/ethash/test/block.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { createBlock, createBlockFromBytesArray, createBlockFromRLP } from '@ethereumjs/block'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import { MapDB, hexToBytes, toBytes } from '@ethereumjs/util'
import { MapDB, hexToBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

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

import { blockTestsData } from './block_tests_data.js'
import { invalidBlockRLP, validBlockRLP } from './ethash_block_rlp_tests.js'

import type { BlockBytes } from '@ethereumjs/block'
import type { PrefixedHexString } from '@ethereumjs/util'

const cacheDB = new MapDB()

const { validBlockRlp, invalidBlockRlp } = require('./ethash_block_rlp_tests.json')

describe('Verify POW for valid and invalid blocks', () => {
it('should work', async () => {
const e = new Ethash(cacheDB as any)
Expand All @@ -22,21 +24,20 @@ describe('Verify POW for valid and invalid blocks', () => {
const genesisResult = await e.verifyPOW(genesis)
assert.ok(genesisResult, 'genesis block should be valid')

const validRlp = hexToBytes(`0x${validBlockRlp}`)
const validRlp = hexToBytes(validBlockRLP)
const validBlock = createBlockFromRLP(validRlp, { common })
const validBlockResult = await e.verifyPOW(validBlock)
assert.ok(validBlockResult, 'should be valid')

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

const testData = require('./block_tests_data.json')
const blockRlp = toBytes(testData.blocks[0].rlp)
const blockRlp = hexToBytes(blockTestsData.blocks[0].rlp as PrefixedHexString)
const block = createBlockFromRLP(blockRlp, { common })
const uncleBlockResult = await e.verifyPOW(block)
assert.ok(uncleBlockResult, 'should be valid')
Expand Down
63 changes: 0 additions & 63 deletions packages/ethash/test/block_tests_data.json

This file was deleted.

66 changes: 66 additions & 0 deletions packages/ethash/test/block_tests_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// cspell:disable
export const blockTestsData = {
_info: {
comment: '',
filledwith: 'cpp-1.3.0+commit.26123543.Linux.g++',
source: '/src/BlockchainTestsFiller/bcUncleHeaderValiditiy/correctFiller.json',
},
blocks: [
{
blockHeader: {
bloom:
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
coinbase: '0x8888f1f195afa192cfee860698584c030f4c9db1',
difficulty: '0x020080',
extraData: '',
gasLimit: '0x2fefba',
gasUsed: '0x5208',
hash: '0xc6208f30be1fb9053b073c49cc16795001bd07c6d2650b28d2e4a37a5eb2dde2',
mixHash: '0x16bd3db367a3b218565e6744de193fb601587af40ba093e8e3cf9b29f0aa4ff1',
nonce: '0xf5c0d237b1a07faa',
number: '0x03',
parentHash: '0x2b530c31b2556d8ad5e12311658f0ec47e35a4ceffecd83d06e7cd918d3a85f1',
receiptTrie: '0x4ede0225773c7a517b91994aca65ade45124e7ef4b8be1e6097c9773a11920af',
stateRoot: '0x77f96f4c766c10cd0207e2672b1b747c741ed75bc94e7be7abacb71cdca3c8fb',
timestamp: '0x5982d2d1',
transactionsTrie: '0x1722b8a91bfc4f5614ce36ee77c7cce6620ab4af36d3c54baa66d7dbeb7bce1a',
uncleHash: '0xbeb175854a56183e630cd77e1c6dcd50a8bab221f81f2376919c649b33c500e0',
},
rlp: '0xf9045df901f9a02b530c31b2556d8ad5e12311658f0ec47e35a4ceffecd83d06e7cd918d3a85f1a0beb175854a56183e630cd77e1c6dcd50a8bab221f81f2376919c649b33c500e0948888f1f195afa192cfee860698584c030f4c9db1a077f96f4c766c10cd0207e2672b1b747c741ed75bc94e7be7abacb71cdca3c8fba01722b8a91bfc4f5614ce36ee77c7cce6620ab4af36d3c54baa66d7dbeb7bce1aa04ede0225773c7a517b91994aca65ade45124e7ef4b8be1e6097c9773a11920afb90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba825208845982d2d180a016bd3db367a3b218565e6744de193fb601587af40ba093e8e3cf9b29f0aa4ff188f5c0d237b1a07faaf862f86002018304cb2f94095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca015eb1cc916728b9799e55c489857727669afb2986433d5f54cde11faaed9f0eea05d36f6d06c34aae8d0a2a5895c8ba4a17ad46a5fa59f361cb3e7e01a23030e38f901faf901f7a0ca028b1318795714d130a99d8023bd7463cf8084f31d2f95f1a2d9eb342e9caca01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a0cb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302004002832fefba80845982d2cf80a0b5488407bc8b147a9b3c4811864ebfc5bdb568fc8f91dcf9232ed6b7429c52f8882b9b47250942c14e',
transactions: [
{
data: '',
gasLimit: '0x04cb2f',
gasPrice: '0x01',
nonce: '0x02',
r: '0x15eb1cc916728b9799e55c489857727669afb2986433d5f54cde11faaed9f0ee',
s: '0x5d36f6d06c34aae8d0a2a5895c8ba4a17ad46a5fa59f361cb3e7e01a23030e38',
to: '0x095e7baea6a6c7c4c2dfeb977efac326af552d87',
v: '0x1c',
value: '0x0a',
},
],
uncleHeaders: [
{
bloom:
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
coinbase: '0x0000000000000000000000000000000000000000',
difficulty: '0x020040',
extraData: '',
gasLimit: '0x2fefba',
gasUsed: '0x00',
hash: '0xcac5903348d2b4ca370227f7bd24bc3101b327a05172a3d7d3106a11d2019c16',
mixHash: '0xb5488407bc8b147a9b3c4811864ebfc5bdb568fc8f91dcf9232ed6b7429c52f8',
nonce: '0x2b9b47250942c14e',
number: '0x02',
parentHash: '0xca028b1318795714d130a99d8023bd7463cf8084f31d2f95f1a2d9eb342e9cac',
receiptTrie: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
stateRoot: '0xcb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878',
timestamp: '0x5982d2cf',
transactionsTrie: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
uncleHash: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
},
],
},
],
}
6 changes: 3 additions & 3 deletions packages/ethash/test/ethash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { assert, describe, it } from 'vitest'
import { Ethash } from '../src/index.js'
import { getCacheSize, getEpoc, getFullSize } from '../src/util.js'

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

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

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

const headerHash = ethash.headerHash(header.raw())
Expand Down
4 changes: 0 additions & 4 deletions packages/ethash/test/ethash_block_rlp_tests.json

This file was deleted.

Loading
Loading