Skip to content

Eth leftover type hinting #1456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 13, 2018
2 changes: 1 addition & 1 deletion eth/beacon/db/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def _get_canonical_head(cls, db: BaseDB) -> BaseBeaconBlock:
canonical_head_hash = db[SchemaV1.make_canonical_head_hash_lookup_key()]
except KeyError:
raise CanonicalHeadNotFound("No canonical head set for this chain")
return cls._get_block_by_hash(db, canonical_head_hash)
return cls._get_block_by_hash(db, Hash32(canonical_head_hash))

def get_block_by_hash(self, block_hash: Hash32) -> BaseBeaconBlock:
return self._get_block_by_hash(self.db, block_hash)
Expand Down
2 changes: 1 addition & 1 deletion eth/beacon/state_machines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __init__(self, chaindb: BaseBeaconChainDB, block: BaseBeaconBlock=None) -> N
# Logging
#
@property
def logger(self):
def logger(self) -> logging.Logger:
return logging.getLogger('eth.beacon.state_machines.base.BeaconStateMachine.{0}'.format(
self.__class__.__name__
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

class SerenityCrystallizedState(CrystallizedState):
@classmethod
def from_crystallized_state(cls, crystallized_state):
def from_crystallized_state(cls,
crystallized_state: CrystallizedState
) -> "SerenityCrystallizedState":
return cls(
validators=crystallized_state.validators,
last_state_recalc=crystallized_state.last_state_recalc,
Expand Down
2 changes: 1 addition & 1 deletion eth/beacon/types/crystallized_states.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,5 @@ def num_validators(self) -> int:
return len(self.validators)

@property
def num_crosslink_records(self):
def num_crosslink_records(self) -> int:
return len(self.crosslink_records)
111 changes: 67 additions & 44 deletions eth/chains/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
cast,
Dict,
Generator,
Iterable,
Iterator,
List,
Optional,
Tuple,
Type,
TYPE_CHECKING,
Union,
TypeVar,
Generic,
)

import logging
Expand All @@ -32,6 +35,12 @@
encode_hex,
)

from eth.constants import (
BLANK_ROOT_HASH,
EMPTY_UNCLE_HASH,
MAX_UNCLE_DEPTH,
)

from eth.db.backends.base import BaseAtomicDB
from eth.db.chain import (
BaseChainDB,
Expand All @@ -40,11 +49,7 @@
from eth.db.header import (
HeaderDB,
)
from eth.constants import (
BLANK_ROOT_HASH,
EMPTY_UNCLE_HASH,
MAX_UNCLE_DEPTH,
)

from eth.estimators import (
get_gas_estimator,
)
Expand All @@ -53,15 +58,7 @@
TransactionNotFound,
VMNotFound,
)
from eth.utils.spoof import (
SpoofTransaction,
)
from eth.validation import (
validate_block_number,
validate_uint256,
validate_word,
validate_vm_configuration,
)

from eth.rlp.blocks import (
BaseBlock,
)
Expand All @@ -76,6 +73,13 @@
BaseTransaction,
BaseUnsignedTransaction,
)

from eth.typing import ( # noqa: F401
AccountState,
BaseOrSpoofTransaction,
StaticMethod,
)

from eth.utils.db import (
apply_state_dict,
)
Expand All @@ -88,9 +92,15 @@
from eth.utils.rlp import (
validate_imported_block_unchanged,
)
from eth.typing import (
AccountState,

from eth.validation import (
validate_block_number,
validate_uint256,
validate_word,
validate_vm_configuration,
)
from eth.vm.computation import BaseComputation
from eth.vm.state import BaseState # noqa: F401

from eth._warnings import catch_and_ignore_import_warning
with catch_and_ignore_import_warning():
Expand All @@ -107,7 +117,9 @@
)

if TYPE_CHECKING:
from eth.vm.base import BaseVM # noqa: F401
from eth.vm.base import ( # noqa: F401
BaseVM,
)


class BaseChain(Configurable, ABC):
Expand Down Expand Up @@ -196,11 +208,11 @@ def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeader:
raise NotImplementedError("Chain classes must implement this method")

