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

Specify finalized head state #113

Merged
merged 6 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sw-utils"
version = "v0.6.20"
version = "v0.6.21"
description = "StakeWise Python utils"
authors = ["StakeWise Labs <[email protected]>"]
license = "GPL-3.0-or-later"
Expand Down
20 changes: 11 additions & 9 deletions sw_utils/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,15 @@ def get_consensus_client(


async def get_chain_finalized_head(
consensus_client: ExtendedAsyncBeacon, slots_per_epoch: int, finality: Finality = 'finalized'
consensus_client: ExtendedAsyncBeacon,
slots_per_epoch: int,
finality: Finality = 'finalized',
) -> ChainHead:
"""Fetches the fork safe chain head."""
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
cyc60 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -224,8 +226,8 @@ async def get_chain_finalized_head(
execution_payload = slot['data']['message']['body']['execution_payload']
return ChainHead(
epoch=epoch,
cyc60 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -252,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 @@ -265,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
12 changes: 8 additions & 4 deletions sw_utils/typings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,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
Loading