{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE UndecidableSuperClasses #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Ledger.Shelley.TxAuxData (
  ShelleyTxAuxData (ShelleyTxAuxData),
  ShelleyTxAuxDataRaw,
  hashShelleyTxAuxData,

  -- * Re-exports
  Metadatum (..),
  validMetadatum,
)
where

import Cardano.Ledger.Binary (Annotator (..), DecCBOR (..), EncCBOR (..))
import qualified Cardano.Ledger.Binary.Plain as Plain (ToCBOR)
import Cardano.Ledger.Core
import Cardano.Ledger.MemoBytes (
  EqRaw (..),
  Mem,
  MemoBytes,
  MemoHashIndex,
  Memoized (RawType),
  getMemoRawType,
  getMemoSafeHash,
  lensMemoRawType,
  mkMemoized,
 )
import Cardano.Ledger.Metadata (Metadatum (..), validMetadatum)
import Cardano.Ledger.Shelley.Era (ShelleyEra)
import Control.DeepSeq (NFData)
import Data.Map.Strict (Map)
import Data.Word (Word64)
import GHC.Generics (Generic)
import NoThunks.Class (InspectHeapNamed (..), NoThunks (..))

newtype ShelleyTxAuxDataRaw era = ShelleyTxAuxDataRaw
  { forall era. ShelleyTxAuxDataRaw era -> Map Word64 Metadatum
stadrMetadata :: Map Word64 Metadatum
  }
  deriving (ShelleyTxAuxDataRaw era -> ShelleyTxAuxDataRaw era -> Bool
forall era.
ShelleyTxAuxDataRaw era -> ShelleyTxAuxDataRaw era -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ShelleyTxAuxDataRaw era -> ShelleyTxAuxDataRaw era -> Bool
$c/= :: forall era.
ShelleyTxAuxDataRaw era -> ShelleyTxAuxDataRaw era -> Bool
== :: ShelleyTxAuxDataRaw era -> ShelleyTxAuxDataRaw era -> Bool
$c== :: forall era.
ShelleyTxAuxDataRaw era -> ShelleyTxAuxDataRaw era -> Bool
Eq, Int -> ShelleyTxAuxDataRaw era -> ShowS
forall era. Int -> ShelleyTxAuxDataRaw era -> ShowS
forall era. [ShelleyTxAuxDataRaw era] -> ShowS
forall era. ShelleyTxAuxDataRaw era -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ShelleyTxAuxDataRaw era] -> ShowS
$cshowList :: forall era. [ShelleyTxAuxDataRaw era] -> ShowS
show :: ShelleyTxAuxDataRaw era -> String
$cshow :: forall era. ShelleyTxAuxDataRaw era -> String
showsPrec :: Int -> ShelleyTxAuxDataRaw era -> ShowS
$cshowsPrec :: forall era. Int -> ShelleyTxAuxDataRaw era -> ShowS
Show, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall era x.
Rep (ShelleyTxAuxDataRaw era) x -> ShelleyTxAuxDataRaw era
forall era x.
ShelleyTxAuxDataRaw era -> Rep (ShelleyTxAuxDataRaw era) x
$cto :: forall era x.
Rep (ShelleyTxAuxDataRaw era) x -> ShelleyTxAuxDataRaw era
$cfrom :: forall era x.
ShelleyTxAuxDataRaw era -> Rep (ShelleyTxAuxDataRaw era) x
Generic)
  deriving newtype (ShelleyTxAuxDataRaw era -> ()
forall era. ShelleyTxAuxDataRaw era -> ()
forall a. (a -> ()) -> NFData a
rnf :: ShelleyTxAuxDataRaw era -> ()
$crnf :: forall era. ShelleyTxAuxDataRaw era -> ()
NFData)

deriving via
  InspectHeapNamed "ShelleyTxAuxDataRaw" (ShelleyTxAuxDataRaw era)
  instance
    NoThunks (ShelleyTxAuxDataRaw era)

deriving newtype instance Era era => EncCBOR (ShelleyTxAuxDataRaw era)

deriving newtype instance Era era => DecCBOR (ShelleyTxAuxDataRaw era)

instance Era era => DecCBOR (Annotator (ShelleyTxAuxDataRaw era)) where
  decCBOR :: forall s. Decoder s (Annotator (ShelleyTxAuxDataRaw era))
decCBOR = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. DecCBOR a => Decoder s a
decCBOR

