Skip to content

Commit

Permalink
Added Core.hs to each era that has a TxBody class
Browse files Browse the repository at this point in the history
  • Loading branch information
Soupstraw committed Sep 15, 2022
1 parent 1176aff commit 5bc9175
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 58 deletions.
1 change: 1 addition & 0 deletions eras/alonzo/impl/cardano-ledger-alonzo.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ library
import: base, project-config
exposed-modules:
Cardano.Ledger.Alonzo
Cardano.Ledger.Alonzo.Core
Cardano.Ledger.Alonzo.Data
Cardano.Ledger.Alonzo.Genesis
Cardano.Ledger.Alonzo.Language
Expand Down
29 changes: 29 additions & 0 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Core.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{-# LANGUAGE DataKinds #-}

module Cardano.Ledger.Alonzo.Core
( AlonzoEraTxBody (..)
, ScriptIntegrityHash
) where

import Cardano.Ledger.ShelleyMA.Core (ShelleyMAEraTxBody)
import Cardano.Ledger.Alonzo.TxOut (AlonzoEraTxOut)
import Lens.Micro (Lens')
import Cardano.Ledger.Core (EraTxBody(..), Era (..), EraIndependentScriptIntegrity)
import Data.Set (Set)
import Cardano.Ledger.TxIn (TxIn(..))
import Cardano.Ledger.Keys (KeyHash(..), KeyRole (..))
import Data.Maybe.Strict (StrictMaybe)
import Cardano.Ledger.SafeHash (SafeHash)
import Cardano.Ledger.BaseTypes (Network)

type ScriptIntegrityHash c = SafeHash c EraIndependentScriptIntegrity

class (ShelleyMAEraTxBody era, AlonzoEraTxOut era) => AlonzoEraTxBody era where
collateralInputsTxBodyL :: Lens' (TxBody era) (Set (TxIn (EraCrypto era)))

reqSignerHashesTxBodyL :: Lens' (TxBody era) (Set (KeyHash 'Witness (EraCrypto era)))

scriptIntegrityHashTxBodyL ::
Lens' (TxBody era) (StrictMaybe (ScriptIntegrityHash (EraCrypto era)))

