@ethereumjs/政治家 v10
| 提供高水平进入埃塞乌姆州的图书馆 |
|---|
- 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运行,测试 |
RPCStateManager | JSON-RPC 供应商 | 没有本地状态的分叉/ mainnet 重放 |
StatefulBinaryTreeStateManager | @ethereumjs/binarytree | EIP-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社区的成员管理. 如果您想参加工作或改进图书馆,请查看 开发者文件 * 概述现行标准和工具,并审查 行为守则。 。 。 .