{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Test.Cardano.Ledger.Conformance.ExecSpecRule.Conway.Deleg (nameDelegCert) where

import Cardano.Ledger.BaseTypes (Inject (..))
import Cardano.Ledger.Conway
import Cardano.Ledger.Conway.TxCert (ConwayDelegCert (..))
import Cardano.Ledger.Credential (Credential)
import Cardano.Ledger.Keys (KeyRole (..))
import Constrained
import Data.Bifunctor (second)
import Data.Set (Set)
import qualified Lib as Agda
import Test.Cardano.Ledger.Conformance
import Test.Cardano.Ledger.Conformance.ExecSpecRule.Conway.Base (
  ConwayCertExecContext (..),
  conwayCertExecContextSpec,
 )
import Test.Cardano.Ledger.Conformance.ExecSpecRule.Core ()
import Test.Cardano.Ledger.Conformance.SpecTranslate.Conway.Base ()
import Test.Cardano.Ledger.Conformance.SpecTranslate.Conway.Cert ()
import Test.Cardano.Ledger.Conformance.SpecTranslate.Conway.Deleg ()
import Test.Cardano.Ledger.Constrained.Conway
import Test.Cardano.Ledger.Constrained.Conway.WitnessUniverse

instance
  Inject
    (WitUniv ConwayEra, ConwayCertExecContext ConwayEra)
    (Set (Credential 'DRepRole))
  where
  inject :: (WitUniv ConwayEra, ConwayCertExecContext ConwayEra)
-> Set (Credential 'DRepRole)
inject (WitUniv ConwayEra
_, ConwayCertExecContext ConwayEra
x) = forall era. ConwayCertExecContext era -> Set (Credential 'DRepRole)
ccecDelegatees ConwayCertExecContext ConwayEra
x

instance IsConwayUniv fn => ExecSpecRule fn "DELEG" ConwayEra where
  type ExecContext fn "DELEG" ConwayEra = (WitUniv ConwayEra, ConwayCertExecContext ConwayEra)

  genExecContext :: HasCallStack => Gen (ExecContext fn "DELEG" ConwayEra)
genExecContext = do
    WitUniv ConwayEra
univ <- forall era.
(GenScript era, HasWitness ScriptHash era) =>
Int -> Gen (WitUniv era)
genWitUniv @ConwayEra Int
300
    ConwayCertExecContext ConwayEra
ccec <- forall (fn :: [*] -> * -> *) a.
(HasCallStack, HasSpec fn a) =>
Specification fn a -> Gen a
genFromSpec @ConwayFn (forall (fn :: [*] -> * -> *) era.
(Reflect era, IsConwayUniv fn) =>
WitUniv era
-> Integer -> Specification fn (ConwayCertExecContext era)
conwayCertExecContextSpec WitUniv ConwayEra
univ Integer
5)
    forall (f :: * -> *) a. Applicative f => a -> f a
pure (WitUniv ConwayEra
univ, ConwayCertExecContext ConwayEra
ccec)

  environmentSpec :: HasCallStack =>
ExecContext fn "DELEG" ConwayEra
-> Specification fn (ExecEnvironment fn "DELEG" ConwayEra)
environmentSpec ExecContext fn "DELEG" ConwayEra
_ = forall era (fn :: [*] -> * -> *).
(EraSpecPParams era, IsConwayUniv fn) =>
Specification fn (ConwayDelegEnv era)
delegEnvSpec

  stateSpec :: HasCallStack =>
ExecContext fn "DELEG" ConwayEra
-> ExecEnvironment fn "DELEG" ConwayEra
-> Specification fn (ExecState fn "DELEG" ConwayEra)
stateSpec (WitUniv ConwayEra
univ, ConwayCertExecContext ConwayEra
ccec) ExecEnvironment fn "DELEG" ConwayEra
_env =
    forall (fn :: [*] -> * -> *) era.
(IsConwayUniv fn, EraSpecDeleg era, Era era) =>
WitUniv era
-> Set (Credential 'DRepRole)
-> Map RewardAccount Coin
-> Specification fn (CertState era)
certStateSpec @_ @ConwayEra WitUniv ConwayEra
univ (forall era. ConwayCertExecContext era -> Set (Credential 'DRepRole)
ccecDelegatees ConwayCertExecContext ConwayEra
ccec) (forall era. ConwayCertExecContext era -> Map RewardAccount Coin
ccecWithdrawals ConwayCertExecContext ConwayEra
ccec)

  signalSpec :: HasCallStack =>
ExecContext fn "DELEG" ConwayEra
-> ExecEnvironment fn "DELEG" ConwayEra
-> ExecState fn "DELEG" ConwayEra
-> Specification fn (ExecSignal fn "DELEG" ConwayEra)
signalSpec ExecContext fn "DELEG" ConwayEra
_ = forall (fn :: [*] -> * -> *) era.
(EraPParams era, IsConwayUniv fn) =>
ConwayDelegEnv era
-> CertState era -> Specification fn ConwayDelegCert
conwayDelegCertSpec

  runAgdaRule :: HasCallStack =>
SpecRep (ExecEnvironment fn "DELEG" ConwayEra)
-> SpecRep (ExecState fn "DELEG" ConwayEra)
-> SpecRep (ExecSignal fn "DELEG" ConwayEra)
-> Either
     OpaqueErrorString (SpecRep (ExecState fn "DELEG" ConwayEra))
runAgdaRule SpecRep (ExecEnvironment fn "DELEG" ConwayEra)
env (Agda.MkCertState DState
dState PState
pState GState
vState) SpecRep (ExecSignal fn "DELEG" ConwayEra)
sig =
    forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second
      (\DState
dState' -> DState -> PState -> GState -> CertState
Agda.MkCertState DState
dState' PState
pState GState
vState)
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a.
ComputationResult T_String_6 a -> Either OpaqueErrorString a
unComputationResult
      forall a b. (a -> b) -> a -> b
$ DelegEnv
-> DState -> DCert -> T_ComputationResult_46 T_String_6 DState
Agda.delegStep SpecRep (ExecEnvironment fn "DELEG" ConwayEra)
env DState
dState SpecRep (ExecSignal fn "DELEG" ConwayEra)
sig

  classOf :: ExecSignal fn "DELEG" ConwayEra -> Maybe String
classOf = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConwayDelegCert -> String
nameDelegCert

nameDelegCert :: ConwayDelegCert -> String
nameDelegCert :: ConwayDelegCert -> String
nameDelegCert ConwayRegCert {} = String
"RegKey"
nameDelegCert ConwayUnRegCert {} = String
"UnRegKey"
nameDelegCert ConwayDelegCert {} = String
"DelegateWithKey"
nameDelegCert ConwayRegDelegCert {} = String
"RegK&DelegateWithKey"