Skip to content

Commit

Permalink
Correct type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgdorf committed Nov 8, 2018
1 parent dc842c1 commit 7d0f188
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 49 deletions.
3 changes: 2 additions & 1 deletion eth/chains/base.py
Expand Up @@ -610,7 +610,8 @@ def get_transaction_result(
This is referred to as a `call()` in web3.
"""
with self.get_vm(at_header).state_in_temp_block() as state:
computation = state.costless_execute_transaction(transaction)
# Ignore is to not bleed the SpoofTransaction deeper into the code base
computation = state.costless_execute_transaction(transaction) # type: ignore

computation.raise_if_error()
return computation.output
Expand Down
3 changes: 1 addition & 2 deletions eth/tools/fixtures/fillers/_utils.py
Expand Up @@ -3,7 +3,6 @@

from typing import (
Any,
cast,
Dict,
List,
Tuple,
Expand Down Expand Up @@ -73,7 +72,7 @@ def calc_state_root(state: AccountState, account_db_class: Type[BaseAccountDB])

def generate_random_keypair() -> Tuple[bytes, Address]:
key_object = keys.PrivateKey(pad32(int_to_big_endian(random.getrandbits(8 * 32))))
return key_object.to_bytes(), cast(Address, key_object.public_key.to_canonical_address())
return key_object.to_bytes(), Address(key_object.public_key.to_canonical_address())


def generate_random_address() -> Address:
Expand Down
5 changes: 3 additions & 2 deletions eth/utils/transactions.py
Expand Up @@ -11,6 +11,7 @@
ValidationError,
)
from eth.typing import (
Address,
VRS,
)
from eth.utils.numeric import (
Expand Down Expand Up @@ -91,7 +92,7 @@ def validate_transaction_signature(transaction: BaseTransaction) -> None:
raise ValidationError("Invalid Signature")


def extract_transaction_sender(transaction: BaseTransaction) -> bytes:
def extract_transaction_sender(transaction: BaseTransaction) -> Address:
if is_eip_155_signed_transaction(transaction):
if is_even(transaction.v):
v = 28
Expand All @@ -108,4 +109,4 @@ def extract_transaction_sender(transaction: BaseTransaction) -> bytes:
message = transaction.get_message_for_signing()
public_key = signature.recover_public_key_from_msg(message)
sender = public_key.to_canonical_address()
return sender
return Address(sender)
26 changes: 15 additions & 11 deletions eth/vm/base.py
Expand Up @@ -132,7 +132,7 @@ def make_receipt(self,
base_header: BlockHeader,
transaction: BaseTransaction,
computation: BaseComputation,
state: BaseState) -> Any:
state: BaseState) -> Receipt:
"""
Generate the receipt resulting from applying the transaction.
Expand Down Expand Up @@ -161,26 +161,26 @@ def set_block_transactions(self,
base_block: BaseBlock,
new_header: BlockHeader,
transactions: Tuple[BaseTransaction, ...],
receipts: Tuple[Receipt, ...]) -> Any:
receipts: Tuple[Receipt, ...]) -> BaseBlock:
raise NotImplementedError("VM classes must implement this method")

#
# Finalization
#
@abstractmethod
def finalize_block(self, block: BaseBlock) -> Any:
def finalize_block(self, block: BaseBlock) -> BaseBlock:
raise NotImplementedError("VM classes must implement this method")

@abstractmethod
def pack_block(self, block: BaseBlock, *args: Any, **kwargs: Any) -> Any:
def pack_block(self, block: BaseBlock, *args: Any, **kwargs: Any) -> BaseBlock:
raise NotImplementedError("VM classes must implement this method")

#
# Headers
#
@classmethod
@abstractmethod
def compute_difficulty(cls, parent_header: BlockHeader, timestamp: int) -> Any:
def compute_difficulty(cls, parent_header: BlockHeader, timestamp: int) -> int:
"""
Compute the difficulty for a block header.
Expand All @@ -190,7 +190,7 @@ def compute_difficulty(cls, parent_header: BlockHeader, timestamp: int) -> Any:
raise NotImplementedError("VM classes must implement this method")

@abstractmethod
def configure_header(self, **header_params: Any) -> Any:
def configure_header(self, **header_params: Any) -> BlockHeader:
"""
Setup the current header with the provided parameters. This can be
used to set fields like the gas limit or timestamp to value different
Expand All @@ -200,7 +200,9 @@ def configure_header(self, **header_params: Any) -> Any:

