@ethereumjs/statemanager

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

执行Etereum州长

中文使用文档
文档目录16 节
  1. 目录
  2. 安装
  3. 概览
  4. 默克勒州管理器
  5. 检查站
  6. 代码和存储
  7. EIP-1186证明
  8. 简单状态管理器
  9. RPC 状态管理器
  10. 二进制树州经理(实验)
  11. 建筑
  12. 浏览器
  13. API 密码
  14. 发展
  15. 爱丽丝
  16. 许可证

@ethereumjs/政治家 v10

NPM Package GitHub Issues Actions Status Code Coverage Discord

提供高水平进入埃塞乌姆州的图书馆
  • EVM/VM的透明状态访问
  • *
  • 控制下的依赖(5个外部+) @Noble 密码)
  • + 基于 Diff 的 Caches 检查站
  • QQ 统一接口( 用于自定义 SM)
  • QQ 4 SM 包含 (Merkle / 简单 / RPC / 二进制树)
  • QQ 233KB 捆绑大小( 用于 Merkle SM)( 63KB gziped)
  • QQ 无 WASM 默认 + 完整浏览器已准备好

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

目录

安装

npm install @ethereumjs/statemanager

概览

这个 StateManager 界面(定义于 @ethereumjs/common披露账户、代码和存储访问 @ethereumjs/evm 财务报告和审定财务报表 @ethereumjs/vm本套装载有四项执行:

执行情况备份商店使用时间
MerkleStateManager@ethereumjs/mpt 尝试生产、证明、国家根源
SimpleStateManager模拟地图轻量级EVM运行,测试
RPCStateManagerJSON-RPC 供应商没有本地状态的分叉/ mainnet 重放
StatefulBinaryTreeStateManager@ethereumjs/binarytreeEIP-7864研究(实验)

所有执行支持嵌入 checkpoint() 编号 : commit() 编号 : revert() 用于可倒行刑.

默克勒州管理器

生产国家经理 @ethereumjs/client 财务报告和审定财务报表 @ethereumjs/vm由账户支持 并储存了Merkle Patricia Tries.

// ./examples/basicUsage.ts

import { MerkleStateManager } from '@ethereumjs/statemanager'
import { Account, Address, hexToBytes } from '@ethereumjs/util'

const main = async () => {
  const stateManager = new MerkleStateManager()
  const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
  const account = new Account(BigInt(0), BigInt(1000))
  await stateManager.checkpoint()
  await stateManager.putAccount(address, account)
  await stateManager.commit()
  await stateManager.flush()

  console.log(
    `Account at address ${address.toString()} has balance ${
      (await stateManager.getAccount(address))?.balance
    }`,
  )
}
void main()

检查站

// ./examples/checkpointRevert.ts

import { MerkleStateManager } from '@ethereumjs/statemanager'
import { Account, Address, hexToBytes } from '@ethereumjs/util'

const main = async () => {
  const sm = new MerkleStateManager()
  const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))

  await sm.checkpoint()
  await sm.putAccount(address, new Account(0n, 1000n))
  await sm.revert()
  console.log(`After revert: ${(await sm.getAccount(address))?.balance ?? 'none'}`)

  await sm.checkpoint()
  await sm.putAccount(address, new Account(0n, 2000n))
  await sm.commit()
  await sm.flush()
  console.log(`After commit: ${(await sm.getAccount(address))?.balance}`)
}

void main()

代码和存储

// ./examples/storageAndCode.ts

import { MerkleStateManager } from '@ethereumjs/statemanager'
import { Account, Address, hexToBytes } from '@ethereumjs/util'

const main = async () => {
  const sm = new MerkleStateManager()
  const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
  const storageKey = hexToBytes(
    '0x0000000000000000000000000000000000000000000000000000000000000001',
  )
  const code = hexToBytes('0x60016001600155')

  await sm.putAccount(address, new Account(0n, 0n))
  await sm.putCode(address, code)
  await sm.putStorage(address, storageKey, hexToBytes('0x01'))

  console.log(`Code length: ${(await sm.getCode(address)).length} bytes`)
  const slot = await sm.getStorage(address, storageKey)
  console.log(`Storage value: ${slot[0]}`)
}

void main()

EIP-1186证明

从 merkle 证书中构建部分状态 getMerkleStateProof(), (中文(简体) ), fromMerkleStateProof(),以及 addMerkleStateProofData()数字 :

// ./examples/fromProofInstantiation.ts

import {
  MerkleStateManager,
  addMerkleStateProofData,
  fromMerkleStateProof,
  getMerkleStateProof,
} from '@ethereumjs/statemanager'
import { Address, hexToBytes } from '@ethereumjs/util'

