{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Ledger.State.SnapShots (
  preDijkstraMaxPool,
  z0Of,
  genNOpt,
  genSigmaAndRelativePledge,
  genMaxPledgeLeverage,
  leverageCap,
  noLeverageCap,
) where

import Cardano.Ledger.BaseTypes (
  BoundedRational (..),
  NonNegativeInterval,
  NonZero,
  StrictMaybe (..),
  knownNonZeroBounded,
  nonZeroOr,
  unNonZero,
 )
import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Core (MaxPledgeLeverage (..))
import Data.Ratio ((%))
import Data.Word (Word16)
import Test.Cardano.Ledger.Common
import Test.Cardano.Ledger.Core.Arbitrary ()
import Test.Cardano.Ledger.Core.Rational ((%!))

-- | A frozen copy of the reward pot formula as it was before the maximum pledge
-- leverage was introduced in Dijkstra. Used to ensure that a pool that is not
-- subject to a leverage cap is rewarded exactly as it was in the previous eras.
preDijkstraMaxPool ::
  NonNegativeInterval ->
  NonZero Word16 ->
  Coin ->
  Rational ->
  Rational ->
  Coin
preDijkstraMaxPool :: NonNegativeInterval
-> NonZero Word16 -> Coin -> Rational -> Rational -> Coin
preDijkstraMaxPool NonNegativeInterval
a0 NonZero Word16
nOpt (Coin Integer
r) Rational
sigma Rational
pR = Integer -> Coin
Coin (Integer -> Coin) -> Integer -> Coin
forall a b. (a -> b) -> a -> b
$ Rational -> Integer
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Rational
factor1 Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* Rational
factor2)
  where
    z0 :: Rational
z0 = NonZero Word16 -> Rational
z0Of NonZero Word16
nOpt
    sigma' :: Rational
sigma' = Rational -> Rational -> Rational
forall a. Ord a => a -> a -> a
min Rational
sigma Rational
z0
    p' :: Rational
p' = Rational -> Rational -> Rational
forall a. Ord a => a -> a -> a
min Rational
pR Rational
z0
    factor1 :: Rational
factor1 = (Integer
r Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Integer
1) Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ (Rational
1 Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+ NonNegativeInterval -> Rational
forall r. BoundedRational r => r -> Rational
unboundRational NonNegativeInterval
a0)
    factor2 :: Rational
factor2 = Rational
sigma' Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+ Rational
p' Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* NonNegativeInterval -> Rational
forall r. BoundedRational r => r -> Rational
unboundRational NonNegativeInterval
a0 Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* Rational
factor3
    factor3 :: Rational
factor3 = (Rational
sigma' Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
- Rational
p' Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* Rational
factor4) Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Rational
z0
    factor4 :: Rational
factor4 = (Rational
z0 Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
- Rational
sigma') Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Rational
z0

-- | @z0 = 1/k@, the global saturation point.
z0Of :: NonZero Word16 -> Rational
z0Of :: NonZero Word16 -> Rational
z0Of NonZero Word16
nOpt = Integer
1 Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Word16 -> Integer
forall a. Integral a => a -> Integer
toInteger (NonZero Word16 -> Word16
forall a. NonZero a -> a
unNonZero NonZero Word16
nOpt)

genNOpt :: Gen (NonZero Word16)
genNOpt :: Gen (NonZero Word16)
genNOpt = do
  n <- (Word16, Word16) -> Gen Word16
forall a. Random a => (a, a) -> Gen a
choose (Word16
1, Word16
2000)
  pure (n `nonZeroOr` knownNonZeroBounded @1)

-- | A pool's relative stake σ together with its relative pledge.
--
-- Both are generated the way they actually occur on chain, otherwise the leverage
-- cap almost never binds and the properties below become vacuous:
--
-- * σ is generated around the saturation point @z0 = 1/k@, rather than uniformly in
--   [0, 1], since @z0@ is where the reward pot stops growing.
-- * the pledge is a fraction of the stake, since a pool that pledges more than its
--   own stake receives no rewards at all. The leverage of the pool, σ over the
--   pledge, ranges over the whole interval that the maximum pledge leverage can be
--   set to, and beyond.
genSigmaAndRelativePledge :: NonZero Word16 -> Gen (Rational, Rational)
genSigmaAndRelativePledge :: NonZero Word16 -> Gen (Rational, Rational)
genSigmaAndRelativePledge NonZero Word16
nOpt = do
  saturation <- (Integer, Integer) -> Gen Integer
forall a. Random a => (a, a) -> Gen a
choose (Integer
0, Integer
2_000)
  leverage <- choose (1, 20_000)
  let sigma = NonZero Word16 -> Rational
z0Of NonZero Word16
nOpt Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* (Integer
saturation Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Integer
1_000)
  pure (sigma, sigma / (leverage % 1))

genMaxPledgeLeverage :: Gen NonNegativeInterval
genMaxPledgeLeverage :: Gen NonNegativeInterval
genMaxPledgeLeverage = do
  l <- (Integer, Integer) -> Gen Integer
forall a. Random a => (a, a) -> Gen a
choose (Integer
1, Integer
10_000)
  pure (l %! 1)

-- | No maximum pledge leverage, ie. the behavior of all the eras prior to Dijkstra.
noLeverageCap :: MaxPledgeLeverage
noLeverageCap :: MaxPledgeLeverage
noLeverageCap = StrictMaybe NonNegativeInterval -> MaxPledgeLeverage
MaxPledgeLeverage StrictMaybe NonNegativeInterval
forall a. StrictMaybe a
SNothing

leverageCap :: NonNegativeInterval -> MaxPledgeLeverage
leverageCap :: NonNegativeInterval -> MaxPledgeLeverage
leverageCap = StrictMaybe NonNegativeInterval -> MaxPledgeLeverage
MaxPledgeLeverage (StrictMaybe NonNegativeInterval -> MaxPledgeLeverage)
-> (NonNegativeInterval -> StrictMaybe NonNegativeInterval)
-> NonNegativeInterval
-> MaxPledgeLeverage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonNegativeInterval -> StrictMaybe NonNegativeInterval
forall a. a -> StrictMaybe a
SJust