networkIdTxBodyL :: Lens' (TxBody era) (StrictMaybe Network)
14 changes: 1 addition & 13 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import Cardano.Binary
ToCBOR (..),
)
import Cardano.Ledger.Alonzo.Data (AuxiliaryDataHash (..))
import Cardano.Ledger.Alonzo.Core (ScriptIntegrityHash, AlonzoEraTxBody (..))
import Cardano.Ledger.Alonzo.Era
import Cardano.Ledger.Alonzo.Scripts ()
import Cardano.Ledger.Alonzo.TxOut
Expand All @@ -97,7 +98,6 @@ import Cardano.Ledger.Mary.Value (MaryValue (MaryValue), MultiAsset (..), polici
import Cardano.Ledger.MemoBytes (Mem, MemoBytes (..), MemoHashIndex, contentsEq, memoBytes)
import Cardano.Ledger.SafeHash
( HashAnnotated (..),
SafeHash,
SafeToHash,
)
import Cardano.Ledger.Shelley.Delegation.Certificates (DCert)
Expand All @@ -122,8 +122,6 @@ import Prelude hiding (lookup)

-- ======================================

type ScriptIntegrityHash c = SafeHash c EraIndependentScriptIntegrity

data TxBodyRaw era = TxBodyRaw
{ _inputs :: !(Set (TxIn (EraCrypto era))),
_collateral :: !(Set (TxIn (EraCrypto era))),
Expand Down Expand Up @@ -238,16 +236,6 @@ instance CC.Crypto c => ShelleyMAEraTxBody (AlonzoEra c) where
mintValueTxBodyF = mintTxBodyL . to (MaryValue 0)
{-# INLINEABLE mintValueTxBodyF #-}

class (ShelleyMAEraTxBody era, AlonzoEraTxOut era) => AlonzoEraTxBody era where
collateralInputsTxBodyL :: Lens' (Core.TxBody era) (Set (TxIn (EraCrypto era)))

reqSignerHashesTxBodyL :: Lens' (Core.TxBody era) (Set (KeyHash 'Witness (EraCrypto era)))

scriptIntegrityHashTxBodyL ::
Lens' (Core.TxBody era) (StrictMaybe (ScriptIntegrityHash (EraCrypto era)))

networkIdTxBodyL :: Lens' (Core.TxBody era) (StrictMaybe Network)

instance CC.Crypto c => AlonzoEraTxBody (AlonzoEra c) where
{-# SPECIALIZE instance AlonzoEraTxBody (AlonzoEra CC.StandardCrypto) #-}

Expand Down
1 change: 1 addition & 0 deletions eras/babbage/impl/cardano-ledger-babbage.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ library
Cardano.Ledger.Babbage.Scripts
Cardano.Ledger.Babbage.Collateral
Cardano.Ledger.Babbage.Rules
Cardano.Ledger.Babbage.Core
Cardano.Ledger.Babbage
other-modules:
Cardano.Ledger.Babbage.Era
Expand Down
28 changes: 28 additions & 0 deletions eras/babbage/impl/src/Cardano/Ledger/Babbage/Core.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Cardano.Ledger.Babbage.Core
( BabbageEraTxBody (..),
)
where

import Cardano.Ledger.Alonzo.Core (AlonzoEraTxBody)
import Cardano.Ledger.Babbage.TxOut (BabbageEraTxOut)
import Cardano.Ledger.Core (EraTxBody (..), EraTxOut (..), Era (..))
import Cardano.Ledger.Serialization (Sized (..))
import Data.Sequence.Strict (StrictSeq)
import Lens.Micro (Lens', SimpleGetter)
import Data.Set (Set)
import Cardano.Ledger.TxIn (TxIn(..))
import Data.Maybe.Strict (StrictMaybe)
import Cardano.Ledger.Coin (Coin(..))

class (AlonzoEraTxBody era, BabbageEraTxOut era) => BabbageEraTxBody era where
sizedOutputsTxBodyL :: Lens' (TxBody era) (StrictSeq (Sized (TxOut era)))

referenceInputsTxBodyL :: Lens' (TxBody era) (Set (TxIn (EraCrypto era)))

totalCollateralTxBodyL :: Lens' (TxBody era) (StrictMaybe Coin)

collateralReturnTxBodyL :: Lens' (TxBody era) (StrictMaybe (TxOut era))

sizedCollateralReturnTxBodyL :: Lens' (TxBody era) (StrictMaybe (Sized (TxOut era)))

allSizedOutputsTxBodyF :: SimpleGetter (TxBody era) (StrictSeq (Sized (TxOut era)))
14 changes: 1 addition & 13 deletions eras/babbage/impl/src/Cardano/Ledger/Babbage/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ import GHC.Generics (Generic)
import Lens.Micro
import NoThunks.Class (NoThunks)
import Prelude hiding (lookup)
import Cardano.Ledger.Babbage.Core (BabbageEraTxBody (..))

-- ======================================

Expand Down Expand Up @@ -457,19 +458,6 @@ instance CC.Crypto c => AlonzoEraTxBody (BabbageEra c) where
networkIdTxBodyL = networkIdBabbageTxBodyL
{-# INLINE networkIdTxBodyL #-}

class (AlonzoEraTxBody era, BabbageEraTxOut era) => BabbageEraTxBody era where
sizedOutputsTxBodyL :: Lens' (Core.TxBody era) (StrictSeq (Sized (Core.TxOut era)))

referenceInputsTxBodyL :: Lens' (Core.TxBody era) (Set (TxIn (EraCrypto era)))

totalCollateralTxBodyL :: Lens' (Core.TxBody era) (StrictMaybe Coin)

collateralReturnTxBodyL :: Lens' (Core.TxBody era) (StrictMaybe (Core.TxOut era))

sizedCollateralReturnTxBodyL :: Lens' (Core.TxBody era) (StrictMaybe (Sized (Core.TxOut era)))

allSizedOutputsTxBodyF :: SimpleGetter (TxBody era) (StrictSeq (Sized (Core.TxOut era)))

instance CC.Crypto c => BabbageEraTxBody (BabbageEra c) where
{-# SPECIALIZE instance BabbageEraTxBody (BabbageEra CC.StandardCrypto) #-}

Expand Down
3 changes: 3 additions & 0 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Core.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Cardano.Ledger.Conway.Core
(
) where
1 change: 1 addition & 0 deletions eras/shelley-ma/impl/cardano-ledger-shelley-ma.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ library
Cardano.Ledger.Mary.Translation
Cardano.Ledger.Mary.Value
Cardano.Ledger.ShelleyMA
Cardano.Ledger.ShelleyMA.Core
Cardano.Ledger.ShelleyMA.Era
Cardano.Ledger.ShelleyMA.AuxiliaryData
Cardano.Ledger.ShelleyMA.Rules
Expand Down
22 changes: 22 additions & 0 deletions eras/shelley-ma/impl/src/Cardano/Ledger/ShelleyMA/Core.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{-# LANGUAGE FlexibleContexts #-}

module Cardano.Ledger.ShelleyMA.Core
( ShelleyMAEraTxBody (..)
) where

import Cardano.Ledger.Shelley.Core (ShelleyEraTxBody)
import Cardano.Ledger.Val (EncodeMint, DecodeMint)
import Cardano.Ledger.Core (Value, EraTxBody (..), Era (..))
import Lens.Micro (Lens', SimpleGetter)
import Cardano.Ledger.ShelleyMA.Timelocks (ValidityInterval(..))
import Cardano.Ledger.Mary.Value (MultiAsset(..))

class
(ShelleyEraTxBody era, EncodeMint (Value era), DecodeMint (Value era)) =>
ShelleyMAEraTxBody era
where
vldtTxBodyL :: Lens' (TxBody era) ValidityInterval

mintTxBodyL :: Lens' (TxBody era) (MultiAsset (EraCrypto era))

mintValueTxBodyF :: SimpleGetter (TxBody era) (Value era)
11 changes: 1 addition & 10 deletions eras/shelley-ma/impl/src/Cardano/Ledger/ShelleyMA/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import Cardano.Ledger.MemoBytes (Mem, MemoBytes (..), MemoHashIndex, memoBytes)
import Cardano.Ledger.SafeHash (HashAnnotated (..), SafeToHash)
import Cardano.Ledger.Serialization (encodeFoldable)
import Cardano.Ledger.Shelley.PParams (Update)
import Cardano.Ledger.ShelleyMA.Core (ShelleyMAEraTxBody (..))
import Cardano.Ledger.Shelley.TxBody
( DCert (..),
ShelleyEraTxBody (..),
Expand Down Expand Up @@ -366,16 +367,6 @@ instance MAClass ma c => ShelleyEraTxBody (ShelleyMAEra ma c) where
lensTxBodyRaw certs (\txBodyRaw certs_ -> txBodyRaw {certs = certs_})
{-# INLINEABLE certsTxBodyL #-}

class
(ShelleyEraTxBody era, EncodeMint (Value era), DecodeMint (Value era)) =>
ShelleyMAEraTxBody era
where
vldtTxBodyL :: Lens' (Core.TxBody era) ValidityInterval

mintTxBodyL :: Lens' (Core.TxBody era) (MultiAsset (EraCrypto era))

mintValueTxBodyF :: SimpleGetter (Core.TxBody era) (Core.Value era)

instance MAClass ma c => ShelleyMAEraTxBody (ShelleyMAEra ma c) where
{-# SPECIALIZE instance ShelleyMAEraTxBody (ShelleyMAEra 'Mary StandardCrypto) #-}
{-# SPECIALIZE instance ShelleyMAEraTxBody (ShelleyMAEra 'Allegra StandardCrypto) #-}
Expand Down
1 change: 1 addition & 0 deletions eras/shelley/impl/cardano-ledger-shelley.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ library
Cardano.Ledger.Shelley.API.Types
Cardano.Ledger.Shelley.AdaPots
Cardano.Ledger.Shelley.BlockChain
Cardano.Ledger.Shelley.Core
Cardano.Ledger.Shelley.CompactAddr
Cardano.Ledger.Shelley.Delegation.Certificates
Cardano.Ledger.Shelley.Delegation.PoolParams
Expand Down
54 changes: 54 additions & 0 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/Core.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Ledger.Shelley.Core
( ShelleyEraTxBody (..),
Wdrl (..),
)
where

import Cardano.Ledger.Core (Era (..), EraTxBody (..), ExactEra)
import Lens.Micro (Lens')
import Cardano.Ledger.Shelley.Era (ShelleyEra)
import Cardano.Ledger.Slot (SlotNo(..))
import Data.Maybe.Strict (StrictMaybe)
import Cardano.Ledger.Shelley.PParams (Update)
import Data.Map.Strict (Map)
import Cardano.Ledger.Address (RewardAcnt(..))
import Cardano.Ledger.Coin (Coin(..))
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks)
import Control.DeepSeq (NFData)
import Cardano.Ledger.Crypto (Crypto)
import Cardano.Binary (ToCBOR (..), FromCBOR (..))
import Cardano.Ledger.Serialization (mapToCBOR, mapFromCBOR)
import Data.Sequence.Strict (StrictSeq)
import Cardano.Ledger.Shelley.Delegation.Certificates (DCert)

class EraTxBody era => ShelleyEraTxBody era where
wdrlsTxBodyL :: Lens' (TxBody era) (Wdrl (EraCrypto era))

ttlTxBodyL :: ExactEra ShelleyEra era => Lens' (TxBody era) SlotNo

updateTxBodyL :: Lens' (TxBody era) (StrictMaybe (Update era))

certsTxBodyL :: Lens' (TxBody era) (StrictSeq (DCert (EraCrypto era)))

newtype Wdrl c = Wdrl {unWdrl :: Map (RewardAcnt c) Coin}
deriving (Show, Eq, Generic)
deriving newtype (NoThunks, NFData)

instance Crypto c => ToCBOR (Wdrl c) where
toCBOR = mapToCBOR . unWdrl

instance Crypto c => FromCBOR (Wdrl c) where
fromCBOR = Wdrl <$> mapFromCBOR
23 changes: 1 addition & 22 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import Cardano.Binary
)
import Cardano.Ledger.Address (RewardAcnt (..))
import Cardano.Ledger.AuxiliaryData (AuxiliaryDataHash)
import Cardano.Ledger.Shelley.Core (ShelleyEraTxBody (..), Wdrl (..))
import Cardano.Ledger.BaseTypes (StrictMaybe (..), Url)
import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Compactible (Compactible (CompactForm))
Expand All @@ -84,8 +85,6 @@ import Cardano.Ledger.Serialization
( decodeSet,
decodeStrictSeq,
encodeFoldable,
mapFromCBOR,
mapToCBOR,
)
import Cardano.Ledger.Shelley.Delegation.Certificates
( DCert (..),
Expand Down Expand Up @@ -120,7 +119,6 @@ import Data.Coders
ofield,
(!>),
)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Sequence.Strict (StrictSeq)
import qualified Data.Sequence.Strict as StrictSeq
Expand All @@ -133,16 +131,6 @@ import NoThunks.Class (NoThunks (..))

-- ========================================================================

newtype Wdrl c = Wdrl {unWdrl :: Map (RewardAcnt c) Coin}
deriving (Show, Eq, Generic)
deriving newtype (NoThunks, NFData)

instance CC.Crypto c => ToCBOR (Wdrl c) where
toCBOR = mapToCBOR . unWdrl

instance CC.Crypto c => FromCBOR (Wdrl c) where
fromCBOR = Wdrl <$> mapFromCBOR

-- ---------------------------
-- WellFormed instances

Expand Down Expand Up @@ -316,15 +304,6 @@ instance CC.Crypto c => EraTxBody (ShelleyEra c) where
(\txBody auxDataHash -> txBody {_mdHash = auxDataHash})
{-# INLINEABLE auxDataHashTxBodyL #-}

class EraTxBody era => ShelleyEraTxBody era where
wdrlsTxBodyL :: Lens' (Core.TxBody era) (Wdrl (EraCrypto era))

ttlTxBodyL :: ExactEra ShelleyEra era => Lens' (Core.TxBody era) SlotNo

updateTxBodyL :: Lens' (Core.TxBody era) (StrictMaybe (Update era))

certsTxBodyL :: Lens' (Core.TxBody era) (StrictSeq (DCert (EraCrypto era)))

instance CC.Crypto c => ShelleyEraTxBody (ShelleyEra c) where
{-# SPECIALIZE instance ShelleyEraTxBody (ShelleyEra CC.StandardCrypto) #-}

Expand Down

0 comments on commit 5bc9175

Please sign in to comment.