From aaa5a5ad40b683fa4f8226391d51fc37a1a51b95 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Tue, 8 Mar 2022 11:38:58 +0100 Subject: [PATCH] add `start_slot` overload for sync periods (#3469) Adds a `start_slot` overload for `SyncCommitteePeriod` as a shortcut for `period.start_epoch.start_slot`. --- beacon_chain/spec/beacon_time.nim | 8 ++++++++ tests/test_beacon_time.nim | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/beacon_chain/spec/beacon_time.nim b/beacon_chain/spec/beacon_time.nim index 41d0ae10d..8d2270cdd 100644 --- a/beacon_chain/spec/beacon_time.nim +++ b/beacon_chain/spec/beacon_time.nim @@ -231,11 +231,19 @@ template is_sync_committee_period*(epoch: Epoch): bool = epoch.since_sync_committee_period_start() == 0 template start_epoch*(period: SyncCommitteePeriod): Epoch = + ## Return the start epoch of ``period``. const maxPeriod = SyncCommitteePeriod( FAR_FUTURE_EPOCH div EPOCHS_PER_SYNC_COMMITTEE_PERIOD) if period >= maxPeriod: FAR_FUTURE_EPOCH else: Epoch(period * EPOCHS_PER_SYNC_COMMITTEE_PERIOD) +template start_slot*(period: SyncCommitteePeriod): Slot = + ## Return the start slot of ``period``. + const maxPeriod = SyncCommitteePeriod( + FAR_FUTURE_SLOT div SLOTS_PER_SYNC_COMMITTEE_PERIOD) + if period >= maxPeriod: FAR_FUTURE_SLOT + else: Slot(period * SLOTS_PER_SYNC_COMMITTEE_PERIOD) + func `$`*(t: BeaconTime): string = if t.ns_since_genesis >= 0: $(timer.nanoseconds(t.ns_since_genesis)) diff --git a/tests/test_beacon_time.nim b/tests/test_beacon_time.nim index fbdef1aee..355534a5b 100644 --- a/tests/test_beacon_time.nim +++ b/tests/test_beacon_time.nim @@ -1,12 +1,16 @@ +# beacon_chain +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed and distributed under either of +# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). +# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). +# at your option. This file may not be copied, modified, or distributed except according to those terms. + {.used.} import unittest2, - ../beacon_chain/spec/beacon_time -{.used.} - suite "Beacon time": test "basics": let @@ -19,9 +23,11 @@ suite "Beacon time": # Roundtrip far times we treat these as "Infinitiy" FAR_FUTURE_SLOT.epoch.start_slot() == FAR_FUTURE_SLOT + FAR_FUTURE_SLOT.sync_committee_period.start_slot() == FAR_FUTURE_SLOT FAR_FUTURE_EPOCH.start_slot().epoch() == FAR_FUTURE_EPOCH FAR_FUTURE_SLOT.start_beacon_time().slotOrZero() == FAR_FUTURE_SLOT FAR_FUTURE_PERIOD.start_epoch().sync_committee_period() == FAR_FUTURE_PERIOD + FAR_FUTURE_PERIOD.start_slot().sync_committee_period() == FAR_FUTURE_PERIOD BeaconTime(ns_since_genesis: -10000000000).slotOrZero == Slot(0) Slot(5).since_epoch_start() == 5 @@ -35,6 +41,9 @@ suite "Beacon time": Epoch(3).start_slot.is_epoch() SyncCommitteePeriod(5).start_epoch().is_sync_committee_period() + SyncCommitteePeriod(5).start_slot().is_sync_committee_period() Epoch(5).start_slot.sync_committee_period == Epoch(5).sync_committee_period + SyncCommitteePeriod(5).start_slot.sync_committee_period == + SyncCommitteePeriod(5)