Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transition types #1038

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ install_requires =
typing_extensions>=4
py_ecc @ git+https://github.com/petertdavies/py_ecc.git@127184f4c57b1812da959586d0fe8f43bb1a2389
ethereum-types>=0.2.1,<0.3
ethereum-rlp>=0.1.2,<0.2

[options.package_data]
ethereum =
Expand Down
69 changes: 67 additions & 2 deletions src/ethereum/arrow_glacier/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
chain.
"""
from dataclasses import dataclass
from typing import Tuple, Union
from typing import Annotated, Optional, Tuple, Union

from ethereum_rlp import rlp
from ethereum_types.bytes import Bytes, Bytes8, Bytes32
from ethereum_types.frozen import slotted_freezable
from ethereum_types.numeric import U256, Uint
from typing_extensions import TypeAlias

from ethereum.exceptions import InvalidBlock
from ethereum.london import blocks as previous_blocks

from ..crypto.hash import Hash32
from .fork_types import Address, Bloom, Root
Expand Down Expand Up @@ -45,6 +50,49 @@ class Header:
base_fee_per_gas: Uint


AnyHeader: TypeAlias = Union[previous_blocks.AnyHeader, Header]
"""
Represents all headers that may have appeared in the blockchain before or in
the current fork.
"""


def decode_header(raw_header: rlp.Simple) -> AnyHeader:
"""
Convert `raw_header` from raw sequences and bytes to a structured block
header.

Checks `raw_header` against this fork's `FORK_CRITERIA`, and if it belongs
to this fork, decodes it accordingly. If not, this function forwards to the
preceding fork where the process is repeated.
"""
from . import FORK_CRITERIA

# First, ensure that `raw_header` is not `bytes` (and is therefore a
# sequence.)
if isinstance(raw_header, bytes):
raise InvalidBlock("header is bytes, expected sequence")

# Next, extract the block number and timestamp (which are always at index 8
# and 11 respectively.)
raw_number = raw_header[8]
if not isinstance(raw_number, bytes):
raise InvalidBlock("header number is sequence, expected bytes")
number = Uint.from_be_bytes(raw_number)

raw_timestamp = raw_header[11]
if not isinstance(raw_timestamp, bytes):
raise InvalidBlock("header timestamp is sequence, expected bytes")
timestamp = U256.from_be_bytes(raw_timestamp)

# Finally, check if this header belongs to this fork.
if FORK_CRITERIA.check(number, timestamp):
return rlp.deserialize_to(Header, raw_header)

# If it doesn't, forward to the preceding fork.
return previous_blocks.decode_header(raw_header)


@slotted_freezable
@dataclass
class Block:
Expand All @@ -54,7 +102,14 @@ class Block:

header: Header
transactions: Tuple[Union[Bytes, LegacyTransaction], ...]
ommers: Tuple[Header, ...]
ommers: Tuple[Annotated[AnyHeader, rlp.With(decode_header)], ...]


AnyBlock: TypeAlias = Union[previous_blocks.AnyBlock, Block]
"""
Represents all blocks that may have appeared in the blockchain before or in the
current fork.
"""


@slotted_freezable
Expand All @@ -80,3 +135,13 @@ class Receipt:
cumulative_gas_used: Uint
bloom: Bloom
logs: Tuple[Log, ...]


def header_base_fee_per_gas(header: AnyHeader) -> Optional[Uint]:
"""
Returns the `base_fee_per_gas` of the given header, or `None` for headers
without that field.
"""
if isinstance(header, Header):
return header.base_fee_per_gas
return previous_blocks.header_base_fee_per_gas(header)
Loading
Loading