Skip to content

Commit

Permalink
✨ Feat: Add getBlockByTag to Chain package
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 de2a2ab commit 14d89e5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/blockchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@ethereumjs/blockchain": "^7.2.0",
"@tevm/block": "workspace:^",
"@tevm/common": "workspace:^",
"@tevm/errors": "workspace:^",
"@tevm/jsonrpc": "workspace:^",
"@tevm/logger": "workspace:^",
"@tevm/trie": "workspace:^",
Expand Down
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
35 changes: 35 additions & 0 deletions packages/blockchain/src/actions/getBlockByTag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { hexToBytes, hexToNumber, isHex } from '@tevm/utils'
import { getBlock } from './getBlock.js'
import { UnknownBlockError } from '@tevm/errors'
import { getBlockFromRpc } from '../utils/getBlockFromRpc.js'
import { putBlock } from './putBlock.js'

/**
* @param {import('../BaseChain.js').BaseChain} baseChain
* @returns {import('../Chain.js').Chain['getBlockByTag']}
*/
export const getBlockByTag = (baseChain) => async (blockId) => {
const _getBlock = getBlock(baseChain)
if (isHex(blockId)) {
return blockId.length === 66 ? _getBlock(hexToBytes(blockId)) : _getBlock(hexToNumber(blockId))
}
if (typeof blockId === 'number' || typeof blockId === 'bigint' || blockId instanceof Uint8Array) {
return _getBlock(blockId)
}
const block = baseChain.blocksByTag.get(blockId)
if (!block && baseChain.options.fork?.transport) {
const block = await getBlockFromRpc(
{
transport: baseChain.options.fork.transport,
blockTag: blockId,
},
baseChain.common,
)
await putBlock(baseChain)(block)
return block
}
if (!block) {
throw new UnknownBlockError(blockId)
}
return block
}
2 changes: 2 additions & 0 deletions packages/blockchain/src/createChain.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { deepCopy } from './actions/deepCopy.js'
import { delBlock } from './actions/delBlock.js'
import { getBlock } from './actions/getBlock.js'
import { getBlockByTag } from './actions/getBlockByTag.js'
import { getCanonicalHeadBlock } from './actions/getCanonicalHeadBlock.js'
import { getIteratorHead } from './actions/getIteratorHead.js'
import { putBlock } from './actions/putBlock.js'
Expand All @@ -18,6 +19,7 @@ export const createChain = async (options) => {
*/
const decorate = (baseChain) => {
return Object.assign(baseChain, {
getBlockByTag: getBlockByTag(baseChain),
deepCopy: async () => decorate(await deepCopy(baseChain)()),
shallowCopy: () => decorate(shallowCopy(baseChain)()),
getBlock: getBlock(baseChain),
Expand Down
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 14d89e5

Please sign in to comment.