{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableSuperClasses #-}
module Cardano.Ledger.Core.Translation (
PreviousEra,
TranslationContext,
TranslationError,
TranslateEra,
translateEra,
translateEraMaybe,
translateEra',
translateEraThroughCBOR,
)
where
import Cardano.Ledger.Binary
import qualified Cardano.Ledger.Binary.Plain as Plain
import Cardano.Ledger.Core.Era
import Cardano.Ledger.Genesis
import Control.Monad.Except (Except, runExcept, throwError)
import Data.Coerce (Coercible, coerce)
import Data.Kind (Type)
import Data.Void (Void, absurd)
type family TranslationContext era :: Type
class (Era era, Era (PreviousEra era)) => TranslateEra era f where
type TranslationError era f :: Type
type TranslationError era f = Void
translateEra ::
TranslationContext era -> f (PreviousEra era) -> Except (TranslationError era f) (f era)
default translateEra ::
(Coercible (f (PreviousEra era)) (f era), TranslationContext era ~ NoGenesis era) =>
TranslationContext era ->
f (PreviousEra era) ->
Except (TranslationError era f) (f era)
translateEra NoGenesis era
TranslationContext era
NoGenesis = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
translateEra' ::
(TranslateEra era f, TranslationError era f ~ Void) =>
TranslationContext era ->
f (PreviousEra era) ->
f era
translateEra' :: forall era (f :: * -> *).
(TranslateEra era f, TranslationError era f ~ Void) =>
TranslationContext era -> f (PreviousEra era) -> f era
translateEra' TranslationContext era
ctxt = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. Void -> a
absurd forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Except e a -> Either e a
runExcept forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall era (f :: * -> *).
TranslateEra era f =>
TranslationContext era
-> f (PreviousEra era) -> Except (TranslationError era f) (f era)
translateEra TranslationContext era
ctxt
translateEraMaybe ::
(TranslateEra era f, TranslationError era f ~ ()) =>
TranslationContext era ->
f (PreviousEra era) ->
Maybe (f era)
translateEraMaybe :: forall era (f :: * -> *).
(TranslateEra era f, TranslationError era f ~ ()) =>
TranslationContext era -> f (PreviousEra era) -> Maybe (f era)
translateEraMaybe TranslationContext era
ctxt =
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Except e a -> Either e a
runExcept forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall era (f :: * -> *).
TranslateEra era f =>
TranslationContext era
-> f (PreviousEra era) -> Except (TranslationError era f) (f era)
translateEra TranslationContext era
ctxt
translateEraThroughCBOR ::
forall era ti to.
(Era era, ToCBOR (ti (PreviousEra era)), DecCBOR (to era)) =>
ti (PreviousEra era) ->
Except DecoderError (to era)
translateEraThroughCBOR :: forall era (ti :: * -> *) (to :: * -> *).
(Era era, ToCBOR (ti (PreviousEra era)), DecCBOR (to era)) =>
ti (PreviousEra era) -> Except DecoderError (to era)
translateEraThroughCBOR ti (PreviousEra era)
prevEraType =
case forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull (forall era. Era era => Version
eraProtVerLow @era) (forall a. ToCBOR a => a -> ByteString
Plain.serialize ti (PreviousEra era)
prevEraType) of
Right to era
curEraType -> forall (f :: * -> *) a. Applicative f => a -> f a
pure to era
curEraType
Left DecoderError
decoderError -> forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError DecoderError
decoderError