Skip to content

Commit ff977b3

Browse files
committed
Add Type Hinting for eth.vm.forks
1 parent 931900d commit ff977b3

26 files changed

+295
-112
lines changed

eth/vm/forks/byzantium/__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
MAX_UNCLE_DEPTH,
1616
)
1717
from eth.rlp.blocks import BaseBlock # noqa: F401
18+
from eth.rlp.headers import BlockHeader
1819
from eth.rlp.receipts import Receipt
20+
from eth.rlp.transactions import BaseTransaction
1921
from eth.validation import (
2022
validate_lte,
2123
)
2224
from eth.vm.forks.spurious_dragon import SpuriousDragonVM
2325
from eth.vm.forks.frontier import make_frontier_receipt
26+
from eth.vm.computation import BaseComputation
2427
from eth.vm.state import BaseState # noqa: F401
2528

2629
from .blocks import ByzantiumBlock
@@ -37,7 +40,10 @@
3740
from .state import ByzantiumState
3841

3942

40-
def make_byzantium_receipt(base_header, transaction, computation, state):
43+
def make_byzantium_receipt(base_header: BlockHeader,
44+
transaction: BaseTransaction,
45+
computation: BaseComputation,
46+
state: BaseState) -> Receipt:
4147
frontier_receipt = make_frontier_receipt(base_header, transaction, computation, state)
4248

4349
if computation.is_error:
@@ -49,7 +55,7 @@ def make_byzantium_receipt(base_header, transaction, computation, state):
4955

5056

5157
@curry
52-
def get_uncle_reward(block_reward, block_number, uncle):
58+
def get_uncle_reward(block_reward: int, block_number: int, uncle: BaseBlock) -> int:
5359
block_number_delta = block_number - uncle.block_number
5460
validate_lte(block_number_delta, MAX_UNCLE_DEPTH)
5561
return (8 - block_number_delta) * block_reward // 8
@@ -70,10 +76,10 @@ class ByzantiumVM(SpuriousDragonVM):
7076
_state_class = ByzantiumState # type: Type[BaseState]
7177

7278
# Methods
73-
create_header_from_parent = staticmethod(create_byzantium_header_from_parent)
74-
compute_difficulty = staticmethod(compute_byzantium_difficulty)
79+
create_header_from_parent = staticmethod(create_byzantium_header_from_parent) # type: ignore
80+
compute_difficulty = staticmethod(compute_byzantium_difficulty) # type: ignore
7581
configure_header = configure_byzantium_header
76-
make_receipt = staticmethod(make_byzantium_receipt)
82+
make_receipt = staticmethod(make_byzantium_receipt) # type: ignore
7783
# Separated into two steps due to mypy bug of staticmethod.
7884
# https://github.com/python/mypy/issues/5530
7985
get_uncle_reward = get_uncle_reward(EIP649_BLOCK_REWARD)
@@ -93,5 +99,5 @@ def validate_receipt(cls, receipt: Receipt) -> None:
9399
)
94100

95101
@staticmethod
96-
def get_block_reward():
102+
def get_block_reward() -> int:
97103
return EIP649_BLOCK_REWARD

eth/vm/forks/byzantium/opcodes.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33

44
from cytoolz import merge
55

6+
from typing import (
7+
Any,
8+
Callable,
9+
)
10+
611
from eth import constants
712

