@ethereumjs/ethash

中文文档
10.0.0发布于 2025/04/29MPL-2.0TypeScriptESM + CJS近 30 天下载 13.8万

An implementation of the Ethash consensus algorithm in JavaScript

原始使用文档
文档目录10 节
  1. Table of Contents
  2. Installation
  3. Usage
  4. PoW Validation
  5. PoW Ethash CPU Miner
  6. API
  7. Docs
  8. Hybrid CJS/ESM Builds
  9. EthereumJS
  10. License

@ethereumjs/ethash v10

NPM Package GitHub Issues Actions Status Code Coverage Discord

Ethash implementation in TypeScript.

Table of Contents

Installation

To obtain the latest version, simply require the project using npm:

npm install @ethereumjs/ethash

Usage

PoW Validation

// ./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 Miner

There is a simple CPU miner included within Ethash package which can be used for testing purposes.

See the following example on how to use the new Miner class:

// ./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

Docs

Generated TypeDoc API Documentation

Hybrid CJS/ESM Builds

With the breaking releases from Summer 2023 we have started to ship our libraries with both CommonJS (cjs folder) and ESM builds (esm folder), see package.json for the detailed setup.

If you use an ES6-style import in your code files from the ESM build will be used:

import { EthereumJSClass } from '@ethereumjs/[PACKAGE_NAME]'

If you use Node.js specific require, the CJS build will be used:

const { EthereumJSClass } = require('@ethereumjs/[PACKAGE_NAME]')

Using ESM will give you additional advantages over CJS beyond browser usage like static code analysis / Tree Shaking which CJS can not provide.

EthereumJS

See our organizational documentation for an introduction to EthereumJS as well as information on current standards and best practices. If you want to join for work or carry out improvements on the libraries, please review our contribution guidelines first.

License

MPL-2.0

版本与发布记录

@ethereumjs/ethash 共发布 24 个版本,首次发布于 2020/10/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
10.0.0latest2025/04/291.5 年前
10.0.0-rc.1rc2025/03/251.6 年前
4.0.0-alpha.1alpha2024/10/172 年前
3.0.42024/08/152 年前
3.0.32024/03/182 年前
3.0.22024/02/092 年前

其他发布通道:beta → 2.0.0-beta.3,alpha → 4.0.0-alpha.1,rc → 10.0.0-rc.1。

关于 @ethereumjs/ethash 的常见问题

@ethereumjs/ethash 是什么?

An implementation of the Ethash consensus algorithm in JavaScript

如何安装 @ethereumjs/ethash?

在项目目录运行 npm install @ethereumjs/ethash,或使用 pnpm add / yarn add / bun add。

@ethereumjs/ethash 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ethereumjs/ethash 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ethereumjs/ethash 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ethereumjs/ethash 可以商用吗?

@ethereumjs/ethash 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@ethereumjs/ethash 的下载量有多少?

根据 npm 官方下载统计,@ethereumjs/ethash 在 2026/10/14 – 2026/11/12 这 30 天内被下载 137,873 次,日均约 4,596 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ethereumjs/ethash 还在维护吗?

最新版本 10.0.0 发布于 2025/04/29(1.5 年前);至今共发布 24 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/10/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。