Skip to content

Commit

Permalink
add typing
Browse files Browse the repository at this point in the history
  • Loading branch information
pacrob committed Feb 12, 2025
1 parent c349be6 commit d371a13
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 18 deletions.
1 change: 1 addition & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ python:
path: .
extra_requirements:
- docs
- test

# Build all formats for RTD Downloads - htmlzip, pdf, epub
formats: all
10 changes: 7 additions & 3 deletions eth/consensus/clique/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import (
Iterable,
cast,
)

from eth_keys import (
Expand Down Expand Up @@ -91,9 +92,12 @@ def get_block_signer(header: BlockHeaderAPI) -> Address:

signature = keys.Signature(signature_bytes=signature_bytes)

return signature.recover_public_key_from_msg_hash(
signature_hash
).to_canonical_address()
return cast(
Address,
signature.recover_public_key_from_msg_hash(
signature_hash
).to_canonical_address(),
)


def is_in_turn(signer: Address, snapshot: Snapshot, header: BlockHeaderAPI) -> bool:
Expand Down
6 changes: 3 additions & 3 deletions eth/consensus/clique/snapshot_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@


def make_snapshot_lookup_key(block_hash: Hash32) -> bytes:
return f"block-hash-to-snapshot:{block_hash}".encode()
return f"block-hash-to-snapshot:{block_hash!r}".encode()


class SnapshotManager:
Expand Down Expand Up @@ -123,11 +123,11 @@ def apply(self, current_snapshot: Snapshot, header: BlockHeaderAPI) -> Snapshot:
if tally.votes > len(snapshot.signers) / 2:
if tally.action is VoteAction.NOMINATE:
snapshot.signers.append(header.coinbase)
self.logger.debug(f"New signer added: {header.coinbase}")
self.logger.debug(f"New signer added: {header.coinbase!r}")
else:
if header.coinbase in snapshot.signers:
snapshot.signers.remove(header.coinbase)
self.logger.debug(f"Signer removed: {header.coinbase}")
self.logger.debug(f"Signer removed: {header.coinbase!r}")

for vote in snapshot.votes.copy():
# Discard any pending votes *from* the added or removed member
Expand Down
4 changes: 2 additions & 2 deletions eth/db/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ def _persist_block(
uncles_hash = EMPTY_UNCLE_HASH
if uncles_hash != block.header.uncles_hash:
raise ValidationError(
f"Block's uncles_hash ({block.header.uncles_hash}) does not match "
f"actual uncles' hash ({uncles_hash})"
f"Block's uncles_hash ({block.header.uncles_hash!r}) does not match "
f"actual uncles' hash ({uncles_hash!r})"
)
new_canonical_hashes = tuple(header.hash for header in new_canonical_headers)
old_canonical_hashes = tuple(header.hash for header in old_canonical_headers)
Expand Down
6 changes: 3 additions & 3 deletions eth/db/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def make_block_number_to_hash_lookup_key(block_number: BlockNumber) -> bytes:

@staticmethod
def make_block_hash_to_score_lookup_key(block_hash: Hash32) -> bytes:
return f"block-hash-to-score:{block_hash}".encode()
return f"block-hash-to-score:{block_hash!r}".encode()

@staticmethod
def make_header_chain_gaps_lookup_key() -> bytes:
Expand All @@ -39,8 +39,8 @@ def make_checkpoint_headers_key() -> bytes:

@staticmethod
def make_transaction_hash_to_block_lookup_key(transaction_hash: Hash32) -> bytes:
return f"transaction-hash-to-block:{transaction_hash}".encode()
return f"transaction-hash-to-block:{transaction_hash!r}".encode()

@staticmethod
def make_withdrawal_hash_to_block_lookup_key(withdrawal_hash: Hash32) -> bytes:
return f"withdrawal-hash-to-block:{withdrawal_hash}".encode()
return f"withdrawal-hash-to-block:{withdrawal_hash!r}".encode()
7 changes: 6 additions & 1 deletion eth/precompiles/point_evaluation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import hashlib
import os
from typing import (
cast,
)

from ckzg import (
load_trusted_setup,
Expand Down Expand Up @@ -29,7 +32,9 @@


def kzg_to_versioned_hash(commitment: bytes) -> Hash32:
return VERSIONED_HASH_VERSION_KZG + hashlib.sha256(commitment).digest()[1:]
return cast(
Hash32, VERSIONED_HASH_VERSION_KZG + hashlib.sha256(commitment).digest()[1:]
)


def point_evaluation_precompile(computation: ComputationAPI) -> ComputationAPI:
Expand Down
27 changes: 27 additions & 0 deletions eth/rlp/blocks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import (
Sequence,
Type,
)

Expand All @@ -12,13 +13,39 @@
)
from eth.abc import (
BlockAPI,
BlockHeaderAPI,
SignedTransactionAPI,
TransactionBuilderAPI,
WithdrawalAPI,
)


class BaseBlock(Configurable, rlp.Serializable, BlockAPI):
transaction_builder: Type[TransactionBuilderAPI] = None

def __init__(
self,
header: BlockHeaderAPI,
transactions: Sequence[SignedTransactionAPI] = None,
uncles: Sequence[BlockHeaderAPI] = None,
withdrawals: Sequence[WithdrawalAPI] = None,
) -> None:
if withdrawals is not None:
rlp.Serializable.__init__(
self,
header=header,
transactions=transactions,
uncles=uncles,
withdrawals=withdrawals,
)
else:
rlp.Serializable.__init__(
self,
header=header,
transactions=transactions,
uncles=uncles,
)

@classmethod
def get_transaction_builder(cls) -> Type[TransactionBuilderAPI]:
if cls.transaction_builder is None:
Expand Down
10 changes: 9 additions & 1 deletion eth/tools/fixtures/loading.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from pytest import (
Mark,
MarkDecorator,
)
import functools
import json
import os
from typing import (
Any,
Callable,
Collection,
Dict,
Iterable,
Tuple,
Union,
)

from eth_utils import (
Expand Down Expand Up @@ -81,7 +87,9 @@ def load_fixture(
def filter_fixtures(
all_fixtures: Iterable[Any],
fixtures_base_dir: str,
mark_fn: Callable[[str, str], bool] = None,
mark_fn: Callable[
[str, str], Union[MarkDecorator, Collection[Union[MarkDecorator, Mark]], None]
] = None,
ignore_fn: Callable[..., bool] = None,
) -> Any:
"""
Expand Down
4 changes: 3 additions & 1 deletion eth/tools/mining.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class POWMiningMixin(VirtualMachineAPI):
"""

def finalize_block(self, block: BlockAPI) -> BlockAndMetaWitness:
block_result = super().finalize_block(block)
# type ignored because as a mixin, we expect to only use this with another
# class that properly implements finalize_block
block_result = super().finalize_block(block) # type: ignore[safe-super]
block = block_result.block

nonce, mix_hash = pow.mine_pow_nonce(
Expand Down
4 changes: 2 additions & 2 deletions eth/vm/forks/berlin/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def max_fee_per_blob_gas(self) -> int:
)

@property
def blob_versioned_hashes(self) -> Hash32:
def blob_versioned_hashes(self) -> Sequence[Hash32]:
raise NotImplementedError(
"blob_versioned_hashes is not implemented until Cancun."
)
Expand Down Expand Up @@ -376,7 +376,7 @@ def max_fee_per_blob_gas(self) -> int:
)

@property
def blob_versioned_hashes(self) -> Hash32:
def blob_versioned_hashes(self) -> Sequence[Hash32]:
raise NotImplementedError(
"blob_versioned_hashes is not implemented until Cancun."
)
Expand Down
6 changes: 5 additions & 1 deletion eth/vm/forks/cancun/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from eth_typing import (
Address,
)

HISTORY_BUFFER_LENGTH = 8191
BEACON_ROOTS_ADDRESS = (
BEACON_ROOTS_ADDRESS = Address(
b'\x00\x0f=\xf6\xd72\x80~\xf11\x9f\xb7\xb8\xbb\x85"\xd0\xbe\xac\x02'
)
BEACON_ROOTS_CONTRACT_CODE = b"3s\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\x14`MW` 6\x14`$W__\xfd[_5\x80\x15`IWb\x00\x1f\xff\x81\x06\x90\x81T\x14`<W__\xfd[b\x00\x1f\xff\x01T_R` _\xf3[__\xfd[b\x00\x1f\xffB\x06B\x81U_5\x90b\x00\x1f\xff\x01U\x00" # noqa: E501
Expand Down
2 changes: 1 addition & 1 deletion eth/vm/forks/london/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def max_fee_per_blob_gas(self) -> int:
)

@property
def blob_versioned_hashes(self) -> Hash32:
def blob_versioned_hashes(self) -> Sequence[Hash32]:
raise NotImplementedError(
"blob_versioned_hashes is not implemented until Cancun."
)
Expand Down

0 comments on commit d371a13

Please sign in to comment.