{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Ledger.Alonzo.Transition (
  TransitionConfig (..),
  toAlonzoTransitionConfigPairs,
) where

import Cardano.Ledger.Alonzo.Era
import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis, toAlonzoGenesisPairs)
import Cardano.Ledger.Alonzo.Translation ()
import Cardano.Ledger.Mary
import Cardano.Ledger.Mary.Transition (TransitionConfig (MaryTransitionConfig))
import Cardano.Ledger.Shelley.Transition
import Data.Aeson (
  FromJSON (..),
  KeyValue (..),
  ToJSON (..),
  Value (..),
  object,
  pairs,
  withObject,
  (.:),
 )
import GHC.Generics
import Lens.Micro
import NoThunks.Class (NoThunks (..))

instance EraTransition AlonzoEra where
  data TransitionConfig AlonzoEra = AlonzoTransitionConfig
    { TransitionConfig AlonzoEra -> AlonzoGenesis
atcAlonzoGenesis :: !AlonzoGenesis
    , TransitionConfig AlonzoEra -> TransitionConfig MaryEra
atcMaryTransitionConfig :: !(TransitionConfig MaryEra)
    }
    deriving (Int -> TransitionConfig AlonzoEra -> ShowS
[TransitionConfig AlonzoEra] -> ShowS
TransitionConfig AlonzoEra -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TransitionConfig AlonzoEra] -> ShowS
$cshowList :: [TransitionConfig AlonzoEra] -> ShowS
show :: TransitionConfig AlonzoEra -> String
$cshow :: TransitionConfig AlonzoEra -> String
showsPrec :: Int -> TransitionConfig AlonzoEra -> ShowS
$cshowsPrec :: Int -> TransitionConfig AlonzoEra -> ShowS
Show, TransitionConfig AlonzoEra -> TransitionConfig AlonzoEra -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TransitionConfig AlonzoEra -> TransitionConfig AlonzoEra -> Bool
$c/= :: TransitionConfig AlonzoEra -> TransitionConfig AlonzoEra -> Bool
== :: TransitionConfig AlonzoEra -> TransitionConfig AlonzoEra -> Bool
$c== :: TransitionConfig AlonzoEra -> TransitionConfig AlonzoEra -> Bool
Eq, forall x.
Rep (TransitionConfig AlonzoEra) x -> TransitionConfig AlonzoEra
forall x.
TransitionConfig AlonzoEra -> Rep (TransitionConfig AlonzoEra) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep (TransitionConfig AlonzoEra) x -> TransitionConfig AlonzoEra
$cfrom :: forall x.
TransitionConfig AlonzoEra -> Rep (TransitionConfig AlonzoEra) x
Generic)

  mkTransitionConfig :: TranslationContext AlonzoEra
-> TransitionConfig (PreviousEra AlonzoEra)
-> TransitionConfig AlonzoEra
mkTransitionConfig = AlonzoGenesis
-> TransitionConfig MaryEra -> TransitionConfig AlonzoEra
AlonzoTransitionConfig

  injectIntoTestState :: TransitionConfig AlonzoEra
-> NewEpochState AlonzoEra -> NewEpochState AlonzoEra
injectIntoTestState = forall era.
EraTransition era =>
TransitionConfig era -> NewEpochState era -> NewEpochState era
registerInitialFundsThenStaking

  tcPreviousEraConfigL :: EraTransition (PreviousEra AlonzoEra) =>
Lens'
  (TransitionConfig AlonzoEra)
  (TransitionConfig (PreviousEra AlonzoEra))
tcPreviousEraConfigL =
    forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens TransitionConfig AlonzoEra -> TransitionConfig MaryEra
atcMaryTransitionConfig (\TransitionConfig AlonzoEra
atc TransitionConfig MaryEra
pc -> TransitionConfig AlonzoEra
atc {atcMaryTransitionConfig :: TransitionConfig MaryEra
atcMaryTransitionConfig = TransitionConfig MaryEra
pc})

  tcTranslationContextL :: Lens' (TransitionConfig AlonzoEra) (TranslationContext AlonzoEra)
