Skip to content

Commit

Permalink
Switch to use newtype wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Oct 21, 2023
1 parent b648ce0 commit 99a7a37
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 95 deletions.
2 changes: 2 additions & 0 deletions cardano-api/internal/Cardano/Api/Eon/BabbageEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import qualified Cardano.Ledger.Alonzo.Scripts as L
import qualified Cardano.Ledger.Alonzo.TxInfo as L
import qualified Cardano.Ledger.Alonzo.UTxO as L
import qualified Cardano.Ledger.Api as L
import qualified Cardano.Ledger.Babbage.TxOut as L
import qualified Cardano.Ledger.BaseTypes as L
import qualified Cardano.Ledger.Core as L
import qualified Cardano.Ledger.Mary.Value as L
Expand Down Expand Up @@ -90,6 +91,7 @@ type BabbageEraOnwardsConstraints era =
, L.ScriptsNeeded (ShelleyLedgerEra era) ~ L.AlonzoScriptsNeeded (ShelleyLedgerEra era)
, L.ShelleyEraTxBody (ShelleyLedgerEra era)
, L.ShelleyEraTxCert (ShelleyLedgerEra era)
, L.TxOut (ShelleyLedgerEra era) ~ L.BabbageTxOut (ShelleyLedgerEra era)
, L.Value (ShelleyLedgerEra era) ~ L.MaryValue L.StandardCrypto

, FromCBOR (Consensus.ChainDepState (ConsensusProtocol era))
Expand Down
79 changes: 66 additions & 13 deletions cardano-api/internal/Cardano/Api/Ledger/Lens.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RankNTypes #-}

{- HLINT ignore "Eta reduce" -}
Expand All @@ -11,23 +12,44 @@ module Cardano.Api.Ledger.Lens
, invalidBeforeTxBodyL
, invalidHereAfterTxBodyL
, ttlAsInvalidHereAfterTxBodyL
, apiUpdateTxBodyL
, updateTxBodyL

, TxBody(..)
, txBodyL
, mintTxBodyL
, scriptIntegrityHashTxBodyL
, collateralInputsTxBodyL
, reqSignerHashesTxBodyL
, referenceInputsTxBodyL
, collateralReturnTxBodyL
, totalCollateralTxBodyL
, certsTxBodyL
, votingProceduresTxBodyL
, proposalProceduresTxBodyL
) where

import Cardano.Api.Eon.AllegraEraOnwards
import Cardano.Api.Eon.AlonzoEraOnwards
import Cardano.Api.Eon.BabbageEraOnwards
import Cardano.Api.Eon.ConwayEraOnwards
import Cardano.Api.Eon.MaryEraOnwards
import Cardano.Api.Eon.ShelleyBasedEra
import Cardano.Api.Eon.ShelleyEraOnly
import Cardano.Api.Eon.ShelleyToBabbageEra
import Cardano.Api.Eras.Case

import qualified Cardano.Ledger.Allegra.Core as L
import qualified Cardano.Ledger.Alonzo.Core as L
import qualified Cardano.Ledger.Api as L
import Cardano.Ledger.BaseTypes (SlotNo, StrictMaybe (..))
import qualified Cardano.Ledger.Coin as L
import qualified Cardano.Ledger.Keys as L
import qualified Cardano.Ledger.Mary.Value as L
import qualified Cardano.Ledger.Shelley.PParams as L
import qualified Cardano.Ledger.TxIn as L

import qualified Data.Sequence.Strict as L
import Data.Set (Set)
import Lens.Micro

newtype TxBody era = TxBody
Expand All @@ -50,8 +72,8 @@ strictMaybeL = lens g s
txBodyL :: Lens' (TxBody era) (L.TxBody (ShelleyLedgerEra era))
txBodyL = lens unTxBody (\_ x -> TxBody x)

invalidBeforeTxBodyL :: AllegraEraOnwards era -> Lens' (L.TxBody (ShelleyLedgerEra era)) (Maybe SlotNo)
invalidBeforeTxBodyL w = allegraEraOnwardsConstraints w $ L.vldtTxBodyL . invalidBeforeL
invalidBeforeTxBodyL :: AllegraEraOnwards era -> Lens' (TxBody era) (Maybe SlotNo)
invalidBeforeTxBodyL w = allegraEraOnwardsConstraints w $ txBodyL . L.vldtTxBodyL . invalidBeforeL

-- | Compatibility lens that provides a consistent interface over 'ttlTxBodyL' and
-- 'vldtTxBodyL . invalidHereAfterStrictL' across all shelley based eras.
Expand All @@ -68,27 +90,27 @@ invalidBeforeTxBodyL w = allegraEraOnwardsConstraints w $ L.vldtTxBodyL . invali
--
-- 'invalidHereAfterTxBodyL' lens over both with a 'Maybe SlotNo' type representation. Withing the
-- Shelley era, setting Nothing will set the ttl to 'maxBound' in the underlying ledger type.
invalidHereAfterTxBodyL :: ShelleyBasedEra era -> Lens' (L.TxBody (ShelleyLedgerEra era)) (Maybe SlotNo)
invalidHereAfterTxBodyL :: ShelleyBasedEra era -> Lens' (TxBody era) (Maybe SlotNo)
invalidHereAfterTxBodyL =
caseShelleyEraOnlyOrAllegraEraOnwards
ttlAsInvalidHereAfterTxBodyL
(const $ L.vldtTxBodyL . invalidHereAfterL)
(const $ txBodyL . L.vldtTxBodyL . invalidHereAfterL)

