bitcoinlib.blocks module

class bitcoinlib.blocks.Block(block_hash, version, prev_block, merkle_root, time, bits, nonce, transactions=None, height=None, confirmations=None, network='bitcoin')[source]

Bases: object

Create a new Block object with provided parameters.

>>> b = Block('0000000000000000000154ba9d02ddd6cee0d71d1ea232753e02c9ac6affd709', version=0x20000000, prev_block='0000000000000000000f9578cda278ae7a2002e50d8e6079d11e2ea1f672b483', merkle_root='20e86f03c24c53c12014264d0e405e014e15a02ad02c174f017ee040750f8d9d', time=1592848036, bits=387044594, nonce=791719079)
>>> b
<Block(0000000000000000000154ba9d02ddd6cee0d71d1ea232753e02c9ac6affd709, None, transactions: 0)>
Parameters
  • block_hash (bytes, str) – Hash value of serialized block

  • version (bytes, str, in) – Block version to indicate which software / BIPs are used to create block

  • prev_block (bytes, str) – Hash of previous block in blockchain

  • merkle_root (bytes, str) – Merkle root. Top item merkle chain tree to validate transactions.

  • time (int, bytes) – Timestamp of time when block was included in blockchain

  • bits (bytes, str, int) – Bits are used to indicate target / difficulty

  • nonce (bytes, str, int) – Number used once, n-once is used to create randomness for miners to find a suitable block hash

  • transactions (list of Transaction, list of str) – List of transaction included in this block. As list of transaction objects or list of transaction IDs strings

  • height (int) – Height of this block in the Blockchain

  • confirmations (int) – Number of confirmations for this block, or depth. Increased when new blocks are found

  • network (str, Network) – Network, leave empty for default network

as_dict()[source]

Get representation of current Block as dictionary.

Return dict

check_proof_of_work()[source]

Check proof of work for this block. Block hash must be below target.

This library is not optimised for mining, but you can use this for testing or learning purposes.

>>> b = Block('0000000000000000000154ba9d02ddd6cee0d71d1ea232753e02c9ac6affd709', version=0x20000000, prev_block='0000000000000000000f9578cda278ae7a2002e50d8e6079d11e2ea1f672b483', merkle_root='20e86f03c24c53c12014264d0e405e014e15a02ad02c174f017ee040750f8d9d', time=1592848036, bits=387044594, nonce=791719079)
>>> b.check_proof_of_work()
True
Return bool

property difficulty

Block difficulty calculated from bits / target. Human readable representation of block’s target.

Genesis block has difficulty of 1.0

>>> from bitcoinlib.services.services import Service
>>> srv = Service()
>>> b = srv.getblock(0)
>>> b.difficulty
1.0
Return float

classmethod from_raw(raw, block_hash=None, height=None, parse_transactions=False, limit=0, network='bitcoin')[source]

Create Block object from raw serialized block in bytes.

Get genesis block:

>>> from bitcoinlib.services.services import Service
>>> srv = Service()
>>> b = srv.getblock(0)
>>> b.block_hash.hex()
'000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
Parameters
  • raw (bytes) – Raw serialize block

  • block_hash (bytes) – Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different than specified.

  • height (int) – Specify height if known. Will be derived from coinbase transaction if not provided.

  • parse_transactions (bool) – Indicate if transactions in raw block need to be parsed and converted to Transaction objects. Default is False

  • limit (int) – Maximum number of transactions to parse. Default is 0: parse all transactions. Only used if parse_transaction is set to True

  • network (str) – Name of network

Return Block

classmethod parse(raw, block_hash=None, height=None, parse_transactions=False, limit=0, network='bitcoin')[source]

Create Block object from raw serialized block in bytes or BytesIO format. Wrapper for parse_bytesio()

Get genesis block:

>>> from bitcoinlib.services.services import Service
>>> srv = Service()
>>> b = srv.getblock(0)
>>> b.block_hash.hex()
'000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
Parameters
  • raw (BytesIO, bytes) – Raw serialize block

  • block_hash (bytes) – Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different than specified.

  • height (int) – Specify height if known. Will be derived from coinbase transaction if not provided.

  • parse_transactions (bool) – Indicate if transactions in raw block need to be parsed and converted to Transaction objects. Default is False

  • limit (int) – Maximum number of transactions to parse. Default is 0: parse all transactions. Only used if parse_transaction is set to True

  • network (str) – Name of network

