{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}

module Cardano.Chain.Slotting.EpochSlots
  ( EpochSlots (..),
    WithEpochSlots (..),
    epochFirstSlot,
  )
where

import Cardano.Binary (FromCBOR (..), ToCBOR (..))
import Cardano.Chain.Slotting.EpochNumber
import Cardano.Chain.Slotting.SlotNumber
import Cardano.Prelude
import Data.Data (Data)
import Formatting.Buildable (Buildable)
import NoThunks.Class (NoThunks (..))

-- | The number of slots per epoch.
newtype EpochSlots = EpochSlots
  { EpochSlots -> Word64
unEpochSlots :: Word64
  }
  deriving (Typeable EpochSlots
DataType
Constr
Typeable EpochSlots
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> EpochSlots -> c EpochSlots)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c EpochSlots)
-> (EpochSlots -> Constr)
-> (EpochSlots -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c EpochSlots))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c EpochSlots))
-> ((forall b. Data b => b -> b) -> EpochSlots -> EpochSlots)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> EpochSlots -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> EpochSlots -> r)
-> (forall u. (forall d. Data d => d -> u) -> EpochSlots -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> EpochSlots -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> EpochSlots -> m EpochSlots)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> EpochSlots -> m EpochSlots)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> EpochSlots -> m EpochSlots)
-> Data EpochSlots
EpochSlots -> DataType
EpochSlots -> Constr
(forall b. Data b => b -> b) -> EpochSlots -> EpochSlots
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> EpochSlots -> c EpochSlots
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c EpochSlots
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> EpochSlots -> u
forall u. (forall d. Data d => d -> u) -> EpochSlots -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> EpochSlots -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> EpochSlots -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> EpochSlots -> m EpochSlots
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> EpochSlots -> m EpochSlots
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c EpochSlots
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> EpochSlots -> c EpochSlots
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c EpochSlots)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c EpochSlots)
$cEpochSlots :: Constr
$tEpochSlots :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> EpochSlots -> m EpochSlots
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> EpochSlots -> m EpochSlots
gmapMp :: (forall d. Data d => d -> m d) -> EpochSlots -> m EpochSlots
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> EpochSlots -> m EpochSlots
gmapM :: (forall d. Data d => d -> m d) -> EpochSlots -> m EpochSlots
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> EpochSlots -> m EpochSlots
gmapQi :: Int -> (forall d. Data d => d -> u) -> EpochSlots -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> EpochSlots -> u
gmapQ :: (forall d. Data d => d -> u) -> EpochSlots -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> EpochSlots -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> EpochSlots -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> EpochSlots -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> EpochSlots -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> EpochSlots -> r
gmapT :: (forall b. Data b => b -> b) -> EpochSlots -> EpochSlots
$cgmapT :: (forall b. Data b => b -> b) -> EpochSlots -> EpochSlots
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c EpochSlots)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c EpochSlots)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c EpochSlots)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c EpochSlots)
dataTypeOf :: EpochSlots -> DataType
$cdataTypeOf :: EpochSlots -> DataType
toConstr :: EpochSlots -> Constr
$ctoConstr :: EpochSlots -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c EpochSlots
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c EpochSlots
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> EpochSlots -> c EpochSlots
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> EpochSlots -> c EpochSlots
$cp1Data :: Typeable EpochSlots
Data, EpochSlots -> EpochSlots -> Bool
(EpochSlots -> EpochSlots -> Bool)
-> (EpochSlots -> EpochSlots -> Bool) -> Eq EpochSlots
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EpochSlots -> EpochSlots -> Bool
$c/= :: EpochSlots -> EpochSlots -> Bool
== :: EpochSlots -> EpochSlots -> Bool
$c== :: EpochSlots -> EpochSlots -> Bool
Eq, Eq EpochSlots
Eq EpochSlots
-> (EpochSlots -> EpochSlots -> Ordering)
-> (EpochSlots -> EpochSlots -> Bool)
-> (EpochSlots -> EpochSlots -> Bool)
-> (EpochSlots -> EpochSlots -> Bool)
-> (EpochSlots -> EpochSlots -> Bool)
-> (EpochSlots -> EpochSlots -> EpochSlots)
-> (EpochSlots -> EpochSlots -> EpochSlots)
-> Ord EpochSlots
EpochSlots -> EpochSlots -> Bool
EpochSlots -> EpochSlots -> Ordering
EpochSlots -> EpochSlots -> EpochSlots
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: EpochSlots -> EpochSlots -> EpochSlots
$cmin :: EpochSlots -> EpochSlots -> EpochSlots
max :: EpochSlots -> EpochSlots -> EpochSlots
$cmax :: EpochSlots -> EpochSlots -> EpochSlots
>= :: EpochSlots -> EpochSlots -> Bool
$c>= :: EpochSlots -> EpochSlots -> Bool
> :: EpochSlots -> EpochSlots -> Bool
$c> :: EpochSlots -> EpochSlots -> Bool
<= :: EpochSlots -> EpochSlots -> Bool
$c<= :: EpochSlots -> EpochSlots -> Bool
< :: EpochSlots -> EpochSlots -> Bool
$c< :: EpochSlots -> EpochSlots -> Bool
compare :: EpochSlots -> EpochSlots -> Ordering
$ccompare :: EpochSlots -> EpochSlots -> Ordering
$cp1Ord :: Eq EpochSlots
Ord, ReadPrec [EpochSlots]
ReadPrec EpochSlots
Int -> ReadS EpochSlots
ReadS [EpochSlots]
(Int -> ReadS EpochSlots)
-> ReadS [EpochSlots]
-> ReadPrec EpochSlots
-> ReadPrec [EpochSlots]
-> Read EpochSlots
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [EpochSlots]
$creadListPrec :: ReadPrec [EpochSlots]
readPrec :: ReadPrec EpochSlots
$creadPrec :: ReadPrec EpochSlots
readList :: ReadS [EpochSlots]
$creadList :: ReadS [EpochSlots]
readsPrec :: Int -> ReadS EpochSlots
$creadsPrec :: Int -> ReadS EpochSlots
Read, Int -> EpochSlots -> ShowS
[EpochSlots] -> ShowS
EpochSlots -> String
(Int -> EpochSlots -> ShowS)
-> (EpochSlots -> String)
-> ([EpochSlots] -> ShowS)
-> Show EpochSlots
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EpochSlots] -> ShowS
$cshowList :: [EpochSlots] -> ShowS
show :: EpochSlots -> String
$cshow :: EpochSlots -> String
showsPrec :: Int -> EpochSlots -> ShowS
$cshowsPrec :: Int -> EpochSlots -> ShowS
Show, EpochSlots -> Builder
(EpochSlots -> Builder) -> Buildable EpochSlots
forall p. (p -> Builder) -> Buildable p
build :: EpochSlots -> Builder
$cbuild :: EpochSlots -> Builder
Buildable, (forall x. EpochSlots -> Rep EpochSlots x)
-> (forall x. Rep EpochSlots x -> EpochSlots) -> Generic EpochSlots
forall x. Rep EpochSlots x -> EpochSlots
forall x. EpochSlots -> Rep EpochSlots x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep EpochSlots x -> EpochSlots
$cfrom :: forall x. EpochSlots -> Rep EpochSlots x
Generic, Context -> EpochSlots -> IO (Maybe ThunkInfo)
Proxy EpochSlots -> String
(Context -> EpochSlots -> IO (Maybe ThunkInfo))
-> (Context -> EpochSlots -> IO (Maybe ThunkInfo))
-> (Proxy EpochSlots -> String)
-> NoThunks EpochSlots
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy EpochSlots -> String
$cshowTypeOf :: Proxy EpochSlots -> String
wNoThunks :: Context -> EpochSlots -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> EpochSlots -> IO (Maybe ThunkInfo)
noThunks :: Context -> EpochSlots -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> EpochSlots -> IO (Maybe ThunkInfo)
NoThunks)

