teos.block_processor

class BlockProcessor(btc_connect_params)[source]

Bases: object

The BlockProcessor contains methods related to the blockchain. Most of its methods require communication with bitcoind.

Parameters

btc_connect_params (dict) – a dictionary with the parameters to connect to bitcoind (rpc user, rpc password, host and port).

logger

the logger for this component.

Type

Logger

decode_raw_transaction(raw_tx)[source]

Deserializes a given raw transaction (hex encoded) and builds a dictionary representing it with all the associated metadata given by bitcoind (e.g. confirmation count).

Parameters

raw_tx (str) – the hex representation of the transaction.

Returns

The decoding of the given raw_tx if the transaction is well formatted.

Return type

dict

Raises

InvalidTransactionFormat – If the provided ``raw_tx` has invalid format.

find_last_common_ancestor(last_known_block_hash)[source]

Finds the last common ancestor between the current best chain tip and the last block known by us (older block).

This is useful to recover from a chain fork happening while offline (crash/shutdown).

Parameters

last_known_block_hash (str) – the hash of the last know block.

Returns

A tuple (str, list) where the first item contains the hash of the last common ancestor and the second item contains the list of transactions from last_known_block_hash to last_common_ancestor.

Return type

tuple

get_best_block_hash()[source]

Gets the hash of the current best chain tip.

Returns

The hash of the block if it can be found.

Returns None otherwise (not even sure this can actually happen).

Return type

str or None

get_block(block_hash)[source]

Gets a block given a block hash by querying bitcoind.

Parameters

block_hash (str) – the block hash to be queried.

Returns

A dictionary containing the requested block data if the block is found.

Returns None otherwise.

Return type

dict or None

get_block_count()[source]

Gets the block count of the best chain.

Returns

The count of the best chain if it can be computed.

Returns None otherwise (not even sure this can actually happen).

Return type

int or None

get_distance_to_tip(target_block_hash)[source]

Compute the distance between a given block hash and the best chain tip.

Parameters

target_block_hash (str) – the hash of the target block (the one to compute the distance form the tip).

Returns

The distance between the target and the best chain tip is the target block can be found on the blockchain.

Returns None otherwise.

Return type

int or None

get_missed_blocks(last_know_block_hash)[source]

Gets the blocks between the current best chain tip and a given block hash (last_know_block_hash).

This method is used to fetch all the missed information when recovering from a crash.

Parameters

last_know_block_hash (str) – the hash of the last known block.

Returns

A list of blocks between the last given block and the current best chain tip, starting from the child of last_know_block_hash.

Return type

list

is_block_in_best_chain(block_hash)[source]

Checks whether a given block is on the best chain or not. Blocks are identified by block_hash.

A block that is not in the best chain will either not exists (block = None) or have a confirmation count of -1 (implying that the block was forked out or the chain never grew from that one).

Parameters

block_hash (str) – the hash of the block to be checked.

Returns

True if the block is on the best chain, False otherwise.

Return type

bool

Raises

KeyError – If the block cannot be found in the blockchain.

exception InvalidTransactionFormat(msg, **kwargs)[source]

Bases: common.exceptions.BasicException

Raised when a transaction is not properly formatted.