{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE EmptyDataDeriving #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE UndecidableSuperClasses #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
#if __GLASGOW_HASKELL__ >= 908
{-# OPTIONS_GHC -Wno-x-unsafe-ledger-internal #-}
#endif
module Cardano.Ledger.Core.Era (
Era (..),
ByronEra,
EraRule,
EraRuleFailure,
EraRuleEvent,
VoidEraRule,
absurdEraRule,
InjectRuleFailure (..),
InjectRuleEvent (..),
AtMostEra,
AtLeastEra,
ExactEra,
ProtVerAtMost,
ProtVerAtLeast,
ProtVerInBounds,
atLeastEra,
atMostEra,
notSupportedInThisEra,
notSupportedInThisEraL,
eraProtVerLow,
eraProtVerHigh,
eraProtVersions,
toEraCBOR,
fromEraCBOR,
fromEraShareCBOR,
eraDecoder,
eraDecoderWithBytes,
) where
import Cardano.Ledger.Binary
import qualified Cardano.Ledger.Binary.Plain as Plain
import Cardano.Ledger.Internal.Era (ByronEra, Era (..), EraFromName)
import Control.DeepSeq (NFData (..))
import Control.State.Transition.Extended (PredicateFailure, STS (..))
import qualified Data.ByteString.Lazy as BSL
import Data.Kind (Constraint, Type)
import GHC.Stack (HasCallStack)
import GHC.TypeLits
import Lens.Micro
type family EraRule (rule :: Symbol) era = (r :: Type) | r -> rule
type family EraRuleFailure (rule :: Symbol) era = (r :: Type) | r -> rule era
type family EraRuleEvent (rule :: Symbol) era = (r :: Type) | r -> rule era
data VoidEraRule (rule :: Symbol) era
deriving (Int -> VoidEraRule rule era -> ShowS
[VoidEraRule rule era] -> ShowS
VoidEraRule rule era -> String
(Int -> VoidEraRule rule era -> ShowS)
-> (VoidEraRule rule era -> String)
-> ([VoidEraRule rule era] -> ShowS)
-> Show (VoidEraRule rule era)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (rule :: Symbol) era. Int -> VoidEraRule rule era -> ShowS
forall (rule :: Symbol) era. [VoidEraRule rule era] -> ShowS
forall (rule :: Symbol) era. VoidEraRule rule era -> String
$cshowsPrec :: forall (rule :: Symbol) era. Int -> VoidEraRule rule era -> ShowS
showsPrec :: Int -> VoidEraRule rule era -> ShowS
$cshow :: forall (rule :: Symbol) era. VoidEraRule rule era -> String
show :: VoidEraRule rule era -> String
$cshowList :: forall (rule :: Symbol) era. [VoidEraRule rule era] -> ShowS
showList :: [VoidEraRule rule era] -> ShowS
Show, VoidEraRule rule era -> VoidEraRule rule era -> Bool
(VoidEraRule rule era -> VoidEraRule rule era -> Bool)
-> (VoidEraRule rule era -> VoidEraRule rule era -> Bool)
-> Eq (VoidEraRule rule era)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (rule :: Symbol) era.
VoidEraRule rule era -> VoidEraRule rule era -> Bool
$c== :: forall (rule :: Symbol) era.
VoidEraRule rule era -> VoidEraRule rule era -> Bool
== :: VoidEraRule rule era -> VoidEraRule rule era -> Bool
$c/= :: forall (rule :: Symbol) era.
VoidEraRule rule era -> VoidEraRule rule era -> Bool
/= :: VoidEraRule rule era -> VoidEraRule rule era -> Bool
Eq, Eq (VoidEraRule rule era)
Eq (VoidEraRule rule era) =>
(VoidEraRule rule era -> VoidEraRule rule era -> Ordering)
-> (VoidEraRule rule era -> VoidEraRule rule era -> Bool)
-> (VoidEraRule rule era -> VoidEraRule rule era -> Bool)
-> (VoidEraRule rule era -> VoidEraRule rule era -> Bool)
-> (VoidEraRule rule era -> VoidEraRule rule era -> Bool)
-> (VoidEraRule rule era
-> VoidEraRule rule era -> VoidEraRule rule era)
-> (VoidEraRule rule era
-> VoidEraRule rule era -> VoidEraRule rule era)
-> Ord (VoidEraRule rule era)
VoidEraRule rule era -> VoidEraRule rule era -> Bool
VoidEraRule rule era -> VoidEraRule rule era -> Ordering
VoidEraRule rule era
-> VoidEraRule rule era -> VoidEraRule rule era
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
forall (rule :: Symbol) era. Eq (VoidEraRule rule era)
forall (rule :: Symbol) era.
VoidEraRule rule era -> VoidEraRule rule era -> Bool
forall (rule :: Symbol) era.
VoidEraRule rule era -> VoidEraRule rule era -> Ordering
forall (rule :: Symbol) era.
VoidEraRule rule era
-> VoidEraRule rule era -> VoidEraRule rule era
$ccompare :: forall (rule :: Symbol) era.
VoidEraRule rule era -> VoidEraRule rule era -> Ordering
compare :: VoidEraRule rule era -> VoidEraRule rule era -> Ordering
$c< :: forall (rule :: Symbol) era.
VoidEraRule rule era -> VoidEraRule rule era -> Bool
< :: VoidEraRule rule era -> VoidEraRule rule era -> Bool
$c<= :: forall (rule :: Symbol) era.
VoidEraRule rule era -> VoidEraRule rule era -> Bool
<= :: VoidEraRule rule era -> VoidEraRule rule era -> Bool
$c> :: forall (rule :: Symbol) era.
VoidEraRule rule era -> VoidEraRule rule era -> Bool
> :: VoidEraRule rule era -> VoidEraRule rule era -> Bool
$c>= :: forall (rule :: Symbol) era.
VoidEraRule rule era -> VoidEraRule rule era -> Bool
>= :: VoidEraRule rule era -> VoidEraRule rule era -> Bool
$cmax :: forall (rule :: Symbol) era.
VoidEraRule rule era
-> VoidEraRule rule era -> VoidEraRule rule era
max :: VoidEraRule rule era
-> VoidEraRule rule era -> VoidEraRule rule era
$cmin :: forall (rule :: Symbol) era.
VoidEraRule rule era
-> VoidEraRule rule era -> VoidEraRule rule era
min :: VoidEraRule rule era
-> VoidEraRule rule era -> VoidEraRule rule era
Ord)
instance NFData (VoidEraRule (rule :: Symbol) era) where
rnf :: VoidEraRule rule era -> ()
rnf = VoidEraRule rule era -> ()
forall (rule :: Symbol) era a. VoidEraRule rule era -> a
absurdEraRule
instance (KnownSymbol rule, Era era) => ToCBOR (VoidEraRule (rule :: Symbol) era) where
toCBOR :: VoidEraRule rule era -> Encoding
toCBOR = VoidEraRule rule era -> Encoding
forall (rule :: Symbol) era a. VoidEraRule rule era -> a
absurdEraRule
instance (KnownSymbol rule, Era era) => EncCBOR (VoidEraRule (rule :: Symbol) era)
instance (KnownSymbol rule, Era era) => FromCBOR (VoidEraRule (rule :: Symbol) era) where
fromCBOR :: forall s. Decoder s (VoidEraRule rule era)
fromCBOR = DecoderError -> Decoder s (VoidEraRule rule era)
forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError DecoderError
DecoderErrorVoid
instance (KnownSymbol rule, Era era) => DecCBOR (VoidEraRule (rule :: Symbol) era)
absurdEraRule :: VoidEraRule rule era -> a
absurdEraRule :: forall (rule :: Symbol) era a. VoidEraRule rule era -> a
absurdEraRule VoidEraRule rule era
a = case VoidEraRule rule era
a of {}
type instance EraRuleFailure "EPOCH" era = VoidEraRule "EPOCH" era
type instance EraRuleFailure "NEWEPOCH" era = VoidEraRule "NEWEPOCH" era
type instance EraRuleFailure "MIR" era = VoidEraRule "MIR" era
type instance EraRuleFailure "NEWPP" era = VoidEraRule "NEWPP" era
type instance EraRuleFailure "SNAP" era = VoidEraRule "SNAP" era
type instance EraRuleFailure "TICK" era = VoidEraRule "TICK" era
type instance EraRuleFailure "TICKF" era = VoidEraRule "TICKF" era
type instance EraRuleFailure "UPEC" era = VoidEraRule "UPEC" era
type instance EraRuleFailure "RUPD" era = VoidEraRule "RUPD" era
type instance EraRuleFailure "POOLREAP" era = VoidEraRule "POOLREAP" era
class
EraRuleFailure rule era ~ PredicateFailure (EraRule rule era) =>
InjectRuleFailure (rule :: Symbol) t era
where
injectFailure :: t era -> EraRuleFailure rule era
default injectFailure :: t era ~ EraRuleFailure rule era => t era -> EraRuleFailure rule era
injectFailure = t era -> t era
t era -> EraRuleFailure rule era
forall a. a -> a
id
class
EraRuleEvent rule era ~ Event (EraRule rule era) =>
InjectRuleEvent (rule :: Symbol) t era
where
injectEvent :: t era -> EraRuleEvent rule era
default injectEvent :: t era ~ EraRuleEvent rule era => t era -> EraRuleEvent rule era
injectEvent = t era -> t era
t era -> EraRuleEvent rule era
forall a. a -> a
id
type family ProtVerIsInBounds (check :: Symbol) era (v :: Nat) (b :: Bool) :: Constraint where
ProtVerIsInBounds check era v 'True = ()
ProtVerIsInBounds check era v 'False =
TypeError
( 'ShowType era
':<>: 'Text " protocol version bounds are: ["
':<>: 'ShowType (ProtVerLow era)
':<>: 'Text ", "
':<>: 'ShowType (ProtVerHigh era)
':<>: 'Text "], but required is "
':<>: 'Text check
':<>: 'Text " "
':<>: 'ShowType v
)
type family ProtVerAtLeast era (l :: Nat) :: Constraint where
ProtVerAtLeast era l = ProtVerIsInBounds "at least" era l (l <=? ProtVerHigh era)
type family ProtVerAtMost era (h :: Nat) :: Constraint where
ProtVerAtMost era h = ProtVerIsInBounds "at most" era h (ProtVerLow era <=? h)
type ProtVerInBounds era l h = (ProtVerAtLeast era l, ProtVerAtMost era h)
type ExactEra inEra era =
ProtVerInBounds era (ProtVerLow inEra) (ProtVerHigh inEra)
type AtLeastEra (atLeastEra :: Symbol) era =
ProtVerAtLeast era (ProtVerLow (EraFromName atLeastEra))
type AtMostEra (eraMostEra :: Symbol) era =
ProtVerAtMost era (ProtVerHigh (EraFromName eraMostEra))
eraProtVerLow :: forall era. Era era => Version
eraProtVerLow :: forall era. Era era => Version
eraProtVerLow = forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @(ProtVerLow era)
eraProtVerHigh :: forall era. Era era => Version
eraProtVerHigh :: forall era. Era era => Version
eraProtVerHigh = forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @(ProtVerHigh era)
eraProtVersions :: forall era. Era era => [Version]
eraProtVersions :: forall era. Era era => [Version]
eraProtVersions = [forall era. Era era => Version
eraProtVerLow @era .. forall era. Era era => Version
eraProtVerHigh @era]
atLeastEra :: AtLeastEra eraName era => ()
atLeastEra :: forall (eraName :: Symbol) era. AtLeastEra eraName era => ()
atLeastEra = ()
atMostEra :: AtMostEra eraName era => ()
atMostEra :: forall (eraName :: Symbol) era. AtMostEra eraName era => ()
atMostEra = ()
notSupportedInThisEra :: HasCallStack => a
notSupportedInThisEra :: forall a. HasCallStack => a
notSupportedInThisEra = String -> a
forall a. HasCallStack => String -> a
error String
"Impossible: Function is not supported in this era"
notSupportedInThisEraL :: HasCallStack => Lens' a b
notSupportedInThisEraL :: forall a b. HasCallStack => Lens' a b
notSupportedInThisEraL = (a -> b) -> (a -> b -> a) -> Lens a a b b
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens a -> b
forall a. HasCallStack => a
notSupportedInThisEra a -> b -> a
forall a. HasCallStack => a
notSupportedInThisEra
toEraCBOR :: forall era t. (Era era, EncCBOR t) => t -> Plain.Encoding
toEraCBOR :: forall era t. (Era era, EncCBOR t) => t -> Encoding
toEraCBOR = Version -> Encoding -> Encoding
toPlainEncoding (forall era. Era era => Version
eraProtVerLow @era) (Encoding -> Encoding) -> (t -> Encoding) -> t -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR
{-# INLINE toEraCBOR #-}
fromEraCBOR :: forall era t s. (Era era, DecCBOR t) => Plain.Decoder s t
fromEraCBOR :: forall era t s. (Era era, DecCBOR t) => Decoder s t
fromEraCBOR = forall era t s. Era era => Decoder s t -> Decoder s t
eraDecoder @era Decoder s t
forall s. Decoder s t
forall a s. DecCBOR a => Decoder s a
decCBOR
{-# INLINE fromEraCBOR #-}
fromEraShareCBOR :: forall era t s. (Era era, DecShareCBOR t) => Plain.Decoder s t
fromEraShareCBOR :: forall era t s. (Era era, DecShareCBOR t) => Decoder s t
fromEraShareCBOR = forall era t s. Era era => Decoder s t -> Decoder s t
eraDecoder @era Decoder s t
forall a s. DecShareCBOR a => Decoder s a
decNoShareCBOR
{-# INLINE fromEraShareCBOR #-}
eraDecoder :: forall era t s. Era era => Decoder s t -> Plain.Decoder s t
eraDecoder :: forall era t s. Era era => Decoder s t -> Decoder s t
eraDecoder = Maybe ByteString -> Version -> Decoder s t -> Decoder s t
forall s a.
Maybe ByteString -> Version -> Decoder s a -> Decoder s a
toPlainDecoder Maybe ByteString
forall a. Maybe a
Nothing (forall era. Era era => Version
eraProtVerLow @era)
{-# INLINE eraDecoder #-}
eraDecoderWithBytes :: forall era t s. Era era => BSL.ByteString -> Decoder s t -> Plain.Decoder s t
eraDecoderWithBytes :: forall era t s. Era era => ByteString -> Decoder s t -> Decoder s t
eraDecoderWithBytes ByteString
bsl = Maybe ByteString -> Version -> Decoder s t -> Decoder s t
forall s a.
Maybe ByteString -> Version -> Decoder s a -> Decoder s a
toPlainDecoder (ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
bsl) (forall era. Era era => Version
eraProtVerLow @era)
{-# INLINE eraDecoderWithBytes #-}