-- | Compatibility lens over 'ttlTxBodyL' which represents 'maxBound' as Nothing and all other values as 'Just'.
ttlAsInvalidHereAfterTxBodyL :: ShelleyEraOnly era -> Lens' (L.TxBody (ShelleyLedgerEra era)) (Maybe SlotNo)
ttlAsInvalidHereAfterTxBodyL :: ShelleyEraOnly era -> Lens' (TxBody era) (Maybe SlotNo)
ttlAsInvalidHereAfterTxBodyL w = lens (g w) (s w)
where
g :: ShelleyEraOnly era -> L.TxBody (ShelleyLedgerEra era) -> Maybe SlotNo
g :: ShelleyEraOnly era -> TxBody era -> Maybe SlotNo
g w' txBody =
shelleyEraOnlyConstraints w' $
let ttl = txBody ^. L.ttlTxBodyL in if ttl == maxBound then Nothing else Just ttl
let ttl = txBody ^. txBodyL . L.ttlTxBodyL in if ttl == maxBound then Nothing else Just ttl

s :: ShelleyEraOnly era -> L.TxBody (ShelleyLedgerEra era) -> Maybe SlotNo -> L.TxBody (ShelleyLedgerEra era)
s :: ShelleyEraOnly era -> TxBody era -> Maybe SlotNo -> TxBody era
s w' txBody mSlotNo =
shelleyEraOnlyConstraints w' $
case mSlotNo of
Nothing -> txBody & L.ttlTxBodyL .~ maxBound
Just ttl -> txBody & L.ttlTxBodyL .~ ttl
Nothing -> txBody & txBodyL . L.ttlTxBodyL .~ maxBound
Just ttl -> txBody & txBodyL . L.ttlTxBodyL .~ ttl

invalidBeforeL :: Lens' L.ValidityInterval (Maybe SlotNo)
invalidBeforeL = invalidBeforeStrictL . strictMaybeL
Expand Down Expand Up @@ -116,5 +138,36 @@ invalidHereAfterStrictL = lens g s
s :: L.ValidityInterval -> StrictMaybe SlotNo -> L.ValidityInterval
s (L.ValidityInterval a _) b = L.ValidityInterval a b

apiUpdateTxBodyL :: ShelleyToBabbageEra era -> Lens' (L.TxBody (ShelleyLedgerEra era)) (StrictMaybe (L.Update (ShelleyLedgerEra era)))
apiUpdateTxBodyL w = shelleyToBabbageEraConstraints w L.updateTxBodyL
updateTxBodyL :: ShelleyToBabbageEra era -> Lens' (TxBody era) (StrictMaybe (L.Update (ShelleyLedgerEra era)))
updateTxBodyL w = shelleyToBabbageEraConstraints w $ txBodyL . L.updateTxBodyL

mintTxBodyL :: MaryEraOnwards era -> Lens' (TxBody era) (L.MultiAsset L.StandardCrypto)
mintTxBodyL w = maryEraOnwardsConstraints w $ txBodyL . L.mintTxBodyL

scriptIntegrityHashTxBodyL :: AlonzoEraOnwards era -> Lens' (TxBody era) (StrictMaybe (L.ScriptIntegrityHash L.StandardCrypto))
scriptIntegrityHashTxBodyL w = alonzoEraOnwardsConstraints w $ txBodyL . L.scriptIntegrityHashTxBodyL

collateralInputsTxBodyL :: AlonzoEraOnwards era -> Lens' (TxBody era) (Set (L.TxIn L.StandardCrypto))
collateralInputsTxBodyL w = alonzoEraOnwardsConstraints w $ txBodyL . L.collateralInputsTxBodyL

reqSignerHashesTxBodyL :: AlonzoEraOnwards era -> Lens' (TxBody era) (Set (L.KeyHash L.Witness L.StandardCrypto))
reqSignerHashesTxBodyL w = alonzoEraOnwardsConstraints w $ txBodyL . L.reqSignerHashesTxBodyL

referenceInputsTxBodyL :: BabbageEraOnwards era -> Lens' (TxBody era) (Set (L.TxIn L.StandardCrypto))
referenceInputsTxBodyL w = babbageEraOnwardsConstraints w $ txBodyL . L.referenceInputsTxBodyL

collateralReturnTxBodyL :: BabbageEraOnwards era -> Lens' (TxBody era) (StrictMaybe (L.TxOut (ShelleyLedgerEra era)))
collateralReturnTxBodyL w = babbageEraOnwardsConstraints w $ txBodyL . L.collateralReturnTxBodyL

totalCollateralTxBodyL :: BabbageEraOnwards era -> Lens' (TxBody era) (StrictMaybe L.Coin)
totalCollateralTxBodyL w = babbageEraOnwardsConstraints w $ txBodyL . L.totalCollateralTxBodyL

certsTxBodyL :: ShelleyBasedEra era -> Lens' (TxBody era) (L.StrictSeq (L.TxCert (ShelleyLedgerEra era)))
certsTxBodyL w = shelleyBasedEraConstraints w $ txBodyL . L.certsTxBodyL

votingProceduresTxBodyL :: ConwayEraOnwards era -> Lens' (TxBody era) (L.VotingProcedures (ShelleyLedgerEra era))
votingProceduresTxBodyL w = conwayEraOnwardsConstraints w $ txBodyL . L.votingProceduresTxBodyL

proposalProceduresTxBodyL :: ConwayEraOnwards era -> Lens' (TxBody era) (L.StrictSeq (L.ProposalProcedure (ShelleyLedgerEra era)))
proposalProceduresTxBodyL w = conwayEraOnwardsConstraints w $ txBodyL . L.proposalProceduresTxBodyL

Loading

0 comments on commit 99a7a37

Please sign in to comment.