{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Ledger.Conway.Genesis (
  ConwayGenesis (..),
  ConwayExtraConfig (..),
  cgDelegsL,
  cgExtraConfigL,
) where

import Cardano.Ledger.BaseTypes (
  KeyValuePairs (..),
  StrictMaybe (..),
  ToKeyValuePairs (..),
  maybeToStrictMaybe,
 )
import Cardano.Ledger.Binary (
  DecCBOR (..),
  EncCBOR (..),
  FromCBOR (..),
  ToCBOR (..),
 )
import Cardano.Ledger.Binary.Coders
import Cardano.Ledger.Conway.Era (ConwayEra)
import Cardano.Ledger.Conway.Governance
import Cardano.Ledger.Conway.PParams (UpgradeConwayPParams)
import Cardano.Ledger.Conway.TxCert (Delegatee)
import Cardano.Ledger.Core
import Cardano.Ledger.Credential (Credential)
import Cardano.Ledger.DRep (DRepState)
import Cardano.Ledger.Genesis (EraGenesis (..))
import Cardano.Ledger.Shelley.Genesis (InjectionData (..))
import Control.DeepSeq (NFData)
import Data.Aeson (
  FromJSON (..),
  KeyValue (..),
  ToJSON (..),
  Value (..),
  withObject,
  (.!=),
  (.:),
  (.:?),
  (.=),
 )
import Data.Default (Default (..))
import Data.Functor.Identity (Identity)
import Data.ListMap (ListMap)
import GHC.Generics (Generic)
import Lens.Micro (Lens', lens)
import NoThunks.Class (NoThunks)

-- | Extra configuration for injecting Conway-specific Genesis data
data ConwayExtraConfig = ConwayExtraConfig
  { ConwayExtraConfig -> InjectionData (Credential Staking) Delegatee
cecDelegs :: !(InjectionData (Credential Staking) Delegatee)
  -- ^ Delegations to DReps and stake pools.
  , ConwayExtraConfig -> InjectionData (Credential DRepRole) DRepState
cecInitialDReps :: !(InjectionData (Credential DRepRole) DRepState)
  -- ^ Initial DRep registrations.
  }
  deriving stock (ConwayExtraConfig -> ConwayExtraConfig -> Bool
(ConwayExtraConfig -> ConwayExtraConfig -> Bool)
-> (ConwayExtraConfig -> ConwayExtraConfig -> Bool)
-> Eq ConwayExtraConfig
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ConwayExtraConfig -> ConwayExtraConfig -> Bool
== :: ConwayExtraConfig -> ConwayExtraConfig -> Bool
$c/= :: ConwayExtraConfig -> ConwayExtraConfig -> Bool
/= :: ConwayExtraConfig -> ConwayExtraConfig -> Bool
Eq, Int -> ConwayExtraConfig -> ShowS
[ConwayExtraConfig] -> ShowS
ConwayExtraConfig -> String
(Int -> ConwayExtraConfig -> ShowS)
-> (ConwayExtraConfig -> String)
-> ([ConwayExtraConfig] -> ShowS)
-> Show ConwayExtraConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConwayExtraConfig -> ShowS
showsPrec :: Int -> ConwayExtraConfig -> ShowS
$cshow :: ConwayExtraConfig -> String
show :: ConwayExtraConfig -> String
$cshowList :: [ConwayExtraConfig] -> ShowS
showList :: [ConwayExtraConfig] -> ShowS
Show, (forall x. ConwayExtraConfig -> Rep ConwayExtraConfig x)
-> (forall x. Rep ConwayExtraConfig x -> ConwayExtraConfig)
-> Generic ConwayExtraConfig
forall x. Rep ConwayExtraConfig x -> ConwayExtraConfig
forall x. ConwayExtraConfig -> Rep ConwayExtraConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ConwayExtraConfig -> Rep ConwayExtraConfig x
from :: forall x. ConwayExtraConfig -> Rep ConwayExtraConfig x
$cto :: forall x. Rep ConwayExtraConfig x -> ConwayExtraConfig
to :: forall x. Rep ConwayExtraConfig x -> ConwayExtraConfig
Generic)
  deriving ([ConwayExtraConfig] -> Value
[ConwayExtraConfig] -> Encoding
ConwayExtraConfig -> Bool
ConwayExtraConfig -> Value
ConwayExtraConfig -> Encoding
(ConwayExtraConfig -> Value)
-> (ConwayExtraConfig -> Encoding)
-> ([ConwayExtraConfig] -> Value)
-> ([ConwayExtraConfig] -> Encoding)
-> (ConwayExtraConfig -> Bool)
-> ToJSON ConwayExtraConfig
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: ConwayExtraConfig -> Value
toJSON :: ConwayExtraConfig -> Value
$ctoEncoding :: ConwayExtraConfig -> Encoding
toEncoding :: ConwayExtraConfig -> Encoding
$ctoJSONList :: [ConwayExtraConfig] -> Value
toJSONList :: [ConwayExtraConfig] -> Value
$ctoEncodingList :: [ConwayExtraConfig] -> Encoding
toEncodingList :: [ConwayExtraConfig] -> Encoding
$comitField :: ConwayExtraConfig -> Bool
omitField :: ConwayExtraConfig -> Bool
ToJSON) via KeyValuePairs ConwayExtraConfig

instance NFData ConwayExtraConfig

instance NoThunks ConwayExtraConfig

instance Default ConwayExtraConfig where
  def :: ConwayExtraConfig
def = InjectionData (Credential Staking) Delegatee
-> InjectionData (Credential DRepRole) DRepState
-> ConwayExtraConfig
ConwayExtraConfig InjectionData (Credential Staking) Delegatee
forall k v. InjectionData k v
NoInjection InjectionData (Credential DRepRole) DRepState
forall k v. InjectionData k v
NoInjection

instance EncCBOR ConwayExtraConfig where
  encCBOR :: ConwayExtraConfig -> Encoding
encCBOR (ConwayExtraConfig InjectionData (Credential Staking) Delegatee
delegs InjectionData (Credential DRepRole) DRepState
dreps) =
    Encode (Closed Dense) ConwayExtraConfig -> Encoding
forall (w :: Wrapped) t. Encode w t -> Encoding
encode (Encode (Closed Dense) ConwayExtraConfig -> Encoding)
-> Encode (Closed Dense) ConwayExtraConfig -> Encoding
forall a b. (a -> b) -> a -> b
$
      (InjectionData (Credential Staking) Delegatee
 -> InjectionData (Credential DRepRole) DRepState
 -> ConwayExtraConfig)
-> Encode
     (Closed Dense)
     (InjectionData (Credential Staking) Delegatee
      -> InjectionData (Credential DRepRole) DRepState
      -> ConwayExtraConfig)
forall t. t -> Encode (Closed Dense) t
Rec InjectionData (Credential Staking) Delegatee
-> InjectionData (Credential DRepRole) DRepState
-> ConwayExtraConfig
ConwayExtraConfig
        Encode
  (Closed Dense)
  (InjectionData (Credential Staking) Delegatee
   -> InjectionData (Credential DRepRole) DRepState
   -> ConwayExtraConfig)
-> Encode
     (Closed Dense) (InjectionData (Credential Staking) Delegatee)
-> Encode
     (Closed Dense)
     (InjectionData (Credential DRepRole) DRepState
      -> ConwayExtraConfig)
forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode (Closed r) a -> Encode w t
!> InjectionData (Credential Staking) Delegatee
-> Encode
     (Closed Dense) (InjectionData (Credential Staking) Delegatee)
forall t. EncCBOR t => t -> Encode (Closed Dense) t
To InjectionData (Credential Staking) Delegatee
delegs
        Encode
  (Closed Dense)
  (InjectionData (Credential DRepRole) DRepState
   -> ConwayExtraConfig)
-> Encode
     (Closed Dense) (InjectionData (Credential DRepRole) DRepState)
-> Encode (Closed Dense) ConwayExtraConfig
forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode (Closed r) a -> Encode w t
!> InjectionData (Credential DRepRole) DRepState
-> Encode
     (Closed Dense) (InjectionData (Credential DRepRole) DRepState)
forall t. EncCBOR t => t -> Encode (Closed Dense) t
To InjectionData (Credential DRepRole) DRepState
dreps

instance DecCBOR ConwayExtraConfig where
  decCBOR :: forall s. Decoder s ConwayExtraConfig
decCBOR =
    Decode (Closed Dense) ConwayExtraConfig
-> Decoder s ConwayExtraConfig
forall t (w :: Wrapped) s. Typeable t => Decode w t -> Decoder s t
decode (Decode (Closed Dense) ConwayExtraConfig
 -> Decoder s ConwayExtraConfig)
-> Decode (Closed Dense) ConwayExtraConfig
-> Decoder s ConwayExtraConfig
forall a b. (a -> b) -> a -> b
$
      (InjectionData (Credential Staking) Delegatee
 -> InjectionData (Credential DRepRole) DRepState
 -> ConwayExtraConfig)
-> Decode
     (Closed Dense)
     (InjectionData (Credential Staking) Delegatee
      -> InjectionData (Credential DRepRole) DRepState
      -> ConwayExtraConfig)
forall t. t -> Decode (Closed Dense) t
RecD InjectionData (Credential Staking) Delegatee
-> InjectionData (Credential DRepRole) DRepState
-> ConwayExtraConfig
ConwayExtraConfig
        Decode
  (Closed Dense)
  (InjectionData (Credential Staking) Delegatee
   -> InjectionData (Credential DRepRole) DRepState
   -> ConwayExtraConfig)
-> Decode
     (Closed (ZonkAny 7)) (InjectionData (Credential Staking) Delegatee)
-> Decode
     (Closed Dense)
     (InjectionData (Credential DRepRole) DRepState
      -> ConwayExtraConfig)
forall a (w1 :: Wrapped) t (w :: Density).
Typeable a =>
Decode w1 (a -> t) -> Decode (Closed w) a -> Decode w1 t
<! Decode
  (Closed (ZonkAny 7)) (InjectionData (Credential Staking) Delegatee)
forall t (w :: Wrapped). DecCBOR t => Decode w t
From
        Decode
  (Closed Dense)
  (InjectionData (Credential DRepRole) DRepState
   -> ConwayExtraConfig)
-> Decode
     (Closed (ZonkAny 6))
     (InjectionData (Credential DRepRole) DRepState)
-> Decode (Closed Dense) ConwayExtraConfig
forall a (w1 :: Wrapped) t (w :: Density).
Typeable a =>
Decode w1 (a -> t) -> Decode (Closed w) a -> Decode w1 t
<! Decode
  (Closed (ZonkAny 6))
  (InjectionData (Credential DRepRole) DRepState)
forall t (w :: Wrapped). DecCBOR t => Decode w t
From

instance ToKeyValuePairs ConwayExtraConfig where
  toKeyValuePairs :: forall e kv. KeyValue e kv => ConwayExtraConfig -> [kv]
toKeyValuePairs (ConwayExtraConfig InjectionData (Credential Staking) Delegatee
delegs InjectionData (Credential DRepRole) DRepState
dreps) =
    [ Key
"delegs" Key -> InjectionData (Credential Staking) Delegatee -> kv
forall v. ToJSON v => Key -> v -> kv
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= InjectionData (Credential Staking) Delegatee
delegs
    , Key
"initialDReps" Key -> InjectionData (Credential DRepRole) DRepState -> kv
forall v. ToJSON v => Key -> v -> kv
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= InjectionData (Credential DRepRole) DRepState
dreps
    ]

instance FromJSON ConwayExtraConfig where
  parseJSON :: Value -> Parser ConwayExtraConfig
parseJSON = String
-> (Object -> Parser ConwayExtraConfig)
-> Value
-> Parser ConwayExtraConfig
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"ConwayExtraConfig" ((Object -> Parser ConwayExtraConfig)
 -> Value -> Parser ConwayExtraConfig)
-> (Object -> Parser ConwayExtraConfig)
-> Value
-> Parser ConwayExtraConfig
forall a b. (a -> b) -> a -> b
$ \Object
obj ->
    InjectionData (Credential Staking) Delegatee
-> InjectionData (Credential DRepRole) DRepState
-> ConwayExtraConfig
ConwayExtraConfig
      (InjectionData (Credential Staking) Delegatee
 -> InjectionData (Credential DRepRole) DRepState
 -> ConwayExtraConfig)
-> Parser (InjectionData (Credential Staking) Delegatee)
-> Parser
     (InjectionData (Credential DRepRole) DRepState
      -> ConwayExtraConfig)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
obj Object
-> Key
-> Parser (Maybe (InjectionData (Credential Staking) Delegatee))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"delegs" Parser (Maybe (InjectionData (Credential Staking) Delegatee))
-> InjectionData (Credential Staking) Delegatee
-> Parser (InjectionData (Credential Staking) Delegatee)
forall a. Parser (Maybe a) -> a -> Parser a
.!= InjectionData (Credential Staking) Delegatee
forall k v. InjectionData k v
NoInjection
      Parser
  (InjectionData (Credential DRepRole) DRepState
   -> ConwayExtraConfig)
-> Parser (InjectionData (Credential DRepRole) DRepState)
-> Parser ConwayExtraConfig
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object
-> Key
-> Parser (Maybe (InjectionData (Credential DRepRole) DRepState))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"initialDReps" Parser (Maybe (InjectionData (Credential DRepRole) DRepState))
-> InjectionData (Credential DRepRole) DRepState
-> Parser (InjectionData (Credential DRepRole) DRepState)
forall a. Parser (Maybe a) -> a -> Parser a
.!= InjectionData (Credential DRepRole) DRepState
forall k v. InjectionData k v
NoInjection

data ConwayGenesis = ConwayGenesis
  { ConwayGenesis -> UpgradeConwayPParams Identity
cgUpgradePParams :: !(UpgradeConwayPParams Identity)
  , ConwayGenesis -> Constitution ConwayEra
cgConstitution :: !(Constitution ConwayEra)
  , ConwayGenesis -> Committee ConwayEra
cgCommittee :: !(Committee ConwayEra)
  , ConwayGenesis -> ListMap (Credential Staking) Delegatee
cgDelegs :: ListMap (Credential Staking) Delegatee
  , ConwayGenesis -> ListMap (Credential DRepRole) DRepState
cgInitialDReps :: ListMap (Credential DRepRole) DRepState
  , ConwayGenesis -> StrictMaybe ConwayExtraConfig
cgExtraConfig :: !(StrictMaybe ConwayExtraConfig)
  -- ^ Optional extra configuration for streaming injection.
  }
  deriving (ConwayGenesis -> ConwayGenesis -> Bool
(ConwayGenesis -> ConwayGenesis -> Bool)
-> (ConwayGenesis -> ConwayGenesis -> Bool) -> Eq ConwayGenesis
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ConwayGenesis -> ConwayGenesis -> Bool
== :: ConwayGenesis -> ConwayGenesis -> Bool
$c/= :: ConwayGenesis -> ConwayGenesis -> Bool
/= :: ConwayGenesis -> ConwayGenesis -> Bool
Eq, (forall x. ConwayGenesis -> Rep ConwayGenesis x)
-> (forall x. Rep ConwayGenesis x -> ConwayGenesis)
-> Generic ConwayGenesis
forall x. Rep ConwayGenesis x -> ConwayGenesis
forall x. ConwayGenesis -> Rep ConwayGenesis x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ConwayGenesis -> Rep ConwayGenesis x
from :: forall x. ConwayGenesis -> Rep ConwayGenesis x
$cto :: forall x. Rep ConwayGenesis x -> ConwayGenesis
to :: forall x. Rep ConwayGenesis x -> ConwayGenesis
Generic, Int -> ConwayGenesis -> ShowS
[ConwayGenesis] -> ShowS
ConwayGenesis -> String
(Int -> ConwayGenesis -> ShowS)
-> (ConwayGenesis -> String)
-> ([ConwayGenesis] -> ShowS)
-> Show ConwayGenesis
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConwayGenesis -> ShowS
showsPrec :: Int -> ConwayGenesis -> ShowS
$cshow :: ConwayGenesis -> String
show :: ConwayGenesis -> String
$cshowList :: [ConwayGenesis] -> ShowS
showList :: [ConwayGenesis] -> ShowS
Show)
  deriving ([ConwayGenesis] -> Value
[ConwayGenesis] -> Encoding
ConwayGenesis -> Bool
ConwayGenesis -> Value
ConwayGenesis -> Encoding
(ConwayGenesis -> Value)
-> (ConwayGenesis -> Encoding)
-> ([ConwayGenesis] -> Value)
-> ([ConwayGenesis] -> Encoding)
-> (ConwayGenesis -> Bool)
-> ToJSON ConwayGenesis
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: ConwayGenesis -> Value
toJSON :: ConwayGenesis -> Value
$ctoEncoding :: ConwayGenesis -> Encoding
toEncoding :: ConwayGenesis -> Encoding
$ctoJSONList :: [ConwayGenesis] -> Value
toJSONList :: [ConwayGenesis] -> Value
$ctoEncodingList :: [ConwayGenesis] -> Encoding
toEncodingList :: [ConwayGenesis] -> Encoding
$comitField :: ConwayGenesis -> Bool
omitField :: ConwayGenesis -> Bool
ToJSON) via KeyValuePairs ConwayGenesis

cgDelegsL :: Lens' ConwayGenesis (ListMap (Credential Staking) Delegatee)
cgDelegsL :: Lens' ConwayGenesis (ListMap (Credential Staking) Delegatee)
cgDelegsL = (ConwayGenesis -> ListMap (Credential Staking) Delegatee)
-> (ConwayGenesis
    -> ListMap (Credential Staking) Delegatee -> ConwayGenesis)
-> Lens' ConwayGenesis (ListMap (Credential Staking) Delegatee)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens ConwayGenesis -> ListMap (Credential Staking) Delegatee
cgDelegs (\ConwayGenesis
x ListMap (Credential Staking) Delegatee
y -> ConwayGenesis
x {cgDelegs = y})

cgExtraConfigL :: Lens' ConwayGenesis (StrictMaybe ConwayExtraConfig)
cgExtraConfigL :: Lens' ConwayGenesis (StrictMaybe ConwayExtraConfig)
cgExtraConfigL = (ConwayGenesis -> StrictMaybe ConwayExtraConfig)
-> (ConwayGenesis
    -> StrictMaybe ConwayExtraConfig -> ConwayGenesis)
-> Lens' ConwayGenesis (StrictMaybe ConwayExtraConfig)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens ConwayGenesis -> StrictMaybe ConwayExtraConfig
cgExtraConfig (\ConwayGenesis
x StrictMaybe ConwayExtraConfig
y -> ConwayGenesis
x {cgExtraConfig = y})

instance EraGenesis ConwayEra where
  type Genesis ConwayEra = ConwayGenesis

instance NoThunks ConwayGenesis

instance NFData ConwayGenesis

-- | Genesis are always encoded with the version of era they are defined in.
instance FromCBOR ConwayGenesis where
  fromCBOR :: forall s. Decoder s ConwayGenesis
fromCBOR = forall era t s. (Era era, DecCBOR t) => Decoder s t
fromEraCBOR @ConwayEra
  {-# INLINE fromCBOR #-}

instance ToCBOR ConwayGenesis where
  toCBOR :: ConwayGenesis -> Encoding
toCBOR x :: ConwayGenesis
x@(ConwayGenesis UpgradeConwayPParams Identity
_ Constitution ConwayEra
_ Committee ConwayEra
_ ListMap (Credential Staking) Delegatee
_ ListMap (Credential DRepRole) DRepState
_ StrictMaybe ConwayExtraConfig
_) =
    let ConwayGenesis {ListMap (Credential DRepRole) DRepState
ListMap (Credential Staking) Delegatee
StrictMaybe ConwayExtraConfig
Constitution ConwayEra
Committee ConwayEra
UpgradeConwayPParams Identity
cgUpgradePParams :: ConwayGenesis -> UpgradeConwayPParams Identity
cgConstitution :: ConwayGenesis -> Constitution ConwayEra
cgCommittee :: ConwayGenesis -> Committee ConwayEra
cgDelegs :: ConwayGenesis -> ListMap (Credential Staking) Delegatee
cgInitialDReps :: ConwayGenesis -> ListMap (Credential DRepRole) DRepState
cgExtraConfig :: ConwayGenesis -> StrictMaybe ConwayExtraConfig
cgUpgradePParams :: UpgradeConwayPParams Identity
cgConstitution :: Constitution ConwayEra
cgCommittee :: Committee ConwayEra
cgDelegs :: ListMap (Credential Staking) Delegatee
cgInitialDReps :: ListMap (Credential DRepRole) DRepState
cgExtraConfig :: StrictMaybe ConwayExtraConfig
..} = ConwayGenesis
x
     in forall era t. (Era era, EncCBOR t) => t -> Encoding
toEraCBOR @ConwayEra (Encoding -> Encoding)
-> (Encode (Closed Dense) ConwayGenesis -> Encoding)
-> Encode (Closed Dense) ConwayGenesis
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encode (Closed Dense) ConwayGenesis -> Encoding
forall (w :: Wrapped) t. Encode w t -> Encoding
encode (Encode (Closed Dense) ConwayGenesis -> Encoding)
-> Encode (Closed Dense) ConwayGenesis -> Encoding
forall a b. (a -> b) -> a -> b
$
          (UpgradeConwayPParams Identity
 -> Constitution ConwayEra
 -> Committee ConwayEra
 -> ListMap (Credential Staking) Delegatee
 -> ListMap (Credential DRepRole) DRepState
 -> StrictMaybe ConwayExtraConfig
 -> ConwayGenesis)
-> Encode
     (Closed Dense)
     (UpgradeConwayPParams Identity
      -> Constitution ConwayEra
      -> Committee ConwayEra
      -> ListMap (Credential Staking) Delegatee
      -> ListMap (Credential DRepRole) DRepState
      -> StrictMaybe ConwayExtraConfig
      -> ConwayGenesis)
forall t. t -> Encode (Closed Dense) t
Rec UpgradeConwayPParams Identity
-> Constitution ConwayEra
-> Committee ConwayEra
-> ListMap (Credential Staking) Delegatee
-> ListMap (Credential DRepRole) DRepState
-> StrictMaybe ConwayExtraConfig
-> ConwayGenesis
ConwayGenesis
            Encode
  (Closed Dense)
  (UpgradeConwayPParams Identity
   -> Constitution ConwayEra
   -> Committee ConwayEra
   -> ListMap (Credential Staking) Delegatee
   -> ListMap (Credential DRepRole) DRepState
   -> StrictMaybe ConwayExtraConfig
   -> ConwayGenesis)
-> Encode (Closed Dense) (UpgradeConwayPParams Identity)
-> Encode
     (Closed Dense)
     (Constitution ConwayEra
      -> Committee ConwayEra
      -> ListMap (Credential Staking) Delegatee
      -> ListMap (Credential DRepRole) DRepState
      -> StrictMaybe ConwayExtraConfig
      -> ConwayGenesis)
forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode (Closed r) a -> Encode w t
!> UpgradeConwayPParams Identity
-> Encode (Closed Dense) (UpgradeConwayPParams Identity)
forall t. EncCBOR t => t -> Encode (Closed Dense) t
To UpgradeConwayPParams Identity
cgUpgradePParams
            Encode
  (Closed Dense)
  (Constitution ConwayEra
   -> Committee ConwayEra
   -> ListMap (Credential Staking) Delegatee
   -> ListMap (Credential DRepRole) DRepState
   -> StrictMaybe ConwayExtraConfig
   -> ConwayGenesis)
-> Encode (Closed Dense) (Constitution ConwayEra)
-> Encode
     (Closed Dense)
     (Committee ConwayEra
      -> ListMap (Credential Staking) Delegatee
      -> ListMap (Credential DRepRole) DRepState
      -> StrictMaybe ConwayExtraConfig
      -> ConwayGenesis)
forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode (Closed r) a -> Encode w t
!> Constitution ConwayEra
-> Encode (Closed Dense) (Constitution ConwayEra)
forall t. EncCBOR t => t -> Encode (Closed Dense) t
To Constitution ConwayEra
cgConstitution
            Encode
  (Closed Dense)
  (Committee ConwayEra
   -> ListMap (Credential Staking) Delegatee
   -> ListMap (Credential DRepRole) DRepState
   -> StrictMaybe ConwayExtraConfig
   -> ConwayGenesis)
-> Encode (Closed Dense) (Committee ConwayEra)
-> Encode
     (Closed Dense)
     (ListMap (Credential Staking) Delegatee
      -> ListMap (Credential DRepRole) DRepState
      -> StrictMaybe ConwayExtraConfig
      -> ConwayGenesis)
forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode (Closed r) a -> Encode w t
!> Committee ConwayEra -> Encode (Closed Dense) (Committee ConwayEra)
forall t. EncCBOR t => t -> Encode (Closed Dense) t
To Committee ConwayEra
cgCommittee
            Encode
  (Closed Dense)
  (ListMap (Credential Staking) Delegatee
   -> ListMap (Credential DRepRole) DRepState
   -> StrictMaybe ConwayExtraConfig
   -> ConwayGenesis)
-> Encode (Closed Dense) (ListMap (Credential Staking) Delegatee)
-> Encode
     (Closed Dense)
     (ListMap (Credential DRepRole) DRepState
      -> StrictMaybe ConwayExtraConfig -> ConwayGenesis)
forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode (Closed r) a -> Encode w t
!> ListMap (Credential Staking) Delegatee
-> Encode (Closed Dense) (ListMap (Credential Staking) Delegatee)
forall t. EncCBOR t => t -> Encode (Closed Dense) t
To ListMap (Credential Staking) Delegatee
cgDelegs
            Encode
  (Closed Dense)
  (ListMap (Credential DRepRole) DRepState
   -> StrictMaybe ConwayExtraConfig -> ConwayGenesis)
-> Encode (Closed Dense) (ListMap (Credential DRepRole) DRepState)
-> Encode
     (Closed Dense) (StrictMaybe ConwayExtraConfig -> ConwayGenesis)
forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode (Closed r) a -> Encode w t
!> ListMap (Credential DRepRole) DRepState
-> Encode (Closed Dense) (ListMap (Credential DRepRole) DRepState)
forall t. EncCBOR t => t -> Encode (Closed Dense) t
To ListMap (Credential DRepRole) DRepState
cgInitialDReps
            Encode
  (Closed Dense) (StrictMaybe ConwayExtraConfig -> ConwayGenesis)
-> Encode (Closed Dense) (StrictMaybe ConwayExtraConfig)
-> Encode (Closed Dense) ConwayGenesis
forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode (Closed r) a -> Encode w t
!> StrictMaybe ConwayExtraConfig
-> Encode (Closed Dense) (StrictMaybe ConwayExtraConfig)
forall t. EncCBOR t => t -> Encode (Closed Dense) t
To StrictMaybe ConwayExtraConfig
cgExtraConfig

instance DecCBOR ConwayGenesis where
  decCBOR :: forall s. Decoder s ConwayGenesis
decCBOR = Decode (Closed Dense) ConwayGenesis -> Decoder s ConwayGenesis
forall t (w :: Wrapped) s. Typeable t => Decode w t -> Decoder s t
decode ((UpgradeConwayPParams Identity
 -> Constitution ConwayEra
 -> Committee ConwayEra
 -> ListMap (Credential Staking) Delegatee
 -> ListMap (Credential DRepRole) DRepState
 -> StrictMaybe ConwayExtraConfig
 -> ConwayGenesis)
-> Decode
     (Closed Dense)
     (UpgradeConwayPParams Identity
      -> Constitution ConwayEra
      -> Committee ConwayEra
      -> ListMap (Credential Staking) Delegatee
      -> ListMap (Credential DRepRole) DRepState
      -> StrictMaybe ConwayExtraConfig
      -> ConwayGenesis)
forall t. t -> Decode (Closed Dense) t
RecD UpgradeConwayPParams Identity
-> Constitution ConwayEra
-> Committee ConwayEra
-> ListMap (Credential Staking) Delegatee
-> ListMap (Credential DRepRole) DRepState
-> StrictMaybe ConwayExtraConfig
-> ConwayGenesis
ConwayGenesis Decode
  (Closed Dense)
  (UpgradeConwayPParams Identity
   -> Constitution ConwayEra
   -> Committee ConwayEra
   -> ListMap (Credential Staking) Delegatee
   -> ListMap (Credential DRepRole) DRepState
   -> StrictMaybe ConwayExtraConfig
   -> ConwayGenesis)
-> Decode (Closed (ZonkAny 5)) (UpgradeConwayPParams Identity)
-> Decode
     (Closed Dense)
     (Constitution ConwayEra
      -> Committee ConwayEra
      -> ListMap (Credential Staking) Delegatee
      -> ListMap (Credential DRepRole) DRepState
      -> StrictMaybe ConwayExtraConfig
      -> ConwayGenesis)
forall a (w1 :: Wrapped) t (w :: Density).
Typeable a =>
Decode w1 (a -> t) -> Decode (Closed w) a -> Decode w1 t
<! Decode (Closed (ZonkAny 5)) (UpgradeConwayPParams Identity)
forall t (w :: Wrapped). DecCBOR t => Decode w t
From Decode
  (Closed Dense)
  (Constitution ConwayEra
   -> Committee ConwayEra
   -> ListMap (Credential Staking) Delegatee
   -> ListMap (Credential DRepRole) DRepState
   -> StrictMaybe ConwayExtraConfig
   -> ConwayGenesis)
-> Decode (Closed (ZonkAny 4)) (Constitution ConwayEra)
-> Decode
     (Closed Dense)
     (Committee ConwayEra
      -> ListMap (Credential Staking) Delegatee
      -> ListMap (Credential DRepRole) DRepState
      -> StrictMaybe ConwayExtraConfig
      -> ConwayGenesis)
forall a (w1 :: Wrapped) t (w :: Density).
Typeable a =>
Decode w1 (a -> t) -> Decode (Closed w) a -> Decode w1 t
<! Decode (Closed (ZonkAny 4)) (Constitution ConwayEra)
forall t (w :: Wrapped). DecCBOR t => Decode w t
From Decode
  (Closed Dense)
  (Committee ConwayEra
   -> ListMap (Credential Staking) Delegatee
   -> ListMap (Credential DRepRole) DRepState
   -> StrictMaybe ConwayExtraConfig
   -> ConwayGenesis)
-> Decode (Closed (ZonkAny 3)) (Committee ConwayEra)
-> Decode
     (Closed Dense)
     (ListMap (Credential Staking) Delegatee
      -> ListMap (Credential DRepRole) DRepState
      -> StrictMaybe ConwayExtraConfig
      -> ConwayGenesis)
forall a (w1 :: Wrapped) t (w :: Density).
Typeable a =>
Decode w1 (a -> t) -> Decode (Closed w) a -> Decode w1 t
<! Decode (Closed (ZonkAny 3)) (Committee ConwayEra)
forall t (w :: Wrapped). DecCBOR t => Decode w t
From Decode
  (Closed Dense)
  (ListMap (Credential Staking) Delegatee
   -> ListMap (Credential DRepRole) DRepState
   -> StrictMaybe ConwayExtraConfig
   -> ConwayGenesis)
-> Decode
     (Closed (ZonkAny 2)) (ListMap (Credential Staking) Delegatee)
-> Decode
     (Closed Dense)
     (ListMap (Credential DRepRole) DRepState
      -> StrictMaybe ConwayExtraConfig -> ConwayGenesis)
forall a (w1 :: Wrapped) t (w :: Density).
Typeable a =>
Decode w1 (a -> t) -> Decode (Closed w) a -> Decode w1 t
<! Decode
  (Closed (ZonkAny 2)) (ListMap (Credential Staking) Delegatee)
forall t (w :: Wrapped). DecCBOR t => Decode w t
From Decode
  (Closed Dense)
  (ListMap (Credential DRepRole) DRepState
   -> StrictMaybe ConwayExtraConfig -> ConwayGenesis)
-> Decode
     (Closed (ZonkAny 1)) (ListMap (Credential DRepRole) DRepState)
-> Decode
     (Closed Dense) (StrictMaybe ConwayExtraConfig -> ConwayGenesis)
forall a (w1 :: Wrapped) t (w :: Density).
Typeable a =>
Decode w1 (a -> t) -> Decode (Closed w) a -> Decode w1 t
<! Decode
  (Closed (ZonkAny 1)) (ListMap (Credential DRepRole) DRepState)
forall t (w :: Wrapped). DecCBOR t => Decode w t
From Decode
  (Closed Dense) (StrictMaybe ConwayExtraConfig -> ConwayGenesis)
-> Decode (Closed (ZonkAny 0)) (StrictMaybe ConwayExtraConfig)
-> Decode (Closed Dense) ConwayGenesis
forall a (w1 :: Wrapped) t (w :: Density).
Typeable a =>
Decode w1 (a -> t) -> Decode (Closed w) a -> Decode w1 t
<! Decode (Closed (ZonkAny 0)) (StrictMaybe ConwayExtraConfig)
forall t (w :: Wrapped). DecCBOR t => Decode w t
From)
  {-# INLINE decCBOR #-}

instance EncCBOR ConwayGenesis

instance FromJSON ConwayGenesis where
  parseJSON :: Value -> Parser ConwayGenesis
parseJSON =
    String
-> (Object -> Parser ConwayGenesis)
-> Value
-> Parser ConwayGenesis
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"ConwayGenesis" ((Object -> Parser ConwayGenesis) -> Value -> Parser ConwayGenesis)
-> (Object -> Parser ConwayGenesis)
-> Value
-> Parser ConwayGenesis
forall a b. (a -> b) -> a -> b
$ \Object
obj -> do
      cgUpgradePParams <- Value -> Parser (UpgradeConwayPParams Identity)
forall a. FromJSON a => Value -> Parser a
parseJSON (Object -> Value
Object Object
obj)
      cgConstitution <- obj .: "constitution"
      cgCommittee <- obj .: "committee"
      cgDelegs <- obj .:? "delegs" .!= mempty
      cgInitialDReps <- obj .:? "initialDReps" .!= mempty
      cgExtraConfig <- maybeToStrictMaybe <$> obj .:? "extraConfig"
      pure ConwayGenesis {..}

instance ToKeyValuePairs ConwayGenesis where
  toKeyValuePairs :: forall e kv. KeyValue e kv => ConwayGenesis -> [kv]
toKeyValuePairs cg :: ConwayGenesis
cg@(ConwayGenesis UpgradeConwayPParams Identity
_ Constitution ConwayEra
_ Committee ConwayEra
_ ListMap (Credential Staking) Delegatee
_ ListMap (Credential DRepRole) DRepState
_ StrictMaybe ConwayExtraConfig
_) =
    let ConwayGenesis {ListMap (Credential DRepRole) DRepState
ListMap (Credential Staking) Delegatee
StrictMaybe ConwayExtraConfig
Constitution ConwayEra
Committee ConwayEra
UpgradeConwayPParams Identity
cgUpgradePParams :: ConwayGenesis -> UpgradeConwayPParams Identity
cgConstitution :: ConwayGenesis -> Constitution ConwayEra
cgCommittee :: ConwayGenesis -> Committee ConwayEra
cgDelegs :: ConwayGenesis -> ListMap (Credential Staking) Delegatee
cgInitialDReps :: ConwayGenesis -> ListMap (Credential DRepRole) DRepState
cgExtraConfig :: ConwayGenesis -> StrictMaybe ConwayExtraConfig
cgUpgradePParams :: UpgradeConwayPParams Identity
cgConstitution :: Constitution ConwayEra
cgCommittee :: Committee ConwayEra
cgDelegs :: ListMap (Credential Staking) Delegatee
cgInitialDReps :: ListMap (Credential DRepRole) DRepState
cgExtraConfig :: StrictMaybe ConwayExtraConfig
..} = ConwayGenesis
cg
     in [ Key
"constitution" Key -> Constitution ConwayEra -> kv
forall v. ToJSON v => Key -> v -> kv
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Constitution ConwayEra
cgConstitution
        , Key
"committee" Key -> Committee ConwayEra -> kv
forall v. ToJSON v => Key -> v -> kv
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Committee ConwayEra
cgCommittee
        ]
          [kv] -> [kv] -> [kv]
forall a. Semigroup a => a -> a -> a
<> UpgradeConwayPParams Identity -> [kv]
forall a e kv. (ToKeyValuePairs a, KeyValue e kv) => a -> [kv]
forall e kv. KeyValue e kv => UpgradeConwayPParams Identity -> [kv]
toKeyValuePairs UpgradeConwayPParams Identity
cgUpgradePParams
          [kv] -> [kv] -> [kv]
forall a. Semigroup a => a -> a -> a
<> [Key
"delegs" Key -> ListMap (Credential Staking) Delegatee -> kv
forall v. ToJSON v => Key -> v -> kv
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ListMap (Credential Staking) Delegatee
cgDelegs | Bool -> Bool
not (ListMap (Credential Staking) Delegatee -> Bool
forall a. ListMap (Credential Staking) a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ListMap (Credential Staking) Delegatee
cgDelegs)]
          [kv] -> [kv] -> [kv]
forall a. Semigroup a => a -> a -> a
<> [Key
"initialDReps" Key -> ListMap (Credential DRepRole) DRepState -> kv
forall v. ToJSON v => Key -> v -> kv
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ListMap (Credential DRepRole) DRepState
cgInitialDReps | Bool -> Bool
not (ListMap (Credential DRepRole) DRepState -> Bool
forall a. ListMap (Credential DRepRole) a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ListMap (Credential DRepRole) DRepState
cgInitialDReps)]
          [kv] -> [kv] -> [kv]
forall a. Semigroup a => a -> a -> a
<> [Key
"extraConfig" Key -> ConwayExtraConfig -> kv
forall v. ToJSON v => Key -> v -> kv
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ConwayExtraConfig
ec | SJust ConwayExtraConfig
ec <- [StrictMaybe ConwayExtraConfig
cgExtraConfig]]