Chain

BaseChain

class eth.chains.base.BaseChain

The base class for all Chain objects

classmethod get_vm_class(header: eth.abc.BlockHeaderAPI) → Type[eth.abc.VirtualMachineAPI]

Return the VM class for the given header

classmethod get_vm_class_for_block_number(block_number: NewType.<locals>.new_type) → Type[eth.abc.VirtualMachineAPI]

Return the VM class for the given block_number

validate_chain(root: eth.abc.BlockHeaderAPI, descendants: Tuple[eth.abc.BlockHeaderAPI, ...], seal_check_random_sample_rate: int = 1) → None

Validate that all of the descendents are valid, given that the root header is valid.

By default, check the seal validity (Proof-of-Work on Ethereum 1.x mainnet) of all headers. This can be expensive. Instead, check a random sample of seals using seal_check_random_sample_rate.

validate_chain_extension(headers: Tuple[eth.abc.BlockHeaderAPI, ...]) → None

Validate a chain of headers under the assumption that the entire chain of headers is present. Headers that are not already in the database must exist in headers. Calling this API is not a replacement for calling validate_chain(), it is an additional API to call at a different stage of header processing to enable consensus schemes where the consensus can not be verified out of order.

Chain

class eth.chains.base.Chain(base_db: eth.abc.AtomicDatabaseAPI)
chaindb_class

alias of eth.db.chain.ChainDB

consensus_context_class

alias of eth.consensus.context.ConsensusContext

build_block_with_transactions(transactions: Sequence[eth.abc.SignedTransactionAPI], parent_header: eth.abc.BlockHeaderAPI = None) → Tuple[eth.abc.BlockAPI, Tuple[eth.abc.ReceiptAPI, ...], Tuple[eth.abc.ComputationAPI, ...]]

Generate a block with the provided transactions. This does not import that block into your chain. If you want this new block in your chain, run import_block() with the result block from this method.

Parameters:
  • transactions – an iterable of transactions to insert to the block
  • parent_header – parent of the new block – or canonical head if None
Returns:

(new block, receipts, computations)

create_header_from_parent(parent_header: eth.abc.BlockHeaderAPI, **header_params) → eth.abc.BlockHeaderAPI

Passthrough helper to the VM class of the block descending from the given header.

create_transaction(*args, **kwargs) → eth.abc.SignedTransactionAPI

Passthrough helper to the current VM class.

create_unsigned_transaction(*, nonce: int, gas_price: int, gas: int, to: NewType.<locals>.new_type, value: int, data: bytes) → eth.abc.UnsignedTransactionAPI

Passthrough helper to the current VM class.

ensure_header(header: eth.abc.BlockHeaderAPI = None) → eth.abc.BlockHeaderAPI

Return header if it is not None, otherwise return the header of the canonical head.

estimate_gas(transaction: eth.abc.SignedTransactionAPI, at_header: eth.abc.BlockHeaderAPI = None) → int

Return an estimation of the amount of gas the given transaction will use if executed on top of the block specified by at_header.

classmethod from_genesis(base_db: eth.abc.AtomicDatabaseAPI, genesis_params: Dict[str, Union[int, None, NewType.<locals>.new_type, bytes, NewType.<locals>.new_type, NewType.<locals>.new_type]], genesis_state: Dict[NewType.<locals>.new_type, eth.typing.AccountDetails] = None) → eth.chains.base.BaseChain

Initialize the Chain from a genesis state.

classmethod from_genesis_header(base_db: eth.abc.AtomicDatabaseAPI, genesis_header: eth.abc.BlockHeaderAPI) → eth.chains.base.BaseChain

Initialize the chain from the genesis header.

get_ancestors(limit: int, header: eth.abc.BlockHeaderAPI) → Tuple[eth.abc.BlockAPI, ...]

Return limit number of ancestor blocks from the current canonical head.

get_block() → eth.abc.BlockAPI

Return the current block at the tip of the chain.

get_block_by_hash(block_hash: NewType.<locals>.new_type) → eth.abc.BlockAPI

Return the requested block as specified by block_hash.

Raises:
get_block_by_header(block_header: eth.abc.BlockHeaderAPI) → eth.abc.BlockAPI

Return the requested block as specified by the block_header.

Raises:eth.exceptions.BlockNotFound – if any part of the block body is missing
get_block_header_by_hash(block_hash: NewType.<locals>.new_type) → eth.abc.BlockHeaderAPI

Return the requested block header as specified by block_hash. Raise BlockNotFound if no block header with the given hash exists in the db.

get_canonical_block_by_number(block_number: NewType.<locals>.new_type) → eth.abc.BlockAPI

Return the block with the given block_number in the canonical chain.

Raise BlockNotFound if no block with the given block_number exists in the canonical chain.

get_canonical_block_hash(block_number: NewType.<locals>.new_type) → NewType.<locals>.new_type

Return the block hash with the given block_number in the canonical chain.

Raise BlockNotFound if there’s no block with the given number in the canonical chain.

get_canonical_block_header_by_number(block_number: NewType.<locals>.new_type) → eth.abc.BlockHeaderAPI

Return the block header with the given number in the canonical chain.

