@ethereumjs/mpt (英语) v10
| 执行《公约》 修改后的梅克勒·帕特里夏·特里 一、导 言 黄纸 |
|---|
- 高度可扩展
- *
- 控制下的依赖(3个外部+)
@Noble密码) - * 检查站功能
- 软存储后端
- ⚖️
EIP-1186证据 - QQ 176KB 捆绑大小( 48KB gziped)
- QQ 无 WASM 默认 + 完整浏览器已准备好
可运行的示例 examples/。额外的走过: examples/README.md。 。 。 .
目录
安装
npm install @ethereumjs/mpt
开始
使用 createMPT() (建议)或 new MerklePatriciaTrie() 直接说 与 useKeyHashing: true (Ethreeum Production 默认),键被先用keccak256来散开后再找取.
// ./examples/basicUsage.ts
import { createMPT } from '@ethereumjs/mpt'
import { MapDB, bytesToUtf8, utf8ToBytes } from '@ethereumjs/util'
async function test() {
const trie = await createMPT({ db: new MapDB() })
await trie.put(utf8ToBytes('test'), utf8ToBytes('one'))
const value = await trie.get(utf8ToBytes('test'))
console.log(value ? bytesToUtf8(value) : 'not found') // 'one'
}
void test()
主构造器 : createMPT(), (中文(简体) ), createMPTFromProof(), (中文(简体) ), new MerklePatriciaTrie(opts)。 。 。 .
酌情在返回的节点上默认冻结财产。 通过 a Common 与 customCrypto wASM的散列后端——见 @ethereumjs/常见。 。 。 .
检查站
嵌入 checkpoint() 编号 : commit() 编号 : revert() 用于投机性写作的堆栈 @ethereumjs/statemanager 财务报告和审定财务报表 @ethereumjs/vm:
// ./examples/checkpoint.ts
import { createMPT } from '@ethereumjs/mpt'
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
const main = async () => {
const trie = await createMPT()
const key = hexToBytes('0x11')
await trie.put(key, hexToBytes('0xaa'))
trie.checkpoint()
await trie.put(key, hexToBytes('0xbb'))
console.log(`In checkpoint: ${bytesToHex((await trie.get(key))!)}`)
await trie.revert()
console.log(`After revert: ${bytesToHex((await trie.get(key))!)}`)
trie.checkpoint()
await trie.put(key, hexToBytes('0xcc'))
await trie.commit()
console.log(`After commit: ${bytesToHex((await trie.get(key))!)}`)
}
void main()
走着走着
walkTrieIterable() 产量 { node, currentKey } 用于 for await 循环。 使用 walkTrie() 与 WalkController 当您需要并行的 DB 读取或每个节点的转录控制时(见 API docs).
// ./examples/trieWalking.ts
import { createMPT } from '@ethereumjs/mpt'
import { utf8ToBytes } from '@ethereumjs/util'
async function main() {
const trie = await createMPT()
await trie.put(utf8ToBytes('key'), utf8ToBytes('val'))
const walk = trie.walkTrieIterable(trie.root())
for await (const { node, currentKey } of walk) {
console.log({ node, currentKey })
}
}
void main()
默克勒证明
EIP-1186 (英语) 建立和核实证据:
// ./examples/proofs.ts
import { MerklePatriciaTrie, createMerkleProof, verifyMPTWithMerkleProof } from '@ethereumjs/mpt'
import { bytesToUtf8, utf8ToBytes } from '@ethereumjs/util'
const trie = new MerklePatriciaTrie()
async function main() {
const k1 = utf8ToBytes('key1')
const k2 = utf8ToBytes('key2')
const v1 = utf8ToBytes('one')
const v2 = utf8ToBytes('two')
// proof-of-inclusion
await trie.put(k1, v1)
let proof = await createMerkleProof(trie, k1)
let value = await verifyMPTWithMerkleProof(trie, trie.root(), k1, proof)
console.log(value ? bytesToUtf8(value) : 'not found') // 'one'
// proof-of-exclusion
await trie.put(k1, v1)
await trie.put(k2, v2)
proof = await createMerkleProof(trie, utf8ToBytes('key3'))
value = await verifyMPTWithMerkleProof(trie, trie.root(), utf8ToBytes('key3'), proof)
console.log(value ? bytesToUtf8(value) : 'null') // null
// invalid proof
await trie.put(k1, v1)
await trie.put(k2, v2)
proof = await createMerkleProof(trie, k2)
proof[0].reverse()
try {
const _value = await verifyMPTWithMerkleProof(trie, trie.root(), k2, proof)
} catch (err) {
console.log(`Invalid proof rejected: ${(err as Error).message.split('\n')[0]}`)
}
}
void main()
范围证明 : verifyMPTRangeProof() 用于snap-sync风格的叶片范围.
证明书上的崔
从现有证据中构建一个部分的三进制——对轻客户端和 @ethereumjs/statemanager数字 :
// ./examples/createFromProof.ts
import {
MerklePatriciaTrie,
createMPTFromProof,
createMerkleProof,
updateMPTFromMerkleProof,
} from '@ethereumjs/mpt'
import { bytesToUtf8, utf8ToBytes } from '@ethereumjs/util'
async function main() {
const k1 = utf8ToBytes('keyOne')
const k2 = utf8ToBytes('keyTwo')
const someOtherTrie = new MerklePatriciaTrie({ useKeyHashing: true })
await someOtherTrie.put(k1, utf8ToBytes('valueOne'))
await someOtherTrie.put(k2, utf8ToBytes('valueTwo'))
const proof = await createMerkleProof(someOtherTrie, k1)
const trie = await createMPTFromProof(proof, { useKeyHashing: true })
const otherProof = await createMerkleProof(someOtherTrie, k2)
await updateMPTFromMerkleProof(trie, otherProof)
const value = await trie.get(k1)
console.log(bytesToUtf8(value!)) // valueOne
const otherValue = await trie.get(k2)
console.log(bytesToUtf8(otherValue!)) // valueTwo
}
void main()
数值地图
倾出叶键/值对 getValueMap()数字 :
// ./examples/getValueMap.ts
import { createMPT } from '@ethereumjs/mpt'
import { bigIntToBytes, hexToBytes } from '@ethereumjs/util'
const main = async () => {
const trie = await createMPT({})
const entries: [Uint8Array, string][] = [
[bigIntToBytes(1n), '0x' + '0a'.repeat(32)],
[bigIntToBytes(2n), '0x' + '0b'.repeat(32)],
[bigIntToBytes(3n), '0x' + '0c'.repeat(32)],
]
for (const entry of entries) {
await trie.put(entry[0], hexToBytes(entry[1]))
}
const dump = await trie.getValueMap()
console.log(`All leaf values: ${Object.keys(dump.values).length} entries`)
const selectiveDump = await trie.getValueMap(1n, 2)
console.log(`Selective dump: ${Object.keys(selectiveDump.values).length} entries`)
}
void main()
存储后端
传出任何 @ethereumjs/util DB 执行 opts.db默认为记忆 MapDB。 。 。 .
| 示例 | 后端 |
|---|---|
examples/level.js | LevelDB( 由 @ethereumjs/client(中文(简体) ) |
examples/lmdb.js | 语言数据库 |
recipes/level.ts | TypeScript LevelDB 包装食谱 |
// ./examples/customLevelDB.ts (excerpt)
// const trie = new MerklePatriciaTrie({ db: new LevelDB(new Level('MY_TRIE_DB_LOCATION')) })
执行 DB 接口 用于自定义后端.
根持久与奔跑
坚持三重击根 :
// ./examples/rootPersistence.ts
import { createMPT } from '@ethereumjs/mpt'
import { bytesToHex } from '@ethereumjs/util'
async function main() {
const trie = await createMPT({
useRootPersistence: true,
})
console.log(bytesToHex(trie.root())) // 0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421
}
void main()
启用 useNodePruning: true 以删除 DB 中已替换的节点( 保存磁盘空间; 只有在不需要历史三重状态时才安全).
浏览器
提供了混合无害环境管理/CJS大楼。 见 ./例/浏览器.html。 。 。 .
API 密码
生成类型文件 文档。 。 。 .
基准制定
npm run benchmarks # random PUT + checkpointing benchmarks
npm run profiling # flamegraph via 0x
调试
使用 调试 软件包。 启用方式 DEBUG=ethjs,mpt (见先前文件中的完整日志表或运行) DEBUG=ethjs,mpt:* npm test) (中文(简体) ).
参考资料
爱丽丝
这个 EthereumJS GitHub组织及其寄存器由原Ethereum基金会JavaScript团队和更广泛的Ethereum社区的成员管理. 如果您想参加工作或改进图书馆,请查看 开发者文件 * 概述现行标准和工具,并审查 行为守则。 。 。 .