Skip to content

Enables eth.vm Type Hinting #1451

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 5 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion eth/_warnings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from contextlib import contextmanager
from typing import Iterator
import warnings


# TODO: drop once https://github.com/cython/cython/issues/1720 is resolved
@contextmanager
def catch_and_ignore_import_warning():
def catch_and_ignore_import_warning() -> Iterator[None]:
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=ImportWarning)
yield
33 changes: 26 additions & 7 deletions eth/chains/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import logging

from eth_typing import (
Address,
BlockNumber,
Hash32,
)
Expand Down Expand Up @@ -241,9 +242,14 @@ def create_transaction(self, *args: Any, **kwargs: Any) -> BaseTransaction:
raise NotImplementedError("Chain classes must implement this method")

@abstractmethod
def create_unsigned_transaction(self,
*args: Any,
**kwargs: Any) -> BaseUnsignedTransaction:
def create_unsigned_transaction(cls,
*,
nonce: int,
gas_price: int,
gas: int,
to: Address,
value: int,
data: bytes) -> BaseUnsignedTransaction:
raise NotImplementedError("Chain classes must implement this method")

@abstractmethod
Expand Down Expand Up @@ -573,12 +579,24 @@ def create_transaction(self, *args: Any, **kwargs: Any) -> BaseTransaction:
return self.get_vm().create_transaction(*args, **kwargs)

def create_unsigned_transaction(self,
*args: Any,
**kwargs: Any) -> BaseUnsignedTransaction:
*,
nonce: int,
gas_price: int,
gas: int,
to: Address,
value: int,
data: bytes) -> BaseUnsignedTransaction:
"""
Passthrough helper to the current VM class.
"""
return self.get_vm().create_unsigned_transaction(*args, **kwargs)
return self.get_vm().create_unsigned_transaction(
nonce=nonce,
gas_price=gas_price,
gas=gas,
to=to,
value=value,
data=data,
)

#
# Execution API
Expand All @@ -592,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a better solution here is an abstract interface for the transaction that SpoofTransaction and the main RLP Transaction both implement.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That ignore is gone in the final version that was merged.


computation.raise_if_error()
return computation.output
Expand Down
4 changes: 2 additions & 2 deletions eth/db/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def get(self, key: bytes) -> bytes:
raise NotImplementedError("ChainDB classes must implement this method")

@abstractmethod
def persist_trie_data_dict(self, trie_data_dict: Dict[bytes, bytes]) -> None:
def persist_trie_data_dict(self, trie_data_dict: Dict[Hash32, bytes]) -> None:
raise NotImplementedError("ChainDB classes must implement this method")


Expand Down Expand Up @@ -463,7 +463,7 @@ def get(self, key: bytes) -> bytes:
"""
return self.db[key]

def persist_trie_data_dict(self, trie_data_dict: Dict[bytes, bytes]) -> None:
def persist_trie_data_dict(self, trie_data_dict: Dict[Hash32, bytes]) -> None:
"""
Store raw trie data to db from a dict
"""
Expand Down
2 changes: 1 addition & 1 deletion eth/rlp/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def from_parent(cls,
return header

def create_execution_context(
self, prev_hashes: Union[Tuple[bytes], Tuple[bytes, bytes]]) -> ExecutionContext:
self, prev_hashes: Tuple[Hash32, ...]) -> ExecutionContext:

return ExecutionContext(
coinbase=self.coinbase,
Expand Down
4 changes: 3 additions & 1 deletion eth/rlp/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
binary,
)

from typing import List

from .sedes import (
address,
int32,
Expand All @@ -17,7 +19,7 @@ class Log(rlp.Serializable):
('data', binary)
]

def __init__(self, address: bytes, topics: bytes, data: bytes) -> None:
def __init__(self, address: bytes, topics: List[int], data: bytes) -> None:
super().__init__(address, topics, data)

@property
Expand Down
18 changes: 12 additions & 6 deletions eth/rlp/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
ABC,
abstractmethod
)
from typing import (
Any,
)

import rlp
from rlp.sedes import (
Expand All @@ -17,7 +14,9 @@
)

from eth_hash.auto import keccak

from eth_keys.datatypes import (
PrivateKey
)
from eth_utils import (
ValidationError,
)
Expand Down Expand Up @@ -150,7 +149,14 @@ def get_message_for_signing(self) -> bytes:

@classmethod
@abstractmethod
def create_unsigned_transaction(self, *args: Any, **kwargs: Any) -> 'BaseTransaction':
def create_unsigned_transaction(cls,
*,
nonce: int,
gas_price: int,
gas: int,
to: Address,
value: int,
data: bytes) -> 'BaseUnsignedTransaction':
"""
Create an unsigned transaction.
"""
Expand All @@ -171,7 +177,7 @@ class BaseUnsignedTransaction(rlp.Serializable, BaseTransactionMethods, ABC):
# API that must be implemented by all Transaction subclasses.
#
@abstractmethod
def as_signed_transaction(self, private_key: bytes) -> 'BaseTransaction':
def as_signed_transaction(self, private_key: PrivateKey) -> 'BaseTransaction':
"""
Return a version of this transaction which has been signed using the
provided `private_key`
Expand Down
3 changes: 2 additions & 1 deletion eth/tools/_utils/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from typing import (
Iterable,
List,
Tuple,
)

Expand All @@ -14,7 +15,7 @@
from eth.rlp.logs import Log


def hash_log_entries(log_entries: Iterable[Tuple[bytes, bytes, bytes]]) -> Hash32:
def hash_log_entries(log_entries: Iterable[Tuple[bytes, List[int], bytes]]) -> Hash32:
"""
Helper function for computing the RLP hash of the logs from transaction
execution.
Expand Down
2 changes: 1 addition & 1 deletion eth/tools/fixtures/fillers/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,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(), 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
3 changes: 2 additions & 1 deletion eth/tools/fixtures/fillers/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Any,
Dict,
Iterable,
List,
Tuple,
Union,
)
Expand All @@ -28,7 +29,7 @@ def fill_vm_test(
call_creates: Any=None,
gas_price: Union[int, str]=None,
gas_remaining: Union[int, str]=0,
logs: Iterable[Tuple[bytes, bytes, bytes]]=None,
logs: Iterable[Tuple[bytes, List[int], bytes]]=None,
output: bytes=b"") -> Dict[str, Dict[str, Any]]:

test_name = get_test_name(filler)
Expand Down
6 changes: 5 additions & 1 deletion eth/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
TYPE_CHECKING,
)

from eth_typing import (
Hash32,
)

from eth.db.account import (
BaseAccountDB,
)
Expand All @@ -24,7 +28,7 @@ def get_parent_header(block_header: BlockHeader, db: 'BaseChainDB') -> BlockHead
return db.get_block_header_by_hash(block_header.parent_hash)


def get_block_header_by_hash(block_hash: BlockHeader, db: 'BaseChainDB') -> BlockHeader:
def get_block_header_by_hash(block_hash: Hash32, db: 'BaseChainDB') -> BlockHeader:
"""
Returns the header for the parent block.
"""
Expand Down
5 changes: 3 additions & 2 deletions eth/utils/transactions.py
Original file line number Diff line number Diff line change
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)
Loading