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

✨ Feat: Add getBlockByTag to Chain package #1315

Merged
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
5 changes: 5 additions & 0 deletions .changeset/tall-phones-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tevm/blockchain": minor
---

Added new getBlockByTag method to Chain. It gets blocks by hash or number just like getBlock but also accepts a named tag such as 'latest', 'pending', or 'forked'

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

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

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

Original file line number Diff line number Diff line change
@@ -1,55 +1,16 @@
// Bun Snapshot v1, https://goo.gl/fbAQLP

exports[`cloneVmWithBlockTag should handle errors during VM cloning 1`] = `
"Invalid blocktag Infinity

Docs: https://tevm.sh/reference/tevm/errors/classes/internalerror/
Details: Invalid blocktag Infinity
Version: 1.1.0.next-73"
`;

exports[`cloneVmWithBlockTag should properly handle a fork client requestiong a block prefork 1`] = `
Uint8Array [
107,
141,
207,
138,
42,
50,
122,
161,
178,
227,
235,
237,
89,
43,
27,
29,
9,
190,
201,
146,
105,
196,
134,
37,
180,
103,
155,
110,
214,
164,
226,
87,
]
`;

exports[`cloneVmWithBfockTag should handle errors during forking 1`] = `[ForkError: Invalid blocktag Infinity

Docs: https://tevm.sh/reference/tevm/errors/classes/invalidblockerror/
Version: 1.1.0.next-73
Invalid blocktag Infinity

Docs: https://tevm.sh/reference/tevm/errors/classes/accountlockederror/
Details: Invalid blocktag Infinity
Docs: https://tevm.sh/reference/tevm/errors/classes/invalidblockerror/
Version: 1.1.0.next-73

Docs: https://tevm.sh/reference/tevm/errors/classes/invalidblockerror/
Details: /reference/tevm/errors/classes/invalidblockerror/
Version: 1.1.0.next-73]`;

exports[`cloneVmWithBfockTag should handle errors during VM cloning 1`] = `[InternalError: deepCopy error
Expand Down
2 changes: 1 addition & 1 deletion packages/blockchain/docs/functions/createChain.md

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

6 changes: 4 additions & 2 deletions packages/blockchain/docs/functions/getBlockFromRpc.md

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

27 changes: 27 additions & 0 deletions packages/blockchain/docs/type-aliases/Chain.md

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

166 changes: 84 additions & 82 deletions packages/blockchain/package.json
Original file line number Diff line number Diff line change
@@ -1,84 +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/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
}
16 changes: 15 additions & 1 deletion packages/blockchain/src/Chain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type BlockchainEvents, type Consensus, type OnBlock } from '@ethereumjs/blockchain'
import type { Block, BlockHeader } from '@tevm/block'
import type { AsyncEventEmitter } from '@tevm/utils'
import type { AsyncEventEmitter, BlockTag, Hex } from '@tevm/utils'
import type { BaseChain } from './BaseChain.js'

/**
Expand Down Expand Up @@ -41,6 +41,20 @@ export type Chain = {} & BaseChain & {
*/
getBlock(blockId: Uint8Array | number | bigint): Promise<Block>

/**
* Gets block given one of the following inputs:
* - Hex block hash
* - Hex block number (if length is 32 bytes, it is treated as a hash)
* - Uint8Array block hash
* - Number block number
* - BigInt block number
* - BlockTag block tag
* - Named block tag (e.g. 'latest', 'earliest', 'pending')
* @throws {UnknownBlockError} - If the block is not found
* @throw {InvalidBlockTagError} - If the block tag is invalid}
*/
getBlockByTag(blockTag: Hex | Uint8Array | number | bigint | BlockTag): Promise<Block>

/**
* Iterates through blocks starting at the specified iterator head and calls
* the onBlock function on each block.
Expand Down
1 change: 1 addition & 0 deletions packages/blockchain/src/actions/getBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const getBlock = (baseChain) => async (blockId) => {
baseChain.logger.debug('Fetching block from remote rpc...')

const fetchedBlock = await getBlockFromRpc(
baseChain,
{
transport: baseChain.options.fork?.transport,
blockTag: blockId instanceof Uint8Array ? bytesToHex(blockId) : BigInt(blockId),
Expand Down
Loading
Loading