Skip to content

Commit da79f10

Browse files
committed
Fix flake8 & mypy
1 parent bd5f34c commit da79f10

25 files changed

+209
-214
lines changed

eth/_utils/headers.py

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import time
2-
from typing import Callable, Tuple, Optional
1+
from typing import (
2+
Dict,
3+
Tuple,
4+
)
35

46
from eth_typing import (
5-
Address
7+
Address,
68
)
79

810
from eth.abc import BlockHeaderAPI
911
from eth.constants import (
1012
BLANK_ROOT_HASH,
11-
EMPTY_UNCLE_HASH,
1213
GENESIS_BLOCK_NUMBER,
13-
GENESIS_GAS_LIMIT,
14-
GENESIS_NONCE,
1514
GENESIS_PARENT_HASH,
1615
GAS_LIMIT_EMA_DENOMINATOR,
1716
GAS_LIMIT_ADJUSTMENT_FACTOR,
@@ -20,10 +19,10 @@
2019
GAS_LIMIT_USAGE_ADJUSTMENT_NUMERATOR,
2120
GAS_LIMIT_USAGE_ADJUSTMENT_DENOMINATOR,
2221
ZERO_ADDRESS,
23-
ZERO_HASH32,
2422
)
25-
from eth.rlp.headers import (
26-
BlockHeader,
23+
from eth.typing import (
24+
BlockNumber,
25+
HeaderParams,
2726
)
2827

2928

@@ -38,41 +37,41 @@ def fill_header_params_from_parent(
3837
transaction_root: bytes = None,
3938
state_root: bytes = None,
4039
mix_hash: bytes = None,
41-
receipt_root: bytes = None) -> 'BlockHeaderAPI':
42-
43-
if parent is None:
44-
parent_hash = GENESIS_PARENT_HASH
45-
block_number = GENESIS_BLOCK_NUMBER
46-
if state_root is None:
47-
state_root = BLANK_ROOT_HASH
48-
else:
49-
parent_hash = parent.hash
50-
block_number = parent.block_number + 1
51-
52-
if state_root is None:
53-
state_root = parent.state_root
54-
55-
header_kwargs: Dict[str, HeaderParams] = {
56-
'parent_hash': parent_hash,
57-
'coinbase': coinbase,
58-
'state_root': state_root,
59-
'gas_limit': gas_limit,
60-
'difficulty': difficulty,
61-
'block_number': block_number,
62-
'timestamp': timestamp,
63-
}
64-
if nonce is not None:
65-
header_kwargs['nonce'] = nonce
66-
if extra_data is not None:
67-
header_kwargs['extra_data'] = extra_data
68-
if transaction_root is not None:
69-
header_kwargs['transaction_root'] = transaction_root
70-
if receipt_root is not None:
71-
header_kwargs['receipt_root'] = receipt_root
72-
if mix_hash is not None:
73-
header_kwargs['mix_hash'] = mix_hash
74-
75-
return header_kwargs
40+
receipt_root: bytes = None) -> Dict[str, HeaderParams]:
41+
42+
if parent is None:
43+
parent_hash = GENESIS_PARENT_HASH
44+
block_number = GENESIS_BLOCK_NUMBER
45+
if state_root is None:
46+
state_root = BLANK_ROOT_HASH
47+
else:
48+
parent_hash = parent.hash
49+
block_number = BlockNumber(parent.block_number + 1)
50+
51+
if state_root is None:
52+
state_root = parent.state_root
53+
54+
header_kwargs: Dict[str, HeaderParams] = {
55+
'parent_hash': parent_hash,
56+
'coinbase': coinbase,
57+
'state_root': state_root,
58+
'gas_limit': gas_limit,
59+
'difficulty': difficulty,
60+
'block_number': block_number,
61+
'timestamp': timestamp,
62+
}
63+
if nonce is not None:
64+
header_kwargs['nonce'] = nonce
65+
if extra_data is not None:
66+
header_kwargs['extra_data'] = extra_data
67+
if transaction_root is not None:
68+
header_kwargs['transaction_root'] = transaction_root
69+
if receipt_root is not None:
70+
header_kwargs['receipt_root'] = receipt_root
71+
if mix_hash is not None:
72+
header_kwargs['mix_hash'] = mix_hash
73+
74+
return header_kwargs
7675

7776

7877
def compute_gas_limit_bounds(previous_limit: int) -> Tuple[int, int]:

