Skip to content

Commit

Permalink
Add comments and shortcuts to Cardano.Slotting.EpochInfo.Impl
Browse files Browse the repository at this point in the history
These shortcuts are used in eg the Consensus Layer mocks and tests.
  • Loading branch information
nfrisby committed Apr 7, 2021
1 parent 0f0f139 commit 82ac084
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions slotting/src/Cardano/Slotting/EpochInfo/Impl.hs
@@ -1,20 +1,36 @@
-- | For use in trivial cases, such as in mocks, tests, etc.
module Cardano.Slotting.EpochInfo.Impl
( fixedEpochInfo,
-- * Shortcuts
fixedEpochInfoEpoch,
fixedEpochInfoFirst,
)
where

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)

0 comments on commit 82ac084

Please sign in to comment.