{-# 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 ((%!))
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
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)
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)
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