From 82ac0844e19ba2fe1b777a4deb4e2b1765adbb86 Mon Sep 17 00:00:00 2001 From: Nicolas Frisby Date: Tue, 6 Apr 2021 17:20:34 -0700 Subject: [PATCH] Add comments and shortcuts to Cardano.Slotting.EpochInfo.Impl These shortcuts are used in eg the Consensus Layer mocks and tests. --- .../src/Cardano/Slotting/EpochInfo/Impl.hs | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/slotting/src/Cardano/Slotting/EpochInfo/Impl.hs b/slotting/src/Cardano/Slotting/EpochInfo/Impl.hs index ecccbbb935..d1cba8d70d 100644 --- a/slotting/src/Cardano/Slotting/EpochInfo/Impl.hs +++ b/slotting/src/Cardano/Slotting/EpochInfo/Impl.hs @@ -1,5 +1,9 @@ +-- | For use in trivial cases, such as in mocks, tests, etc. module Cardano.Slotting.EpochInfo.Impl ( fixedEpochInfo, + -- * Shortcuts + fixedEpochInfoEpoch, + fixedEpochInfoFirst, ) where @@ -7,14 +11,26 @@ import Cardano.Slotting.EpochInfo.API import Cardano.Slotting.Slot (EpochNo (..), EpochSize (..), SlotNo (..)) import Cardano.Slotting.Time (RelativeTime (..), SlotLength, getSlotLength) +-- | The 'EpochInfo' induced by assuming the epoch size and slot length are +-- fixed for the entire system lifetime fixedEpochInfo :: Monad m => EpochSize -> SlotLength -> EpochInfo m fixedEpochInfo (EpochSize size) slotLength = EpochInfo { epochInfoSize_ = \_ -> return $ EpochSize size, - epochInfoFirst_ = \(EpochNo epochNo) -> - return $ SlotNo (epochNo * size), - epochInfoEpoch_ = \(SlotNo slot) -> - return $ EpochNo (slot `div` size), + epochInfoFirst_ = \e -> return $ fixedEpochInfoFirst (EpochSize size) e, + epochInfoEpoch_ = \sl -> return $ fixedEpochInfoEpoch (EpochSize size) sl, epochInfoSlotToRelativeTime_ = \(SlotNo slot) -> return $ RelativeTime (fromIntegral slot * getSlotLength slotLength) } + +-- | The pure computation underlying 'epochInfoFirst' applied to +-- 'fixedEpochInfo' +fixedEpochInfoFirst :: EpochSize -> EpochNo -> SlotNo +fixedEpochInfoFirst (EpochSize size) (EpochNo epochNo) = + SlotNo (epochNo * size) + +-- | The pure computation underlying 'epochInfoEpoch' applied to +-- 'fixedEpochInfo' +fixedEpochInfoEpoch :: EpochSize -> SlotNo -> EpochNo +fixedEpochInfoEpoch (EpochSize size) (SlotNo slot) = + EpochNo (slot `div` size)