Class: ChainBlock

ChainBlock

new ChainBlock(chain, data, prev)

Represents an entry in the chain. Unlike other bitcoin fullnodes, we store the chainwork with the entry in order to avoid reading the entire chain index on boot and recalculating the chainworks.

Parameters:
Name Type Description
chain Chain
data Object
prev ChainBlock
Properties:
Name Type Description
hash Hash
version Number

Transaction version. Note that BCoin reads versions as unsigned even though they are signed at the protocol level. This value will never be negative.

prevBlock Hash
merkleRoot Hash
ts Number
bits Number
nonce Number
height Number
chainwork BN
ancestors Array.<ChainBlock>

Ancestors are temporarily advocate for IsSuperMajority and retargeting.

rhash ReversedHash

Reversed block hash (uint256le).

Source:

Methods

(static) fromJSON(chain, json) → {ChainBlock}

Instantiate block from jsonified object.

Parameters:
Name Type Description
chain Chain
json Object
Source:
Returns:
Type
ChainBlock

(static) fromRaw(chain, buf) → {ChainBlock}

Deserialize the entry.

Parameters:
Name Type Description
chain Chain
buf Buffer
Source:
Returns:
Type
ChainBlock

(static) isChainBlock(obj) → {Boolean}

Test whether an object is a ChainBlock.

Parameters:
Name Type Description
obj Object
Source:
Returns:
Type
Boolean

alloc(max, callback)

Allocate ancestors.

Parameters:
Name Type Description
max Number

Number of ancestors.

callback function
Source:

ensureAncestors(callback)

Allocate ancestors based on retarget interval and majority window. These ancestors will be stored in the ancestors array and enable use of synchronous ChainBlock methods.

Parameters:
Name Type Description
callback function
Source:

free()

Free up ancestors. This is very important because chain entries are cached in the ChainDB's LRU cache.

Source:

getAncestor(index) → {function}

Get a single ancestor by index. Note that index-0 is the same entry. This is done for sane porting of bitcoind functions to BCoin.

Parameters:
Name Type Description
index Number
Source:
Returns:

callback - Returns [Error, ChainBlock].

Type
function

getAncestorByHeight(height, callback)

Collect ancestors up to height.

Parameters:
Name Type Description
height Number
callback function

Returns [Error, ChainBlock[]].

Source:

getAncestors(max, callback)

Collect ancestors.

Parameters:
Name Type Description
max Number

Number of ancestors.

callback function

Returns [Error, ChainBlock[]].

Source:

getChainwork() → {BN}

Calculate the chainwork by adding proof to previous chainwork.

Source:
Returns:

chainwork

Type
BN

getMedianTime(ancestors) → {Number}

Get median time past.

Parameters:
Name Type Description
ancestors Array.<ChainBlock>

Note that index 0 is the same entry.

Source:
See:
  • GetMedianTimePast().
Returns:

Median time past.

Type
Number

getMedianTimeAsync(callback)

Get median time past asynchronously.

Parameters:
Name Type Description
callback function

Returns [Error, Number].

Source:

getNext(callback)

Get next entry.

Parameters:
Name Type Description
callback function

Returns [Error, ChainBlock].

Source:

getPrevious(callback)

Get previous entry.

Parameters:
Name Type Description
callback function

Returns [Error, ChainBlock].

Source:

getProof() → {BN}

Calculate the proof: (1 << 256) / (target + 1)

Source:
Returns:

proof

Type
BN

inspect() → {Object}

Return a more user-friendly object.

Source:
Returns:
Type
Object

isGenesis() → {Boolean}

Test against the genesis block.

Source:
Returns:
Type
Boolean

isMainChain(callback)

Test whether the entry is in the main chain.

Parameters:
Name Type Description
callback function

Return [Error, Boolean].

Source:

isOutdated(version, ancestors) → {Boolean}

Check isSuperMajority against majorityRejectOutdated.

Parameters:
Name Type Description
version Number
ancestors Array.<ChainBlock>
Source:
Returns:
Type
Boolean

isSuperMajority(version, required, ancestors) → {Boolean}

Calculate found number of block versions within the majority window.

Parameters:
Name Type Description
version Number
required Number
ancestors Array.<ChainBlock>
Source:
Returns:
Type
Boolean

isUpgraded(version, ancestors) → {Boolean}

Check isSuperMajority against majorityEnforceUpgrade.

Parameters:
Name Type Description
version Number
ancestors Array.<ChainBlock>
Source:
Returns:
Type
Boolean

toJSON() → {Object}

Serialize the entry to an object more suitable for JSON serialization.

Source:
Returns:
Type
Object

toRaw() → {Buffer}

Serialize the entry to internal database format.

Source:
Returns:
Type
Buffer