module Ouroboros.Consensus.HardFork.History.Util (
    -- * Adding and subtracting slots/epochs
    addEpochs
  , addSlots
  , countEpochs
  , countSlots
  , subSlots
  ) where

import           Control.Exception (assert)
import           Data.Word
import           GHC.Stack
import           Ouroboros.Consensus.Block
import           Ouroboros.Consensus.Util.RedundantConstraints

{-------------------------------------------------------------------------------
  Adding and subtracting slots/epochs
-------------------------------------------------------------------------------}

addSlots :: Word64 -> SlotNo -> SlotNo
addSlots :: Word64 -> SlotNo -> SlotNo
addSlots Word64
n (SlotNo Word64
x) = Word64 -> SlotNo
SlotNo (Word64
x Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
n)

subSlots :: Word64 -> SlotNo -> SlotNo
subSlots :: Word64 -> SlotNo -> SlotNo
subSlots Word64
n (SlotNo Word64
x) = Bool -> SlotNo -> SlotNo
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Word64
x Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
n) (SlotNo -> SlotNo) -> SlotNo -> SlotNo
forall a b. (a -> b) -> a -> b
$ Word64 -> SlotNo
SlotNo (Word64
x Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
n)

addEpochs :: Word64 -> EpochNo -> EpochNo
addEpochs :: Word64 -> EpochNo -> EpochNo
addEpochs Word64
n (EpochNo Word64
x) = Word64 -> EpochNo
EpochNo (Word64
x Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
n)

-- | @countSlots to fr@ counts the slots from @fr@ to @to@ (@to >= fr@)
countSlots :: HasCallStack => SlotNo -> SlotNo -> Word64
countSlots :: (?callStack::CallStack) => SlotNo -> SlotNo -> Word64
countSlots (SlotNo Word64
to) (SlotNo Word64
fr) = Bool -> Word64 -> Word64
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Word64
to Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
fr) (Word64 -> Word64) -> Word64 -> Word64
forall a b. (a -> b) -> a -> b
$ Word64
to Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
fr
  where
    ()
_ = Proxy (?callStack::CallStack) -> ()
forall (c :: Constraint) (proxy :: Constraint -> *).
c =>
proxy c -> ()
keepRedundantConstraint (Proxy (?callStack::CallStack)
forall {k} (t :: k). Proxy t
Proxy :: Proxy HasCallStack)

-- | @countEpochs to fr@ counts the epochs from @fr@ to @to@ (@to >= fr@)
countEpochs :: HasCallStack => EpochNo -> EpochNo -> Word64
countEpochs :: (?callStack::CallStack) => EpochNo -> EpochNo -> Word64
countEpochs (EpochNo Word64
to) (EpochNo Word64
fr) = Bool -> Word64 -> Word64
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Word64
to Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
fr) (Word64 -> Word64) -> Word64 -> Word64
forall a b. (a -> b) -> a -> b
$ Word64
to Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
fr
  where
    ()
_ = Proxy (?callStack::CallStack) -> ()
forall (c :: Constraint) (proxy :: Constraint -> *).
c =>
proxy c -> ()
keepRedundantConstraint (Proxy (?callStack::CallStack)
forall {k} (t :: k). Proxy t
Proxy :: Proxy HasCallStack)