Skip to content

Commit 4d4f160

Browse files
committed
Fix flake8 and mypy
1 parent 06b1428 commit 4d4f160

File tree

21 files changed

+203
-211
lines changed

21 files changed

+203
-211
lines changed

eth/_utils/headers.py

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
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,
1817
GAS_LIMIT_MINIMUM,
1918
GAS_LIMIT_USAGE_ADJUSTMENT_NUMERATOR,
2019
GAS_LIMIT_USAGE_ADJUSTMENT_DENOMINATOR,
2120
ZERO_ADDRESS,
22-
ZERO_HASH32,
2321
)
24-
from eth.rlp.headers import (
25-
BlockHeader,
22+
from eth.typing import (
23+
BlockNumber,
24+
HeaderParams,
2625
)
2726

2827

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

7675

7776
def compute_gas_limit_bounds(parent: BlockHeaderAPI) -> Tuple[int, int]:

eth/abc.py

Lines changed: 76 additions & 38 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.
130+
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.
126139
127-
class BlockHeaderAPI(MiningHeaderAPI):
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:
@@ -343,6 +378,22 @@ def gas_price(self) -> int:
343378
"""
344379
...
345380

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.
394+
"""
395+
...
396+
346397
@property
347398
@abstractmethod
348399
def gas(self) -> int:
@@ -1020,9 +1071,12 @@ class ChainDatabaseAPI(HeaderDatabaseAPI):
10201071
# Header API
10211072
#
10221073
@abstractmethod
1023-
def get_block_uncles(self, uncles_hash: Hash32) -> Tuple[BlockHeaderAPI, ...]:
1074+
def get_block_uncles(self, niece_header: BlockHeaderAPI) -> Tuple[BlockHeaderAPI, ...]:
10241075
"""
1025-
Return an iterable of uncle headers specified by the given ``uncles_hash``
1076+
Return an iterable of uncle headers, specified by the header's ``uncles_hash``.
1077+
1078+
The header class is used to specify the RLP deserialization used when
1079+
decoding the uncle headers from the binary blob.
10261080
"""
10271081
...
10281082

@@ -1072,10 +1126,15 @@ def persist_unexecuted_block(self,
10721126
"""
10731127

10741128
@abstractmethod
1075-
def persist_uncles(self, uncles: Tuple[BlockHeaderAPI]) -> Hash32:
1129+
def persist_uncles(
1130+
self,
1131+
uncles: Tuple[BlockHeaderAPI],
1132+
header_sedes: BlockHeaderSedesAPI) -> Hash32:
10761133
"""
10771134
Persist the list of uncles to the database.
10781135
1136+
Use header_sedes to decode the rlp-encoded uncle headers.
1137+
10791138
Return the uncles hash.
10801139
"""
10811140
...
@@ -2619,6 +2678,15 @@ def gas_limit(self) -> int:
26192678
"""
26202679
...
26212680

2681+
@abstractmethod
2682+
def get_gas_price(self, transaction: SignedTransactionAPI) -> int:
2683+
"""
2684+
Return the gas price of the given transaction.
2685+
2686+
Factor in the current block's base gase price, if appropriate. (See EIP-1559)
2687+
"""
2688+
...
2689+
26222690
#
26232691
# Access to account db
26242692
#
@@ -2994,36 +3062,6 @@ def get_fee_recipient(cls, header: BlockHeaderAPI) -> Address:
29943062
...
29953063

29963064

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-
30273065
class VirtualMachineAPI(ConfigurableAPI):
30283066
"""
30293067
The :class:`~eth.abc.VirtualMachineAPI` class represents the Chain rules for a

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

@@ -498,7 +497,6 @@ def persist_block(
498497
if perform_validation:
499498
self.validate_block(block)
500499

501-
vm = self.get_vm(block.header)
502500
(
503501
new_canonical_hashes,
504502
old_canonical_hashes,

eth/db/chain.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@
5454
)
5555
from eth.db.header import HeaderDB
5656
from eth.db.schema import SchemaV1
57-
from eth.rlp.headers import (
58-
BlockHeader,
59-
)
6057
from eth.rlp.sedes import chain_gaps
6158
from eth.typing import ChainGaps
6259
from eth.validation import (
@@ -155,7 +152,7 @@ def _update_header_chain_gaps(
155152
#
156153
# Header API
157154
#
158-
def get_block_uncles(self, niece_header: BlockHeader) -> Tuple[BlockHeaderAPI, ...]:
155+
def get_block_uncles(self, niece_header: BlockHeaderAPI) -> Tuple[BlockHeaderAPI, ...]:
159156
uncles_hash = niece_header.uncles_hash
160157
validate_word(uncles_hash, title="Uncles Hash")
161158
if uncles_hash == EMPTY_UNCLE_HASH:

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 (
@@ -60,9 +59,6 @@
6059
LondonBackwardsHeader,
6160
)
6261

63-
from rlp.exceptions import (
64-
ObjectDeserializationError
65-
)
6662

6763
class HeaderDB(HeaderDatabaseAPI):
6864
def __init__(self, db: AtomicDatabaseAPI) -> None:

eth/rlp/headers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import time
22
from typing import (
3-
Dict,
43
Optional,
54
overload,
65
)

eth/vm/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def generate_block_from_parent_header_and_coinbase(cls,
444444
return block
445445

446446
@classmethod
447-
def create_genesis_header(cls, **genesis_params) -> BlockHeaderAPI:
447+
def create_genesis_header(cls, **genesis_params: Any) -> BlockHeaderAPI:
448448
# Create genesis header by setting the parent to None
449449
return cls.create_header_from_parent(None, **genesis_params)
450450

eth/vm/execution_context.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def chain_id(self) -> int:
7474
@property
7575
def base_fee_per_gas(self) -> int:
7676
if self._base_fee_per_gas is None:
77-
raise AttributeError(f"This header at Block #{self.block_number} does not have a base gas fee")
77+
raise AttributeError(
78+
f"This header at Block #{self.block_number} does not have a base gas fee"
79+
)
7880
else:
7981
return self._base_fee_per_gas

eth/vm/forks/frontier/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from typing import Type
1+
from typing import (
2+
ClassVar,
3+
Type,
4+
)
25

36
from eth_bloom import (
47
BloomFilter,
@@ -73,7 +76,7 @@ class FrontierVM(VM):
7376
# classes
7477
block_class: Type[BlockAPI] = FrontierBlock
7578
_state_class: Type[StateAPI] = FrontierState
76-
all_header_sedes: Type[BlockHeaderSedesAPI] = BlockHeader
79+
all_header_sedes: ClassVar[Type[BlockHeaderSedesAPI]] = BlockHeader
7780

7881
# methods
7982
create_header_from_parent = staticmethod(create_frontier_header_from_parent) # type: ignore

0 commit comments

Comments
 (0)