@ethereumjs/ 二元树 v10 (警告)
| 实施二进制树 EIP-7864 (英语) |
|---|
二进制树(英語:Binary Trees)是一款为埃特鲁姆州存储提出的新式密码数据结构,其证明尺寸比默克勒·帕特里夏·特里斯要小. 密钥映射到 a 树干 (头31字节)和 指数 (最后字节)在干节点内.
这个图书馆是实验性的 ——API不稳定,不得用于生产.
可运行的示例 examples/。 。 。 .
目录
安装
npm install @ethereumjs/binarytree
开始
使用 createBinaryTree() {\fn黑体\fs22\bord1\shad0\3aHBE\4aH00\fscx67\fscy66\2cHFFFFFF\3cH808080}把一棵树当瞬间 默认为内存 MapDB 已使用; 通过 db 坚持不懈.
// ./examples/basicUsage.ts
import { createBinaryTree } from '@ethereumjs/binarytree'
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
const main = async () => {
const tree = await createBinaryTree()
const key = hexToBytes(`0x${'00'.repeat(32)}`)
const value = hexToBytes(`0x${'01'.repeat(32)}`)
const stem = key.slice(0, 31)
const index = key[31]
await tree.put(stem, [index], [value])
const [retrieved] = await tree.get(stem, [index])
console.log(`Root: ${bytesToHex(tree.root())}`)
console.log(`Value match: ${bytesToHex(retrieved!)}`)
}
void main()
put(stem, indices, values) 接受一个干线内并行的槽指数和32字节值. get(stem, indices) 返回相同顺序的值.
选项( E)BinaryTreeOpts: db, (中文(简体) ), useRootPersistence, (中文(简体) ), cacheSize, (中文(简体) ), hashFunction (默认为 BLAKE3 ).
证据
创建包含证明 tree.createBinaryProof(key),以 verifyBinaryProof(),并用 binaryTreeFromProof()数字 :
// ./examples/binaryProof.ts
import { binaryTreeFromProof, createBinaryTree, verifyBinaryProof } from '@ethereumjs/binarytree'
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
import { blake3 } from '@noble/hashes/blake3.js'
const main = async () => {
const tree = await createBinaryTree()
const key = hexToBytes(`0x${'00'.repeat(31)}01`)
const hashedKey = blake3(key)
const value = hexToBytes(`0x${'02'.repeat(32)}`)
const stem = hashedKey.slice(0, 31)
const index = hashedKey[31]
await tree.put(stem, [index], [value])
const proof = await tree.createBinaryProof(hashedKey)
const verified = await verifyBinaryProof(tree.root(), hashedKey, proof)
const sparse = await binaryTreeFromProof(proof)
console.log(`Proof length: ${proof.length} nodes`)
console.log(`Verified value: ${bytesToHex(verified!)}`)
console.log(`Sparse tree root match: ${bytesToHex(sparse.root()) === bytesToHex(tree.root())}`)
}
void main()
典型的证明键 blake3(address || treeIndex || subIndex) 用于Verkle/binary-tree状态层。 不存在返回的有效证明 null 编号 : undefined 从 verifyBinaryProof()。 。 。 .
浏览器
提供了混合无害环境管理/CJS大楼。 浏览器设置见 ./例/浏览器.html (建造包,然后 npx vite 从软件包根开始).
API 密码
生成类型文件 文档。 。 。 .
主要出口: BinaryTree, (中文(简体) ), createBinaryTree, (中文(简体) ), binaryTreeFromProof, (中文(简体) ), verifyBinaryProof, (中文(简体) ), CheckpointDB,节点类型 ./node。 。 。 .
用于 StatefulBinaryTreeStateManager 内 @ethereumjs/statemanager eIP-7864启动时 Common。 。 。 .
爱丽丝
这个 EthereumJS GitHub组织及其寄存器由原Ethereum基金会JavaScript团队和更广泛的Ethereum社区的成员管理. 如果您想参加工作或改进图书馆,请查看 开发者文件 * 概述现行标准和工具,并审查 行为守则。 。 。 .