A module to store and interact with blocks.
npm install ethereumjs-blockchain
The following is an example to iterate through an existing Geth DB (needs level to be
installed separately):
const level = require('level')
const Blockchain = require('ethereumjs-blockchain')
const utils = require('ethereumjs-util')
const gethDbPath = './chaindata' // Add your own path here
const db = level(gethDbPath)
new Blockchain({db: db}).iterator('i', (block, reorg, cb) => {
const blockNumber = utils.bufferToInt(block.header.number)
const blockHash = block.hash().toString('hex')
console.log(`BLOCK ${blockNumber}: ${blockHash}`)
cb()
}, (err) => console.log(err || 'Done.'))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.
Blockchainnew Blockchain(opts)BlockChainmethodsblockchain.putGenesis(genesis, [cb])blockchain.getHead(name, [cb])blockchain.getLatestHeader([cb])blockchain.getLatestBlock([cb])blockchain.putBlocks(blocks, [cb])blockchain.putBlock(block, [cb])blockchain.getBlock(hash, [cb])blockchain.getBlocks(blockId, maxBlocks, skip, reverse, [cb])blockchain.putHeaders(headers, [cb])blockchain.putHeader(header, [cb])blockchain.selectNeededHashes(hashes, [cb])blockchain.delBlock(blockHash, [cb])blockchain.iterator(name, onBlock, [cb])
Implements functions for retrieving, manipulating and storing Ethereum's blockchain
Creates new Blockchain object
opts.db- Database to store blocks and metadata. Should be a levelup instance.opts.validate- this the flag to validate blocks (e.g. Proof-of-Work), latest HF rules supported:Constantinople.
[DEPRECATION NOTE]
The old separated DB constructor parameters opts.blockDB and opts.detailsDB from before the Geth DB-compatible v3.0.0 release are deprecated and continued usage is discouraged. When provided opts.blockDB will be used
as opts.db and opts.detailsDB is ignored. On the storage level the DB formats are not compatible and it is not
possible to load an old-format DB state into a post-v3.0.0 Blockchain object.
Puts the genesis block in the database.
genesis- the genesis block to be addedcb- the callback. It is given two parameterserrand the savedblock
Returns the specified iterator head.
name- Optional name of the state root head (default: 'vm')cb- the callback. It is given two parameterserrand the returnedblock
Returns the latest header in the canonical chain.
cb- the callback. It is given two parameterserrand the returnedheader
Returns the latest full block in the canonical chain.
cb- the callback. It is given two parameterserrand the returnedblock
Adds many blocks to the blockchain.
blocks- the blocks to be added to the blockchaincb- the callback. It is given two parameterserrand the last of the savedblocks
Adds a block to the blockchain.
block- the block to be added to the blockchaincb- the callback. It is given two parameterserrand the savedblock
Gets a block by its blockTag.
blockTag- the block's hash or numbercb- the callback. It is given two parameterserrand the foundblock(an instance of https://github.com/ethereumjs/ethereumjs-block) if any.
Looks up many blocks relative to blockId.
blockId- the block's hash or numbermaxBlocks- max number of blocks to returnskip- number of blocks to skipreverse- fetch blocks in reversecb- the callback. It is given two parameterserrand the foundblocksif any.
Adds many headers to the blockchain.
headers- the headers to be added to the blockchaincb- the callback. It is given two parameterserrand the last of the savedheaders
Adds a header to the blockchain.
header- the header to be added to the blockchaincb- the callback. It is given two parameterserrand the savedheader
[DEPRECATED] Returns an empty object
Given an ordered array, returns to the callback an array of hashes that are not in the blockchain yet.
hashes- Ordered array of hashescb- the callback. It is given two parameterserrand hashes found.
Deletes a block from the blockchain. All child blocks in the chain are deleted and any encountered heads are set to the parent block
blockHash- the hash of the block to be deletedcb- A callback.
Iterates through blocks starting at the specified verified state root head and calls the onBlock function on each block
name- name of the state root headonBlock- function called on each block with params (block, reorg, cb)cb- A callback function
Tests can be found in the test directory and run with npm run test.
These can also be valuable as examples/inspiration on how to run the library and invoke different parts of the API.