{"name":"@ethereumjs/blockchain","version":"10.1.0","keywords":["ethereum","blockchain"],"author":{"name":"mjbecze","email":"mjbecze@gmail.com"},"license":"MPL-2.0","_id":"@ethereumjs/blockchain@10.1.0","maintainers":[{"name":"axic","email":"alex@rtfs.hu"},{"name":"holgerd77","email":"Holger.Drewes@gmail.com"},{"name":"acolytec3","email":"metroman374@gmail.com"},{"name":"g11tech","email":"gajinder@g11.in"}],"homepage":"https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/blockchain#readme","bugs":{"url":"https://github.com/ethereumjs/ethereumjs-monorepo/issues?q=is%3Aissue+label%3A%22package%3A+blockchain%22"},"dist":{"shasum":"72af2dccc65102a2033f697a206ae5e75419a212","tarball":"http://registry-npm.ywwl.com/@ethereumjs/blockchain/download/@ethereumjs/blockchain-10.1.0.tgz","fileCount":132,"integrity":"sha512-KDp1C7XYAXLIl/HQ459K7PIsLtGT58BEprEMrAseAsagVmgVv7+cBacrEppwV7Fx33+JYm8tQ3vACt82bHbTWQ==","signatures":[{"sig":"MEUCIQCCjbF9ihYffzAOqQjjhLJLb1cds8iUezURQFiDh0/1zwIgcIsoWwr5eDwkCS0Lf3GQuMAia5si7kXqOCRngHkcjOM=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":694079,"size":132163,"noattachment":false,"key":"/@ethereumjs/blockchain/-/@ethereumjs/blockchain-10.1.0.tgz"},"main":"dist/cjs/index.js","type":"module","types":"./dist/cjs/index.d.ts","module":"dist/esm/index.js","engines":{"node":">=18"},"exports":{".":{"import":{"default":"./dist/esm/index.js","typescript":"./src/index.ts"},"require":"./dist/cjs/index.js"}},"gitHead":"7d5c1304a74002d3ad646cd2f7685b3f320e9a74","scripts":{"sc":"npm run spellcheck","tsc":"../../config/cli/ts-compile.sh","lint":"npm run biome && eslint --config ./eslint.config.mjs .","test":"npm run test:node && npm run test:browser","biome":"npx @biomejs/biome check","build":"../../config/cli/ts-build.sh","clean":"../../config/cli/clean-package.sh","coverage":"DEBUG=ethjs npx vitest run -c ../../config/vitest.config.coverage.mts","examples":"tsx ../../scripts/examples-runner.ts -- blockchain","lint:fix":"npm run biome:fix && eslint --fix --config ./eslint.config.mjs .","biome:fix":"npx @biomejs/biome check --write","test:node":"npx vitest run -c ../../config/vitest.config.mts","docs:build":"typedoc --options typedoc.mjs","spellcheck":"npm run spellcheck:ts && npm run spellcheck:md","test:browser":"npx vitest run --config=./vitest.config.browser.mts","spellcheck:md":"npx cspell --gitignore -c ../../config/cspell-md.json \"**.md\" --cache --show-suggestions --show-context","spellcheck:ts":"npx cspell --gitignore -c ../../config/cspell-ts.json \"./**/*.ts\" --cache --show-suggestions --show-context","examples:build":"npx embedme README.md","prepublishOnly":"../../config/cli/prepublish.sh","coverage:istanbul":"DEBUG=ethjs npx vitest run -c ../../config/vitest.config.coverage.istanbul.mts"},"_npmUser":{"name":"holgerd77","email":"Holger.Drewes@gmail.com"},"repository":{"url":"git+https://github.com/ethereumjs/ethereumjs-monorepo.git","type":"git"},"_npmVersion":"11.6.2","description":"A module to store and interact with blocks","directories":{},"sideEffects":false,"_nodeVersion":"23.10.0","dependencies":{"debug":"^4.4.0","lru-cache":"11.0.2","eventemitter3":"^5.0.1","@ethereumjs/mpt":"^10.1.0","@ethereumjs/rlp":"^10.1.0","@ethereumjs/util":"^10.1.0","@ethereumjs/block":"^10.1.0","@ethereumjs/common":"^10.1.0"},"_hasShrinkwrap":false,"devDependencies":{"@types/debug":"^4.1.12","@ethereumjs/ethash":"^10.0.0"},"_npmOperationalInternal":{"tmp":"tmp/blockchain_10.1.0_1762449854428_0.7929462726446743","host":"s3://npm-registry-packages-npm-production"},"readme":"# @ethereumjs/blockchain `v10`\n\n[![NPM Package][blockchain-npm-badge]][blockchain-npm-link]\n[![GitHub Issues][blockchain-issues-badge]][blockchain-issues-link]\n[![Actions Status][blockchain-actions-badge]][blockchain-actions-link]\n[![Code Coverage][blockchain-coverage-badge]][blockchain-coverage-link]\n[![Discord][discord-badge]][discord-link]\n\n| A module to store and interact with blocks. |\n| ------------------------------------------- |\n\nStores a sequential chain of [@ethereumjs/block](../block) blocks, tracks the canonical head, and supports reorgs via `putBlock()`. Used by `@ethereumjs/client` and `@ethereumjs/vm`.\n\nRunnable examples live in [`examples/`](./examples/).\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Getting Started](#getting-started)\n- [Block Lookup](#block-lookup)\n- [Chain Iteration](#chain-iteration)\n- [Hardfork by Head](#hardfork-by-head)\n- [Custom Genesis](#custom-genesis)\n- [Consensus Types](#consensus-types)\n- [Supported Block Features](#supported-block-features)\n- [Events and Debugging](#events-and-debugging)\n- [Browser](#browser)\n- [API](#api)\n- [EthereumJS](#ethereumjs)\n- [License](#license)\n\n## Installation\n\n```shell\nnpm install @ethereumjs/blockchain\n```\n\n## Getting Started\n\nUse `createBlockchain()` — it awaits async initialization (genesis setup, consensus wiring):\n\n```ts\nimport { createBlockchain } from '@ethereumjs/blockchain'\nimport { Common, Mainnet } from '@ethereumjs/common'\n\nconst common = new Common({ chain: Mainnet })\nconst blockchain = await createBlockchain({ common })\nconsole.log(`Genesis hash: ${blockchain.genesisBlock.hash()}`)\n```\n\nMain constructors: `createBlockchain()`, `createBlockchainFromBlocksData()`.\n\nPass `validateBlocks: false` / `validateConsensus: false` in examples and tests when blocks are intentionally incomplete.\n\n## Block Lookup\n\nRetrieve blocks by number or hash after adding them with `putBlock()`:\n\n```ts\n// ./examples/getBlock.ts\n\nimport { createBlock } from '@ethereumjs/block'\nimport { createBlockchain } from '@ethereumjs/blockchain'\nimport { Common, Hardfork, Mainnet } from '@ethereumjs/common'\nimport { bytesToHex } from '@ethereumjs/util'\n\nconst main = async () => {\n  const common = new Common({ chain: Mainnet, hardfork: Hardfork.Prague })\n  const blockchain = await createBlockchain({\n    validateBlocks: false,\n    validateConsensus: false,\n    common,\n  })\n\n  const block1 = createBlock(\n    {\n      header: {\n        number: 1n,\n        parentHash: blockchain.genesisBlock.hash(),\n        difficulty: blockchain.genesisBlock.header.difficulty + 1n,\n      },\n    },\n    { common, setHardfork: true },\n  )\n  const block2 = createBlock(\n    {\n      header: {\n        number: 2n,\n        parentHash: block1.header.hash(),\n        difficulty: block1.header.difficulty + 1n,\n      },\n    },\n    { common, setHardfork: true },\n  )\n  await blockchain.putBlock(block1)\n  await blockchain.putBlock(block2)\n\n  const byNumber = await blockchain.getBlock(2n)\n  const byHash = await blockchain.getBlock(block2.hash())\n\n  console.log(`Block ${byNumber.header.number} hash: ${bytesToHex(byNumber.hash())}`)\n  console.log(`Lookup by hash matches: ${bytesToHex(byHash.hash()) === bytesToHex(block2.hash())}`)\n}\n\nvoid main()\n```\n\nAlso: `getBlocks()`, `getIteratorHead()`, `getLatestHeader()`.\n\n## Chain Iteration\n\nWalk the canonical chain with `iterator()`:\n\n```ts\n// ./examples/iterateChain.ts\n\nimport { createBlock } from '@ethereumjs/block'\nimport { createBlockchain } from '@ethereumjs/blockchain'\nimport { Common, Hardfork, Mainnet } from '@ethereumjs/common'\nimport { bytesToHex } from '@ethereumjs/util'\n\nconst main = async () => {\n  const common = new Common({ chain: Mainnet, hardfork: Hardfork.Prague })\n  const blockchain = await createBlockchain({\n    validateBlocks: false,\n    validateConsensus: false,\n    common,\n  })\n\n  const block = createBlock(\n    {\n      header: {\n        number: 1n,\n        parentHash: blockchain.genesisBlock.hash(),\n        difficulty: blockchain.genesisBlock.header.difficulty + 1n,\n      },\n    },\n    { common, setHardfork: true },\n  )\n  const block2 = createBlock(\n    {\n      header: {\n        number: 2n,\n        parentHash: block.header.hash(),\n        difficulty: block.header.difficulty + 1n,\n      },\n    },\n    { common, setHardfork: true },\n  )\n  await blockchain.putBlock(block)\n  await blockchain.putBlock(block2)\n\n  await blockchain.iterator('i', (block) => {\n    const blockNumber = block.header.number.toString()\n    const blockHash = bytesToHex(block.hash())\n    console.log(`Block ${blockNumber}: ${blockHash}`)\n  })\n}\nvoid main()\n```\n\n## Hardfork by Head\n\nPin `Common` hardfork to the current head block number on init:\n\n```ts\n// ./examples/hardforkByHead.ts\n\nimport { createBlockchain } from '@ethereumjs/blockchain'\nimport { Common, Mainnet } from '@ethereumjs/common'\n\nconst main = async () => {\n  const common = new Common({ chain: Mainnet })\n  await createBlockchain({ common, hardforkByHeadBlockNumber: true })\n  console.log(`Hardfork at genesis head: ${common.hardfork()}`)\n}\n\nvoid main()\n```\n\n## Custom Genesis\n\nBuild a chain from a Geth genesis JSON file:\n\n```ts\n// ./examples/gethGenesis.ts\n\nimport { createBlockchain } from '@ethereumjs/blockchain'\nimport { createCommonFromGethGenesis, parseGethGenesisState } from '@ethereumjs/common'\nimport { postMergeGethGenesis } from '@ethereumjs/testdata'\nimport { bytesToHex } from '@ethereumjs/util'\n\nconst main = async () => {\n  const common = createCommonFromGethGenesis(postMergeGethGenesis, { chain: 'customChain' })\n  const genesisState = parseGethGenesisState(postMergeGethGenesis)\n  const blockchain = await createBlockchain({\n    genesisState,\n    common,\n  })\n  const genesisBlockHash = blockchain.genesisBlock.hash()\n  common.setForkHashes(genesisBlockHash)\n  console.log(\n    `Genesis hash from geth genesis parameters - ${bytesToHex(blockchain.genesisBlock.hash())}`,\n  )\n}\n\nvoid main()\n```\n\nBuilt-in network genesis state lives in [@ethereumjs/genesis](../genesis). Access the genesis block via `blockchain.genesisBlock`.\n\n## Consensus Types\n\n| Algorithm | Class | Notes |\n| --- | --- | --- |\n| PoS (default post-merge) | `CasperConsensus` | Difficulty `0`, beacon client does validation |\n| PoW (pre-merge) | `EthashConsensus` | Ethash difficulty rules |\n| PoA (historical testnets) | `CliqueConsensus` | Goerli-style signer voting |\n\nClique example:\n\n```ts\n// ./examples/clique.ts\n\nimport { CliqueConsensus, createBlockchain } from '@ethereumjs/blockchain'\nimport { Common, ConsensusAlgorithm, Hardfork } from '@ethereumjs/common'\nimport { goerliChainConfig } from '@ethereumjs/testdata'\n\nimport type { ConsensusDict } from '@ethereumjs/blockchain'\n\nconst main = async () => {\n  const common = new Common({ chain: goerliChainConfig, hardfork: Hardfork.London })\n\n  const consensusDict: ConsensusDict = {}\n  consensusDict[ConsensusAlgorithm.Clique] = new CliqueConsensus()\n  const blockchain = await createBlockchain({\n    consensusDict,\n    common,\n  })\n  console.log(`Created blockchain with ${blockchain.consensus!.algorithm} consensus algorithm`)\n}\n\nvoid main()\n```\n\nCustom consensus: implement the `Consensus` interface and pass via `consensusDict` or `consensus` option. See [customConsensus.spec.ts](./test/customConsensus.spec.ts).\n\n### Storage\n\nDefault DB is in-memory `MapDB`. For persistence, pass a `DB`-conforming backend (see [@ethereumjs/client level wrapper](https://github.com/ethereumjs/ethereumjs-monorepo/blob/master/packages/client/src/execution/level.ts)).\n\n## Supported Block Features\n\n- **EIP-1559** — base fee blocks (default post-London)\n- **EIP-4844** — blob txs (requires KZG on `Common`, see [@ethereumjs/tx](../tx#kzg-setup))\n- **EIP-7685** — CL requests in block headers\n- **EIP-4895** — withdrawals\n\n## Events and Debugging\n\n`blockchain.events` emits `deletedCanonicalBlocks` on reorgs.\n\nDebug loggers: `blockchain:#`, `blockchain:clique`, `blockchain:ethash`. Enable with `DEBUG=ethjs,blockchain:clique`.\n\n## Browser\n\nHybrid ESM/CJS builds are provided. See [./examples/browser.html](./examples/browser.html).\n\n## API\n\nGenerated TypeDoc [documentation](./docs/README.md).\n\n## EthereumJS\n\nThe `EthereumJS` GitHub organization and its repositories are managed by members of the former Ethereum Foundation JavaScript team and the broader Ethereum community. If you want to join for work or carry out improvements on the libraries see the [developer docs](../../DEVELOPER.md) for an overview of current standards and tools and review our [code of conduct](../../CODE_OF_CONDUCT.md).\n\n## License\n\n[MPL-2.0](<https://tldrlegal.com/license/mozilla-public-license-2.0-(mpl-2)>)\n\n[discord-badge]: https://img.shields.io/static/v1?logo=discord&label=discord&message=Join&color=blue\n[discord-link]: https://discord.gg/TNwARpR\n[blockchain-npm-badge]: https://img.shields.io/npm/v/@ethereumjs/blockchain.svg\n[blockchain-npm-link]: https://www.npmjs.com/package/@ethereumjs/blockchain\n[blockchain-issues-badge]: https://img.shields.io/github/issues/ethereumjs/ethereumjs-monorepo/package:%20blockchain?label=issues\n[blockchain-issues-link]: https://github.com/ethereumjs/ethereumjs-monorepo/issues?q=is%3Aopen+is%3Aissue+label%3A\"package%3A+blockchain\"\n[blockchain-actions-badge]: https://github.com/ethereumjs/ethereumjs-monorepo/workflows/Blockchain/badge.svg\n[blockchain-actions-link]: https://github.com/ethereumjs/ethereumjs-monorepo/actions?query=workflow%3A%22Blockchain%22\n[blockchain-coverage-badge]: https://codecov.io/gh/ethereumjs/ethereumjs-monorepo/branch/master/graph/badge.svg?flag=blockchain\n[blockchain-coverage-link]: https://codecov.io/gh/ethereumjs/ethereumjs-monorepo/tree/master/packages/blockchain\n","publish_time":1762449854644,"_cnpm_publish_time":1762449854644,"dist-tags":{"alpha":"8.0.0-alpha.1","beta":"6.0.0-beta.3","latest":"10.1.3","nightly":"10.1.1-nightly.2","rc":"10.0.0-rc.1"}}