Class: ChainDB

blockchain.ChainDB

new ChainDB(chain)

The database backend for the Chain object.

Parameters:
Name Type Attributes Description
chain Chain
options.prune Boolean <nullable>

Whether to prune the chain.

options.spv Boolean <nullable>

SPV-mode, will not save block data, only entries.

options.name String <nullable>

Database name

options.location String <nullable>

Database location

options.db String <nullable>

Database backend name

Properties:
Name Type Description
prune Boolean
Source:
Fires:
  • ChainDB#event:open
  • ChainDB#event:error

Members

(static) layout :Object

Database layout.

Type:
  • Object
Source:

Methods

(private) _disconnect(entry, block) → {Promise}

Disconnect block without a batch.

Parameters:
Name Type Description
entry ChainEntry
block Block
Source:
Returns:
  • Returns CoinView.
Type
Promise

(private) _reconnect(entry, block, view) → {Promise}

Reconnect block without a batch.

Parameters:
Name Type Description
entry ChainEntry
block Block
view CoinView
Source:
Returns:
Type
Promise

(private) _removeChain(hash) → {Promise}

Remove an alternate chain.

Parameters:
Name Type Description
hash Hash

Alternate chain tip.

Source:
Returns:
Type
Promise

(private) _save(entry, block, viewnullable) → {Promise}

Save an entry without a batch.

Parameters:
Name Type Attributes Description
entry ChainEntry
block Block
view CoinView <nullable>
Source:
Returns:
Type
Promise

batch() → {Batch}

Get current batch.

Source:
Returns:
Type
Batch

(private) checkDeployments() → {Promise}

Check for outdated deployments.

Source:
Returns:
Type
Promise

commit() → {Promise}

Commit current batch.

Source:
Returns:
Type
Promise

connectBlock(entry, block, view) → {Promise}

Connect block inputs.

Parameters:
Name Type Description
entry ChainEntry
block Block
view CoinView
Source:
Returns:
Type
Promise

del(key)

Delete key from current batch.

Parameters:
Name Type Description
key String
Source:

disconnect(entry, block) → {Promise}

Disconnect block from the chain.

Parameters:
Name Type Description
entry ChainEntry
block Block
Source:
Returns:
Type
Promise

disconnectBlock(entry, block) → {Promise}

Disconnect block inputs.

Parameters:
Name Type Description
entry ChainEntry
block Block
Source:
Returns:
  • Returns CoinView.
Type
Promise

drop() → {Batch}

Drop current batch.

Source:
Returns:
Type
Batch

getBlock(hash) → {Promise}

Retrieve a block from the database (not filled with coins).

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
Promise

getBlockView(hash) → {Promise}

Get a historical block coin viewpoint.

Parameters:
Name Type Description
hash Block
Source:
Returns:
  • Returns CoinView.
Type
Promise

getCache(block)

Get an entry directly from the LRU cache. This is useful for optimization if we don't want to wait on a nextTick during a get() call.

Parameters:
Name Type Description
block Hash | Number

Hash or height.

Source:

getCoin(hash, index) → {Promise}

Get a coin (unspents only).

Parameters:
Name Type Description
hash Hash
index Number
Source:
Returns:
  • Returns Coin.
Type
Promise

getCoins(hash) → {Promise}

Get coins (unspents only).

Parameters:
Name Type Description
hash Hash
Source:
Returns:
  • Returns Coins.
Type
Promise

getCoinsByAddress(addresses) → {Promise}

Get all coins pertinent to an address.

Parameters:
Name Type Description
addresses Array:.<Address:>
Source:
Returns:
  • Returns Coin[].
Type
Promise

getCoinView(tx) → {Promise}

Get coin viewpoint.

Parameters:
Name Type Description
tx TX
Source:
Returns:
  • Returns CoinView.
Type
Promise

getEntries() → {Promise}

Get all entries.

Source:
Returns:
  • Returns ChainEntry[].
Type
Promise

getEntry(block) → {Promise}

Retrieve a chain entry.

Parameters:
Name Type Description
block Number | Hash

Height or hash.

Source:
Returns:
  • Returns ChainEntry.
Type
Promise

getEntryByHash(hash) → {Promise}

Retrieve a chain entry by hash.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
  • Returns ChainEntry.
Type
Promise

getEntryByHeight(height) → {Promise}

Retrieve a chain entry by height.

Parameters:
Name Type Description
height Number
Source:
Returns:
  • Returns ChainEntry.
Type
Promise

getFlags() → {Promise}

Retrieve the database flags.

Source:
Returns:
  • Returns ChainFlags.
Type
Promise

getHash(height) → {Promise}

Get the hash of a block by height. Note that this will only return hashes in the main chain.

Parameters:
Name Type Description
height Number
Source:
Returns:
Type
Promise

getHashesByAddress(addresses) → {Promise}

Get all transaction hashes to an address.

Parameters:
Name Type Description
addresses Array:.<Address:>
Source:
Returns:
Type
Promise

getHeight(hash) → {Promise}

Get the height of a block by hash.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
  • Returns Number.
Type
Promise

getMeta(hash) → {Promise}

Get a transaction with metadata.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
  • Returns TXMeta.
Type
Promise

getMetaByAddress(addresses) → {Promise}

Get all transactions pertinent to an address.

Parameters:
Name Type Description
addresses Array:.<Address:>
Source:
Returns:
  • Returns TXMeta[].
