@ethereumjs/blockchain

中文文档
10.1.3发布于 2026/08/21MPL-2.0TypeScriptESM + CJS近 30 天下载 15.8万

存储和与块交互的模块

中文使用文档
文档目录15 节
  1. 目录
  2. 安装
  3. 开始
  4. 封锁搜索
  5. 链式迭代
  6. 头儿的硬叉
  7. 自定义创世纪
  8. 协商一致类型
  9. 存储
  10. 支持的块特性
  11. 事件和调试
  12. 浏览器
  13. API 密码
  14. 爱丽丝
  15. 许可证

@ethereumjs/ 块链 v10

NPM Package GitHub Issues Actions Status Code Coverage Discord

一个存储和与块交互的模块.

存储顺序链 @ethereumjs/块 块,跟踪条形头,支持通过 putBlock()用于 @ethereumjs/client 财务报告和审定财务报表 @ethereumjs/vm。 。 。 .

可运行的示例 examples/。 。 。 .

目录

安装

npm install @ethereumjs/blockchain

开始

使用 createBlockchain() ——正等待aync初始化(起源设置,共识接线):

import { createBlockchain } from '@ethereumjs/blockchain'
import { Common, Mainnet } from '@ethereumjs/common'

const common = new Common({ chain: Mainnet })
const blockchain = await createBlockchain({ common })
console.log(`Genesis hash: ${blockchain.genesisBlock.hash()}`)

主构造器 : createBlockchain(), (中文(简体) ), createBlockchainFromBlocksData()。 。 。 .

过 validateBlocks: false 编号 : validateConsensus: false 在实例和测试中,当区块故意不完整.

封锁搜索

在添加后按数字或散列检索块 putBlock()数字 :

// ./examples/getBlock.ts

import { createBlock } from '@ethereumjs/block'
import { createBlockchain } from '@ethereumjs/blockchain'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { bytesToHex } from '@ethereumjs/util'

const main = async () => {
  const common = new Common({ chain: Mainnet, hardfork: Hardfork.Prague })
  const blockchain = await createBlockchain({
    validateBlocks: false,
    validateConsensus: false,
    common,
  })

  const block1 = createBlock(
    {
      header: {
        number: 1n,
        parentHash: blockchain.genesisBlock.hash(),
        difficulty: blockchain.genesisBlock.header.difficulty + 1n,
      },
    },
    { common, setHardfork: true },
  )
  const block2 = createBlock(
    {
      header: {
        number: 2n,
        parentHash: block1.header.hash(),
        difficulty: block1.header.difficulty + 1n,
      },
    },
    { common, setHardfork: true },
  )
  await blockchain.putBlock(block1)
  await blockchain.putBlock(block2)

  const byNumber = await blockchain.getBlock(2n)
  const byHash = await blockchain.getBlock(block2.hash())

  console.log(`Block ${byNumber.header.number} hash: ${bytesToHex(byNumber.hash())}`)
  console.log(`Lookup by hash matches: ${bytesToHex(byHash.hash()) === bytesToHex(block2.hash())}`)
}

void main()

还: getBlocks(), (中文(简体) ), getIteratorHead(), (中文(简体) ), getLatestHeader()。 。 。 .

链式迭代

沿着峡谷链走 iterator()数字 :

// ./examples/iterateChain.ts

import { createBlock } from '@ethereumjs/block'
import { createBlockchain } from '@ethereumjs/blockchain'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { bytesToHex } from '@ethereumjs/util'

const main = async () => {
  const common = new Common({ chain: Mainnet, hardfork: Hardfork.Prague })
  const blockchain = await createBlockchain({
    validateBlocks: false,
    validateConsensus: false,
    common,
  })

  const block = createBlock(
    {
      header: {
        number: 1n,
        parentHash: blockchain.genesisBlock.hash(),
        difficulty: blockchain.genesisBlock.header.difficulty + 1n,
      },
    },
    { common, setHardfork: true },
  )
  const block2 = createBlock(
    {
      header: {
        number: 2n,
        parentHash: block.header.hash(),
        difficulty: block.header.difficulty + 1n,
      },
    },
    { common, setHardfork: true },
  )
  await blockchain.putBlock(block)
  await blockchain.putBlock(block2)

  await blockchain.iterator('i', (block) => {
    const blockNumber = block.header.number.toString()
    const blockHash = bytesToHex(block.hash())
    console.log(`Block ${blockNumber}: ${blockHash}`)
  })
}
void main()

头儿的硬叉

键 Common 硬叉当前头部块编号在 init 上 :

// ./examples/hardforkByHead.ts

import { createBlockchain } from '@ethereumjs/blockchain'
import { Common, Mainnet } from '@ethereumjs/common'

const main = async () => {
  const common = new Common({ chain: Mainnet })
  await createBlockchain({ common, hardforkByHeadBlockNumber: true })
  console.log(`Hardfork at genesis head: ${common.hardfork()}`)
}

void main()

自定义创世纪

从 Geth 起源 JSON 文件建立链 :

// ./examples/gethGenesis.ts