@classmethod
@abstractmethod
def create_header_from_parent(cls, parent_header: BlockHeader, **header_params: Any) -> Any:
def create_header_from_parent(cls,
parent_header: BlockHeader,
**header_params: Any) -> BlockHeader:
"""
Creates and initializes a new block header from the provided
`parent_header`.
Expand All @@ -214,7 +216,7 @@ def create_header_from_parent(cls, parent_header: BlockHeader, **header_params:
@abstractmethod
def generate_block_from_parent_header_and_coinbase(cls,
parent_header: BlockHeader,
coinbase: Address) -> Any:
coinbase: Address) -> BaseBlock:
raise NotImplementedError("VM classes must implement this method")

@classmethod
Expand Down Expand Up @@ -248,7 +250,9 @@ def get_nephew_reward(cls) -> int:
@classmethod
@abstractmethod
@to_tuple
def get_prev_hashes(cls, last_block_hash: Hash32, chaindb: BaseChainDB) -> Any:
def get_prev_hashes(cls,
last_block_hash: Hash32,
chaindb: BaseChainDB) -> Optional[Iterable[Hash32]]:
raise NotImplementedError("VM classes must implement this method")

@staticmethod
Expand Down Expand Up @@ -334,12 +338,12 @@ def validate_uncle(
#
@classmethod
@abstractmethod
def get_state_class(cls) -> Any:
def get_state_class(cls) -> Type[BaseState]:
raise NotImplementedError("VM classes must implement this method")

@abstractmethod
@contextlib.contextmanager
def state_in_temp_block(self) -> Any:
def state_in_temp_block(self) -> Iterator[BaseState]:
raise NotImplementedError("VM classes must implement this method")


Expand Down
2 changes: 1 addition & 1 deletion eth/vm/computation.py
Expand Up @@ -115,7 +115,7 @@ class BaseComputation(Configurable, ABC):
accounts_to_delete = None # type: Dict[bytes, bytes]

# VM configuration
opcodes = None # type: Dict[int, Opcode]
opcodes = None # type: Dict[int, Any]
_precompiles = None # type: Dict[Address, Callable[['BaseComputation'], Any]]

logger = cast(TraceLogger, logging.getLogger('eth.vm.computation.Computation'))
Expand Down
2 changes: 1 addition & 1 deletion eth/vm/forks/frontier/computation.py
Expand Up @@ -41,7 +41,7 @@ class FrontierComputation(BaseComputation):
Inherits from :class:`~eth.vm.computation.BaseComputation`
"""
# Override
opcodes = FRONTIER_OPCODES # type: ignore # Mypy doesn't allow overwrite type
opcodes = FRONTIER_OPCODES
_precompiles = FRONTIER_PRECOMPILES

def apply_message(self) -> BaseComputation:
Expand Down
3 changes: 1 addition & 2 deletions eth/vm/forks/frontier/transactions.py
@@ -1,5 +1,4 @@
import rlp
from typing import cast

from eth_keys.datatypes import PrivateKey

Expand Down Expand Up @@ -72,7 +71,7 @@ def check_signature_validity(self) -> None:
validate_transaction_signature(self)

def get_sender(self) -> Address:
return cast(Address, extract_transaction_sender(self))
return extract_transaction_sender(self)

def get_intrinsic_gas(self) -> int:
return _get_frontier_intrinsic_gas(self.data)
Expand Down
5 changes: 2 additions & 3 deletions eth/vm/logic/call.py
Expand Up @@ -4,7 +4,6 @@
)

from typing import (
Any,
Tuple,
)

Expand Down Expand Up @@ -38,11 +37,11 @@ def compute_msg_extra_gas(self,
computation: BaseComputation,
gas: int,
to: Address,
value: int) -> Any:
value: int) -> int:
raise NotImplementedError("Must be implemented by subclasses")

@abstractmethod
def get_call_params(self, computation: BaseComputation) -> Any:
def get_call_params(self, computation: BaseComputation) -> CallParams:
raise NotImplementedError("Must be implemented by subclasses")

def compute_msg_gas(self,
Expand Down
10 changes: 1 addition & 9 deletions eth/vm/logic/context.py
@@ -1,13 +1,5 @@
from eth import constants

from typing import (
cast,
)

from eth_typing import (
Hash32,
)

from eth.exceptions import (
OutOfBoundsRead,
)
Expand Down Expand Up @@ -54,7 +46,7 @@ def calldataload(computation: BaseComputation) -> None:
padded_value = value.ljust(32, b'\x00')
normalized_value = padded_value.lstrip(b'\x00')

computation.stack_push(cast(Hash32, normalized_value))
computation.stack_push(normalized_value)


def calldatasize(computation: BaseComputation) -> None:
Expand Down
6 changes: 1 addition & 5 deletions eth/vm/logic/memory.py
@@ -1,9 +1,5 @@
from typing import cast

from eth import constants

from eth_typing import Hash32

from eth.vm.computation import BaseComputation


Expand Down Expand Up @@ -37,7 +33,7 @@ def mload(computation: BaseComputation) -> None:
computation.extend_memory(start_position, 32)

value = computation.memory_read(start_position, 32)
computation.stack_push(cast(Hash32, value))
computation.stack_push(value)


def msize(computation: BaseComputation) -> None:
Expand Down
8 changes: 1 addition & 7 deletions eth/vm/logic/stack.py
@@ -1,13 +1,7 @@
import functools

from typing import (
cast,
)

from eth import constants

from eth_typing import Hash32

from eth.vm.computation import BaseComputation


Expand All @@ -22,7 +16,7 @@ def push_XX(computation: BaseComputation, size: int) -> None:
computation.stack_push(0)
else:
padded_value = raw_value.ljust(size, b'\x00')
computation.stack_push(cast(Hash32, padded_value))
computation.stack_push(padded_value)


push1 = functools.partial(push_XX, size=1)
Expand Down
9 changes: 4 additions & 5 deletions eth/vm/state.py
Expand Up @@ -5,13 +5,12 @@
import contextlib
import logging
from typing import ( # noqa: F401
Any,
Callable,
cast,
Callable,
Iterator,
Tuple,
Type,
TYPE_CHECKING
TYPE_CHECKING,
)
from uuid import UUID

Expand Down Expand Up @@ -200,7 +199,7 @@ def get_ancestor_hash(self, block_number: int) -> Hash32:
ancestor_depth >= len(self.execution_context.prev_hashes)
)
if is_ancestor_depth_out_of_range:
return cast(Hash32, b'')
return Hash32(b'')
ancestor_hash = self.execution_context.prev_hashes[ancestor_depth]
return ancestor_hash

Expand Down Expand Up @@ -275,7 +274,7 @@ def execute_transaction(self, transaction: 'BaseTransaction') -> 'BaseComputatio
raise NotImplementedError()

@abstractmethod
def validate_transaction(self, transaction: 'BaseTransaction') -> Any:
def validate_transaction(self, transaction: 'BaseTransaction') -> None:
raise NotImplementedError

@classmethod
Expand Down

0 comments on commit 7d0f188

Please sign in to comment.