Type
Promise

getNextHash(hash) → {Promise}

Get the next block hash (does not work by height).

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
Promise

getRawBlock(hash) → {Promise}

Retrieve a block from the database (not filled with coins).

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
Promise

getSpentView(tx) → {Promise}

Get coin viewpoint (historical).

Parameters:
Name Type Description
tx TX
Source:
Returns:
  • Returns CoinView.
Type
Promise

getState() → {Promise}

Retrieve the tip entry from the tip record.

Source:
Returns:
  • Returns ChainState.
Type
Promise

getStateCache() → {Promise}

Get state caches.

Source:
Returns:
  • Returns StateCache.
Type
Promise

getTip() → {Promise}

Retrieve the tip entry from the tip record.

Source:
Returns:
  • Returns ChainEntry.
Type
Promise

getTips() → {Promise}

Get all tip hashes.

Source:
Returns:
Type
Promise

getTX(hash) → {Promise}

Retrieve a transaction.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
  • Returns TX.
Type
Promise

getTXByAddress(addresses) → {Promise}

Get all transactions pertinent to an address.

Parameters:
Name Type Description
addresses Array:.<Address:>
Source:
Returns:
  • Returns TX[].
Type
Promise

getUndoCoins(hash) → {Promise}

Get coins necessary to be resurrected during a reorg.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
  • Returns Coin[].
Type
Promise

hasCache(block)

Test the cache for a present entry hash or height.

Parameters:
Name Type Description
block Hash | Number

Hash or height.

Source:

hasCoins(hash) → {Promise}

Check whether coins are still unspent. Necessary for bip30.

Parameters:
Name Type Description
hash Hash
Source:
See:
Returns:
  • Returns Boolean.
Type
Promise

hasEntry(hash) → {Promise}

Test whether the chain contains a block.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
  • Returns Boolean.
Type
Promise

hasTX(hash) → {Promise}

Parameters:
Name Type Description
hash Hash
Source:
Returns:
  • Returns Boolean.
Type
Promise

(private) indexTX(tx, view, entry, index)

Index a transaction by txid and address.

Parameters:
Name Type Description
tx TX
view CoinView
entry ChainEntry
index Number
Source:

(private) invalidateCache() → {Promise}

Invalidate state cache.

Source:
Returns:
Type
Promise

isMainChain(hash) → {Promise}

Check to see if a block is on the main chain.

Parameters:
Name Type Description
hash ChainEntry | Hash
Source:
Returns:
  • Returns Boolean.
Type
Promise

(private) pruneBlock(entry) → {Promise}

Prune a block from the chain and add current block to the prune queue.

Parameters:
Name Type Description
entry ChainEntry
Source:
Returns:
Type
Promise

put(key, value)

Put key and value to current batch.

Parameters:
Name Type Description
key String
value Buffer
Source:

reconnect(entry, block, view) → {Promise}

Reconnect the block to the chain.

Parameters:
Name Type Description
entry ChainEntry
block Block
view CoinView
Source:
Returns:
Type
Promise

removeBlock(entry) → {Promise}

Remove a block (not an entry) to the database. Disconnect inputs.

Parameters:
Name Type Description
entry ChainEntry
Source:
Returns:
Type
Promise

removeChains() → {Promise}

Remove all alternate chains.

Source:
Returns:
Type
Promise

reset(block) → {Promise}

Reset the chain to a height or hash. Useful for replaying the blockchain download for SPV.

Parameters:
Name Type Description
block Hash | Number

hash/height

Source:
Returns:
Type
Promise

save(entry, block, viewnullable) → {Promise}

Save an entry to the database and optionally connect it as the tip. Note that this method does not perform any verification which is instead performed in Chain#add.

Parameters:
Name Type Attributes Description
entry ChainEntry
block Block
view CoinView <nullable>

Will not connect if null.

Source:
Returns:
Type
Promise

saveBlock(entry, block, viewnullable) → {Promise}

Save a block (not an entry) to the database and potentially connect the inputs.

Parameters:
Name Type Attributes Description
entry ChainEntry
block Block
view CoinView <nullable>
Source:
Returns:
Type
Promise

saveDeployments() → {Promise}

Save deployment table.

Source:
Returns:
Type
Promise

saveFlags() → {Promise}

Save database options.

Source:
Returns:
Type
Promise

saveGenesis() → {Promise}

Write genesis block to database.

Source:
Returns:
Type
Promise

(private) saveUpdates()

Save state cache updates.

Source:

(private) saveView(view)

Commit coin view to database.

Parameters:
Name Type Description
view CoinView
Source:

scan(start, filter, iter) → {Promise}

Scan the blockchain for transactions containing specified address hashes.

Parameters:
Name Type Description
start Hash

Block hash to start at.

filter Bloom

Bloom filter containing tx and address hashes.

iter function

Iterator.

Source:
Returns:
Type
Promise

start() → {Batch}

Start a batch.

Source:
Returns:
Type
Batch

(private) unindexTX(tx, view)

Remove transaction from index.

Parameters:
Name Type Description
tx TX
view CoinView
Source:

verifyDeployments() → {Promise}

Potentially invalidate state cache.

Source:
Returns:
Type
Promise

verifyFlags() → {Promise}

Verify current options against db options.

Source:
Returns:
Type
Promise

writeDeployments() → {Promise}

Save deployment table.

Source:
Returns:
Type
Promise