Return Block

classmethod parse_bytes(raw_bytes, block_hash=None, height=None, parse_transactions=False, limit=0, network='bitcoin')[source]

Create Block object from raw serialized block in bytes or BytesIO format. Wrapper for parse_bytesio()

Get genesis block:

>>> from bitcoinlib.services.services import Service
>>> srv = Service()
>>> b = srv.getblock(0)
>>> b.block_hash.hex()
'000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
Parameters
  • raw_bytes (bytes) – Raw serialize block

  • block_hash (bytes) – Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different than specified.

  • height (int) – Specify height if known. Will be derived from coinbase transaction if not provided.

  • parse_transactions (bool) – Indicate if transactions in raw block need to be parsed and converted to Transaction objects. Default is False

  • limit (int) – Maximum number of transactions to parse. Default is 0: parse all transactions. Only used if parse_transaction is set to True

  • network (str) – Name of network

Return Block

classmethod parse_bytesio(raw, block_hash=None, height=None, parse_transactions=False, limit=0, network='bitcoin')[source]

Create Block object from raw serialized block in BytesIO format

Get genesis block:

>>> from bitcoinlib.services.services import Service
>>> srv = Service()
>>> b = srv.getblock(0)
>>> b.block_hash.hex()
'000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
Parameters
  • raw (BytesIO) – Raw serialize block

  • block_hash (bytes) – Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different than specified.

  • height (int) – Specify height if known. Will be derived from coinbase transaction if not provided.

  • parse_transactions (bool) – Indicate if transactions in raw block need to be parsed and converted to Transaction objects. Default is False

  • limit (int) – Maximum number of transactions to parse. Default is 0: parse all transactions. Only used if parse_transaction is set to True

  • network (str) – Name of network

Return Block

parse_transaction()[source]

Parse a single transaction from Block, if transaction data is available in txs_data attribute. Add Transaction object in Block and return the transaction

Return Tranasaction

parse_transaction_dict()[source]

Parse a single transaction from Block, if transaction data is available in txs_data attribute. Add Transaction object in Block and return the transaction

Return Transaction

parse_transactions(limit=0)[source]

Parse raw transactions from Block, if transaction data is available in txs_data attribute. Creates Transaction objects in Block.

Parameters

limit (int) – Maximum number of transactions to parse

Returns

parse_transactions_dict()[source]

Parse raw transactions from Block, if transaction data is available in txs_data attribute. Returns a list of transactions dictionaries.

Only works if transactions are not parsed yet with parse_transactions() or parse_transactions=True when creating a new block object.

Returns

serialize()[source]

Serialize raw block in bytes.

A block consists of a 80 bytes header: * version - 4 bytes * previous block - 32 bytes * merkle root - 32 bytes * timestamp - 4 bytes * bits - 4 bytes * nonce - 4 bytes

Followed by a list of raw serialized transactions.

Method will raise an error if one of the header fields is missing or has an incorrect size.

Return bytes

property target

Block target calculated from block’s bits. Block hash must be below this target. Used to calculate block difficulty.

Return int

property target_hex

Block target in hexadecimal string of 64 characters.

Return str

update_totals()[source]
property version_bin

Get the block version as binary string. Since BIP9 protocol changes are signaled by changing one of the 29 last bits of the version number.

>>> from bitcoinlib.services.services import Service
>>> srv = Service()
>>> b = srv.getblock(450001)
>>> print(b.version_bin)
00100000000000000000000000000010
Return str

version_bips()[source]

Extract version signaling information from the block’s version number.

The block version shows which software the miner used to create the block. Changes to the bitcoin protocol are described in Bitcoin Improvement Proposals (BIPs) and a miner shows which BIPs it supports in the block version number.

This method returns a list of BIP version number as string.

Example: This block uses the BIP9 versioning system and signals BIP141 (segwit) >>> from bitcoinlib.services.services import Service >>> srv = Service() >>> b = srv.getblock(450001) >>> print(b.version_bips()) [‘BIP9’, ‘BIP141’]

Return list of str