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

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

import Cardano.Chain.Slotting.EpochNumber
import Cardano.Chain.Slotting.SlotNumber
import Cardano.Ledger.Binary (
  DecCBOR (..),
  EncCBOR (..),
  FromCBOR (..),
  ToCBOR (..),
  fromByronCBOR,
  toByronCBOR,
 )
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
EpochSlots -> DataType
EpochSlots -> Constr
(forall b. Data b => b -> b) -> EpochSlots -> 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)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
Monad m =>
(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 :: forall u. Int -> (forall d. Data d => d -> u) -> EpochSlots -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> EpochSlots -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> EpochSlots -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> EpochSlots -> [u]
gmapQr :: forall r r'.
(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 :: forall r r'.
(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 (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(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 (t :: * -> *) (c :: * -> *).
Typeable t =>
(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 (c :: * -> *).
(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 (c :: * -> *).
(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
Data, EpochSlots -> EpochSlots -> Bool
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
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
Ord, ReadPrec [EpochSlots]
ReadPrec EpochSlots
Int -> ReadS EpochSlots
ReadS [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
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
forall p. (p -> Builder) -> Buildable p
build :: EpochSlots -> Builder
$cbuild :: EpochSlots -> Builder
Buildable, 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
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 = forall a. EncCBOR a => a -> Encoding
toByronCBOR

instance FromCBOR EpochSlots where
  fromCBOR :: forall s. Decoder s EpochSlots
fromCBOR = forall a s. DecCBOR a => Decoder s a
fromByronCBOR

instance EncCBOR EpochSlots where
  encCBOR :: EpochSlots -> Encoding
encCBOR = forall a. EncCBOR a => a -> Encoding
encCBOR 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 DecCBOR EpochSlots where
  decCBOR :: forall s. Decoder s EpochSlots
decCBOR = Word64 -> EpochSlots
EpochSlots forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. DecCBOR a => Decoder s a
decCBOR

-- | Data with an accompanying slots per epoch context.
data WithEpochSlots a = WithEpochSlots
  { forall a. WithEpochSlots a -> EpochSlots
epochSlots :: EpochSlots
  , forall a. WithEpochSlots a -> a
unWithEpochSlots :: a
  }
  deriving (Int -> WithEpochSlots a -> ShowS
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
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 forall a b. (a -> b) -> a -> b
$ Word64
n forall a. Num a => a -> a -> a
* Word64
k