{-# LANGUAGE TypeApplications #-}

module Shelley.Spec.Ledger.StabilityWindow
  ( computeStabilityWindow,
    computeRandomnessStabilisationWindow,
  )
where

import Data.Word (Word64)
import Shelley.Spec.Ledger.BaseTypes

-- | Calculate the stability window (e.g. the number of slots needed for a block
-- to become stable) from the security param and the active slot coefficient.
--
-- The value 3k/f is determined to be a suitabe value as per
-- https://docs.google.com/document/d/1B8BNMx8jVWRjYiUBOaI3jfZ7dQNvNTSDODvT5iOuYCU/edit#heading=h.qh2zcajmu6hm
computeStabilityWindow ::
  Word64 ->
  ActiveSlotCoeff ->
  Word64
computeStabilityWindow :: Word64 -> ActiveSlotCoeff -> Word64
computeStabilityWindow Word64
k ActiveSlotCoeff
asc =
  Double -> Word64
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (Double -> Word64) -> Double -> Word64
forall a b. (a -> b) -> a -> b
$ Word64 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral @_ @Double (Word64
3 Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
k) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Rational -> Double
forall a. Fractional a => Rational -> a
fromRational (Ratio Word64 -> Rational
forall a. Real a => a -> Rational
toRational Ratio Word64
f)
  where
    f :: Ratio Word64
f = UnitInterval -> Ratio Word64
intervalValue (UnitInterval -> Ratio Word64)
-> (ActiveSlotCoeff -> UnitInterval)
-> ActiveSlotCoeff
-> Ratio Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ActiveSlotCoeff -> UnitInterval
activeSlotVal (ActiveSlotCoeff -> Ratio Word64)
-> ActiveSlotCoeff -> Ratio Word64
forall a b. (a -> b) -> a -> b
$ ActiveSlotCoeff
asc

-- | Calculate the randomness stabilisation window from the security param and
-- the active slot coefficient.
--
-- The value 4k/f is determined to be a suitabe value as per
-- https://docs.google.com/document/d/1B8BNMx8jVWRjYiUBOaI3jfZ7dQNvNTSDODvT5iOuYCU/edit#heading=h.qh2zcajmu6hm
computeRandomnessStabilisationWindow ::
  Word64 ->
  ActiveSlotCoeff ->
  Word64
computeRandomnessStabilisationWindow :: Word64 -> ActiveSlotCoeff -> Word64
computeRandomnessStabilisationWindow Word64
k ActiveSlotCoeff
asc =
  Double -> Word64
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (Double -> Word64) -> Double -> Word64
forall a b. (a -> b) -> a -> b
$ Word64 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral @_ @Double (Word64
4 Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
k) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Rational -> Double
forall a. Fractional a => Rational -> a
fromRational (Ratio Word64 -> Rational
forall a. Real a => a -> Rational
toRational Ratio Word64
f)
  where
    f :: Ratio Word64
f = UnitInterval -> Ratio Word64
intervalValue (UnitInterval -> Ratio Word64)
-> (ActiveSlotCoeff -> UnitInterval)
-> ActiveSlotCoeff
-> Ratio Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ActiveSlotCoeff -> UnitInterval
activeSlotVal (ActiveSlotCoeff -> Ratio Word64)
-> ActiveSlotCoeff -> Ratio Word64
forall a b. (a -> b) -> a -> b
$ ActiveSlotCoeff
asc