tcTranslationContextL =
    forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens TransitionConfig AlonzoEra -> AlonzoGenesis
atcAlonzoGenesis (\TransitionConfig AlonzoEra
atc AlonzoGenesis
ag -> TransitionConfig AlonzoEra
atc {atcAlonzoGenesis :: AlonzoGenesis
atcAlonzoGenesis = AlonzoGenesis
ag})

instance NoThunks (TransitionConfig AlonzoEra)

instance ToJSON (TransitionConfig AlonzoEra) where
  toJSON :: TransitionConfig AlonzoEra -> Value
toJSON = [Pair] -> Value
object forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. KeyValue e a => TransitionConfig AlonzoEra -> [a]
toAlonzoTransitionConfigPairs
  toEncoding :: TransitionConfig AlonzoEra -> Encoding
toEncoding = Series -> Encoding
pairs forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. KeyValue e a => TransitionConfig AlonzoEra -> [a]
toAlonzoTransitionConfigPairs

toAlonzoTransitionConfigPairs :: KeyValue e a => TransitionConfig AlonzoEra -> [a]
toAlonzoTransitionConfigPairs :: forall e a. KeyValue e a => TransitionConfig AlonzoEra -> [a]
toAlonzoTransitionConfigPairs TransitionConfig AlonzoEra
alonzoConfig =
  forall e a. KeyValue e a => TransitionConfig ShelleyEra -> [a]
toShelleyTransitionConfigPairs TransitionConfig ShelleyEra
shelleyConfig
    forall a. [a] -> [a] -> [a]
++ [Key
"alonzo" forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object (forall e a. KeyValue e a => AlonzoGenesis -> [a]
toAlonzoGenesisPairs (TransitionConfig AlonzoEra
alonzoConfig forall s a. s -> Getting a s a -> a
^. forall era.
EraTransition era =>
Lens' (TransitionConfig era) (TranslationContext era)
tcTranslationContextL))]
  where
    maryConfig :: TransitionConfig MaryEra
maryConfig = TransitionConfig AlonzoEra
alonzoConfig forall s a. s -> Getting a s a -> a
^. forall era.
(EraTransition era, EraTransition (PreviousEra era)) =>
Lens' (TransitionConfig era) (TransitionConfig (PreviousEra era))
tcPreviousEraConfigL
    allegraConfig :: TransitionConfig AllegraEra
allegraConfig = TransitionConfig MaryEra
maryConfig forall s a. s -> Getting a s a -> a
^. forall era.
(EraTransition era, EraTransition (PreviousEra era)) =>
Lens' (TransitionConfig era) (TransitionConfig (PreviousEra era))
tcPreviousEraConfigL
    shelleyConfig :: TransitionConfig ShelleyEra
shelleyConfig = TransitionConfig AllegraEra
allegraConfig forall s a. s -> Getting a s a -> a
^. forall era.
(EraTransition era, EraTransition (PreviousEra era)) =>
Lens' (TransitionConfig era) (TransitionConfig (PreviousEra era))
tcPreviousEraConfigL

instance FromJSON (TransitionConfig AlonzoEra) where
  parseJSON :: Value -> Parser (TransitionConfig AlonzoEra)
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"AlonzoTransitionConfig" forall a b. (a -> b) -> a -> b
$ \Object
o -> do
    AlonzoGenesis
pc <- forall a. FromJSON a => Value -> Parser a
parseJSON (Object -> Value
Object Object
o)
    TransitionConfig MaryEra
ag <- Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"alonzo"
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall era.
EraTransition era =>
TranslationContext era
-> TransitionConfig (PreviousEra era) -> TransitionConfig era
mkTransitionConfig AlonzoGenesis
pc TransitionConfig MaryEra
ag