blockchain-spv
[](https://david-dm.org/dashevo/blockchain-spv
Stores blockchain headers and verifies transactions with SPV
Usage
npm install @dashevo/blockchain-spv
var params = require('webcoin-dash').blockchain
var db = levelup('dash.chain', { db: require('memdown') })
var Blockchain = require('@dashevo/blockchain-spv')
var chain = new Blockchain(params, db)
chain.on('ready', function () {
}
Blockchain
stores and verifies block headers, and does SPV (lite client) verification. It is compatible with Dash and Bitcoin-derived blockchains.
new Blockchain(params, db)
Creates an SPV Blockchain
which stores and verifies block headers.
params
should be the blockchain parameters for the blockchain you wish to use. Parameters for Dash are available at require('webcoin-dash').blockchain
. For more info about params you can use, see the Parameters section.
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).
Make sure to wait for the ready
event before using the blockchain.
Adds block headers to the chain. headers
should be an array of contiguous, ascending block headers. The headers will be verified (checked to make sure the expected amount of work was done, the difficulty was correct, etc.). The callback will be called with cb(err, header)
where header
is an invalid header if there was a validation error.
chain.createWriteStream()
Returns a writable stream that takes in arrays of block headers and adds them to the chain. This is essentially just a stream wrapper for chain.addHeaders()
, making it easier to get headers from a HeaderStream
(from the bitcoin-net
module).
chain.createReadStream([opts])
Returns a readable stream that outputs blocks from the blockchain (in order). The stream will stay open even when it reaches the chain tip, and will output new blocks as they are received (this means you don't have to think about whether the chain is done syncing or not).
If a reorg happens (blocks that have been emitted are now on an invalid fork), the stream will emit the now-invalid blocks again in descending order (so that each can be un-processed). Each block has a boolean add
property which is false
if it is being removed from the chain. NOTE: It is important to always check the value of block.add
and un-process blocks when they are invalidated.
opts
may contain the following options:
from
Buffer (default: null
) - start the stream at the block with this hash (exclusive). If from
is null
, the stream will start at the genesis block.
chain.getBlock(hash, callback)
Gets a block in the chain with hash hash
. hash
must be a Buffer. The callback is called with cb(err, block)
.
chain.getBlockAtHeight(height, callback)
Gets a block in the chain with height height
. The callback is called with cb(err, block)
.
Note that this requires the blockchain to be traversed (from the tip or genesis block, whichever is closest), so it runs in O(N/2)
time.
chain.getBlockAtTime(time, callback)
Gets the highest block with a time that comes before or on time
. time
should be in Unix time measured in seconds (not milliseconds as returned by Date.now()
). The callback is called with cb(err, block)
.
Note that this requires the blockchain to be traversed (from the tip or genesis block, whichever is closest), so it runs in O(N)
time.
chain.getTip()
Returns the highest block added to the chain.
chain.getPath(from, to, callback)
Gets the path of blocks between from
and to
. This is useful to know which blocks to process or unprocess when getting from one part of a chain to another (including going across forks). Calls the callback with cb(err, path)
where path
is the following:
{
add: Array,
remove: Array,
fork: Block
}
Examples:
[a]<-[b]<-[c]<-[d]
'getPath(a, d)' results in:
{
add: [ b, c, d ],
remove: [],
fork: undefined
}
'getPath(d, a)' results in:
{
add: [],
remove: [ d, c, b ],
fork: undefined
}
[a]<-[b]<-[c]<-[d]
\
[e]<-[f]
'getPath(f, d)' results in:
{
remove: [ f, e ],
add: [ b, c, d ],
fork: e
}
chain.getPathToTip(from, callback)
A convenience method for chain.getPath(from, chain.getTip(), cb)
.
Parameters
Parameters specify blockchain rules and constants for different cryptocurrencies and blockchains. Parameters should contain the following:
{
genesisHeader: {
version: Number,
prevHash: Buffer,
merkleRoot: Buffer,
time: Number,
bits: Number,
nonce: Number
},
shouldRetarget: function (block, callback) { ... },
calculateTarget: function (block, blockchain, callback) { ... },
miningHash: function (header, callback) { ... },
checkpoints: [
{
height: Number,
header: {
version: Number,
prevHash: Buffer,
merkleRoot: Buffer,
time: Number,
bits: Number,
nonce: Number
}
}
]
}
For some examples, see these parameter repos: