Skip to content

Commit

Permalink
Vitest/ESM: VM File Path Additions and Ethash Transition (#2786)
Browse files Browse the repository at this point in the history
* VM: fix linting

* VM: add .js path references

* Ethash: tape -> vitest transition

* Ethash: ad .js file path references
  • Loading branch information
holgerd77 authored Jun 15, 2023
1 parent 9961da2 commit a59a6ab
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 209 deletions.
4 changes: 2 additions & 2 deletions packages/ethash/examples/example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bytesToHex } from '@ethereumjs/util'
import { hexToBytes } from 'ethereum-cryptography/utils'
import { Ethash } from '../src'
import { hexToBytes } from 'ethereum-cryptography/utils.js'
import { Ethash } from '../src/index.js'

const ethash = new Ethash()

Expand Down
4 changes: 2 additions & 2 deletions packages/ethash/examples/rawExample.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Ethash } from '../src'
import { Ethash } from '../src/index.js'
import { MemoryLevel } from 'memory-level'
import { bytesToHex } from '@ethereumjs/util'
import { hexToBytes } from 'ethereum-cryptography/utils'
import { hexToBytes } from 'ethereum-cryptography/utils.js'

const ethash = new Ethash(new MemoryLevel())

Expand Down
6 changes: 3 additions & 3 deletions packages/ethash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"license": "MPL-2.0",
"author": "mjbecze <[email protected]>",
"type": "module",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
Expand All @@ -32,14 +33,13 @@
"scripts": {
"build": "../../config/cli/ts-build.sh",
"clean": "../../config/cli/clean-package.sh",
"coverage": "../../config/cli/coverage.sh",
"coverage": "npx vitest run --coverage.enabled --coverage.reporter=lcov",
"docs:build": "typedoc --options typedoc.js",
"lint": "../../config/cli/lint.sh",
"lint:diff": "../../config/cli/lint-diff.sh",
"lint:fix": "../../config/cli/lint-fix.sh",
"prepublishOnly": "../../config/cli/prepublish.sh",
"tape": "tape -r ts-node/register",
"test": "tape -r ts-node/register test/*.spec.ts",
"test": "npx vitest run --testTimeout=30000",
"tsc": "../../config/cli/ts-compile.sh"
},
"dependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/ethash/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
setLengthLeft,
zeros,
} from '@ethereumjs/util'
import { keccak256, keccak512 } from 'ethereum-cryptography/keccak'
import { hexToBytes } from 'ethereum-cryptography/utils'
import { keccak256, keccak512 } from 'ethereum-cryptography/keccak.js'
import { hexToBytes } from 'ethereum-cryptography/utils.js'

import {
bytesReverse,
Expand All @@ -24,7 +24,7 @@ import {
getFullSize,
getSeed,
params,
} from './util'
} from './util.js'

import type { BlockData, HeaderData } from '@ethereumjs/block'
import type { DB, DBObject } from '@ethereumjs/util'
Expand Down
2 changes: 1 addition & 1 deletion packages/ethash/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isProbablyPrime } from 'bigint-crypto-utils'
import { keccak256 } from 'ethereum-cryptography/keccak'
import { keccak256 } from 'ethereum-cryptography/keccak.js'