deriving via
  InspectHeapNamed "ShelleyTxAuxDataRaw" (ShelleyTxAuxData era)
  instance
    NoThunks (ShelleyTxAuxData era)

deriving via
  (Mem ShelleyTxAuxDataRaw era)
  instance
    Era era => DecCBOR (Annotator (ShelleyTxAuxData era))

newtype ShelleyTxAuxData era
  = AuxiliaryDataConstr (MemoBytes ShelleyTxAuxDataRaw era)
  deriving (ShelleyTxAuxData era -> ShelleyTxAuxData era -> Bool
forall era. ShelleyTxAuxData era -> ShelleyTxAuxData era -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ShelleyTxAuxData era -> ShelleyTxAuxData era -> Bool
$c/= :: forall era. ShelleyTxAuxData era -> ShelleyTxAuxData era -> Bool
== :: ShelleyTxAuxData era -> ShelleyTxAuxData era -> Bool
$c== :: forall era. ShelleyTxAuxData era -> ShelleyTxAuxData era -> Bool
Eq, Int -> ShelleyTxAuxData era -> ShowS
forall era. Int -> ShelleyTxAuxData era -> ShowS
forall era. [ShelleyTxAuxData era] -> ShowS
forall era. ShelleyTxAuxData era -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ShelleyTxAuxData era] -> ShowS
$cshowList :: forall era. [ShelleyTxAuxData era] -> ShowS
show :: ShelleyTxAuxData era -> String
$cshow :: forall era. ShelleyTxAuxData era -> String
showsPrec :: Int -> ShelleyTxAuxData era -> ShowS
$cshowsPrec :: forall era. Int -> ShelleyTxAuxData era -> ShowS
Show, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall era x. Rep (ShelleyTxAuxData era) x -> ShelleyTxAuxData era
forall era x. ShelleyTxAuxData era -> Rep (ShelleyTxAuxData era) x
$cto :: forall era x. Rep (ShelleyTxAuxData era) x -> ShelleyTxAuxData era
$cfrom :: forall era x. ShelleyTxAuxData era -> Rep (ShelleyTxAuxData era) x
Generic)
  deriving newtype (ShelleyTxAuxData era -> ()
forall era. ShelleyTxAuxData era -> ()
forall a. (a -> ()) -> NFData a
rnf :: ShelleyTxAuxData era -> ()
$crnf :: forall era. ShelleyTxAuxData era -> ()
NFData, ShelleyTxAuxData era -> Encoding
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [ShelleyTxAuxData era] -> Size
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (ShelleyTxAuxData era) -> Size
forall {era}. Typeable era => Typeable (ShelleyTxAuxData era)
forall era. Typeable era => ShelleyTxAuxData era -> Encoding
forall a.
Typeable a
-> (a -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy [a] -> Size)
-> ToCBOR a
forall era.
Typeable era =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [ShelleyTxAuxData era] -> Size
forall era.
Typeable era =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (ShelleyTxAuxData era) -> Size
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [ShelleyTxAuxData era] -> Size
$cencodedListSizeExpr :: forall era.
Typeable era =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [ShelleyTxAuxData era] -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (ShelleyTxAuxData era) -> Size
$cencodedSizeExpr :: forall era.
Typeable era =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (ShelleyTxAuxData era) -> Size
toCBOR :: ShelleyTxAuxData era -> Encoding
$ctoCBOR :: forall era. Typeable era => ShelleyTxAuxData era -> Encoding
Plain.ToCBOR, ShelleyTxAuxData era -> Int
ShelleyTxAuxData era -> ByteString
forall i. Proxy i -> ShelleyTxAuxData era -> SafeHash i
forall era. ShelleyTxAuxData era -> Int
forall era. ShelleyTxAuxData era -> ByteString
forall t.
(t -> ByteString)
-> (t -> Int)
-> (forall i. Proxy i -> t -> SafeHash i)
-> SafeToHash t
forall era i. Proxy i -> ShelleyTxAuxData era -> SafeHash i
makeHashWithExplicitProxys :: forall i. Proxy i -> ShelleyTxAuxData era -> SafeHash i
$cmakeHashWithExplicitProxys :: forall era i. Proxy i -> ShelleyTxAuxData era -> SafeHash i
originalBytesSize :: ShelleyTxAuxData era -> Int
$coriginalBytesSize :: forall era. ShelleyTxAuxData era -> Int
originalBytes :: ShelleyTxAuxData era -> ByteString
$coriginalBytes :: forall era. ShelleyTxAuxData era -> ByteString
SafeToHash)

