Skip to content

Commit

Permalink
Use finalized checkpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
cyc60 committed Aug 21, 2024
1 parent 9327097 commit 678558a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
21 changes: 10 additions & 11 deletions sw_utils/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from sw_utils.common import urljoin
from sw_utils.decorators import can_be_retried_aiohttp_error, retry_aiohttp_errors
from sw_utils.typings import ChainHead, ConsensusFork, Finality, State
from sw_utils.typings import ChainHead, ConsensusFork, Finality

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -209,13 +209,12 @@ async def get_chain_finalized_head(
consensus_client: ExtendedAsyncBeacon,
slots_per_epoch: int,
finality: Finality = 'finalized',
state: State = 'finalized',
) -> ChainHead:
"""Fetches the fork safe chain head."""
checkpoints = await consensus_client.get_finality_checkpoint(state)
checkpoints = await consensus_client.get_finality_checkpoint()
epoch: int = int(checkpoints['data'][finality]['epoch'])
last_slot_id: int = (epoch * slots_per_epoch) + slots_per_epoch - 1
for i in range(slots_per_epoch):
last_slot_id: int = epoch * slots_per_epoch
for i in range(slots_per_epoch + 1):
try:
slot = await consensus_client.get_block(str(last_slot_id - i))
except ClientResponseError as e:
Expand All @@ -227,8 +226,8 @@ async def get_chain_finalized_head(
execution_payload = slot['data']['message']['body']['execution_payload']
return ChainHead(
epoch=epoch,
consensus_block=last_slot_id - i,
execution_block=BlockNumber(int(execution_payload['block_number'])),
slot=last_slot_id - i,
block_number=BlockNumber(int(execution_payload['block_number'])),
execution_ts=Timestamp(int(execution_payload['timestamp'])),
)

Expand All @@ -255,8 +254,8 @@ async def get_chain_epoch_head(
execution_payload = slot['data']['message']['body']['execution_payload']
return ChainHead(
epoch=epoch,
consensus_block=slot_id - i,
execution_block=BlockNumber(int(execution_payload['block_number'])),
slot=slot_id - i,
block_number=BlockNumber(int(execution_payload['block_number'])),
execution_ts=Timestamp(int(execution_payload['timestamp'])),
)
except KeyError: # pre shapella slot
Expand All @@ -268,8 +267,8 @@ async def get_chain_epoch_head(

return ChainHead(
epoch=epoch,
consensus_block=slot_id - i,
execution_block=BlockNumber(int(block['number'])),
slot=slot_id - i,
block_number=BlockNumber(int(block['number'])),
execution_ts=Timestamp(int(block['timestamp'])),
)

Expand Down
13 changes: 8 additions & 5 deletions sw_utils/typings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Bytes32 = NewType('Bytes32', bytes)

Finality: TypeAlias = Literal['finalized', 'current_justified', 'previous_justified']
State: TypeAlias = Literal['genesis', 'finalized', 'justified'] | str


@dataclass
Expand All @@ -21,13 +20,17 @@ class ConsensusFork:
@dataclass
class ChainHead:
epoch: int
consensus_block: int
execution_block: BlockNumber
slot: int
block_number: BlockNumber
execution_ts: Timestamp

@property
def slot(self) -> int:
return self.consensus_block
def consensus_block(self) -> int:
return self.slot

@property
def execution_block(self) -> BlockNumber:
return self.block_number


@dataclass
Expand Down

0 comments on commit 678558a

Please sign in to comment.