@ethereumjs/ethash (英语) v10
| 埃萨什 在类型脚本中执行. |
|---|
目录
安装
要获得最新版本,只需使用 npm数字 :
npm install @ethereumjs/ethash
使用量
PoW 验证
// ./examples/powBlock.ts
import { createBlockFromRLP } from '@ethereumjs/block'
import { Ethash } from '@ethereumjs/ethash'
import { MapDB, hexToBytes } from '@ethereumjs/util'
import type { DBObject } from '@ethereumjs/util'
const cacheDB = new MapDB<number, DBObject>()
const ethash = new Ethash(cacheDB)
const validblockRlp =
'0xf90667f905fba0a8d5b7a4793baaede98b5236954f634a0051842df6a252f6a80492fd888678bda01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0f93c8db1e931daa2e22e39b5d2da6fb4074e3d544094857608536155e3521bc1a0bb7495628f9160ddbcf6354380ee32c300d594e833caec3a428041a66e7bade1a0c7778a7376099ee2e5c455791c1885b5c361b95713fddcbe32d97fd01334d296b90100000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000400000000000000000000000000000000000000000000000000000008302000001832fefd882560b84559c17b9b9040001020304050607080910111213141516171819202122232410000000000000000000200000000000000000003000000000000000000040000000000000000000500000000000000000006000000000000000000070000000000000000000800000000000000000009000000000000000000010000000000000000000100000000000000000002000000000000000000030000000000000000000400000000000000000005000000000000000000060000000000000000000700000000000000000008000000000000000000090000000000000000000100000000000000000001000000000000000000020000000000000000000300000000000000000004000000000000000000050000000000000000000600000000000000000007000000000000000000080000000000000000000900000000000000000001000000000000000000010000000000000000000200000000000000000003000000000000000000040000000000000000000500000000000000000006000000000000000000070000000000000000000800000000000000000009000000000000000000010000000000000000000100000000000000000002000000000000000000030000000000000000000400000000000000000005000000000000000000060000000000000000000700000000000000000008000000000000000000090000000000000000000100000000000000000001000000000000000000020000000000000000000300000000000000000004000000000000000000050000000000000000000600000000000000000007000000000000000000080000000000000000000900000000000000000001000000000000000000010000000000000000000200000000000000000003000000000000000000040000000000000000000500000000000000000006000000000000000000070000000000000000000800000000000000000009000000000000000000010000000000000000000100000000000000000002000000000000000000030000000000000000000400000000000000000005000000000000000000060000000000000000000700000000000000000008000000000000000000090000000000000000000100000000000000000001000000000000000000020000000000000000000300000000000000000004000000000000000000050000000000000000000600000000000000000007000000000000000000080000000000000000000900000000000000000001000000000000000000010000000000000000000200000000000000000003000000000000000000040000000000000000000500000000000000000006000000000000000000070000000000000000000800000000000000000009000000000000000000010000000000000000000a09c7b47112a3afb385c12924bf6280d273c106eea7caeaf5131d8776f61056c148876ae05d46b58d1fff866f864800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d8785012a05f200801ba01d2c92cfaeb04e53acdff2b5d42005ff6aacdb0105e64eb8c30c273f445d2782a01e7d50ffce57840360c57d94977b8cdebde614da23e8d1e77dc07928763cfe21c0'
const validBlock = createBlockFromRLP(hexToBytes(validblockRlp), {
setHardfork: true,
skipConsensusFormatValidation: true,
})
const result = await ethash.verifyPOW(validBlock)
console.log(result) // => true
PoW Ethash CPU 矿工
里面有一个简单的CPU矿工 Ethash 用于测试目的的软件包.
参见下例如何使用新的 Miner 类 :
// ./examples/miner.ts
import { createBlock } from '@ethereumjs/block'
import { Ethash } from '@ethereumjs/ethash'
import { MapDB, bytesToHex } from '@ethereumjs/util'
import type { DBObject } from '@ethereumjs/util'
const block = createBlock(
{
header: {
difficulty: BigInt(100),
number: BigInt(1),
},
},
{ setHardfork: true, skipConsensusFormatValidation: true },
)
const cacheDB = new MapDB<number, DBObject>()
const e = new Ethash(cacheDB)
const miner = e.getMiner(block.header)
const solution = await miner.iterate(-1) // iterate until solution is found
console.log(bytesToHex(solution!.mixHash)) // 0x892177e7bbb1f31ade0610707c96c6bf86e1415b26073d17b2da2dbd2daefd1e
API 密码
文档
生成类型Doc API 文档
混合 CJS/ESM 构建
随着2023年夏季发行的中断,我们开始将我们的图书馆与两个共同JS(共同JS)一起运送cjs 和建立无害环境管理esm 文件夹,参见 package.json 为详细设置.
如果你使用ES6型的话 import 将在您来自无害环境管理架构的代码文件中使用:
import { EthereumJSClass } from '@ethereumjs/[PACKAGE_NAME]'
如果您使用 Node.js 特定 require,CJS建筑将使用:
const { EthereumJSClass } = require('@ethereumjs/[PACKAGE_NAME]')
使用无害环境管理将比CJS给您更多的优势,超出浏览器的使用范围,如静态代码分析/树摇,CJS无法提供.
爱丽丝
看我们的组织 文档 介绍 EthereumJS 以及关于现行标准和最佳做法的资料。 如果你想参加工作或改进图书馆,请审查我们 捐款准则 先说.