{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Ledger.Core.Utils (
  unsafeBoundRational,
  testGlobals,
  mkDummySafeHash,
  txInAt,
)
where

import Cardano.Ledger.BaseTypes (
  EpochSize (..),
  Globals (..),
  Network (..),
  mkActiveSlotCoeff,
 )
import Cardano.Ledger.Core
import Cardano.Ledger.Crypto (Crypto (..))
import Cardano.Ledger.SafeHash (SafeHash, unsafeMakeSafeHash)
import Cardano.Ledger.TxIn (TxIn, mkTxInPartial)
import Cardano.Slotting.EpochInfo (fixedEpochInfo)
import Cardano.Slotting.Time (SystemStart (..), mkSlotLength)
import Data.Data (Proxy)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import Test.Cardano.Ledger.Binary.Random (mkDummyHash)
import Test.Cardano.Ledger.Common
import Test.Cardano.Ledger.Core.Rational (unsafeBoundRational)

testGlobals :: Globals
testGlobals :: Globals
testGlobals =
  Globals
    { epochInfo :: EpochInfo (Either Text)
epochInfo = forall (m :: * -> *).
Monad m =>
EpochSize -> SlotLength -> EpochInfo m
fixedEpochInfo (Word64 -> EpochSize
EpochSize Word64
100) (NominalDiffTime -> SlotLength
mkSlotLength NominalDiffTime
1)
    , slotsPerKESPeriod :: Word64
slotsPerKESPeriod = Word64
20
    , stabilityWindow :: Word64
stabilityWindow = Word64
33
    , randomnessStabilisationWindow :: Word64
randomnessStabilisationWindow = Word64
33
    , securityParameter :: Word64
securityParameter = Word64
10
    , maxKESEvo :: Word64
maxKESEvo = Word64
10
    , quorum :: Word64
quorum = Word64
5
    , maxLovelaceSupply :: Word64
maxLovelaceSupply = Word64
45 forall a. Num a => a -> a -> a
* Word64
1000 forall a. Num a => a -> a -> a
* Word64
1000 forall a. Num a => a -> a -> a
* Word64
1000 forall a. Num a => a -> a -> a
* Word64
1000 forall a. Num a => a -> a -> a
* Word64
1000
    , activeSlotCoeff :: ActiveSlotCoeff
activeSlotCoeff = PositiveUnitInterval -> ActiveSlotCoeff
mkActiveSlotCoeff forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r.
(HasCallStack, Typeable r, BoundedRational r) =>
Rational -> r
unsafeBoundRational forall a b. (a -> b) -> a -> b
$ Rational
0.9
    , networkId :: Network
networkId = Network
Testnet
    , systemStart :: SystemStart
systemStart = UTCTime -> SystemStart
SystemStart forall a b. (a -> b) -> a -> b
$ NominalDiffTime -> UTCTime
posixSecondsToUTCTime NominalDiffTime
0
    }

mkDummySafeHash :: forall c a. Crypto c => Proxy c -> Int -> SafeHash c a
mkDummySafeHash :: forall c a. Crypto c => Proxy c -> Int -> SafeHash c a
mkDummySafeHash Proxy c
_ = forall c index. Hash (HASH c) index -> SafeHash c index
unsafeMakeSafeHash forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall h a b. (HashAlgorithm h, EncCBOR a) => a -> Hash h b
mkDummyHash @(HASH c)

txInAt :: (HasCallStack, Integral i, EraTx era) => i -> Tx era -> TxIn (EraCrypto era)
txInAt :: forall i era.
(HasCallStack, Integral i, EraTx era) =>
i -> Tx era -> TxIn (EraCrypto era)
txInAt i
index Tx era
tx =
  let txId :: TxId (EraCrypto era)
txId = forall era. EraTx era => Tx era -> TxId (EraCrypto era)
txIdTx Tx era
tx
   in forall c. HasCallStack => TxId c -> Integer -> TxIn c
mkTxInPartial TxId (EraCrypto era)
txId (forall a. Integral a => a -> Integer
toInteger i
index)