eth/abc.py

Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class MiningHeaderAPI(ABC):
7474
gas_used: int
7575
timestamp: int
7676
extra_data: bytes
77-
base_fee_per_gas: int # EIP-1559, set to None in pre-London header
7877

7978
@property
8079
@abstractmethod
@@ -123,8 +122,47 @@ def as_dict(self) -> Dict[Hashable, Any]:
123122
"""
124123
...
125124

125+
@property
126+
@abstractmethod
127+
def base_fee_per_gas(self) -> Optional[int]:
128+
"""
129+
Return the base fee per gas of the block.
126130
127-
class BlockHeaderAPI(MiningHeaderAPI):
131+
Set to None in pre-EIP-1559 (London) header.
132+
"""
133+
...
134+
135+
136+
class BlockHeaderSedesAPI(ABC):
137+
"""
138+
Serialize and deserialize RLP for a header.
139+
140+
The header may be one of several definitions, like a London (EIP-1559) or
141+
pre-London header.
142+
"""
143+
144+
@classmethod
145+
@abstractmethod
146+
def deserialize(cls, encoded: List[bytes]) -> 'BlockHeaderAPI':
147+
"""
148+
Extract a header from an encoded RLP object.
149+
150+
This method is used by rlp.decode(..., sedes=TransactionBuilderAPI).
151+
"""
152+
...
153+
154+
@classmethod
155+
@abstractmethod
156+
def serialize(cls, obj: 'BlockHeaderAPI') -> List[bytes]:
157+
"""
158+
Encode a header to a series of bytes used by RLP.
159+
160+
This method is used by rlp.encode(obj).
161+
"""
162+
...
163+
164+
165+
class BlockHeaderAPI(MiningHeaderAPI, BlockHeaderSedesAPI):
128166
"""
129167
A class derived from :class:`~eth.abc.MiningHeaderAPI` to define a block header after it is
130168
sealed.
@@ -327,9 +365,6 @@ class TransactionFieldsAPI(ABC):
327365
"""
328366
A class to define all common transaction fields.
329367
"""
330-
max_fee_per_gas: int
331-
max_priority_fee_per_gas: int
332-
333368
@property
334369
@abstractmethod
335370
def nonce(self) -> int:
@@ -339,7 +374,23 @@ def nonce(self) -> int:
339374
@abstractmethod
340375
def gas_price(self) -> int:
341376
"""
342-
Will raise :cls:`AttributeError` if get or set on a 1559 transaction.
377+
Will raise :class:`AttributeError` if get or set on a 1559 transaction.
378+
"""
379+
...
380+
381+
@property
382+
@abstractmethod
383+
def max_fee_per_gas(self) -> int:
384+
"""
385+
Will default to gas_price if this is a pre-1559 transaction.
386+
"""
387+
...
388+
389+
@property
390+
@abstractmethod
391+
def max_priority_fee_per_gas(self) -> int:
392+
"""
393+
Will default to gas_price if this is a pre-1559 transaction.
343394
"""
344395
...
345396

@@ -2619,6 +2670,15 @@ def gas_limit(self) -> int:
26192670
"""
26202671
...
26212672

2673+
@abstractmethod
2674+
def get_gas_price(self, transaction: SignedTransactionAPI) -> int:
2675+
"""
2676+
Return the gas price of the given transaction.
2677+
2678+
Factor in the current block's base gase price, if appropriate. (See EIP-1559)
2679+
"""
2680+
...
2681+
26222682
#
26232683
# Access to account db
26242684
#
@@ -2994,36 +3054,6 @@ def get_fee_recipient(cls, header: BlockHeaderAPI) -> Address:
29943054
...
29953055

29963056

2997-
2998-
class BlockHeaderSedesAPI(ABC):
2999-
"""
3000-
Serialize and deserialize RLP for a header.
3001-
3002-
The header may be one of several definitions, like a London (EIP-1559) or
3003-
pre-London header.
3004-
"""
3005-
3006-
@classmethod
3007-
@abstractmethod
3008-
def deserialize(cls, encoded: List[bytes]) -> BlockHeaderAPI:
3009-
"""
3010-
Extract a header from an encoded RLP object.
3011-
3012-
This method is used by rlp.decode(..., sedes=TransactionBuilderAPI).
3013-
"""
3014-
...
3015-
3016-
@classmethod
3017-
@abstractmethod
3018-
def serialize(cls, obj: BlockHeaderAPI) -> List[bytes]:
3019-
"""
3020-
Encode a header to a series of bytes used by RLP.
3021-
3022-
This method is used by rlp.encode(obj).
3023-
"""
3024-
...
3025-
3026-
30273057
class VirtualMachineAPI(ConfigurableAPI):
30283058
"""
30293059
The :class:`~eth.abc.VirtualMachineAPI` class represents the Chain rules for a
@@ -3321,7 +3351,8 @@ def create_genesis_header(cls, **genesis_params: Any) -> BlockHeaderAPI:
33213351
"""
33223352
Create a genesis header using this VM's rules.
33233353
3324-
This is currently equivalent to create_header_from_parent(None, **genesis_params)
3354+
This is equivalent to calling :meth:`create_header_from_parent`
3355+
with ``parent_header`` set to None.
33253356
"""
33263357
...
33273358