Raise HeaderNotFound if there’s no block header with the given number in the canonical chain.

get_canonical_head() → eth.abc.BlockHeaderAPI

Return the block header at the canonical chain head.

Raise CanonicalHeadNotFound if there’s no head defined for the canonical chain.

get_canonical_transaction(transaction_hash: NewType.<locals>.new_type) → eth.abc.SignedTransactionAPI

Return the requested transaction as specified by the transaction_hash from the canonical chain.

Raise TransactionNotFound if no transaction with the specified hash is found in the canonical chain.

get_canonical_transaction_by_index(block_number: NewType.<locals>.new_type, index: int) → eth.abc.SignedTransactionAPI

Return the requested transaction as specified by the block_number and index from the canonical chain.

Raise TransactionNotFound if no transaction exists at index at block_number in the canonical chain.

get_canonical_transaction_index(transaction_hash: NewType.<locals>.new_type) → Tuple[NewType.<locals>.new_type, int]

Return a 2-tuple of (block_number, transaction_index) indicating which block the given transaction can be found in and at what index in the block transactions.

Raise TransactionNotFound if the transaction does not exist in the canoncial chain.

classmethod get_chaindb_class() → Type[eth.abc.ChainDatabaseAPI]

Return the class for the used ChainDatabaseAPI.

get_score(block_hash: NewType.<locals>.new_type) → int

Return the difficulty score of the block with the given block_hash.

Raise HeaderNotFound if there is no matching block hash.

get_transaction_receipt(transaction_hash: NewType.<locals>.new_type) → eth.abc.ReceiptAPI

Return the requested receipt for the transaction as specified by the transaction_hash.

Raise ReceiptNotFound if not receipt for the specified transaction_hash is found in the canonical chain.

get_transaction_receipt_by_index(block_number: NewType.<locals>.new_type, index: int) → eth.abc.ReceiptAPI

Return the requested receipt for the transaction as specified by the block_number and index.

Raise ReceiptNotFound if not receipt for the specified block_number and index is found in the canonical chain.

get_transaction_result(transaction: eth.abc.SignedTransactionAPI, at_header: eth.abc.BlockHeaderAPI) → bytes

Return the result of running the given transaction. This is referred to as a call() in web3.

get_vm(at_header: eth.abc.BlockHeaderAPI = None) → eth.abc.VirtualMachineAPI

Return the VM instance for the given header.

import_block(block: eth.abc.BlockAPI, perform_validation: bool = True) → eth.abc.BlockImportResult

Import the given block and return a 3-tuple

  • the imported block
  • a tuple of blocks which are now part of the canonical chain.
  • a tuple of blocks which were canonical and now are no longer canonical.
validate_block(block: eth.abc.BlockAPI) → None

Validate a block that is either being mined or imported.

Since block validation (specifically the uncle validation) must have access to the ancestor blocks, this validation must occur at the Chain level.

Cannot be used to validate genesis block.

validate_receipt(receipt: eth.abc.ReceiptAPI, at_header: eth.abc.BlockHeaderAPI) → None

Validate the given receipt at the given header.

validate_seal(header: eth.abc.BlockHeaderAPI) → None

Validate the seal on the given header.

validate_uncles(block: eth.abc.BlockAPI) → None

Validate the uncles for the given block.

MiningChain

class eth.chains.base.MiningChain(base_db: eth.abc.AtomicDatabaseAPI, header: eth.abc.BlockHeaderAPI = None)
apply_transaction(transaction: eth.abc.SignedTransactionAPI) → Tuple[eth.abc.BlockAPI, eth.abc.ReceiptAPI, eth.abc.ComputationAPI]

Apply the transaction to the current tip block.

WARNING: ReceiptAPI and Transaction trie generation is computationally heavy and incurs significant performance overhead.

get_vm(at_header: eth.abc.BlockHeaderAPI = None) → eth.abc.VirtualMachineAPI

Return the VM instance for the given header.

import_block(block: eth.abc.BlockAPI, perform_validation: bool = True) → eth.abc.BlockImportResult

Import the given block and return a 3-tuple

  • the imported block
  • a tuple of blocks which are now part of the canonical chain.
  • a tuple of blocks which were canonical and now are no longer canonical.
mine_all(transactions: Sequence[eth.abc.SignedTransactionAPI], *args, parent_header: eth.abc.BlockHeaderAPI = None, **kwargs) → Tuple[eth.abc.BlockImportResult, Tuple[eth.abc.ReceiptAPI, ...], Tuple[eth.abc.ComputationAPI, ...]]

Build a block with the given transactions, and mine it.

Optionally, supply the parent block header to mine on top of.

This is much faster than individually running apply_transaction() and then mine_block().

mine_block(*args, **kwargs) → eth.abc.BlockAPI

Mine whatever transactions have been incrementally applied so far.

mine_block_extended(*args, **kwargs) → eth.abc.BlockAndMetaWitness

Just like mine_block(), but includes extra returned info. Currently, the only extra info returned is the MetaWitness.

set_header_timestamp(timestamp: int) → None

Set the timestamp of the pending header to mine.

This is mostly useful for testing, as the timestamp will be chosen automatically if this method is not called.