Class: Block

Block

new Block(data)

Represents a full block.

Parameters:
Name Type Description
data NakedBlock
Properties:
Name Type Attributes Description
type String

"block" (inv type).

version Number

Block version. Note that BCoin reads versions as unsigned despite them being signed on the protocol level. This number will never be negative.

prevBlock Hash

Previous block hash.

merkleRoot Hash

Merkle root hash.

ts Number

Timestamp.

bits Number
nonce Number
totalTX Number

Transaction count.

height Number

Block height (-1 if not in the chain).

txs Array.<TX>

Transaction vector.

commitmentHash Hash <nullable>

Commitment hash for segwit.

witnessNonce Buffer <nullable>

Witness nonce for segwit.

rhash ReversedHash

Reversed block hash (uint256le).

Source:

Extends

Methods

(static) fromJSON(json) → {Block}

Instantiate a block from a jsonified block object.

Parameters:
Name Type Description
json Object

The jsonified block object.

Source:
Returns:
Type
Block

(static) fromRaw(data, encnullable) → {Block}

Instantiate a block from a serialized Buffer.

Parameters:
Name Type Attributes Description
data Buffer
enc String <nullable>

Encoding, can be 'hex' or null.

Source:
Returns:
Type
Block

(static) isBlock(obj) → {Boolean}

Test whether an object is a Block.

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

(static) parseJSON() → {Object}

Handle a deserialized JSON transaction object.

Source:
Returns:

A "naked" block (a plain javascript object which is suitable for passing to the Block constructor).

Type
Object

(static) parseRaw(data, encnullable) → {Object}

Parse a serialized block.

Parameters:
Name Type Attributes Description
data Buffer
enc String <nullable>

Encoding, can be 'hex' or null.

Source:
Returns:

A "naked" block object.

Type
Object

(static) reward(height) → {Amount}

Calculate block subsidy.

Parameters:
Name Type Description
height Number

Reward era by height.

Source:
Returns:
Type
Amount

abbr() → {Buffer}

Serialize the block headers.

Inherited From:
Source:
Returns:
Type
Buffer

addTX(tx) → {TX}

Add a transaction to the block's tx vector.

Parameters:
Name Type Description
tx TX | NakedTX
Source:
Returns:
Type
TX

getBaseSize() → {Number}

Get base block size (without witness).

Source:
Returns:

size

Type
Number

getClaimed() → {Amount}

Get the "claimed" reward by the coinbase.

Source:
Returns:

claimed

Type
Amount

getCoinbaseHeight() → {Number}

Retrieve the coinbase height from the coinbase input script.

Source:
Returns:

height (-1 if not present).

Type
Number

getCommitmentHash(encnullable) → {Buffer|Hash}

Calculate commitment hash (the root of the witness merkle tree hashed with the witnessNonce).

Parameters:
Name Type Attributes Description
enc String <nullable>

Encoding, can be 'hex' or null.

Source:
Returns:

hash

Type
Buffer | Hash

getCost() → {Number}

Calculate block cost.

Source:
Returns:

cost

Type
Number

getMerkleRoot(encnullable) → {Buffer|Hash}

Calculate merkle root.

Parameters:
Name Type Attributes Description
enc String <nullable>

Encoding, can be 'hex' or null.

Source:
Returns:

hash

Type
Buffer | Hash

getPrevout() → {Array.<Hash>}

Get all unique outpoint hashes in the block. Coinbases are ignored.

Source:
Returns:

Outpoint hashes.

Type
Array.<Hash>

getRaw() → {Buffer}

Get the raw block serialization. Include witnesses if present.

Source:
Returns:
Type
Buffer

getReward() → {Amount}

Calculate the block reward.

Source:
Returns:

reward

Type
Amount

getSize() → {Number}

Get real block size.

Source:
Returns:

size

Type
Number

getSizes() → {Object}

Calculate real size and size of the witness bytes.

Source:
Returns:

Contains size and witnessSize.

Type
Object

getVirtualSize() → {Number}

Calculate virtual block size.

Source:
Returns:

Virtual size.

Type
Number

hash(encnullable) → {Hash|Buffer}

Hash the block headers.

Parameters:
Name Type Attributes Description
enc String <nullable>

Can be 'hex' or null.

Inherited From:
Source:
Returns:

hash

Type
Hash | Buffer

hasTX(hash) → {Boolean}

Test the block's transaction vector against a hash.

Parameters:
Name Type Description
hash Hash | TX
Source:
Returns:
Type
Boolean

hasWitness() → {Boolean}

Test whether the block contains a transaction with a non-empty witness.

Source:
Returns:
Type
Boolean

indexOf(hash) → {Number}

Find the index of a transaction in the block.

Parameters:
Name Type Description
hash Hash | TX
Source:
Returns:

index (-1 if not present).

Type
Number

inspect() → {Object}

Inspect the block and return a more user-friendly representation of the data.

Source:
Returns:
Type
Object

render() → {Buffer}

Serialize the block. Include witnesses if present.

Source:
Returns:
Type
Buffer

renderNormal() → {Buffer}

Serialize the block, do not include witnesses.

Source:
Returns:
Type
Buffer

renderWitness() → {Buffer}

Serialize the block. Include witnesses if present.

Source:
Returns:
Type
Buffer

setHeight(height)

Set the height property and the height property of all transactions within the block.

Parameters:
Name Type Description
height Number
Inherited From:
Source:

toInv() → {InvItem}

Convert the block to an inv item.

Inherited From:
Source:
Returns:
Type
InvItem

toJSON() → {Object}

Convert the block to an object suitable for JSON serialization. Note that the hashes will be reversed to abide by bitcoind's legacy of little-endian uint256s.

Source:
Returns:
Type
Object

toMerkle(filter) → {MerkleBlock}

Convert the Block to a MerkleBlock.

Parameters:
Name Type Description
filter Bloom

Bloom filter for transactions to match. The merkle block will contain only the matched transactions.

Source:
Returns:
Type
MerkleBlock

toRaw(encnullable) → {Buffer|String}

Serialize the block.

Parameters:
Name Type Attributes Description
enc String <nullable>

Encoding, can be 'hex' or null.

Source:
See:
Returns:
Type
Buffer | String

verify(retnullable) → {Boolean}

Do non-contextual verification on the block. Including checking the block size, the coinbase and the merkle root. This is consensus-critical.

Parameters:
Name Type Attributes Description
ret Object <nullable>

Return object, may be set with properties reason and score.

Overrides:
Source:
Returns:
Type
Boolean

verifyHeaders(retnullable) → {Boolean}

Verify the block headers (called by verify() in all objects which inherit from AbstractBlock).

Parameters:
Name Type Attributes Description
ret Object <nullable>

Return object, may be set with properties reason and score.

Inherited From:
Source:
Returns:
Type
Boolean