instance ToCBOR EpochSlots where
  toCBOR :: EpochSlots -> Encoding
toCBOR = Word64 -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Word64 -> Encoding)
-> (EpochSlots -> Word64) -> EpochSlots -> Encoding
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. EpochSlots -> Word64
unEpochSlots

instance FromCBOR EpochSlots where
  fromCBOR :: Decoder s EpochSlots
fromCBOR = Word64 -> EpochSlots
EpochSlots (Word64 -> EpochSlots) -> Decoder s Word64 -> Decoder s EpochSlots
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall a s. FromCBOR a => Decoder s a
fromCBOR

-- | Data with an accompanying slots per epoch context.
data WithEpochSlots a = WithEpochSlots
  { WithEpochSlots a -> EpochSlots
epochSlots :: EpochSlots,
    WithEpochSlots a -> a
unWithEpochSlots :: a
  }
  deriving (Int -> WithEpochSlots a -> ShowS
[WithEpochSlots a] -> ShowS
WithEpochSlots a -> String
(Int -> WithEpochSlots a -> ShowS)
-> (WithEpochSlots a -> String)
-> ([WithEpochSlots a] -> ShowS)
-> Show (WithEpochSlots a)
forall a. Show a => Int -> WithEpochSlots a -> ShowS
forall a. Show a => [WithEpochSlots a] -> ShowS
forall a. Show a => WithEpochSlots a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WithEpochSlots a] -> ShowS
$cshowList :: forall a. Show a => [WithEpochSlots a] -> ShowS
show :: WithEpochSlots a -> String
$cshow :: forall a. Show a => WithEpochSlots a -> String
showsPrec :: Int -> WithEpochSlots a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> WithEpochSlots a -> ShowS
Show, WithEpochSlots a -> WithEpochSlots a -> Bool
(WithEpochSlots a -> WithEpochSlots a -> Bool)
-> (WithEpochSlots a -> WithEpochSlots a -> Bool)
-> Eq (WithEpochSlots a)
forall a. Eq a => WithEpochSlots a -> WithEpochSlots a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WithEpochSlots a -> WithEpochSlots a -> Bool
$c/= :: forall a. Eq a => WithEpochSlots a -> WithEpochSlots a -> Bool
== :: WithEpochSlots a -> WithEpochSlots a -> Bool
$c== :: forall a. Eq a => WithEpochSlots a -> WithEpochSlots a -> Bool
Eq)

-- | Calculate the first slot in an epoch.
--
-- Note that this function will give an undetermined result if Byron is not the
-- first and only era - a more robust method should use 'EpochInfo' from
-- cardano-slotting.
epochFirstSlot :: EpochSlots -> EpochNumber -> SlotNumber
epochFirstSlot :: EpochSlots -> EpochNumber -> SlotNumber
epochFirstSlot (EpochSlots Word64
n) (EpochNumber Word64
k) = Word64 -> SlotNumber
SlotNumber (Word64 -> SlotNumber) -> Word64 -> SlotNumber
forall a b. (a -> b) -> a -> b
$ Word64
n Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
k