@abstractmethod
def get_canonical_head(self):
def get_canonical_head(self) -> BlockHeader:
raise NotImplementedError("Chain classes must implement this method")

@abstractmethod
def get_score(self, block_hash):
def get_score(self, block_hash: Hash32) -> int:
raise NotImplementedError("Chain classes must implement this method")

#
Expand All @@ -227,11 +239,15 @@ def get_canonical_block_by_number(self, block_number: BlockNumber) -> BaseBlock:
raise NotImplementedError("Chain classes must implement this method")

@abstractmethod
def get_canonical_block_hash(self, block_number):
def get_canonical_block_hash(self, block_number: BlockNumber) -> Hash32:
raise NotImplementedError("Chain classes must implement this method")

@abstractmethod
def build_block_with_transactions(self, transactions, parent_header):
def build_block_with_transactions(
self,
transactions: Tuple[BaseTransaction, ...],
parent_header: BlockHeader=None
) -> Tuple[BaseBlock, Tuple[Receipt, ...], Tuple[BaseComputation, ...]]:
raise NotImplementedError("Chain classes must implement this method")

#
Expand Down Expand Up @@ -262,14 +278,14 @@ def get_canonical_transaction(self, transaction_hash: Hash32) -> BaseTransaction
@abstractmethod
def get_transaction_result(
self,
transaction: Union[BaseTransaction, SpoofTransaction],
transaction: BaseOrSpoofTransaction,
at_header: BlockHeader) -> bytes:
raise NotImplementedError("Chain classes must implement this method")

@abstractmethod
def estimate_gas(
self,
transaction: Union[BaseTransaction, SpoofTransaction],
transaction: BaseOrSpoofTransaction,
at_header: BlockHeader=None) -> int:
raise NotImplementedError("Chain classes must implement this method")

Expand Down Expand Up @@ -320,7 +336,7 @@ class Chain(BaseChain):
current block number.
"""
logger = logging.getLogger("eth.chain.chain.Chain")
gas_estimator = None # type: Callable
gas_estimator = None # type: StaticMethod[Callable[[BaseState, BaseOrSpoofTransaction], int]]

chaindb_class = ChainDB # type: Type[BaseChainDB]

Expand All @@ -335,7 +351,7 @@ def __init__(self, base_db: BaseAtomicDB) -> None:
self.chaindb = self.get_chaindb_class()(base_db)
self.headerdb = HeaderDB(base_db)
if self.gas_estimator is None:
self.gas_estimator = get_gas_estimator() # type: ignore
self.gas_estimator = get_gas_estimator()

#
# Helpers
Expand Down Expand Up @@ -414,7 +430,9 @@ def get_vm(self, at_header: BlockHeader=None) -> 'BaseVM':
#
# Header API
#
def create_header_from_parent(self, parent_header, **header_params):
def create_header_from_parent(self,
parent_header: BlockHeader,
**header_params: HeaderParams) -> BlockHeader:
"""
Passthrough helper to the VM class of the block descending from the
given header.
Expand All @@ -432,15 +450,15 @@ def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeader:
validate_word(block_hash, title="Block Hash")
return self.chaindb.get_block_header_by_hash(block_hash)

def get_canonical_head(self):
def get_canonical_head(self) -> BlockHeader:
"""
Returns the block header at the canonical chain head.

Raises CanonicalHeadNotFound if there's no head defined for the canonical chain.
"""
return self.chaindb.get_canonical_head()

def get_score(self, block_hash):
def get_score(self, block_hash: Hash32) -> int:
"""
Returns the difficulty score of the block with the given hash.

Expand Down Expand Up @@ -498,7 +516,7 @@ def get_block_by_hash(self, block_hash: Hash32) -> BaseBlock:
block_header = self.get_block_header_by_hash(block_hash)
return self.get_block_by_header(block_header)

def get_block_by_header(self, block_header):
def get_block_by_header(self, block_header: BlockHeader) -> BaseBlock:
"""
Returns the requested block as specified by the block header.
"""
Expand All @@ -524,7 +542,11 @@ def get_canonical_block_hash(self, block_number: BlockNumber) -> Hash32:
"""
return self.chaindb.get_canonical_block_hash(block_number)

