Skip to content

Commit

Permalink
add test for reading era1
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyPoi committed Feb 4, 2025
1 parent 86f2028 commit 6e183df
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/era/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
"lint": "npm run biome && eslint --config .eslintrc.cjs . --ext .js,.ts",
"lint:fix": "npm run biome:fix && eslint --fix --config .eslintrc.cjs . --ext .js,.ts",
"prepublishOnly": "../../config/cli/prepublish.sh",
"test": "npm run test:node && npm run test:browser",
"test:browser": "npx vitest run --config=../../config/vitest.config.browser.mts",
"test": "npm run test:node",
"test:node": "npx vitest run",
"tsc": "../../config/cli/ts-compile.sh"
},
Expand Down
63 changes: 63 additions & 0 deletions packages/era/test/era1.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { createBlockHeaderFromBytesArray } from '@ethereumjs/block'
import { bytesToHex } from '@ethereumjs/util'
import { readFileSync } from 'fs'
import { assert, describe, expect, it } from 'vitest'

import {
Era1Types,
getBlockIndex,
parseBlockTuple,
readBlockIndex,
readBlockTupleAtOffset,
readOtherEntries,
validateERA1,
} from '../src/index.js'

// Reference file donwloaded from era1.ethportal.net
const filePath = './test/mainnet-00000-5ec1ffb8.era1'
const expectedLength = 3891337

function readBinaryFile(filePath: string): Uint8Array {
const buffer = readFileSync(filePath)
return new Uint8Array(buffer)
}

describe('Read Era1', async () => {
const era1File = readBinaryFile(filePath)
it('should read the file', () => {
expect(era1File.length).toEqual(expectedLength)
})
const blockIndex = getBlockIndex(era1File)
it('should have block index type', () => {
assert.deepEqual(blockIndex.type, Era1Types.BlockIndex)
})
it('should have correct count', () => {
expect(blockIndex.count).toEqual(8192)
})
const { startingNumber, offsets } = readBlockIndex(blockIndex.data, blockIndex.count)
it('should read block index', () => {
expect(startingNumber).toEqual(0)
expect(offsets.length).toEqual(8192)
})
const { accumulatorRoot, otherEntries } = await readOtherEntries(era1File)
it('should read accumulator root', () => {
assert.equal(
bytesToHex(accumulatorRoot),
'0x5ec1ffb8c3b146f42606c74ced973dc16ec5a107c0345858c343fc94780b4218',
)
})
it('should read other entries', () => {
expect(otherEntries.length).toEqual(0)
})
it('should read first block tuple', async () => {
const tupleEntry = readBlockTupleAtOffset(era1File, blockIndex.recordStart, offsets[0])
const { header, totalDifficulty } = await parseBlockTuple(tupleEntry)
const blockHeader = createBlockHeaderFromBytesArray(header.data, { setHardfork: true })
expect(blockHeader.number).toEqual(0n)
expect(totalDifficulty.data).toEqual(blockHeader.difficulty)
})
const valid = await validateERA1(era1File)
it('should validate era1 file', () => {
expect(valid).toEqual(true)
})
})
Binary file added packages/era/test/mainnet-00000-5ec1ffb8.era1
Binary file not shown.

0 comments on commit 6e183df

Please sign in to comment.