instance Memoized ShelleyTxAuxData where
  type RawType ShelleyTxAuxData = ShelleyTxAuxDataRaw

instance EraTxAuxData ShelleyEra where
  type TxAuxData ShelleyEra = ShelleyTxAuxData ShelleyEra

  mkBasicTxAuxData :: TxAuxData ShelleyEra
mkBasicTxAuxData = forall era. Era era => Map Word64 Metadatum -> ShelleyTxAuxData era
ShelleyTxAuxData forall a. Monoid a => a
mempty

  metadataTxAuxDataL :: Lens' (TxAuxData ShelleyEra) (Map Word64 Metadatum)
metadataTxAuxDataL =
    forall era (t :: * -> *) a b.
(Era era, EncCBOR (RawType t era), Memoized t) =>
(RawType t era -> a)
-> (RawType t era -> b -> RawType t era)
-> Lens (t era) (t era) a b
lensMemoRawType forall era. ShelleyTxAuxDataRaw era -> Map Word64 Metadatum
stadrMetadata forall a b. (a -> b) -> a -> b
$ \RawType ShelleyTxAuxData ShelleyEra
txAuxDataRaw Map Word64 Metadatum
md -> RawType ShelleyTxAuxData ShelleyEra
txAuxDataRaw {stadrMetadata :: Map Word64 Metadatum
stadrMetadata = Map Word64 Metadatum
md}

  -- Calling this partial function will result in compilation error, since ByronEra has
  -- no instance for EraTxOut type class.
  upgradeTxAuxData :: EraTxAuxData (PreviousEra ShelleyEra) =>
TxAuxData (PreviousEra ShelleyEra) -> TxAuxData ShelleyEra
upgradeTxAuxData = forall a. HasCallStack => String -> a
error String
"It is not possible to translate Byron TxOut with 'upgradeTxOut'"

  validateTxAuxData :: ProtVer -> TxAuxData ShelleyEra -> Bool
validateTxAuxData ProtVer
_ (ShelleyTxAuxData Map Word64 Metadatum
m) = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Metadatum -> Bool
validMetadatum Map Word64 Metadatum
m

instance EqRaw (ShelleyTxAuxData era)

instance HashAnnotated (ShelleyTxAuxData era) EraIndependentTxAuxData where
  hashAnnotated :: ShelleyTxAuxData era -> SafeHash EraIndependentTxAuxData
hashAnnotated = forall (t :: * -> *) era.
Memoized t =>
t era -> SafeHash (MemoHashIndex (RawType t))
getMemoSafeHash

hashShelleyTxAuxData ::
  ShelleyTxAuxData era ->
  SafeHash EraIndependentTxAuxData
hashShelleyTxAuxData :: forall era.
ShelleyTxAuxData era -> SafeHash EraIndependentTxAuxData
hashShelleyTxAuxData = forall x i. HashAnnotated x i => x -> SafeHash i
hashAnnotated
{-# DEPRECATED hashShelleyTxAuxData "In favor of `hashAnnotated`" #-}

pattern ShelleyTxAuxData :: forall era. Era era => Map Word64 Metadatum -> ShelleyTxAuxData era
pattern $bShelleyTxAuxData :: forall era. Era era => Map Word64 Metadatum -> ShelleyTxAuxData era
$mShelleyTxAuxData :: forall {r} {era}.
Era era =>
ShelleyTxAuxData era
-> (Map Word64 Metadatum -> r) -> ((# #) -> r) -> r
ShelleyTxAuxData m <-
  (getMemoRawType -> ShelleyTxAuxDataRaw m)
  where
    ShelleyTxAuxData Map Word64 Metadatum
m = forall era (t :: * -> *).
(Era era, EncCBOR (RawType t era), Memoized t) =>
RawType t era -> t era
mkMemoized forall a b. (a -> b) -> a -> b
$ forall era. Map Word64 Metadatum -> ShelleyTxAuxDataRaw era
ShelleyTxAuxDataRaw Map Word64 Metadatum
m

{-# COMPLETE ShelleyTxAuxData #-}

-- | Encodes memoized bytes created upon construction.
instance Era era => EncCBOR (ShelleyTxAuxData era)

type instance MemoHashIndex ShelleyTxAuxDataRaw = EraIndependentTxAuxData