813
from eth.exceptions import (
914
WriteProtection,
1015
)
1116
from eth.vm import mnemonics
1217
from eth.vm import opcode_values
18+
from eth.vm.computation import BaseComputation
1319
from eth.vm.forks.tangerine_whistle.constants import (
1420
GAS_CALL_EIP150,
1521
GAS_SELFDESTRUCT_EIP150
@@ -26,9 +32,9 @@
2632
from eth.vm.forks.spurious_dragon.opcodes import SPURIOUS_DRAGON_OPCODES
2733

2834

29-
def ensure_no_static(opcode_fn):
35+
def ensure_no_static(opcode_fn: Callable[..., Any]) -> Callable[..., Any]:
3036
@functools.wraps(opcode_fn)
31-
def inner(computation):
37+
def inner(computation: BaseComputation) -> Callable[..., Any]:
3238
if computation.msg.is_static:
3339
raise WriteProtection("Cannot modify state while inside of a STATICCALL context")
3440
return opcode_fn(computation)

eth/vm/forks/byzantium/transactions.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
1-
from eth.vm.forks.spurious_dragon.transactions import (
2-
SpuriousDragonTransaction,
3-
SpuriousDragonUnsignedTransaction,
1+
from eth_keys.datatypes import PrivateKey
2+
from eth_typing import Address
3+
4+
from eth.rlp.transactions import (
5+
BaseTransaction,
6+
BaseUnsignedTransaction,
47
)
58

69
from eth.utils.transactions import (
710
create_transaction_signature,
811
)
912

13+
from eth.vm.forks.spurious_dragon.transactions import (
14+
SpuriousDragonTransaction,
15+
SpuriousDragonUnsignedTransaction,
16+
)
17+
1018

1119
class ByzantiumTransaction(SpuriousDragonTransaction):
1220
@classmethod
13-
def create_unsigned_transaction(cls, *, nonce, gas_price, gas, to, value, data):
21+
def create_unsigned_transaction(cls, # type: ignore
22+
*,
23+
nonce: int,
24+
gas_price: int,
25+
gas: int,
26+
to: Address,
27+
value: int,
28+
data: bytes) -> BaseTransaction:
1429
return ByzantiumUnsignedTransaction(nonce, gas_price, gas, to, value, data)
1530

1631

1732
class ByzantiumUnsignedTransaction(SpuriousDragonUnsignedTransaction):
18-
def as_signed_transaction(self, private_key, chain_id=None):
33+
def as_signed_transaction(self, # type: ignore
34+
private_key: PrivateKey,
35+
chain_id: int=None) -> BaseUnsignedTransaction:
1936
v, r, s = create_transaction_signature(self, private_key, chain_id=chain_id)
2037
return ByzantiumTransaction(
2138
nonce=self.nonce,

eth/vm/forks/constantinople/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class ConstantinopleVM(ByzantiumVM):
2828
_state_class = ConstantinopleState # type: Type[BaseState]
2929

3030
# Methods
31-
create_header_from_parent = staticmethod(create_constantinople_header_from_parent)
32-
compute_difficulty = staticmethod(compute_constantinople_difficulty)
31+
create_header_from_parent = staticmethod(create_constantinople_header_from_parent) # type: ignore # noqa: E501
32+
compute_difficulty = staticmethod(compute_constantinople_difficulty) # type: ignore
3333
configure_header = configure_constantinople_header
3434
get_uncle_reward = staticmethod(get_uncle_reward(EIP1234_BLOCK_REWARD))
3535

3636
@staticmethod
37-
def get_block_reward():
37+
def get_block_reward() -> int:
3838
return EIP1234_BLOCK_REWARD

eth/vm/forks/constantinople/storage.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from eth.constants import (
22
UINT256
33
)
4+
5+
from eth.vm.computation import BaseComputation
46
from eth.vm.forks.constantinople import (
57
constants
68
)
@@ -10,7 +12,7 @@
1012
)
1113

1214

13-
def sstore_eip1283(computation):
15+
def sstore_eip1283(computation: BaseComputation) -> None:
1416
slot, value = computation.stack_pop(num_items=2, type_hint=UINT256)
1517

1618
current_value = computation.state.account_db.get_storage(

eth/vm/forks/constantinople/transactions.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
from eth_keys.datatypes import PrivateKey
2+
from eth_typing import Address
3+
4+
from eth.rlp.transactions import (
5+
BaseTransaction,
6+
BaseUnsignedTransaction,
7+
)
8+
19
from eth.vm.forks.byzantium.transactions import (
210
ByzantiumTransaction,
311
ByzantiumUnsignedTransaction,
@@ -10,12 +18,21 @@
1018

1119
class ConstantinopleTransaction(ByzantiumTransaction):
1220
@classmethod
13-
def create_unsigned_transaction(cls, *, nonce, gas_price, gas, to, value, data):
21+
def create_unsigned_transaction(cls, # type: ignore
22+
*,
23+
nonce: int,
24+
gas_price: int,
25+
gas: int,
26+
to: Address,
27+
value: int,
28+
data: bytes) -> BaseTransaction:
1429
return ConstantinopleUnsignedTransaction(nonce, gas_price, gas, to, value, data)
1530

1631

1732
class ConstantinopleUnsignedTransaction(ByzantiumUnsignedTransaction):
18-
def as_signed_transaction(self, private_key, chain_id=None):
33+
def as_signed_transaction(self, # type: ignore
34+
private_key: PrivateKey,
35+
chain_id: int=None) -> BaseUnsignedTransaction:
1936
v, r, s = create_transaction_signature(self, private_key, chain_id=chain_id)
2037
return ConstantinopleTransaction(
2138
nonce=self.nonce,

eth/vm/forks/frontier/__init__.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
from typing import Type # noqa: F401
2-
from eth.rlp.blocks import BaseBlock # noqa: F401
3-
from eth.vm.state import BaseState # noqa: F401
4-
52

63
from eth.constants import (
74
BLOCK_REWARD,
85
UNCLE_DEPTH_PENALTY_FACTOR,
96
)
7+
8+
from eth.rlp.blocks import BaseBlock # noqa: F401
9+
from eth.rlp.headers import BlockHeader
10+
from eth.rlp.logs import Log
11+
from eth.rlp.receipts import Receipt
12+
from eth.rlp.transactions import BaseTransaction
13+
1014
from eth.vm.base import VM
11-
from eth.rlp.receipts import (
12-
Receipt,
13-
)
14-
from eth.rlp.logs import (
15-
Log,
16-
)
15+
from eth.vm.computation import BaseComputation
16+
from eth.vm.state import BaseState # noqa: F401
1717

1818
from .blocks import FrontierBlock
1919
from .state import FrontierState
@@ -25,7 +25,10 @@
2525
from .validation import validate_frontier_transaction_against_header
2626

2727

28-
def make_frontier_receipt(base_header, transaction, computation, state):
28+
def make_frontier_receipt(base_header: BlockHeader,
29+
transaction: BaseTransaction,
30+
computation: BaseComputation,
31+
state: BaseState) -> Receipt:
2932
# Reusable for other forks
3033

3134
logs = [
@@ -62,22 +65,22 @@ class FrontierVM(VM):
6265
_state_class = FrontierState # type: Type[BaseState]
6366

6467
# methods
65-
create_header_from_parent = staticmethod(create_frontier_header_from_parent)
66-
compute_difficulty = staticmethod(compute_frontier_difficulty)
68+
create_header_from_parent = staticmethod(create_frontier_header_from_parent) # type: ignore
69+
compute_difficulty = staticmethod(compute_frontier_difficulty) # type: ignore
6770
configure_header = configure_frontier_header
68-
make_receipt = staticmethod(make_frontier_receipt)
71+
make_receipt = staticmethod(make_frontier_receipt) # type: ignore
6972
validate_transaction_against_header = validate_frontier_transaction_against_header
7073

7174
@staticmethod
72-
def get_block_reward():
75+
def get_block_reward() -> int:
7376
return BLOCK_REWARD
7477

7578
@staticmethod
76-
def get_uncle_reward(block_number, uncle):
79+
def get_uncle_reward(block_number: int, uncle: BaseBlock) -> int:
7780
return BLOCK_REWARD * (
7881
UNCLE_DEPTH_PENALTY_FACTOR + uncle.block_number - block_number
7982
) // UNCLE_DEPTH_PENALTY_FACTOR
8083

8184
@classmethod
82-
def get_nephew_reward(cls):
85+
def get_nephew_reward(cls) -> int:
8386
return cls.get_block_reward() // 32

eth/vm/forks/frontier/blocks.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import ( # noqa: F401
2-
List
2+
Iterable,
3+
List,
4+
Type,
35
)
46

57
import rlp
@@ -11,20 +13,32 @@
1113
BloomFilter,
1214
)
1315

16+
from eth_typing import (
17+
Hash32,
18+
)
19+
1420
from eth_hash.auto import keccak
1521

1622
from eth.constants import (
1723
EMPTY_UNCLE_HASH,
1824
)
19-
from eth.rlp.receipts import (
20-
Receipt,
25+
26+
from eth.db.chain import (
27+
BaseChainDB,
2128
)
29+
2230
from eth.rlp.blocks import (
2331
BaseBlock,
2432
)
2533
from eth.rlp.headers import (
2634
BlockHeader,
2735
)
36+
from eth.rlp.receipts import (
37+
Receipt,
38+
)
39+
from eth.rlp.transactions import (
40+
BaseTransaction,
41+
)
2842

2943
from .transactions import (
3044
FrontierTransaction,
@@ -41,7 +55,10 @@ class FrontierBlock(BaseBlock):
4155

4256
bloom_filter = None
4357

44-
def __init__(self, header, transactions=None, uncles=None):
58+
def __init__(self,
59+
header: BlockHeader,
60+
transactions: Iterable[BaseTransaction]=None,
61+
uncles: Iterable[BlockHeader]=None) -> None:
4562
if transactions is None:
4663
transactions = []
4764
if uncles is None:
@@ -60,36 +77,36 @@ def __init__(self, header, transactions=None, uncles=None):
6077
# Helpers
6178
#
6279
@property
63-
def number(self):
80+
def number(self) -> int:
6481
return self.header.block_number
6582

6683
@property
67-
def hash(self):
84+
def hash(self) -> Hash32:
6885
return self.header.hash
6986

7087
#
7188
# Transaction class for this block class
7289
#
7390
@classmethod
74-
def get_transaction_class(cls):
91+
def get_transaction_class(cls) -> Type[BaseTransaction]:
7592
return cls.transaction_class
7693

7794
#
7895
# Receipts API
7996
#
80-
def get_receipts(self, chaindb):
97+
def get_receipts(self, chaindb: BaseChainDB) -> Iterable[Receipt]:
8198
return chaindb.get_receipts(self.header, Receipt)
8299

83100
#
84101
# Header API
85102
#
86103
@classmethod
87-
def from_header(cls, header, chaindb):
104+
def from_header(cls, header: BlockHeader, chaindb: BaseChainDB) -> BaseBlock:
88105
"""
89106
Returns the block denoted by the given block header.
90107
"""
91108
if header.uncles_hash == EMPTY_UNCLE_HASH:
92-
uncles = [] # type: List[bytes]
109+
uncles = [] # type: List[BlockHeader]
93110
else:
94111
uncles = chaindb.get_block_uncles(header.uncles_hash)
95112

@@ -104,7 +121,7 @@ def from_header(cls, header, chaindb):
104121
#
105122
# Execution API
106123
#
107-
def add_uncle(self, uncle):
124+
def add_uncle(self, uncle: BlockHeader) -> "FrontierBlock":
108125
self.uncles.append(uncle)
109126
self.header.uncles_hash = keccak(rlp.encode(self.uncles))
110127
return self

0 commit comments

Comments
 (0)