Skip to content

Commit

Permalink
Replace pytz with stdlib zoneinfo (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
eth2353 authored Jan 18, 2025
1 parent 76f639a commit 2fc73d3
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 30 deletions.
7 changes: 3 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ repos:
- id: mypy
additional_dependencies:
[
aiohttp==3.10.10,
msgspec==0.18.6,
pytest==8.3.3,
aiohttp==3.11.11,
msgspec==0.19.0,
pytest==8.3.4,
remerkleable==0.1.28,
types-pytz==2024.2.0.20240913
]

- repo: https://github.com/hadolint/hadolint
Expand Down
1 change: 0 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ opentelemetry-sdk
prometheus-client
pyroscope-io
pyroscope-otel
pytz
remerkleable
4 changes: 0 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,6 @@ pyroscope-otel==0.4.0 \
--hash=sha256:3dec6c9f1fa7935bdf246ac41de7afdad96f5571d8d1fccd5ddca5897abd38fa \
--hash=sha256:c74f9bcfdbcd3093c25032215e17924755fcb91ae7c5d061099d8bd51cee7505
# via -r requirements.in
pytz==2024.2 \
--hash=sha256:2aa355083c50a0f93fa581709deac0c9ad65cca8a9e9beac660adcbd493c798a \
--hash=sha256:31c7c1817eb7fae7ca4b8c7ee50c72f93aa2dd863de768e1ef4245d426aa0725
# via -r requirements.in
remerkleable==0.1.28 \
--hash=sha256:133cb1283f1d6fd2a020f47ee3653e93385acf2aa4ddc30f3d00eb9404751bda \
--hash=sha256:1e19e9a927961b29c4efa210795bfad5d20a28545a6f78f7552b1e4449c24890
Expand Down
5 changes: 2 additions & 3 deletions src/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
from pathlib import Path

import pytz
from apscheduler.schedulers.asyncio import AsyncIOScheduler

from args import CLIArgs
Expand All @@ -26,11 +25,11 @@

async def _wait_for_genesis(genesis_datetime: datetime.datetime) -> None:
# Waits for genesis to occur
time_to_genesis = genesis_datetime - datetime.datetime.now(tz=pytz.UTC)
time_to_genesis = genesis_datetime - datetime.datetime.now(tz=datetime.UTC)
while time_to_genesis.total_seconds() > 0:
_logger.info(f"Waiting for genesis - {time_to_genesis} remaining")
await asyncio.sleep(min(time_to_genesis.total_seconds(), 10))
time_to_genesis = genesis_datetime - datetime.datetime.now(tz=pytz.UTC)
time_to_genesis = genesis_datetime - datetime.datetime.now(tz=datetime.UTC)


def _register_event_handlers(
Expand Down
4 changes: 2 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import asyncio
import datetime
import functools
import logging
import signal
import sys
from pathlib import Path
from typing import TYPE_CHECKING

import pytz
from apscheduler.schedulers.asyncio import AsyncIOScheduler

from args import CLIArgs, parse_cli_args
Expand Down Expand Up @@ -38,7 +38,7 @@ async def main(cli_args: CLIArgs) -> None:
prep_datadir(data_dir=Path(cli_args.data_dir))

scheduler = AsyncIOScheduler(
timezone=pytz.UTC,
timezone=datetime.UTC,
job_defaults=dict(
coalesce=True, # default value
max_instances=1, # default value
Expand Down
15 changes: 7 additions & 8 deletions src/providers/beacon_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from math import floor
from typing import TYPE_CHECKING, Any

import pytz

from schemas import SchemaRemoteSigner
from spec.base import Fork, Genesis, Spec
from tasks import TaskManager
Expand Down Expand Up @@ -73,12 +71,12 @@ def get_fork_info(self, slot: int) -> SchemaRemoteSigner.ForkInfo:

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=pytz.UTC)
return datetime.datetime.fromtimestamp(slot_timestamp, tz=datetime.UTC)

def _get_slots_since_genesis(self) -> int:
seconds_elapsed = floor(datetime.datetime.now(tz=pytz.UTC).timestamp()) - int(
self.genesis.genesis_time
)
seconds_elapsed = floor(
datetime.datetime.now(tz=datetime.UTC).timestamp()
) - int(self.genesis.genesis_time)
seconds_elapsed = max(0, seconds_elapsed)
return seconds_elapsed // int(self.spec.SECONDS_PER_SLOT)

Expand All @@ -90,7 +88,8 @@ async def wait_for_next_slot(self) -> None:
# A slightly more accurate version of asyncio.sleep()
_next_slot = self.current_slot + 1
_delay = (
self.get_datetime_for_slot(_next_slot) - datetime.datetime.now(tz=pytz.UTC)
self.get_datetime_for_slot(_next_slot)
- datetime.datetime.now(tz=datetime.UTC)
).total_seconds()

# asyncio.sleep can be off by up to 16ms (on Windows)
Expand All @@ -112,7 +111,7 @@ async def on_new_slot(self) -> None:

def time_since_slot_start(self, slot: int) -> float:
return (
datetime.datetime.now(tz=pytz.UTC) - self.get_datetime_for_slot(slot)
datetime.datetime.now(tz=datetime.UTC) - self.get_datetime_for_slot(slot)
).total_seconds()

@property
Expand Down
5 changes: 2 additions & 3 deletions src/providers/multi_beacon_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from types import TracebackType
from typing import Any

import pytz
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from opentelemetry import trace
from opentelemetry.trace import Span
Expand Down Expand Up @@ -472,7 +471,7 @@ async def _produce_attestation_data_from_head_event(
head_match_count = 0
for coro in asyncio.as_completed(
tasks,
timeout=(deadline - datetime.datetime.now(tz=pytz.UTC)).total_seconds(),
timeout=(deadline - datetime.datetime.now(tz=datetime.UTC)).total_seconds(),
):
try:
host, att_data = await coro
Expand Down Expand Up @@ -517,7 +516,7 @@ async def _produce_attestation_data_without_head_event(
host_to_block_root: dict[str, str] = dict()
head_block_root_counter: Counter[str] = Counter()

while datetime.datetime.now(pytz.UTC) < deadline:
while datetime.datetime.now(datetime.UTC) < deadline:
_round_start = asyncio.get_running_loop().time()

tasks = [
Expand Down
3 changes: 1 addition & 2 deletions src/services/block_proposal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from collections import defaultdict
from typing import Unpack

import pytz
from opentelemetry import trace
from opentelemetry.trace import (
NonRecordingSpan,
Expand Down Expand Up @@ -201,7 +200,7 @@ async def register_validators(self, current_slot: int) -> None:
if v.index % slots_per_epoch == current_slot % slots_per_epoch
]

_timestamp = int(datetime.datetime.now(tz=pytz.UTC).timestamp())
_timestamp = int(datetime.datetime.now(tz=datetime.UTC).timestamp())

for i in range(0, len(validators_to_register), _batch_size):
validator_batch = validators_to_register[i : i + _batch_size]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from functools import partial

import pytest
import pytz
from aiohttp.web_exceptions import HTTPRequestTimeout
from aioresponses import CallbackResult, aioresponses

Expand Down Expand Up @@ -219,7 +218,7 @@ async def test_produce_attestation_data(
match="Failed to reach consensus on attestation data",
):
_ = await multi_beacon_node_three_inited_nodes.produce_attestation_data(
deadline=datetime.datetime.now(tz=pytz.UTC)
deadline=datetime.datetime.now(tz=datetime.UTC)
+ datetime.timedelta(seconds=0.1),
slot=123,
committee_index=3,
Expand All @@ -229,7 +228,8 @@ async def test_produce_attestation_data(

# We expect to reach consensus on attestation data here
att_data = await multi_beacon_node_three_inited_nodes.produce_attestation_data(
deadline=datetime.datetime.now(tz=pytz.UTC) + datetime.timedelta(seconds=1),
deadline=datetime.datetime.now(tz=datetime.UTC)
+ datetime.timedelta(seconds=1),
slot=123,
committee_index=3,
head_event=head_event,
Expand Down

0 comments on commit 2fc73d3

Please sign in to comment.