const main = async () => {
  const stateManager = new MerkleStateManager()
  const contractAddress = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
  const byteCode = hexToBytes('0x67ffffffffffffffff600160006000fb')
  const storageKey1 = hexToBytes(
    '0x0000000000000000000000000000000000000000000000000000000000000001',
  )
  const storageKey2 = hexToBytes(
    '0x0000000000000000000000000000000000000000000000000000000000000002',
  )
  const storageValue1 = hexToBytes('0x01')
  const storageValue2 = hexToBytes('0x02')

  await stateManager.putCode(contractAddress, byteCode)
  await stateManager.putStorage(contractAddress, storageKey1, storageValue1)
  await stateManager.putStorage(contractAddress, storageKey2, storageValue2)

  const proof = await getMerkleStateProof(stateManager, contractAddress)
  const proofWithStorage = await getMerkleStateProof(stateManager, contractAddress, [
    storageKey1,
    storageKey2,
  ])
  const partialStateManager = await fromMerkleStateProof(proof)

  await addMerkleStateProofData(partialStateManager, proofWithStorage)
  console.log(await partialStateManager.getCode(contractAddress))
  console.log(await partialStateManager.getStorage(contractAddress, storageKey1), storageValue1)
  console.log(await partialStateManager.getStorage(contractAddress, storageKey2), storageValue2)

  const accountFromNewSM = await partialStateManager.getAccount(contractAddress)
  const accountFromOldSM = await stateManager.getAccount(contractAddress)
  console.log(accountFromNewSM, accountFromOldSM)
}
void main()

简单状态管理器

最小的记忆中执行——无三相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相 轻量级默认值 @ethereumjs/evm 用途.

// ./examples/simpleStateManager.ts

import { SimpleStateManager } from '@ethereumjs/statemanager'
import { Account, createAddressFromPrivateKey, randomBytes } from '@ethereumjs/util'

const main = async () => {
  const sm = new SimpleStateManager()
  const address = createAddressFromPrivateKey(randomBytes(32))
  const account = new Account(0n, 0xfffffn)
  await sm.putAccount(address, account)
  const read = await sm.getAccount(address)
  console.log(`Account balance: ${read?.balance}`)
}

void main()

RPC 状态管理器

来自JSON-RPC供应商的源头状态. 需要固定区块编号——不 latest编号 :pending。 。 。 .

// ./examples/rpcStateManager.ts

import { RPCStateManager } from '@ethereumjs/statemanager'
import { createAddressFromString } from '@ethereumjs/util'

const main = async () => {
  try {
    const provider = 'https://path.to.my.provider.com'
    const stateManager = new RPCStateManager({ provider, blockTag: 500000n })
    const vitalikDotEth = createAddressFromString('0xd8da6bf26964af9d7eed9e03e53415d37aa96045')
    const account = await stateManager.getAccount(vitalikDotEth)
    console.log('Vitalik has a current ETH balance of ', account?.balance)
  } catch (e) {
    console.log(e.message)
  }
}
void main()

与 RPCBlockChain 页:1 BLOCKHASH opcode 支持 — 见 evm.ts。 。 。 .

二进制树州经理(实验)

StatefulBinaryTreeStateManager 用途 @ethereumjs/ 二元树 (EIP-7864): (中文(简体) )

// ./examples/binaryTreeStateManager.ts

import { Common, Mainnet } from '@ethereumjs/common'
import { StatefulBinaryTreeStateManager } from '@ethereumjs/statemanager'
import { Account, createAddressFromString } from '@ethereumjs/util'

const main = async () => {
  const common = new Common({ chain: Mainnet, eips: [7864] })
  const sm = new StatefulBinaryTreeStateManager({ common })
  const address = createAddressFromString('0x9e5ef720fa2cdfa5291eb7e711cfd2e62196f4b3')

  await sm.putAccount(address, new Account(1n, 1000n))
  const account = await sm.getAccount(address)
  console.log(`Binary tree SM balance: ${account?.balance}`)
}

void main()

还没准备好.

建筑

所有执行都满意 StateManagerInterface 从 @ethereumjs/common自定义执行可以直接执行该接口.

可选 consumeBAL(bal, expectedStateRoot?) 应用 a EIP-7928 (中文(简体) ) block-level access list to state 不使用 EVM 执行(最后的后余额 / notce / 代码 / 存储; EIP-161 空账户删除). 本一揽子方案中的每一个执行都向共同的 consumeBAL() 帮手.

浏览器

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

API 密码

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

发展

见 D. 发 展。 。 。 .

爱丽丝

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

许可证

MPL - 2.0 导弹

版本与发布记录

@ethereumjs/statemanager 共发布 28 个版本,首次发布于 2022/06/30;过去 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 → 1.0.0-beta.3,alpha → 3.0.0-alpha.1,rc → 10.0.0-rc.1,nightly → 10.1.1-nightly.2。

关于 @ethereumjs/statemanager 的常见问题

@ethereumjs/statemanager 是什么?

执行Etereum州长

如何安装 @ethereumjs/statemanager?

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

@ethereumjs/statemanager 支持 TypeScript 吗?

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

@ethereumjs/statemanager 支持 ESM 还是 CommonJS?

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

@ethereumjs/statemanager 可以商用吗?

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

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

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

@ethereumjs/statemanager 还在维护吗?

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