From 075970445095d9ec27cae536934b8de1911bee17 Mon Sep 17 00:00:00 2001 From: William Cory Date: Sun, 14 Jul 2024 14:49:55 -0700 Subject: [PATCH] :white_check_mark: Test: Start adding test --- packages/blockchain/package.json | 167 +++++++++--------- .../src/utils/getBlockFromRpc.spec.ts | 79 +++++++++ pnpm-lock.yaml | 3 + 3 files changed, 166 insertions(+), 83 deletions(-) create mode 100644 packages/blockchain/src/utils/getBlockFromRpc.spec.ts diff --git a/packages/blockchain/package.json b/packages/blockchain/package.json index f5eee8d170..9c4679c3c8 100644 --- a/packages/blockchain/package.json +++ b/packages/blockchain/package.json @@ -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 " - ], - "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 " + ], + "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 } diff --git a/packages/blockchain/src/utils/getBlockFromRpc.spec.ts b/packages/blockchain/src/utils/getBlockFromRpc.spec.ts new file mode 100644 index 0000000000..b83a259472 --- /dev/null +++ b/packages/blockchain/src/utils/getBlockFromRpc.spec.ts @@ -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() + }) +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2b93cd3bdd..61cc4626e5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1551,6 +1551,9 @@ importers: specifier: ^2.14.2 version: 2.14.2(bufferutil@4.0.8)(typescript@5.5.2)(utf-8-validate@6.0.4)(zod@3.23.8) devDependencies: + '@tevm/test-utils': + specifier: workspace:^ + version: link:../../test/test-utils '@tevm/tsconfig': specifier: workspace:^ version: link:../../configs/tsconfig