import { createBlockchain } from '@ethereumjs/blockchain'
import { createCommonFromGethGenesis, parseGethGenesisState } from '@ethereumjs/common'
import { postMergeGethGenesis } from '@ethereumjs/testdata'
import { bytesToHex } from '@ethereumjs/util'

const main = async () => {
  const common = createCommonFromGethGenesis(postMergeGethGenesis, { chain: 'customChain' })
  const genesisState = parseGethGenesisState(postMergeGethGenesis)
  const blockchain = await createBlockchain({
    genesisState,
    common,
  })
  const genesisBlockHash = blockchain.genesisBlock.hash()
  common.setForkHashes(genesisBlockHash)
  console.log(
    `Genesis hash from geth genesis parameters - ${bytesToHex(blockchain.genesisBlock.hash())}`,
  )
}

void main()

内建网络起源状态 @ ethereumjs/ 来源。通过 blockchain.genesisBlock。 。 。 .

协商一致类型

算法类页:1
PoS( 默认发生后)CasperConsensus困难 0,信标客户端进行验证
POW( 预现)EthashConsensusEthash 难度规则
PoA(历史测试网)CliqueConsensusGoerli 式签名者投票

Clique 实例 :

// ./examples/clique.ts

import { CliqueConsensus, createBlockchain } from '@ethereumjs/blockchain'
import { Common, ConsensusAlgorithm, Hardfork } from '@ethereumjs/common'
import { goerliChainConfig } from '@ethereumjs/testdata'

import type { ConsensusDict } from '@ethereumjs/blockchain'

const main = async () => {
  const common = new Common({ chain: goerliChainConfig, hardfork: Hardfork.London })

  const consensusDict: ConsensusDict = {}
  consensusDict[ConsensusAlgorithm.Clique] = new CliqueConsensus()
  const blockchain = await createBlockchain({
    consensusDict,
    common,
  })
  console.log(`Created blockchain with ${blockchain.consensus!.algorithm} consensus algorithm`)
}

void main()

自定义共识:执行 Consensus 接口和通过 consensusDict 或者说 consensus 选项。 见 自定义共识. spec.ts。 。 。 .

存储

默认 DB 是内存 MapDB。对于持久性,通过 DB- 符合要求的后端(参见 @ ethereumjs/ 客户端层包装器) (中文(简体) ).

支持的块特性

  • EIP-1559 (中文(简体) ) ——基准收费区块(伦敦后默认)
  • EIP-4844 (单位:千美元) - blob txs(需要 KZG 打开 Common,参见 @ethereumjs/tx (英语)(中文(简体) )
  • EIP-7685 (英语) ——块头中的 CL 请求
  • EIP-4895 (中文(简体) ) ——退出

事件和调试

blockchain.events 发射 deletedCanonicalBlocks {\fn黑体\fs22\bord1\shad0\3ahbe\4ah00\fscx67\fscy66\2chffffff\3ch808080}在重击中.

调试日志 : blockchain:#, (中文(简体) ), blockchain:clique, (中文(简体) ), blockchain:ethash启用 DEBUG=ethjs,blockchain:clique。 。 。 .

浏览器

提供了混合无害环境管理/CJS大楼。 见 ./例/浏览器.html。 。 。 .

API 密码

生成类型文件 文档。 。 。 .

爱丽丝

这个 EthereumJS GitHub组织及其寄存器由原Ethereum基金会JavaScript团队和更广泛的Ethereum社区的成员管理. 如果您想参加工作或改进图书馆,请查看 开发者文件 * 概述现行标准和工具,并审查 行为守则。 。 。 .

许可证

MPL - 2.0 导弹

版本与发布记录

@ethereumjs/blockchain 共发布 77 个版本,首次发布于 2020/10/22;过去 12 个月发布了 5 个版本。

版本发布日期距今
10.1.3latest2026/08/212 个月前
10.1.22026/05/295 个月前
10.1.12026/01/289 个月前
10.1.1-nightly.2nightly2026/01/199 个月前
10.1.1-nightly.12025/12/0411 个月前
10.1.02025/11/071 年前

其他发布通道:beta → 6.0.0-beta.3,alpha → 8.0.0-alpha.1,rc → 10.0.0-rc.1,nightly → 10.1.1-nightly.2。

关于 @ethereumjs/blockchain 的常见问题

@ethereumjs/blockchain 是什么?

存储和与块交互的模块

如何安装 @ethereumjs/blockchain?

在项目目录运行 npm install @ethereumjs/blockchain,或使用 pnpm add / yarn add / bun add。

@ethereumjs/blockchain 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ethereumjs/blockchain 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ethereumjs/blockchain 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ethereumjs/blockchain 可以商用吗?

@ethereumjs/blockchain 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@ethereumjs/blockchain 的下载量有多少?

根据 npm 官方下载统计,@ethereumjs/blockchain 在 2026/10/14 – 2026/11/12 这 30 天内被下载 157,930 次,日均约 5,264 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ethereumjs/blockchain 还在维护吗?

最新版本 10.1.3 发布于 2026/08/21(2 个月前);至今共发布 77 个版本,其中过去 12 个月发布了 5 个;首次发布于 2020/10/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。