{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Ledger.CanonicalState.Namespace.GovConstitution.V0 (
  CanonicalConstitution (..),
  GovConstitutionIn (..),
  GovConstitutionOut (..),
) where

import Cardano.Ledger.BaseTypes (Anchor (..), EpochNo (..), StrictMaybe (..))
import Cardano.Ledger.CanonicalState.BasicTypes ()
import Cardano.Ledger.CanonicalState.Namespace (Era, NamespaceEra)
import Cardano.Ledger.Hashes (ScriptHash (..))
import Cardano.SCLS.CBOR.Canonical.Decoder (FromCanonicalCBOR (..))
import Cardano.SCLS.CBOR.Canonical.Encoder (ToCanonicalCBOR (..))
import Cardano.SCLS.Entry.IsKey (IsKey (..))
import Cardano.SCLS.NamespaceCodec (
  CanonicalCBOREntryDecoder (..),
  CanonicalCBOREntryEncoder (..),
  KnownNamespace (..),
  namespaceKeySize,
 )
import Cardano.SCLS.Versioned (Versioned (..))
import Data.MemPack.ByteOrdered (packWord64beM, unpackBigEndianM)
import Data.Proxy (Proxy (..))
import GHC.Generics (Generic)

newtype GovConstitutionIn = GovConstitutionIn EpochNo
  deriving (GovConstitutionIn -> GovConstitutionIn -> Bool
(GovConstitutionIn -> GovConstitutionIn -> Bool)
-> (GovConstitutionIn -> GovConstitutionIn -> Bool)
-> Eq GovConstitutionIn
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GovConstitutionIn -> GovConstitutionIn -> Bool
== :: GovConstitutionIn -> GovConstitutionIn -> Bool
$c/= :: GovConstitutionIn -> GovConstitutionIn -> Bool
/= :: GovConstitutionIn -> GovConstitutionIn -> Bool
Eq, Eq GovConstitutionIn
Eq GovConstitutionIn =>
(GovConstitutionIn -> GovConstitutionIn -> Ordering)
-> (GovConstitutionIn -> GovConstitutionIn -> Bool)
-> (GovConstitutionIn -> GovConstitutionIn -> Bool)
-> (GovConstitutionIn -> GovConstitutionIn -> Bool)
-> (GovConstitutionIn -> GovConstitutionIn -> Bool)
-> (GovConstitutionIn -> GovConstitutionIn -> GovConstitutionIn)
-> (GovConstitutionIn -> GovConstitutionIn -> GovConstitutionIn)
-> Ord GovConstitutionIn
GovConstitutionIn -> GovConstitutionIn -> Bool
GovConstitutionIn -> GovConstitutionIn -> Ordering
GovConstitutionIn -> GovConstitutionIn -> GovConstitutionIn
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: GovConstitutionIn -> GovConstitutionIn -> Ordering
compare :: GovConstitutionIn -> GovConstitutionIn -> Ordering
$c< :: GovConstitutionIn -> GovConstitutionIn -> Bool
< :: GovConstitutionIn -> GovConstitutionIn -> Bool
$c<= :: GovConstitutionIn -> GovConstitutionIn -> Bool
<= :: GovConstitutionIn -> GovConstitutionIn -> Bool
$c> :: GovConstitutionIn -> GovConstitutionIn -> Bool
> :: GovConstitutionIn -> GovConstitutionIn -> Bool
$c>= :: GovConstitutionIn -> GovConstitutionIn -> Bool
>= :: GovConstitutionIn -> GovConstitutionIn -> Bool
$cmax :: GovConstitutionIn -> GovConstitutionIn -> GovConstitutionIn
max :: GovConstitutionIn -> GovConstitutionIn -> GovConstitutionIn
$cmin :: GovConstitutionIn -> GovConstitutionIn -> GovConstitutionIn
min :: GovConstitutionIn -> GovConstitutionIn -> GovConstitutionIn
Ord, Int -> GovConstitutionIn -> ShowS
[GovConstitutionIn] -> ShowS
GovConstitutionIn -> String
(Int -> GovConstitutionIn -> ShowS)
-> (GovConstitutionIn -> String)
-> ([GovConstitutionIn] -> ShowS)
-> Show GovConstitutionIn
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GovConstitutionIn -> ShowS
showsPrec :: Int -> GovConstitutionIn -> ShowS
$cshow :: GovConstitutionIn -> String
show :: GovConstitutionIn -> String
$cshowList :: [GovConstitutionIn] -> ShowS
showList :: [GovConstitutionIn] -> ShowS
Show)

newtype GovConstitutionOut = GovConstitutionOut CanonicalConstitution
  deriving (GovConstitutionOut -> GovConstitutionOut -> Bool
(GovConstitutionOut -> GovConstitutionOut -> Bool)
-> (GovConstitutionOut -> GovConstitutionOut -> Bool)
-> Eq GovConstitutionOut
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GovConstitutionOut -> GovConstitutionOut -> Bool
== :: GovConstitutionOut -> GovConstitutionOut -> Bool
$c/= :: GovConstitutionOut -> GovConstitutionOut -> Bool
/= :: GovConstitutionOut -> GovConstitutionOut -> Bool
Eq, Int -> GovConstitutionOut -> ShowS
[GovConstitutionOut] -> ShowS
GovConstitutionOut -> String
(Int -> GovConstitutionOut -> ShowS)
-> (GovConstitutionOut -> String)
-> ([GovConstitutionOut] -> ShowS)
-> Show GovConstitutionOut
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GovConstitutionOut -> ShowS
showsPrec :: Int -> GovConstitutionOut -> ShowS
$cshow :: GovConstitutionOut -> String
show :: GovConstitutionOut -> String
$cshowList :: [GovConstitutionOut] -> ShowS
showList :: [GovConstitutionOut] -> ShowS
Show, (forall x. GovConstitutionOut -> Rep GovConstitutionOut x)
-> (forall x. Rep GovConstitutionOut x -> GovConstitutionOut)
-> Generic GovConstitutionOut
forall x. Rep GovConstitutionOut x -> GovConstitutionOut
forall x. GovConstitutionOut -> Rep GovConstitutionOut x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. GovConstitutionOut -> Rep GovConstitutionOut x
from :: forall x. GovConstitutionOut -> Rep GovConstitutionOut x
$cto :: forall x. Rep GovConstitutionOut x -> GovConstitutionOut
to :: forall x. Rep GovConstitutionOut x -> GovConstitutionOut
Generic)

instance IsKey GovConstitutionIn where
  keySize :: Int
keySize = forall (ns :: Symbol). KnownNat (NamespaceKeySize ns) => Int
namespaceKeySize @"gov/constitution/v0"
  packKeyM :: forall b. GovConstitutionIn -> Pack b ()
packKeyM (GovConstitutionIn (EpochNo Word64
epochNo)) = do
    Word64 -> Pack b ()
forall s. Word64 -> Pack s ()
packWord64beM Word64
epochNo
  unpackKeyM :: forall b s. Buffer b => Unpack' s b GovConstitutionIn
unpackKeyM = do
    epochNo <- Unpack s b Word64
forall a b s. (Bytes a, MemPack a, Buffer b) => Unpack s b a
unpackBigEndianM
    return $ GovConstitutionIn (EpochNo epochNo)

data CanonicalConstitution = CanonicalConstitution
  { CanonicalConstitution -> Anchor
constitutionAnchor :: !Anchor
  , CanonicalConstitution -> StrictMaybe ScriptHash
constitutionGuardrailsScriptHash :: !(StrictMaybe ScriptHash)
  }
  deriving (CanonicalConstitution -> CanonicalConstitution -> Bool
(CanonicalConstitution -> CanonicalConstitution -> Bool)
-> (CanonicalConstitution -> CanonicalConstitution -> Bool)
-> Eq CanonicalConstitution
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CanonicalConstitution -> CanonicalConstitution -> Bool
== :: CanonicalConstitution -> CanonicalConstitution -> Bool
$c/= :: CanonicalConstitution -> CanonicalConstitution -> Bool
/= :: CanonicalConstitution -> CanonicalConstitution -> Bool
Eq, Int -> CanonicalConstitution -> ShowS
[CanonicalConstitution] -> ShowS
CanonicalConstitution -> String
(Int -> CanonicalConstitution -> ShowS)
-> (CanonicalConstitution -> String)
-> ([CanonicalConstitution] -> ShowS)
-> Show CanonicalConstitution
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CanonicalConstitution -> ShowS
showsPrec :: Int -> CanonicalConstitution -> ShowS
$cshow :: CanonicalConstitution -> String
show :: CanonicalConstitution -> String
$cshowList :: [CanonicalConstitution] -> ShowS
showList :: [CanonicalConstitution] -> ShowS
Show, (forall x. CanonicalConstitution -> Rep CanonicalConstitution x)
-> (forall x. Rep CanonicalConstitution x -> CanonicalConstitution)
-> Generic CanonicalConstitution
forall x. Rep CanonicalConstitution x -> CanonicalConstitution
forall x. CanonicalConstitution -> Rep CanonicalConstitution x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CanonicalConstitution -> Rep CanonicalConstitution x
from :: forall x. CanonicalConstitution -> Rep CanonicalConstitution x
$cto :: forall x. Rep CanonicalConstitution x -> CanonicalConstitution
to :: forall x. Rep CanonicalConstitution x -> CanonicalConstitution
Generic)

instance (NamespaceEra v ~ era, Era era) => ToCanonicalCBOR v CanonicalConstitution where
  toCanonicalCBOR :: forall (proxy :: Symbol -> *).
proxy v -> CanonicalConstitution -> CanonicalEncoding
toCanonicalCBOR proxy v
v (CanonicalConstitution {StrictMaybe ScriptHash
Anchor
constitutionAnchor :: CanonicalConstitution -> Anchor
constitutionGuardrailsScriptHash :: CanonicalConstitution -> StrictMaybe ScriptHash
constitutionAnchor :: Anchor
constitutionGuardrailsScriptHash :: StrictMaybe ScriptHash
..}) =
    proxy v -> (Anchor, StrictMaybe ScriptHash) -> CanonicalEncoding
forall (v :: Symbol) a (proxy :: Symbol -> *).
ToCanonicalCBOR v a =>
proxy v -> a -> CanonicalEncoding
forall (proxy :: Symbol -> *).
proxy v -> (Anchor, StrictMaybe ScriptHash) -> CanonicalEncoding
toCanonicalCBOR proxy v
v (Anchor
constitutionAnchor, StrictMaybe ScriptHash
constitutionGuardrailsScriptHash)

instance (NamespaceEra v ~ era, Era era) => FromCanonicalCBOR v CanonicalConstitution where
  fromCanonicalCBOR :: forall s. CanonicalDecoder s (Versioned v CanonicalConstitution)
fromCanonicalCBOR = do
    Versioned (anchor, script) <- forall (v :: Symbol) a s.
FromCanonicalCBOR v a =>
CanonicalDecoder s (Versioned v a)
fromCanonicalCBOR @v
    return $ Versioned $ CanonicalConstitution anchor script

instance (NamespaceEra "gov/constitution/v0" ~ era, Era era) => KnownNamespace "gov/constitution/v0" where
  type NamespaceKey "gov/constitution/v0" = GovConstitutionIn
  type NamespaceEntry "gov/constitution/v0" = GovConstitutionOut

instance
  (NamespaceEra "gov/constitution/v0" ~ era, Era era) =>
  CanonicalCBOREntryEncoder "gov/constitution/v0" GovConstitutionOut
  where
  encodeEntry :: GovConstitutionOut -> CanonicalEncoding
encodeEntry (GovConstitutionOut CanonicalConstitution
n) = Proxy "gov/constitution/v0"
-> CanonicalConstitution -> CanonicalEncoding
forall (v :: Symbol) a (proxy :: Symbol -> *).
ToCanonicalCBOR v a =>
proxy v -> a -> CanonicalEncoding
forall (proxy :: Symbol -> *).
proxy "gov/constitution/v0"
-> CanonicalConstitution -> CanonicalEncoding
toCanonicalCBOR (forall {k} (t :: k). Proxy t
forall (t :: Symbol). Proxy t
Proxy @"gov/constitution/v0") CanonicalConstitution
n

instance
  (NamespaceEra "gov/constitution/v0" ~ era, Era era) =>
  CanonicalCBOREntryDecoder "gov/constitution/v0" GovConstitutionOut
  where
  decodeEntry :: forall s.
CanonicalDecoder
  s (Versioned "gov/constitution/v0" GovConstitutionOut)
decodeEntry = (CanonicalConstitution -> GovConstitutionOut)
-> Versioned "gov/constitution/v0" CanonicalConstitution
-> Versioned "gov/constitution/v0" GovConstitutionOut
forall a b.
(a -> b)
-> Versioned "gov/constitution/v0" a
-> Versioned "gov/constitution/v0" b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CanonicalConstitution -> GovConstitutionOut
GovConstitutionOut (Versioned "gov/constitution/v0" CanonicalConstitution
 -> Versioned "gov/constitution/v0" GovConstitutionOut)
-> CanonicalDecoder
     s (Versioned "gov/constitution/v0" CanonicalConstitution)
-> CanonicalDecoder
     s (Versioned "gov/constitution/v0" GovConstitutionOut)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CanonicalDecoder
  s (Versioned "gov/constitution/v0" CanonicalConstitution)
forall s.
CanonicalDecoder
  s (Versioned "gov/constitution/v0" CanonicalConstitution)
forall (v :: Symbol) a s.
FromCanonicalCBOR v a =>
CanonicalDecoder s (Versioned v a)
fromCanonicalCBOR