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

Add support for Electra fork #72

Merged
merged 2 commits into from
Feb 6, 2025
Merged
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 requirements-dev.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-c requirements.txt
aioresponses
milagro-bls-binding
pre-commit
pytest
pytest-asyncio
10 changes: 10 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -289,6 +289,16 @@ iniconfig==2.0.0 \
--hash=sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3 \
--hash=sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374
# via pytest
milagro-bls-binding==1.9.0 \
--hash=sha256:2c47c5e45b30d0df02b65c2da4f9e10a49cc1a773f47796294663fc564ca56ee \
--hash=sha256:43fb41b335b2a40ee21f2698c6ae27ed83921f5f6109443705f793c77d4b6d6e \
--hash=sha256:4d91da896d8de735c828dc1815d7dcaeeed9363c25a5d4f725ec3916672cbc79 \
--hash=sha256:5646113ffa12a43acda419341817cf3b1b327b9e81dfd2c2a98c6aa1b38422c0 \
--hash=sha256:8800b9a8c61c20d1fdb5593b8e1940cbf6e521b454cc6f764fc22f026337651f \
--hash=sha256:a28cbae598a01f76c5204a29b2d060c9aee8c66898e37c37b708aecc16d1b482 \
--hash=sha256:b2362f0d14318a3f44a3d1e186e2069eb25859cc4b9cae3e473505a28954803e \
--hash=sha256:d723eac25c1c5d8ebc9937a4eebcea40be1b2fff742dcf181d83e4ee59d2b12d
# via -r requirements-dev.in
multidict==6.1.0 \
--hash=sha256:052e10d2d37810b99cc170b785945421141bf7bb7d2f8799d431e7db229c385f \
--hash=sha256:06809f4f0f7ab7ea2cabf9caca7d79c22c0758b58a71f9d32943ae13c7ace056 \
14 changes: 7 additions & 7 deletions src/initialize.py
Original file line number Diff line number Diff line change
@@ -119,6 +119,11 @@ async def run_services(
) -> None:
spec = load_spec(cli_args=cli_args)

beacon_chain = BeaconChain(
spec=spec,
task_manager=task_manager,
)

async with (
RemoteSigner(url=cli_args.remote_signer_url) as remote_signer,
MultiBeaconNode(
@@ -130,13 +135,9 @@ async def run_services(
cli_args=cli_args,
) as multi_beacon_node,
):
beacon_chain = BeaconChain(
spec=spec,
multi_beacon_node=multi_beacon_node,
task_manager=task_manager,
)

beacon_chain.initialize(genesis=multi_beacon_node.best_beacon_node.genesis)
await _wait_for_genesis(genesis_datetime=beacon_chain.get_datetime_for_slot(0))
beacon_chain.start_slot_ticker()

_logger.info(f"Current epoch: {beacon_chain.current_epoch}")
_logger.info(f"Current slot: {beacon_chain.current_slot}")
@@ -157,7 +158,6 @@ async def run_services(
validator_service_args = ValidatorDutyServiceOptions(
multi_beacon_node=multi_beacon_node,
beacon_chain=beacon_chain,
spec=spec,
remote_signer=remote_signer,
validator_status_tracker_service=validator_status_tracker_service,
scheduler=scheduler,
46 changes: 25 additions & 21 deletions src/providers/beacon_chain.py
Original file line number Diff line number Diff line change
@@ -6,58 +6,44 @@
from math import floor
from typing import TYPE_CHECKING, Any

from schemas import SchemaRemoteSigner
from schemas import SchemaBeaconAPI, SchemaRemoteSigner
from spec._ascii import ELECTRA as ELECTRA_ASCII_ART
from spec.base import Fork, Genesis, Spec
from tasks import TaskManager

if TYPE_CHECKING:
from collections.abc import Callable, Coroutine

from providers import MultiBeaconNode


class BeaconChain:
def __init__(
self,
spec: Spec,
multi_beacon_node: "MultiBeaconNode",
task_manager: TaskManager,
):
self.logger = logging.getLogger(self.__class__.__name__)
self.logger.setLevel(logging.getLogger().level)

self.spec = spec
self.multi_beacon_node = multi_beacon_node
self.task_manager = task_manager

self.genesis = Genesis()
self.current_fork_version = SchemaBeaconAPI.ForkVersion.DENEB

self.new_slot_handlers: list[
Callable[[int, bool], Coroutine[Any, Any, None]]
] = []

self.task_manager.submit_task(self.on_new_slot())

@property
def genesis(self) -> Genesis:
return next(
bn.genesis for bn in self.multi_beacon_node.beacon_nodes if bn.initialized
)

def get_fork(self, slot: int) -> Fork:
slot_epoch = slot // self.spec.SLOTS_PER_EPOCH

if (
hasattr(self.spec, "ELECTRA_FORK_EPOCH")
and slot_epoch >= self.spec.ELECTRA_FORK_EPOCH
):
if slot_epoch >= self.spec.ELECTRA_FORK_EPOCH:
return Fork(
previous_version=self.spec.DENEB_FORK_VERSION,
current_version=self.spec.ELECTRA_FORK_VERSION,
epoch=self.spec.ELECTRA_FORK_EPOCH,
)
if (
hasattr(self.spec, "DENEB_FORK_EPOCH")
and slot_epoch >= self.spec.DENEB_FORK_EPOCH
):
if slot_epoch >= self.spec.DENEB_FORK_EPOCH:
return Fork(
previous_version=self.spec.CAPELLA_FORK_VERSION,
current_version=self.spec.DENEB_FORK_VERSION,
@@ -71,6 +57,18 @@ def get_fork_info(self, slot: int) -> SchemaRemoteSigner.ForkInfo:
genesis_validators_root=self.genesis.genesis_validators_root.to_obj(),
)

def initialize(self, genesis: Genesis) -> None:
self.genesis = genesis

current_epoch = self.current_slot // self.spec.SLOTS_PER_EPOCH
if current_epoch >= self.spec.ELECTRA_FORK_EPOCH:
self.current_fork_version = SchemaBeaconAPI.ForkVersion.ELECTRA
else:
self.current_fork_version = SchemaBeaconAPI.ForkVersion.DENEB

def start_slot_ticker(self) -> None:
self.task_manager.submit_task(self.on_new_slot())

def get_datetime_for_slot(self, slot: int) -> datetime.datetime:
slot_timestamp = self.genesis.genesis_time + slot * self.spec.SECONDS_PER_SLOT
return datetime.datetime.fromtimestamp(slot_timestamp, tz=datetime.UTC)
@@ -105,6 +103,12 @@ async def on_new_slot(self) -> None:
self.logger.info(f"Slot {_current_slot}")
_is_new_epoch = _current_slot % self.spec.SLOTS_PER_EPOCH == 0

if _is_new_epoch:
_current_epoch = _current_slot // self.spec.SLOTS_PER_EPOCH
if _current_epoch == self.spec.ELECTRA_FORK_EPOCH:
self.current_fork_version = SchemaBeaconAPI.ForkVersion.ELECTRA
self.logger.info(f"Electra fork epoch reached! {ELECTRA_ASCII_ART}")

for handler in self.new_slot_handlers:
self.task_manager.submit_task(handler(_current_slot, _is_new_epoch))

Loading