{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Ledger.Dijkstra.Genesis (
  DijkstraGenesis (..),
) where

import Cardano.Ledger.BaseTypes (KeyValuePairs (..), ToKeyValuePairs (..))
import Cardano.Ledger.Binary (
  DecCBOR (..),
  EncCBOR (..),
  FromCBOR (..),
  ToCBOR (..),
 )
import Cardano.Ledger.Binary.Coders
import Cardano.Ledger.Core
import Cardano.Ledger.Dijkstra.Era (DijkstraEra)
import Cardano.Ledger.Dijkstra.PParams (UpgradeDijkstraPParams)
import Cardano.Ledger.Genesis (EraGenesis (..))
import Data.Aeson (FromJSON (..), ToJSON, Value (..), withObject)
import Data.Functor.Identity (Identity)
import GHC.Generics
import NoThunks.Class (NoThunks)

-- TODO: Currently it is just a placeholder for all the new protocol parameters that will be added
-- in the Dijkstra era
data DijkstraGenesis = DijkstraGenesis
  { DijkstraGenesis -> UpgradeDijkstraPParams Identity DijkstraEra
dgUpgradePParams :: !(UpgradeDijkstraPParams Identity DijkstraEra)
  }
  deriving (DijkstraGenesis -> DijkstraGenesis -> Bool
(DijkstraGenesis -> DijkstraGenesis -> Bool)
-> (DijkstraGenesis -> DijkstraGenesis -> Bool)
-> Eq DijkstraGenesis
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DijkstraGenesis -> DijkstraGenesis -> Bool
== :: DijkstraGenesis -> DijkstraGenesis -> Bool
$c/= :: DijkstraGenesis -> DijkstraGenesis -> Bool
/= :: DijkstraGenesis -> DijkstraGenesis -> Bool
Eq, Int -> DijkstraGenesis -> ShowS
[DijkstraGenesis] -> ShowS
DijkstraGenesis -> String
(Int -> DijkstraGenesis -> ShowS)
-> (DijkstraGenesis -> String)
-> ([DijkstraGenesis] -> ShowS)
-> Show DijkstraGenesis
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DijkstraGenesis -> ShowS
showsPrec :: Int -> DijkstraGenesis -> ShowS
$cshow :: DijkstraGenesis -> String
show :: DijkstraGenesis -> String
$cshowList :: [DijkstraGenesis] -> ShowS
showList :: [DijkstraGenesis] -> ShowS
Show, (forall x. DijkstraGenesis -> Rep DijkstraGenesis x)
-> (forall x. Rep DijkstraGenesis x -> DijkstraGenesis)
-> Generic DijkstraGenesis
forall x. Rep DijkstraGenesis x -> DijkstraGenesis
forall x. DijkstraGenesis -> Rep DijkstraGenesis x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. DijkstraGenesis -> Rep DijkstraGenesis x
from :: forall x. DijkstraGenesis -> Rep DijkstraGenesis x
$cto :: forall x. Rep DijkstraGenesis x -> DijkstraGenesis
to :: forall x. Rep DijkstraGenesis x -> DijkstraGenesis
Generic)
  deriving ([DijkstraGenesis] -> Value
[DijkstraGenesis] -> Encoding
DijkstraGenesis -> Bool
DijkstraGenesis -> Value
DijkstraGenesis -> Encoding
(DijkstraGenesis -> Value)
-> (DijkstraGenesis -> Encoding)
-> ([DijkstraGenesis] -> Value)
-> ([DijkstraGenesis] -> Encoding)
-> (DijkstraGenesis -> Bool)
-> ToJSON DijkstraGenesis
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: DijkstraGenesis -> Value
toJSON :: DijkstraGenesis -> Value
$ctoEncoding :: DijkstraGenesis -> Encoding
toEncoding :: DijkstraGenesis -> Encoding
$ctoJSONList :: [DijkstraGenesis] -> Value
toJSONList :: [DijkstraGenesis] -> Value
$ctoEncodingList :: [DijkstraGenesis] -> Encoding
toEncodingList :: [DijkstraGenesis] -> Encoding
$comitField :: DijkstraGenesis -> Bool
omitField :: DijkstraGenesis -> Bool
ToJSON) via KeyValuePairs DijkstraGenesis

instance FromJSON DijkstraGenesis where
  parseJSON :: Value -> Parser DijkstraGenesis
parseJSON = String
-> (Object -> Parser DijkstraGenesis)
-> Value
-> Parser DijkstraGenesis
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"DijkstraGenesis" ((Object -> Parser DijkstraGenesis)
 -> Value -> Parser DijkstraGenesis)
-> (Object -> Parser DijkstraGenesis)
-> Value
-> Parser DijkstraGenesis
forall a b. (a -> b) -> a -> b
$ \Object
obj -> do
    UpgradeDijkstraPParams Identity DijkstraEra
dgUpgradePParams <- Value -> Parser (UpgradeDijkstraPParams Identity DijkstraEra)
forall a. FromJSON a => Value -> Parser a
parseJSON (Object -> Value
Object Object
obj)
    DijkstraGenesis -> Parser DijkstraGenesis
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DijkstraGenesis {UpgradeDijkstraPParams Identity DijkstraEra
dgUpgradePParams :: UpgradeDijkstraPParams Identity DijkstraEra
dgUpgradePParams :: UpgradeDijkstraPParams Identity DijkstraEra
..}

instance NoThunks DijkstraGenesis

instance EraGenesis DijkstraEra where
  type Genesis DijkstraEra = DijkstraGenesis

-- TODO: Implement this and use for ToJSON instance
instance ToKeyValuePairs DijkstraGenesis where
  toKeyValuePairs :: forall e kv. KeyValue e kv => DijkstraGenesis -> [kv]
toKeyValuePairs dg :: DijkstraGenesis
dg@(DijkstraGenesis UpgradeDijkstraPParams Identity DijkstraEra
_) =
    let DijkstraGenesis {UpgradeDijkstraPParams Identity DijkstraEra
dgUpgradePParams :: DijkstraGenesis -> UpgradeDijkstraPParams Identity DijkstraEra
dgUpgradePParams :: UpgradeDijkstraPParams Identity DijkstraEra
..} = DijkstraGenesis
dg
     in UpgradeDijkstraPParams Identity DijkstraEra -> [kv]
forall a e kv. (ToKeyValuePairs a, KeyValue e kv) => a -> [kv]
forall e kv.
KeyValue e kv =>
UpgradeDijkstraPParams Identity DijkstraEra -> [kv]
toKeyValuePairs UpgradeDijkstraPParams Identity DijkstraEra
dgUpgradePParams

instance FromCBOR DijkstraGenesis where
  fromCBOR :: forall s. Decoder s DijkstraGenesis
fromCBOR =
    forall era t s. Era era => Decoder s t -> Decoder s t
eraDecoder @DijkstraEra (Decoder s DijkstraGenesis -> Decoder s DijkstraGenesis)
-> Decoder s DijkstraGenesis -> Decoder s DijkstraGenesis
forall a b. (a -> b) -> a -> b
$
      Decode ('Closed 'Dense) DijkstraGenesis
-> Decoder s DijkstraGenesis
forall t (w :: Wrapped) s. Typeable t => Decode w t -> Decoder s t
decode (Decode ('Closed 'Dense) DijkstraGenesis
 -> Decoder s DijkstraGenesis)
-> Decode ('Closed 'Dense) DijkstraGenesis
-> Decoder s DijkstraGenesis
forall a b. (a -> b) -> a -> b
$
        (UpgradeDijkstraPParams Identity DijkstraEra -> DijkstraGenesis)
-> Decode
     ('Closed 'Dense)
     (UpgradeDijkstraPParams Identity DijkstraEra -> DijkstraGenesis)
forall t. t -> Decode ('Closed 'Dense) t
RecD UpgradeDijkstraPParams Identity DijkstraEra -> DijkstraGenesis
DijkstraGenesis
          Decode
  ('Closed 'Dense)
  (UpgradeDijkstraPParams Identity DijkstraEra -> DijkstraGenesis)
-> Decode
     ('Closed Any) (UpgradeDijkstraPParams Identity DijkstraEra)
-> Decode ('Closed 'Dense) DijkstraGenesis
forall a (w1 :: Wrapped) t (w :: Density).
Typeable a =>
Decode w1 (a -> t) -> Decode ('Closed w) a -> Decode w1 t
<! Decode ('Closed Any) (UpgradeDijkstraPParams Identity DijkstraEra)
forall t (w :: Wrapped). DecCBOR t => Decode w t
From

instance ToCBOR DijkstraGenesis where
  toCBOR :: DijkstraGenesis -> Encoding
toCBOR dg :: DijkstraGenesis
dg@(DijkstraGenesis UpgradeDijkstraPParams Identity DijkstraEra
_) =
    let DijkstraGenesis {UpgradeDijkstraPParams Identity DijkstraEra
dgUpgradePParams :: DijkstraGenesis -> UpgradeDijkstraPParams Identity DijkstraEra
dgUpgradePParams :: UpgradeDijkstraPParams Identity DijkstraEra
..} = DijkstraGenesis
dg
     in forall era t. (Era era, EncCBOR t) => t -> Encoding
toEraCBOR @DijkstraEra (Encoding -> Encoding)
-> (Encode ('Closed 'Dense) DijkstraGenesis -> Encoding)
-> Encode ('Closed 'Dense) DijkstraGenesis
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encode ('Closed 'Dense) DijkstraGenesis -> Encoding
forall (w :: Wrapped) t. Encode w t -> Encoding
encode (Encode ('Closed 'Dense) DijkstraGenesis -> Encoding)
-> Encode ('Closed 'Dense) DijkstraGenesis -> Encoding
forall a b. (a -> b) -> a -> b
$
          (UpgradeDijkstraPParams Identity DijkstraEra -> DijkstraGenesis)
-> Encode
     ('Closed 'Dense)
     (UpgradeDijkstraPParams Identity DijkstraEra -> DijkstraGenesis)
forall t. t -> Encode ('Closed 'Dense) t
Rec UpgradeDijkstraPParams Identity DijkstraEra -> DijkstraGenesis
DijkstraGenesis
            Encode
  ('Closed 'Dense)
  (UpgradeDijkstraPParams Identity DijkstraEra -> DijkstraGenesis)
-> Encode
     ('Closed 'Dense) (UpgradeDijkstraPParams Identity DijkstraEra)
-> Encode ('Closed 'Dense) DijkstraGenesis
forall (w :: Wrapped) a t (r :: Density).
Encode w (a -> t) -> Encode ('Closed r) a -> Encode w t
!> UpgradeDijkstraPParams Identity DijkstraEra
-> Encode
     ('Closed 'Dense) (UpgradeDijkstraPParams Identity DijkstraEra)
forall t. EncCBOR t => t -> Encode ('Closed 'Dense) t
To UpgradeDijkstraPParams Identity DijkstraEra
dgUpgradePParams

instance DecCBOR DijkstraGenesis

instance EncCBOR DijkstraGenesis