def build_block_with_transactions(self, transactions, parent_header=None):
def build_block_with_transactions(
self,
transactions: Tuple[BaseTransaction, ...],
parent_header: BlockHeader=None
) -> Tuple[BaseBlock, Tuple[Receipt, ...], Tuple[BaseComputation, ...]]:
"""
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,
Expand Down Expand Up @@ -554,12 +576,12 @@ def get_canonical_transaction(self, transaction_hash: Hash32) -> BaseTransaction
found in the main chain.
"""
(block_num, index) = self.chaindb.get_transaction_index(transaction_hash)
VM = self.get_vm_class_for_block_number(block_num)
VM_class = self.get_vm_class_for_block_number(block_num)

transaction = self.chaindb.get_transaction_by_index(
block_num,
index,
VM.get_transaction_class(),
VM_class.get_transaction_class(),
)

if transaction.hash == transaction_hash:
Expand Down Expand Up @@ -603,22 +625,21 @@ def create_unsigned_transaction(self,
#
def get_transaction_result(
self,
transaction: Union[BaseTransaction, SpoofTransaction],
transaction: BaseOrSpoofTransaction,
at_header: BlockHeader) -> bytes:
"""
Return the result of running the given transaction.
This is referred to as a `call()` in web3.
"""
with self.get_vm(at_header).state_in_temp_block() as state:
# Ignore is to not bleed the SpoofTransaction deeper into the code base
computation = state.costless_execute_transaction(transaction) # type: ignore
computation = state.costless_execute_transaction(transaction)

computation.raise_if_error()
return computation.output

def estimate_gas(
self,
transaction: Union[BaseTransaction, SpoofTransaction],
transaction: BaseOrSpoofTransaction,
at_header: BlockHeader=None) -> int:
"""
Returns an estimation of the amount of gas the given transaction will
Expand Down Expand Up @@ -689,8 +710,8 @@ def import_block(self,
# Validation API
#
def validate_receipt(self, receipt: Receipt, at_header: BlockHeader) -> None:
VM = self.get_vm_class(at_header)
VM.validate_receipt(receipt)
VM_class = self.get_vm_class(at_header)
VM_class.validate_receipt(receipt)

def validate_block(self, block: BaseBlock) -> None:
"""
Expand All @@ -704,18 +725,18 @@ def validate_block(self, block: BaseBlock) -> None:
"""
if block.is_genesis:
raise ValidationError("Cannot validate genesis block this way")
VM = self.get_vm_class_for_block_number(BlockNumber(block.number))
VM_class = self.get_vm_class_for_block_number(BlockNumber(block.number))
parent_block = self.get_block_by_hash(block.header.parent_hash)
VM.validate_header(block.header, parent_block.header, check_seal=True)
VM_class.validate_header(block.header, parent_block.header, check_seal=True)
self.validate_uncles(block)
self.validate_gaslimit(block.header)

def validate_seal(self, header: BlockHeader) -> None:
"""
Validate the seal on the given header.
"""
VM = self.get_vm_class_for_block_number(BlockNumber(header.block_number))
VM.validate_seal(header)
VM_class = self.get_vm_class_for_block_number(BlockNumber(header.block_number))
VM_class.validate_seal(header)

def validate_gaslimit(self, header: BlockHeader) -> None:
"""
Expand Down Expand Up @@ -830,7 +851,7 @@ def validate_chain(


@to_set
def _extract_uncle_hashes(blocks):
def _extract_uncle_hashes(blocks: Iterable[BaseBlock]) -> Iterable[Hash32]:
for block in blocks:
for uncle in block.uncles:
yield uncle.hash
Expand All @@ -843,7 +864,9 @@ def __init__(self, base_db: BaseAtomicDB, header: BlockHeader=None) -> None:
super().__init__(base_db)
self.header = self.ensure_header(header)

def apply_transaction(self, transaction):
def apply_transaction(self,
transaction: BaseTransaction
) -> Tuple[BaseBlock, Receipt, BaseComputation]:
"""
Applies the transaction to the current tip block.

Expand Down
Loading