This library manages a blockchain in the most basic sense: It accepts blocks, which point to previous blocks, and allows retrieval of them. It has no notion of "transactions", "addresses" or the like.
- Saves block data to a back-end service (LevelDB by default) for persistence
- Adds a height attribute to blocks added to it (Genesis block(s) are height zero)
- Allows retrieval of block data by hash
- Keeps track of chain tips
- Keeps track of chain roots
- Allows deleting blocks (and optionally all children of that block)
- Sends events to alert other modules of chain activity
- Lookup by transaction ID
- Track unspent transaction outputs (UTXO)
- Automatic pruning/fork resolutions
For maximum space savings, the block objects passed to the chain should be Buffer objects representing the binary block data. This is the same structure the default bitcoind
client uses for its block storage, and still the blockchain cache is many, many gigabytes in size; saving JSON or other representations would make the database even larger, so module aims to avoid that.
Adds a block to the chain. key
is the block's unique identifier (hash), parent
is the parent's unique identifier, and data
is the block data itself. All three are Buffer objects.
When the first block is added, the parent block is unknown, so the new block is given a height of zero (genesis/root), and is also a chain tip. Since it is both root and tip, it is also an orphan at that point.
Subsequent blocks on the main chain will find their parent already exists, so will get a height one more than their parent.
When a new block is added, its hash is also checked against all the known root blocks, to see if this block formed a link to a detached chain. If a match is found, the root block being attach has its height adjusted to place it after its parent, and all children of that detached root get their height updated too.
This module is perfectly happy to have multiple side-branches and even parallel trees being maintained. It's up to other modules to set the logic and take the initiative when pruning/reorganizing is needed.
All three events get the same structured data payload: { hash: HASH, data: BLOCKDATA, height: INTEGER, isRoot: BOOLEAN, isTip: BOOLEAN }
Block hash
has been added and assigned the height of height
.
Block hash
has been removed from the chain. It previously had the height of height
.
Block hash
has been been reorganized, and now has the height of height
.