npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sophonjs-blockchain

v3.4.0

Published

A module to store and interact with blocks

Downloads

5

Readme

SYNOPSIS

NPM Package Build Status Coverage Status Gitter or #sophonjs on freenode

A module to store and interact with blocks.

INSTALL

npm install sophonjs-blockchain

EXAMPLE

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

const level = require('level')
const Blockchain = require('sophonjs-blockchain')
const utils = require('sophonjs-util')

const gravitonDbPath = './chaindata' // Add your own path here
const db = level(gravitonDbPath)

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

API

Blockchain

Implements functions for retrieving, manipulating and storing Sophon's blockchain

new Blockchain(opts)

Creates new Blockchain object

  • opts.chain (String | Number) The chain for the block [default: 'mainnet']
  • opts.hardfork String Hardfork for the block [default: null, block number-based behavior]
  • opts.common Object Alternatively pass a Common instance (sophonjs-common) instead of setting chain/hardfork directly
  • 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 Graviton 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.

BlockChain methods

blockchain.putGenesis(genesis, cb)

Puts the genesis block in the database.

  • genesis - the genesis block to be added
  • cb - the callback. It is given two parameters err and the saved block

blockchain.getHead(name, cb)

Returns the specified iterator head.

  • name - Optional name of the state root head (default: 'vm')
  • cb - the callback. It is given two parameters err and the returned block

blockchain.getLatestHeader(cb)

Returns the latest header in the canonical chain.

  • cb - the callback. It is given two parameters err and the returned header

blockchain.getLatestBlock(cb)

Returns the latest full block in the canonical chain.

  • cb - the callback. It is given two parameters err and the returned block

blockchain.putBlocks(blocks, cb)

Adds many blocks to the blockchain.

  • blocks - the blocks to be added to the blockchain
  • cb - the callback. It is given two parameters err and the last of the saved blocks

blockchain.putBlock(block, cb)

Adds a block to the blockchain.

  • block - the block to be added to the blockchain
  • cb - the callback. It is given two parameters err and the saved block

blockchain.getBlock(blockTag, cb)

Gets a block by its blockTag.

  • blockTag - the block's hash or number
  • cb - the callback. It is given two parameters err and the found block (an instance of https://octonion.institute/susy-js/sophonjs-block) if any.

blockchain.getBlocks(blockId, maxBlocks, skip, reverse, cb)

Looks up many blocks relative to blockId.

  • blockId - the block's hash or number
  • maxBlocks - max number of blocks to return
  • skip - number of blocks to skip
  • reverse - fetch blocks in reverse
  • cb - the callback. It is given two parameters err and the found blocks if any.

blockchain.putHeaders(headers, cb)

Adds many headers to the blockchain.

  • headers - the headers to be added to the blockchain
  • cb - the callback. It is given two parameters err and the last of the saved headers

blockchain.putHeader(header, cb)

Adds a header to the blockchain.

  • header - the header to be added to the blockchain
  • cb - the callback. It is given two parameters err and the saved header

blockchain.getDetails(hash, cb)

[DEPRECATED] Returns an empty object


blockchain.selectNeededHashes(hashes, cb)

Given an ordered array, returns to the callback an array of hashes that are not in the blockchain yet.

  • hashes - Ordered array of hashes
  • cb - the callback. It is given two parameters err and hashes found.

blockchain.delBlock(blockHash, cb)

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 deleted
  • cb - A callback.

blockchain.iterator(name, onBlock, cb)

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 head
  • onBlock - function called on each block with params (block, reorg, cb)
  • cb - A callback function

TESTS

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.