Skip to content

Commit 2406ecb

Browse files
committed
Fix flake8 and mypy
1 parent 87f1742 commit 2406ecb

26 files changed

+228
-228
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: 79 additions & 40 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:
@@ -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

@@ -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
@@ -3322,7 +3360,8 @@ def create_genesis_header(cls, **genesis_params: Any) -> BlockHeaderAPI:
33223360
"""
33233361
Create a genesis header using this VM's rules.
33243362
3325-
This is currently equivalent to create_header_from_parent(None, **genesis_params)
3363+
This is equivalent to calling :meth:`create_header_from_parent`
3364+
with ``parent_header`` set to None.
33263365
"""
33273366
...
33283367

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: 4 additions & 6 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 (
@@ -54,9 +55,6 @@
5455
)
5556
from eth.db.header import HeaderDB
5657
from eth.db.schema import SchemaV1
57-
from eth.rlp.headers import (
58-
BlockHeader,
59-
)
6058
from eth.rlp.sedes import chain_gaps
6159
from eth.typing import ChainGaps
6260
from eth.validation import (
@@ -155,7 +153,7 @@ def _update_header_chain_gaps(
155153
#
156154
# Header API
157155
#
158-
def get_block_uncles(self, niece_header: BlockHeader) -> Tuple[BlockHeaderAPI, ...]:
156+
def get_block_uncles(self, niece_header: BlockHeaderAPI) -> Tuple[BlockHeaderAPI, ...]:
159157
uncles_hash = niece_header.uncles_hash
160158
validate_word(uncles_hash, title="Uncles Hash")
161159
if uncles_hash == EMPTY_UNCLE_HASH:
@@ -294,7 +292,7 @@ def _persist_uncles(
294292
db.set(
295293
uncles_hash,
296294
rlp.encode(uncles, sedes=rlp.sedes.CountableList(header_sedes)))
297-
return uncles_hash
295+
return cast(Hash32, uncles_hash)
298296

299297
#
300298
# Transaction API
@@ -338,7 +336,7 @@ def _get_block_transaction_hashes(
338336
block_header.transaction_root,
339337
)
340338
for encoded_transaction in all_encoded_transactions:
341-
yield keccak(encoded_transaction)
339+
yield cast(Hash32, keccak(encoded_transaction))
342340

343341
@to_tuple
344342
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 (
@@ -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:

0 commit comments

Comments
 (0)