@ethereumjs/ 块链 v10
| 一个存储和与块交互的模块. |
|---|
存储顺序链 @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( 预现) | EthashConsensus | Ethash 难度规则 |
| PoA(历史测试网) | CliqueConsensus | Goerli 式签名者投票 |
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社区的成员管理. 如果您想参加工作或改进图书馆,请查看 开发者文件 * 概述现行标准和工具,并审查 行为守则。 。 。 .