Skip to content

Commit

Permalink
refactor: rework types for block attestations
Browse files Browse the repository at this point in the history
  • Loading branch information
madlabman committed Dec 23, 2024
1 parent 2a13586 commit e884a2a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
7 changes: 2 additions & 5 deletions src/modules/csm/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from src.metrics.prometheus.csm import CSM_MIN_UNPROCESSED_EPOCH, CSM_UNPROCESSED_EPOCHS_COUNT
from src.modules.csm.state import State
from src.providers.consensus.client import ConsensusClient
from src.providers.consensus.types import BlockAttestationElectra, BlockAttestationPhase0
from src.providers.consensus.types import BlockAttestation, BlockAttestationEIP7549
from src.types import BlockRoot, BlockStamp, EpochNumber, SlotNumber, ValidatorIndex
from src.utils.range import sequence
from src.utils.timeit import timeit
Expand All @@ -24,9 +24,6 @@
class MinStepIsNotReached(Exception): ...


type BlockAttestation = BlockAttestationPhase0 | BlockAttestationElectra


@dataclass
class FrameCheckpoint:
slot: SlotNumber # Slot for the state to get the trusted block roots from.
Expand Down Expand Up @@ -249,7 +246,7 @@ def get_committee_indices(attestation: BlockAttestation) -> list[CommitteeIndex]
return [attestation.data.index]


def is_eip7549_attestation(attestation: BlockAttestation) -> TypeGuard[BlockAttestationElectra]:
def is_eip7549_attestation(attestation: BlockAttestation) -> TypeGuard[BlockAttestationEIP7549]:
# @see https://eips.ethereum.org/EIPS/eip-7549
has_committee_bits = getattr(attestation, "committee_bits") is not None
has_zero_index = attestation.data.index == "0"
Expand Down
13 changes: 7 additions & 6 deletions src/providers/consensus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
from src.metrics.logging import logging
from src.metrics.prometheus.basic import CL_REQUESTS_DURATION
from src.providers.consensus.types import (
BlockAttestationElectra,
BlockAttestationPhase0,
BlockAttestation,
BlockAttestationResponse,
BlockDetailsResponse,
BlockHeaderFullResponse,
BlockHeaderResponseData,
BlockRootResponse,
Validator,
BeaconSpecResponse,
GenesisResponse,
SlotAttestationCommittee, BlockAttestation,
SlotAttestationCommittee,
)
from src.providers.http_provider import HTTPProvider, NotOkResponse
from src.types import BlockRoot, BlockStamp, SlotNumber, EpochNumber
Expand Down Expand Up @@ -111,8 +111,9 @@ def get_block_details(self, state_id: SlotNumber | BlockRoot) -> BlockDetailsRes

@lru_cache(maxsize=256)
def get_block_attestations(
self, state_id: SlotNumber | BlockRoot
) -> list[BlockAttestationPhase0 | BlockAttestationElectra]:
self,
state_id: SlotNumber | BlockRoot,
) -> list[BlockAttestation]:
"""Spec: https://ethereum.github.io/beacon-APIs/#/Beacon/getBlockAttestations"""
data, _ = self._get(
self.API_GET_BLOCK_ATTESTATIONS,
Expand All @@ -121,7 +122,7 @@ def get_block_attestations(
)
if not isinstance(data, list):
raise ValueError("Expected list response from getBlockAttestations")
return [BlockAttestation.from_response(**att) for att in data]
return [BlockAttestationResponse.from_response(**att) for att in data]

@list_of_dataclasses(SlotAttestationCommittee.from_response)
def get_attestation_committees(
Expand Down
7 changes: 5 additions & 2 deletions src/providers/consensus/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class AttestationData(Nested, FromResponse):


@dataclass
class BlockAttestation(Nested, FromResponse):
class BlockAttestationResponse(Nested, FromResponse):
aggregation_bits: str
data: AttestationData
committee_bits: str | None = None
Expand All @@ -94,10 +94,13 @@ class BlockAttestationPhase0(Protocol):
data: AttestationData


class BlockAttestationElectra(BlockAttestationPhase0):
class BlockAttestationEIP7549(BlockAttestationPhase0):
committee_bits: str


type BlockAttestation = BlockAttestationPhase0 | BlockAttestationEIP7549


@dataclass
class BeaconBlockBody(Nested, FromResponse):
execution_payload: ExecutionPayload
Expand Down
4 changes: 2 additions & 2 deletions tests/factory/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from src.providers.consensus.types import (
BeaconSpecResponse,
SlotAttestationCommittee,
BlockAttestation,
BlockAttestationResponse,
AttestationData,
Checkpoint,
)
Expand Down Expand Up @@ -61,7 +61,7 @@ class SlotAttestationCommitteeFactory(Web3Factory):


class BlockAttestationFactory(Web3Factory):
__model__ = BlockAttestation
__model__ = BlockAttestationResponse

aggregation_bits = "0x"
committee_bits = None
Expand Down
2 changes: 1 addition & 1 deletion tests/modules/csm/test_checkpoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from copy import deepcopy
from typing import Iterator, cast
from typing import cast
from unittest.mock import Mock

import pytest
Expand Down

0 comments on commit e884a2a

Please sign in to comment.