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

blockchain-state

v0.2.0

Published

A writable stream for applications that consume blocks

Downloads

11

Readme

blockchain-state

npm version Build Status Dependency Status

A writable stream for applications that consume blocks

This module should be used whenever you need to process blockchain blocks in order (e.g. wallets or any other application that needs to scan the blockchain). Handling of reorgs (switching from one blockchain fork to another) is simplified, and block sync progress is stored in a LevelUp database.

Usage

npm install blockchain-state

var BlockchainState = require('blockchain-state')

function add (block, tx, cb) {
  // called when a block should be processed
  cb() // call cb when done
}
function remove (block, tx, cb) {
  // called when a block should be un-processed
  cb() // call cb when done
}
var db = level('foo') // a LevelUp-compatible datastore
var state = new BlockchainState(add, remove, db)

// write blocks to the BlockchainState
// 'chain' is an instance of a Blockchain (from the
// 'blockchain-spv' module)
state.getHash(function (err, hash) {
  if (err) return console.log(err)
  // BlockchainState is a writable stream,
  // so we can pipe blocks directly into it
  chain.createReadStream({ from: hash }).pipe(state)
})

new BlockchainState(add, remove, db, [opts])

Creates a BlockchainState which can process blocks. BlockchainState is a writable stream, and expects an input of blocks in the format output by chain.createReadStream() from the blockchain-spv module.

add(block, tx, cb) should be a function which will be called every time a new block should be processed. The arguments passed to it are:

  • block: the block to be processed
  • tx: a database transaction, which may be used to modify the LevelUp database while ensuring consistency with the blockchain state
  • cb: a callback function that must be called when done processing the block (optionally with an error)

remove(block, tx, cb) is similar to add, but is instead called whenever a block should be un-processed (when it is now in an invalid fork)

db should be a LevelUp instance where block data will be stored. The db should not be shared with another Blockchain (if you need to, use level-sublevel to create a sub-section of your db).


state.getHash(callback)

Calls callback(err, hash) with the hash of the block most recently processed by the BlockchainState. If no blocks have been processed yet, hash will be null.