export const params = {
DATASET_BYTES_INIT: 1073741824, // 2^30
Expand Down
63 changes: 32 additions & 31 deletions packages/ethash/test/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,44 @@ import { Block } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import { MapDB, toBytes } from '@ethereumjs/util'
import { hexToBytes } from 'ethereum-cryptography/utils'
import * as tape from 'tape'
import { hexToBytes } from 'ethereum-cryptography/utils.js'
import { assert, describe, it } from 'vitest'

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

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

const cacheDB = new MapDB()

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

tape('Verify POW for valid and invalid blocks', async function (t) {
const e = new Ethash(cacheDB as any)

const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })

const genesis = Block.fromBlockData({}, { common })
const genesisResult = await e.verifyPOW(genesis)
t.ok(genesisResult, 'genesis block should be valid')

const validRlp = hexToBytes(validBlockRlp)
const validBlock = Block.fromRLPSerializedBlock(validRlp, { common })
const validBlockResult = await e.verifyPOW(validBlock)
t.ok(validBlockResult, 'should be valid')

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 = Block.fromValuesArray(values, { common })
const invalidBlockResult = await e.verifyPOW(invalidBlock)
t.ok(!invalidBlockResult, 'should be invalid')

const testData = require('./block_tests_data.json')
const blockRlp = toBytes(testData.blocks[0].rlp)
const block = Block.fromRLPSerializedBlock(blockRlp, { common })
const uncleBlockResult = await e.verifyPOW(block)
t.ok(uncleBlockResult, 'should be valid')
t.end()
describe('Verify POW for valid and invalid blocks', () => {
it('should work', async () => {
const e = new Ethash(cacheDB as any)

const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })

const genesis = Block.fromBlockData({}, { common })
const genesisResult = await e.verifyPOW(genesis)
assert.ok(genesisResult, 'genesis block should be valid')

const validRlp = hexToBytes(validBlockRlp)
const validBlock = Block.fromRLPSerializedBlock(validRlp, { common })
const validBlockResult = await e.verifyPOW(validBlock)
assert.ok(validBlockResult, 'should be valid')

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 = Block.fromValuesArray(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 block = Block.fromRLPSerializedBlock(blockRlp, { common })
const uncleBlockResult = await e.verifyPOW(block)
assert.ok(uncleBlockResult, 'should be valid')
})
})
40 changes: 21 additions & 19 deletions packages/ethash/test/ethash.spec.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
import { BlockHeader } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { bytesToHex } from '@ethereumjs/util'
import { hexToBytes } from 'ethereum-cryptography/utils'
import * as tape from 'tape'
import { hexToBytes } from 'ethereum-cryptography/utils.js'
import { assert, describe, it } from 'vitest'

import { Ethash } from '../src'
import { getCacheSize, getEpoc, getFullSize } from '../src/util'
import { Ethash } from '../src/index.js'
import { getCacheSize, getEpoc, getFullSize } from '../src/util.js'

const powTests = require('./ethash_tests.json')

const ethash = new Ethash()
const tests = Object.keys(powTests)
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })

tape('POW tests', async function (t) {
for (const key of tests) {
const test = powTests[key]
const header = BlockHeader.fromRLPSerializedHeader(hexToBytes(test.header), { common })
describe('POW tests', () => {
it('should work', async () => {
for (const key of tests) {
const test = powTests[key]
const header = BlockHeader.fromRLPSerializedHeader(hexToBytes(test.header), { common })

const headerHash = ethash.headerHash(header.raw())
t.equal(bytesToHex(headerHash), test.header_hash, 'generate header hash')
const headerHash = ethash.headerHash(header.raw())
assert.equal(bytesToHex(headerHash), test.header_hash, 'generate header hash')

const epoc = getEpoc(header.number)
t.equal(await getCacheSize(epoc), test.cache_size, 'generate cache size')
t.equal(await getFullSize(epoc), test.full_size, 'generate full cache size')
const epoc = getEpoc(header.number)
assert.equal(await getCacheSize(epoc), test.cache_size, 'generate cache size')
assert.equal(await getFullSize(epoc), test.full_size, 'generate full cache size')

ethash.mkcache(test.cache_size, hexToBytes(test.seed))
t.equal(bytesToHex(ethash.cacheHash()), test.cache_hash, 'generate cache')
ethash.mkcache(test.cache_size, hexToBytes(test.seed))
assert.equal(bytesToHex(ethash.cacheHash()), test.cache_hash, 'generate cache')

const r = ethash.run(headerHash, hexToBytes(test.nonce), test.full_size)
t.equal(bytesToHex(r.hash), test.result, 'generate result')
t.equal(bytesToHex(r.mix), test.mixHash, 'generate mix hash')
}
const r = ethash.run(headerHash, hexToBytes(test.nonce), test.full_size)
assert.equal(bytesToHex(r.hash), test.result, 'generate result')
assert.equal(bytesToHex(r.mix), test.mixHash, 'generate mix hash')
}
})
})
Loading

0 comments on commit a59a6ab

Please sign in to comment.