Skip to content

Commit

Permalink
feat(main): Update main entrypoint for restructured code
Browse files Browse the repository at this point in the history
* Call updated fetcher functions
* Delete unnecessary functions
* Update imports and use GracefulKiller constant
* Update docstrings
* Delete unnecessary pylint disable
  • Loading branch information
TobiWo committed Dec 17, 2022
1 parent 15298a9 commit 8cab26c
Showing 1 changed file with 22 additions and 56 deletions.
78 changes: 22 additions & 56 deletions duties/duties.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
"""Entrypoint for eth-duties to check for upcoming duties for one or many validators
"""
Entrypoint for the simple cli tool to check for upcoming duties for one or many validators
"""

# pylint: disable=import-error

from argparse import Namespace
from time import sleep
from typing import List, Callable
from logging import getLogger
from fetcher.fetcher import ValidatorDutyFetcher
from fetcher import fetcher
from fetcher.data_types import ValidatorDuty, DutyType
from fetcher.printer import print_time_to_next_duties
from helper.killer import GracefulKiller
from fetcher.logger import log_time_to_next_duties
from cli.cli import get_arguments
from protocol.protocol import get_current_slot
from constants.program import GRACEFUL_KILLER

__sort_duties: Callable[[ValidatorDuty], int] = lambda duty: duty.slot


def __fetch_validator_duties(
arguments: Namespace,
duty_fetcher: ValidatorDutyFetcher,
duties: List[ValidatorDuty],
) -> List[ValidatorDuty]:
def __fetch_validator_duties(duties: List[ValidatorDuty]) -> List[ValidatorDuty]:
"""Fetches upcoming validator duties
Args:
arguments (Namespace): CLI arguments
duty_fetcher (ValidatorDutyFetcher): Holds all logic for fetching validator duties
duties (List[ValidatorDuty]): List of validator duties for last logging interval
Returns:
List[ValidatorDuty]: Sorted list with all upcoming validator duties
"""
if not __is_current_data_outdated(duty_fetcher, duties):
if not __is_current_data_outdated(duties):
return duties
next_attestation_duties: dict[int, ValidatorDuty] = {}
if not arguments.omit_attestation_duties:
next_attestation_duties = duty_fetcher.get_next_attestation_duties()
next_proposing_duties = duty_fetcher.get_next_proposing_duties()
next_sync_committee_duties = duty_fetcher.get_next_sync_committee_duties()
next_attestation_duties = fetcher.get_next_attestation_duties()
next_proposing_duties = fetcher.get_next_proposing_duties()
next_sync_committee_duties = fetcher.get_next_sync_committee_duties()
duties = [
duty
for duties in [
Expand All @@ -53,10 +41,16 @@ def __fetch_validator_duties(
return duties


def __is_current_data_outdated(
duty_fetcher: ValidatorDutyFetcher, current_duties: List[ValidatorDuty]
) -> bool:
current_slot = get_current_slot(duty_fetcher.genesis_time)
def __is_current_data_outdated(current_duties: List[ValidatorDuty]) -> bool:
"""Checks if the current fetched validator duties are outdated.
Args:
current_duties (List[ValidatorDuty]): Current validator duties fetched during last interval
Returns:
bool: True if current data is outdated
"""
current_slot = get_current_slot()
first_non_sync_committee_duty = next(
filter(lambda duty: duty.type is not DutyType.SYNC_COMMITTEE, current_duties),
None,
Expand All @@ -70,40 +64,12 @@ def __is_current_data_outdated(
return True


def __create_validator_duty_fetcher_instance(
arguments: Namespace, graceful_killer: GracefulKiller
) -> ValidatorDutyFetcher:
"""Creates an instance of the class which holds logic for fetching validator duties
Args:
arguments (Namespace): CLI arguments
graceful_killer (GracefulKiller): Instance of helper class to shutdown program gracefully
Returns:
ValidatorDutyFetcher: Instance of ValidatorDutyFetcher which
holds logic for fetching validator duties
"""
if arguments.validators:
user_passed_validators = arguments.validators
else:
user_passed_validators = [
validator.strip() for validator in arguments.validator_file
]
return ValidatorDutyFetcher(
arguments.beacon_node, user_passed_validators, graceful_killer
)


if __name__ == "__main__":
killer = GracefulKiller()
args = get_arguments()
validator_duty_fetcher = __create_validator_duty_fetcher_instance(args, killer)
upcoming_duties: List[ValidatorDuty] = []
while not killer.kill_now:
upcoming_duties = __fetch_validator_duties(
args, validator_duty_fetcher, upcoming_duties
)
print_time_to_next_duties(upcoming_duties, validator_duty_fetcher.genesis_time)
while not GRACEFUL_KILLER.kill_now:
upcoming_duties = __fetch_validator_duties(upcoming_duties)
log_time_to_next_duties(upcoming_duties)
sleep(args.interval)
logger = getLogger(__name__)
logger.info("Happy staking. See you for next maintenance \U0001F642 !")

0 comments on commit 8cab26c

Please sign in to comment.