vaporyjs-blockchain
v1.4.1
Published
A module to store and interact with blocks
Downloads
2
Readme
SYNOPSIS
A module to store and interact with blocks.
INSTALL
npm install vaporyjs-blockchain
API
vaporyjs-blockchain
A module to store and interact with blocks
Blockchain
Implements functions for retrieving, manipulating and storing Vapory's blockchain
new Blockchain(blockDB, detailsDB)
Creates new Blockchain object
blockDB
- the database that backs the Blockchain. thedb
object is tested with levelup but should with any store that implements put, get and del.detailsDB
- this is the same asblockDB
except this db houses the meta infomation about the blockchain.
BlockChain
Properties
head
- The Head, the block that has the most weightparentHead
- The parent of the head blockgenesisHash
- The hash of the genesis blockheight
- The height of the blockchaintotallDifficulty
- The totall difficulty which is the some of a the difficulty of all the prevous blocks
BlockChain
methods
blockchain.init(cb)
Initializes the blockchain. This must be done before you can use the blockchain
cb
- the callback
blockchain.addBlock(block, [callback])
Adds a block to the blockchain.
block
- the block to be added to the blockchaincallback
- the callback. It is given two parameterserr
and the savedblock
blockchain.getBlock(hash, [callback])
Gets a block by it hash.
hash
- the block's hashcallback
- the callback. It is given two parameterserr
and the foundblock
if any.
blockchain.getBlockInfo(hash, cb)
Retrieves meta infromation about the block and passed it to the callback
hash
- the hash of the block as aBuffer
or a hexString
cb
- the callback which is passed anObject
containing two proptiesparent
, the hash of the this block's parents andchildren
anArray
of hashes of children blocks.
blockchain.getBlockHashes(parentHash, count, cb)
Gets a segment of the blockchain starting at the parent hash and contuning for count
blocks returning an array of block hashes orders from oldest to youngest.
parentHash
- the block to start from. Given as aBuffer
or a hexString
count
- aNumber
specifing how many block hashes to returncb
- the callback which is give an array of block hashes
blockchain.getBlockChain(startingHashes, count, cb)
gets a section of the blockchain in a form of an array starting at the parent hash, up count
blocks
startingHashes
- an array of hashes or a single hash to start returning the chain from. The first hash in the array that is found in the blockchain will be used.count
- the max number of blocks to returncallback
- the callback. It is given two parameterserr
andblockchain
.err
is any errors. If none of the starting hashes were founderr
will benotFound
.blockchain
is an array of blocks.
blockchain.selectNeededHashes(hashes, cb)
Given an ordered array, returns to the callback an array of hashes that are not in the blockchain yet.
hashes
- anArray
hashescb
- the callback