eth/chains/base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
)
2323
from eth_utils.toolz import (
2424
concatv,
25-
keyfilter,
2625
sliding_window,
2726
)
2827

@@ -495,7 +494,6 @@ def persist_block(
495494
if perform_validation:
496495
self.validate_block(block)
497496

498-
vm = self.get_vm(block.header)
499497
(
500498
new_canonical_hashes,
501499
old_canonical_hashes,

eth/db/chain.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Sequence,
88
Tuple,
99
Type,
10+
cast,
1011
)
1112

1213
from eth_typing import (
@@ -53,9 +54,6 @@
5354
)
5455
from eth.db.header import HeaderDB
5556
from eth.db.schema import SchemaV1
56-
from eth.rlp.headers import (
57-
BlockHeader,
58-
)
5957
from eth.rlp.sedes import chain_gaps
6058
from eth.typing import ChainGaps
6159
from eth.validation import (
@@ -334,7 +332,7 @@ def _get_block_transaction_hashes(
334332
block_header.transaction_root,
335333
)
336334
for encoded_transaction in all_encoded_transactions:
337-
yield keccak(encoded_transaction)
335+
yield cast(Hash32, keccak(encoded_transaction))
338336

339337
@to_tuple
340338
def get_receipts(self,

eth/db/hash_trie.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
class HashTrie(KeyMapDB):
16-
keymap = keccak
16+
keymap = keccak # type: ignore # mypy doesn't like that keccak accepts bytearray
1717

1818
@contextlib.contextmanager
1919
def squash_changes(self) -> Iterator['HashTrie']:

eth/db/header.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
ParentNotFound,
5050
)
5151
from eth.db.schema import SchemaV1
52-
from eth.rlp.headers import BlockHeader
5352
from eth.rlp.sedes import chain_gaps
5453
from eth.typing import ChainGaps
5554
from eth.validation import (
@@ -58,9 +57,6 @@
5857
)
5958
from eth.vm.header import HeaderSedes
6059

61-
from rlp.exceptions import (
62-
ObjectDeserializationError
63-
)
6460

6561
class HeaderDB(HeaderDatabaseAPI):
6662
def __init__(self, db: AtomicDatabaseAPI) -> None:

eth/rlp/headers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
22
from typing import (
3+
cast,
34
overload,
45
)
56

@@ -149,11 +150,12 @@ def __str__(self) -> str:
149150
def hash(self) -> Hash32:
150151
if self._hash is None:
151152
self._hash = keccak(rlp.encode(self))
152-
return self._hash
153+
return cast(Hash32, self._hash)
153154

154155
@property
155156
def mining_hash(self) -> Hash32:
156-
return keccak(rlp.encode(self[:-2], MiningHeader))
157+
result = keccak(rlp.encode(self[:-2], MiningHeader))
158+
return cast(Hash32, result)
157159

158160
@property
159161
def hex_hash(self) -> str:

eth/rlp/transactions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Optional,
33
Sequence,
44
Tuple,
5+
cast,
56
)
67

78
from cached_property import cached_property
@@ -73,7 +74,7 @@ class BaseTransactionFields(rlp.Serializable, TransactionFieldsAPI):
7374

7475
@property
7576
def hash(self) -> Hash32:
76-
return keccak(rlp.encode(self))
77+
return cast(Hash32, keccak(rlp.encode(self)))
7778

7879

7980
class SignedTransactionMethods(BaseTransactionMethods, SignedTransactionAPI):

0 commit comments

Comments
 (0)