Skip to content

Latest commit

 

History

History
 
 

blockchain

@ethereumjs/blockchain

NPM Package GitHub Issues Actions Status Code Coverage Discord

A module to store and interact with blocks.

Note: this README reflects the state of the library from v5.0.0 onwards. See README from the standalone repository for an introduction on the last preceeding release.

INSTALL

npm install @ethereumjs/blockchain

USAGE

Introduction

The following is an example to iterate through an existing Geth DB (needs level to be installed separately).

This module performs write operations. Making a backup of your data before trying it is recommended. Otherwise, you can end up with a compromised DB state.

import Blockchain from '@ethereumjs/blockchain'
import Common, { Chain } from '@ethereumjs/common'

const level = require('level')

const gethDbPath = './chaindata' // Add your own path here. It will get modified, see remarks.

const common = new Common({ chain: Chain.Ropsten })
const db = level(gethDbPath)
// Use the safe static constructor which awaits the init method
const blockchain = Blockchain.create({ common, db })

blockchain.iterator('i', (block) => {
  const blockNumber = block.header.number.toString()
  const blockHash = block.hash().toString('hex')
  console.log(`Block ${blockNumber}: ${blockHash}`)
})

WARNING: Since @ethereumjs/blockchain is also doing write operations on the DB for safety reasons only run this on a copy of your database, otherwise this might lead to a compromised DB state.

EIP-1559 Support

This library supports the handling of EIP-1559 blocks and transactions starting with the v5.3.0 release.

API

Documentation

DEVELOPER

For debugging blockchain control flows the debug library is used and can be activated on the CL with DEBUG=[Logger Selection] node [Your Script to Run].js.

The following initial logger is currently available:

Logger Description
blockchain:clique Clique operations like updating the vote and/or signer list

The following is an example for a logger run:

Run with the clique logger:

DEBUG=blockchain:clique ts-node test.ts

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 do improvements on the libraries have a look at our contribution guidelines.