@tevm/block
The @tevm/block
package provides functionality for working with Ethereum blocks. It includes classes and utilities for handling block headers, block data, and block-related operations.
Generated API Documentation: View the full API documentation in the evmts/tevm-monorepo/packages/block/docs folder.
Installation
npm install @tevm/block
Overview
The @tevm/block
package provides:
- Block creation and manipulation
- Block header management
- RLP serialization/deserialization
- JSON-RPC block formatting
- Verkle tree support
API Reference
Core Classes
- Block - Main class for Ethereum block operations
- BlockHeader - Class for managing block headers
- ClRequest - Class for client requests
Interfaces
- BlockData - Block data structure
- BlockOptions - Options for block creation
- HeaderData - Block header data structure
- JsonBlock - JSON representation of a block
- JsonHeader - JSON representation of a header
- JsonRpcBlock - JSON-RPC block format
Verkle Tree Types
- VerkleExecutionWitness - Verkle execution witness
- VerkleProof - Verkle proof structure
- VerkleStateDiff - Verkle state difference
Block Types
- BlockBodyBytes - Block body byte representation
- BlockBytes - Full block byte representation
- BlockHeaderBytes - Block header byte representation
- ExecutionPayload - Execution payload structure
- BeaconPayloadJson - Beacon chain payload JSON
Utility Functions
- blockFromRpc - Create block from RPC response
- executionPayloadFromBeaconPayload - Convert beacon payload to execution payload
- getDifficulty - Calculate block difficulty
- valuesArrayToHeaderData - Convert array to header data
Usage Examples
Creating a New Block
import { Block } from '@tevm/block'
import { createCommon } from '@tevm/common'
import { mainnet } from 'viem/chains'
// Create a new block
const block = new Block({
common: createCommon({ ...mainnet })
})
Creating a Block from Data
import { Block } from '@tevm/block'
import { createCommon } from '@tevm/common'
import { mainnet } from 'viem/chains'
import { EthjsAddress } from '@tevm/utils'
const common = createCommon({ ...mainnet })
const blockData = {
header: {
parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
uncleHash: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
coinbase: EthjsAddress.fromString('0x0000000000000000000000000000000000000000'),
stateRoot: '0x0000000000000000000000000000000000000000000000000000000000000000',
transactionsTrie: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
receiptTrie: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
difficulty: 0n,
number: 0n,
gasLimit: 30000000n,
gasUsed: 0n,
timestamp: BigInt(Math.floor(Date.now() / 1000)),
extraData: '0x',
mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
nonce: '0x0000000000000000',
baseFeePerGas: 1000000000n
}
}
const block = Block.fromBlockData(blockData, { common })
Working with Block Headers
// Get block hash
const hash = block.hash()
// Serialize block
const serialized = block.serialize()
// Convert to JSON
const json = block.toJSON()
Creating from RLP Data
import { Block } from '@tevm/block'
import { createCommon } from '@tevm/common'
import { mainnet } from 'viem/chains'
const common = createCommon({ ...mainnet })
const serializedBlock = new Uint8Array([/* ... */])
const block = Block.fromRLPSerializedBlock(serializedBlock, { common })