Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
don't acquire seconds_per_slot from chain
Browse files Browse the repository at this point in the history
  • Loading branch information
ChihChengLiang committed Apr 10, 2019
1 parent e24abe4 commit 7e6e78d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
9 changes: 1 addition & 8 deletions tests/plugins/eth2/beacon/test_slot_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@ async def test_slot_ticker_ticking(event_bus, event_loop):
slot_ticker = SlotTicker(
genesis_slot=0,
genesis_time=0,
chain=None,
seconds_per_slot=1,
event_bus=event_bus,
)

def get_seconds_per_slot():
return 1

# mock get_seconds_per_slot
slot_ticker.get_seconds_per_slot = get_seconds_per_slot

asyncio.ensure_future(slot_ticker.run(), loop=event_loop)
await slot_ticker.events.started.wait()
try:
Expand Down
47 changes: 22 additions & 25 deletions trinity/plugins/eth2/beacon/slot_ticker.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import asyncio
import time
from eth2.beacon.typing import (
Slot,
Second,
)
from trinity.endpoint import TrinityEventBusEndpoint

from eth2.beacon.chains.base import (
BaseBeaconChain,
)
from trinity._utils.shellart import (
bold_green,
from cancel_token import (
CancelToken,
)
from lahja import (
BaseEvent,
BroadcastConfig,
)

from p2p.service import BaseService

from cancel_token import (
CancelToken,
from eth2.beacon.typing import (
Second,
Slot,
)
from p2p.service import (
BaseService,
)
from trinity._utils.shellart import (
bold_green,
)
from trinity.endpoint import (
TrinityEventBusEndpoint,
)

DEFAULT_CHECK_FREQUENCY = 5
Expand All @@ -38,30 +38,27 @@ def __init__(
self,
genesis_slot: Slot,
genesis_time: int,
chain: BaseBeaconChain,
seconds_per_slot: Second,
event_bus: TrinityEventBusEndpoint,
token: CancelToken = None) -> None:
super().__init__(token)
self.genesis_slot = genesis_slot
self.genesis_time = genesis_time
self.chain = chain
self.latest_slot = Slot(0)
# FIXME: seconds_per_slot is assumed to be constant here.
# Should it changed in the future fork, fix it as #491 described.
self.seconds_per_slot = seconds_per_slot
self.latest_slot = genesis_slot
self.event_bus = event_bus

async def _run(self) -> None:
self.run_daemon_task(self._keep_ticking())
await self.cancellation()

def get_seconds_per_slot(self) -> Second:
state_machine = self.chain.get_state_machine()
return state_machine.config.SECONDS_PER_SLOT

async def _keep_ticking(self) -> None:
while self.is_operational:
seconds_per_slot = self.get_seconds_per_slot()
elapsed_time = Second(int(time.time()) - self.genesis_time)
if elapsed_time >= seconds_per_slot:
slot = Slot(elapsed_time // seconds_per_slot + self.genesis_slot)
if elapsed_time >= self.seconds_per_slot:
slot = Slot(elapsed_time // self.seconds_per_slot + self.genesis_slot)
if slot > self.latest_slot:
self.logger.debug(
bold_green(f"New slot: {slot}\tElapsed time: {elapsed_time}")
Expand All @@ -74,4 +71,4 @@ async def _keep_ticking(self) -> None:
),
BroadcastConfig(internal=True),
)
await asyncio.sleep(seconds_per_slot // DEFAULT_CHECK_FREQUENCY)
await asyncio.sleep(self.seconds_per_slot // DEFAULT_CHECK_FREQUENCY)

0 comments on commit 7e6e78d

Please sign in to comment.