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

Safe head #116

Merged
merged 4 commits into from
Sep 3, 2024
Merged
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
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.21"
version = "v0.6.22"
description = "StakeWise Python utils"
authors = ["StakeWise Labs <[email protected]>"]
license = "GPL-3.0-or-later"
Expand Down
35 changes: 31 additions & 4 deletions sw_utils/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,9 @@ def get_consensus_client(
async def get_chain_finalized_head(
consensus_client: ExtendedAsyncBeacon,
slots_per_epoch: int,
state: str = 'finalized',
) -> ChainHead:
"""Fetches the fork safe chain head."""
block_data = await consensus_client.get_block(state)
"""Fetches the fork finalized chain head."""
block_data = await consensus_client.get_block('finalized')
slot = int(block_data['data']['message']['slot'])

return ChainHead(
Expand All @@ -226,14 +225,42 @@ async def get_chain_finalized_head(
)


async def get_chain_justified_head(
consensus_client: ExtendedAsyncBeacon,
slots_per_epoch: int,
) -> ChainHead:
"""Fetches the fork safe chain head."""
checkpoints = await consensus_client.get_finality_checkpoint()
epoch: int = int(checkpoints['data']['current_justified']['epoch'])
last_slot_id: int = epoch * slots_per_epoch
for i in range(slots_per_epoch):
try:
slot = await consensus_client.get_block(str(last_slot_id - i))
except ClientResponseError as e:
if hasattr(e, 'status') and e.status == 404:
# slot was not proposed, try the previous one
continue
raise e

execution_payload = slot['data']['message']['body']['execution_payload']
return ChainHead(
epoch=epoch,
slot=last_slot_id - i,
block_number=BlockNumber(int(execution_payload['block_number'])),
execution_ts=Timestamp(int(execution_payload['timestamp'])),
)

raise RuntimeError(f'Failed to fetch slot for epoch {epoch}')


async def get_chain_epoch_head(
cyc60 marked this conversation as resolved.
Show resolved Hide resolved
epoch: int,
slots_per_epoch: int,
execution_client: AsyncWeb3,
consensus_client: ExtendedAsyncBeacon,
) -> ChainHead:
"""Fetches the epoch chain head."""
slot_id: int = (epoch * slots_per_epoch) + slots_per_epoch - 1
slot_id: int = epoch * slots_per_epoch
for i in range(slots_per_epoch):
try:
slot = await consensus_client.get_block(str(slot_id - i))
Expand Down
8 changes: 0 additions & 8 deletions sw_utils/typings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ class ChainHead:
block_number: BlockNumber
execution_ts: Timestamp

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

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


@dataclass
class Oracle:
Expand Down
Loading