Skip to content

Commit

Permalink
✅ Test: Start adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
William Cory authored and William Cory committed Jul 14, 2024
1 parent 14d89e5 commit 0759704
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 83 deletions.
167 changes: 84 additions & 83 deletions packages/blockchain/package.json
Original file line number Diff line number Diff line change
@@ -1,85 +1,86 @@
{
"name": "@tevm/blockchain",
"version": "1.1.0-next.97",
"private": false,
"description": "A custom implementation of ethereumjs blockchain",
"keywords": [
"solidity",
"ethereumjs",
"typescript",
"web3",
"blockchain"
],
"repository": {
"type": "git",
"url": "https://github.com/evmts/tevm-monorepo.git",
"directory": "packages/blockchain"
},
"license": "MIT",
"contributors": [
"Will Cory <[email protected]>"
],
"type": "module",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./types/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "types/index.d.ts",
"files": [
"dist",
"types",
"src",
"!src/**/*.spec.ts"
],
"scripts": {
"all": "pnpm i && bun run build && bun lint && bun format && bun test:run && bun generate:docs",
"build": "nx run-many --targets=build:dist,build:types --projects=@tevm/blockchain",
"build:dist": "tsup",
"build:types": "tsup --dts-only && tsc --emitDeclarationOnly --declaration",
"clean": "rm -rf node_modules && rm -rf artifacts && rm -rf dist && rm -rf cache",
"format": "biome format . --write",
"format:check": "biome format .",
"generate:docs": "typedoc",
"lint": "biome check . --write --unsafe",
"lint:check": "biome check . --verbose",
"lint:deps": "bunx depcheck",
"lint:package": "bunx publint --strict && attw --pack",
"package:up": "pnpm up --latest",
"test": "bun test --watch",
"test:coverage": "bun test --coverage",
"test:run": "bun test",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@ethereumjs/blockchain": "^7.2.0",
"@tevm/block": "workspace:^",
"@tevm/common": "workspace:^",
"@tevm/errors": "workspace:^",
"@tevm/jsonrpc": "workspace:^",
"@tevm/logger": "workspace:^",
"@tevm/trie": "workspace:^",
"@tevm/utils": "workspace:^"
},
"devDependencies": {
"@tevm/tsconfig": "workspace:^",
"@tevm/tsupconfig": "workspace:^"
},
"peerDependencies": {
"viem": "^2.14.2"
},
"publishConfig": {
"access": "public"
},
"sideEffect": false
"name": "@tevm/blockchain",
"version": "1.1.0-next.97",
"private": false,
"description": "A custom implementation of ethereumjs blockchain",
"keywords": [
"solidity",
"ethereumjs",
"typescript",
"web3",
"blockchain"
],
"repository": {
"type": "git",
"url": "https://github.com/evmts/tevm-monorepo.git",
"directory": "packages/blockchain"
},
"license": "MIT",
"contributors": [
"Will Cory <[email protected]>"
],
"type": "module",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./types/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "types/index.d.ts",
"files": [
"dist",
"types",
"src",
"!src/**/*.spec.ts"
],
"scripts": {
"all": "pnpm i && bun run build && bun lint && bun format && bun test:run && bun generate:docs",
"build": "nx run-many --targets=build:dist,build:types --projects=@tevm/blockchain",
"build:dist": "tsup",
"build:types": "tsup --dts-only && tsc --emitDeclarationOnly --declaration",
"clean": "rm -rf node_modules && rm -rf artifacts && rm -rf dist && rm -rf cache",
"format": "biome format . --write",
"format:check": "biome format .",
"generate:docs": "typedoc",
"lint": "biome check . --write --unsafe",
"lint:check": "biome check . --verbose",
"lint:deps": "bunx depcheck",
"lint:package": "bunx publint --strict && attw --pack",
"package:up": "pnpm up --latest",
"test": "bun test --watch",
"test:coverage": "bun test --coverage",
"test:run": "bun test",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@ethereumjs/blockchain": "^7.2.0",
"@tevm/block": "workspace:^",
"@tevm/common": "workspace:^",
"@tevm/errors": "workspace:^",
"@tevm/jsonrpc": "workspace:^",
"@tevm/logger": "workspace:^",
"@tevm/trie": "workspace:^",
"@tevm/utils": "workspace:^"
},
"devDependencies": {
"@tevm/test-utils": "workspace:^",
"@tevm/tsconfig": "workspace:^",
"@tevm/tsupconfig": "workspace:^"
},
"peerDependencies": {
"viem": "^2.14.2"
},
"publishConfig": {
"access": "public"
},
"sideEffect": false
}
79 changes: 79 additions & 0 deletions packages/blockchain/src/utils/getBlockFromRpc.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { describe, expect, it } from 'bun:test'
import { Block } from '@tevm/block'
import { transports } from '@tevm/test-utils'
import { getBlockFromRpc } from './getBlockFromRpc.js'
import { optimism } from '@tevm/common'

describe('getBlockFromRpc', () => {
it('should fetch the latest block', async () => {
const transport = transports.optimism
const common = optimism.copy()

const block = await getBlockFromRpc({ transport, blockTag: 'latest' }, common)
expect(block).toBeInstanceOf(Block)
expect(block.header.number).toBeGreaterThanOrEqual(0n)
})

it('should fetch a block by number', async () => {
const transport = transports.optimism
const common = optimism.copy()
const blockNumber = 122606365n

const block = await getBlockFromRpc({ transport, blockTag: blockNumber }, common)
expect(block).toBeInstanceOf(Block)
expect(block.header.number).toBe(blockNumber)
})

it('should fetch a block by hash', async () => {
const transport = transports.optimism
const common = optimism.copy()
const blockHash = '0x6d4f1b3c89f9a26e7b1d8af7b093b8936d4d1af7989d0b1a7b1a2b0b0b6a6a6a'

const block = await getBlockFromRpc({ transport, blockTag: blockHash }, common)
expect(block).toBeInstanceOf(Block)
expect(block.hash().toString('hex')).toBe(blockHash.slice(2))
})

it('should handle invalid block tag', async () => {
const transport = transports.optimism
const common = optimism.copy()
const invalidBlockTag = 'invalid-tag'

await expect(getBlockFromRpc({ transport, blockTag: invalidBlockTag as any }, common)).rejects.toThrow(
`Invalid blocktag ${invalidBlockTag}`,
)
})

it('should handle non-existing block number', async () => {
const transport = transports.optimism
const common = optimism.copy()
const nonExistingBlockNumber = 99999999999n

await expect(getBlockFromRpc({ transport, blockTag: nonExistingBlockNumber }, common)).rejects.toThrow(
'No block found',
)
})

it('should handle non-existing block hash', async () => {
const transport = transports.optimism
const common = optimism.copy()
const nonExistingBlockHash = '0x' + '0'.repeat(64)

await expect(getBlockFromRpc({ transport, blockTag: nonExistingBlockHash }, common)).rejects.toThrow(
'No block found',
)
})

it('should handle Optimism deposit transactions filtering', async () => {
const transport = transports.optimism
const common = optimism.copy()

const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})

const block = await getBlockFromRpc({ transport, blockTag: 'latest' }, common)
expect(block).toBeInstanceOf(Block)
expect(consoleWarnSpy).toHaveBeenCalled()

consoleWarnSpy.mockRestore()
})
})
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0759704

Please sign in to comment.