Skip to content

Commit

Permalink
[ADP-3344] Tidy up Cardano.Write.Era (#4810)
Browse files Browse the repository at this point in the history
This pull request tidies up the API of `Cardano.Write.Era` by removing
several functions.

This pull request also renames the eras to `Babbage` and `Conway`,
without `*Era` prefix, making the names consistent with
`Cardano.Ledger.Api`.

The main, hidden, reason for this pull request is to reduce the
dependency on `Cardano.Api`.

### Issue Number

ADP-3344
  • Loading branch information
HeinrichApfelmus authored Oct 18, 2024
2 parents da7cbb1 + 843e4d6 commit 0d2adc1
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 192 deletions.
8 changes: 5 additions & 3 deletions lib/api/src/Cardano/Wallet/Api/Http/Shelley/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,8 @@ import qualified Cardano.Wallet.Read as Read
import qualified Cardano.Wallet.Read.Hash as Hash
import qualified Cardano.Wallet.Registry as Registry
import qualified Cardano.Write.Eras as Write
( IsRecentEra
( IsRecentEra (recentEra)
, RecentEra
, cardanoEra
, cardanoEraFromRecentEra
)
import qualified Control.Concurrent.Concierge as Concierge
Expand Down Expand Up @@ -4892,8 +4891,11 @@ fromApiRedeemer = \case

sealWriteTx :: forall era. Write.IsRecentEra era => Write.Tx era -> W.SealedTx
sealWriteTx = W.sealedTxFromCardano
. Cardano.InAnyCardanoEra (Write.cardanoEra @era)
. Cardano.InAnyCardanoEra cardanoEra
. Write.toCardanoApiTx
where
cardanoEra =
Write.cardanoEraFromRecentEra (Write.recentEra :: Write.RecentEra era)

toApiSerialisedTransaction
:: Write.IsRecentEra era
Expand Down
10 changes: 6 additions & 4 deletions lib/api/src/Cardano/Wallet/Api/Types/Era.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ import Text.Show

import qualified Cardano.Wallet.Read as Read
import qualified Cardano.Write.Eras as Write
( allRecentEras
, toAnyCardanoEra
)
import qualified Data.Aeson as Aeson
import qualified Data.Set as Set

Expand Down Expand Up @@ -129,4 +126,9 @@ toAnyCardanoEra = \case
--
allRecentEras :: Set ApiEra
allRecentEras =
Set.map (fromAnyCardanoEra . Write.toAnyCardanoEra) Write.allRecentEras
Set.map fromAnyRecentEra Write.allRecentEras

fromAnyRecentEra :: Write.AnyRecentEra -> ApiEra
fromAnyRecentEra = \case
Write.AnyRecentEra Write.RecentEraBabbage -> ApiBabbage
Write.AnyRecentEra Write.RecentEraConway -> ApiConway
133 changes: 45 additions & 88 deletions lib/balance-tx/lib/internal/Internal/Cardano/Write/Eras.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
Expand All @@ -19,31 +18,19 @@
--
-- Recent eras.
module Internal.Cardano.Write.Eras
(
-- * Eras
BabbageEra
, ConwayEra
( Babbage
, Conway
, LatestLedgerEra

-- ** RecentEra
, RecentEra (..)
, IsRecentEra (..)
, CardanoApiEra
, toRecentEra
, fromRecentEra
, MaybeInRecentEra (..)
, LatestLedgerEra
, RecentEraConstraints
, allRecentEras

-- ** Existential wrapper
, AnyRecentEra (..)
, toAnyCardanoEra
, fromAnyCardanoEra
, allRecentEras

-- ** Helpers for cardano-api compatibility
, cardanoEra
, shelleyBasedEra
, CardanoApi.ShelleyLedgerEra
, cardanoEraFromRecentEra
, shelleyBasedEraFromRecentEra
) where
Expand All @@ -67,6 +54,10 @@ import Cardano.Ledger.Alonzo.TxWits
import Cardano.Ledger.Alonzo.UTxO
( AlonzoScriptsNeeded
)
import Cardano.Ledger.Api
( Babbage
, Conway
)
import Cardano.Ledger.Api.UTxO
( EraUTxO (ScriptsNeeded)
)
Expand Down Expand Up @@ -113,10 +104,7 @@ import qualified Data.Set as Set
-- Eras
--------------------------------------------------------------------------------

type BabbageEra = Ledger.BabbageEra StandardCrypto
type ConwayEra = Ledger.ConwayEra StandardCrypto

type LatestLedgerEra = ConwayEra
type LatestLedgerEra = Conway

--------------------------------------------------------------------------------
-- RecentEra
Expand All @@ -129,12 +117,9 @@ type LatestLedgerEra = ConwayEra
-- after a hard-fork, we need to, at that time, support the two latest eras. We
-- could get away with just supporting one era at other times, but for
-- simplicity we stick with always supporting the two latest eras for now.
--
-- NOTE: We /could/ let 'era' refer to eras from the ledger rather than from
-- cardano-api.
data RecentEra era where
RecentEraBabbage :: RecentEra BabbageEra
RecentEraConway :: RecentEra ConwayEra
RecentEraBabbage :: RecentEra Babbage
RecentEraConway :: RecentEra Conway

deriving instance Eq (RecentEra era)
deriving instance Show (RecentEra era)
Expand All @@ -153,10 +138,6 @@ class
) => IsRecentEra era where
recentEra :: RecentEra era

type family CardanoApiEra era = cardanoApiEra | cardanoApiEra -> era
type instance CardanoApiEra BabbageEra = CardanoApi.BabbageEra
type instance CardanoApiEra ConwayEra = CardanoApi.ConwayEra

-- | Convenient constraints. Constraints may be dropped as we move to new eras.
--
-- Adding too many constraints shouldn't be a concern as the point of
Expand Down Expand Up @@ -189,75 +170,24 @@ type RecentEraConstraints era =
, Core.NativeScript era ~ Timelock era
)

-- | Returns a proof that the given era is a recent era.
--
-- Otherwise, returns @Nothing@.
toRecentEra
:: CardanoApi.CardanoEra era
-> Maybe (RecentEra (CardanoApi.ShelleyLedgerEra era))
toRecentEra = \case
CardanoApi.ConwayEra -> Just RecentEraConway
CardanoApi.BabbageEra -> Just RecentEraBabbage
CardanoApi.AlonzoEra -> Nothing
CardanoApi.MaryEra -> Nothing
CardanoApi.AllegraEra -> Nothing
CardanoApi.ShelleyEra -> Nothing
CardanoApi.ByronEra -> Nothing

fromRecentEra :: RecentEra era -> CardanoApi.CardanoEra (CardanoApiEra era)
fromRecentEra = \case
RecentEraConway -> CardanoApi.ConwayEra
RecentEraBabbage -> CardanoApi.BabbageEra

instance IsRecentEra BabbageEra where
instance IsRecentEra Babbage where
recentEra = RecentEraBabbage

instance IsRecentEra ConwayEra where
instance IsRecentEra Conway where
recentEra = RecentEraConway

cardanoEraFromRecentEra
:: RecentEra era
-> CardanoApi.CardanoEra (CardanoApiEra era)
cardanoEraFromRecentEra era = case shelleyBasedEraFromRecentEra era of
CardanoApi.ShelleyBasedEraBabbage -> CardanoApi.toCardanoEra CardanoApi.BabbageEra
CardanoApi.ShelleyBasedEraConway -> CardanoApi.toCardanoEra CardanoApi.ConwayEra
_ -> error "we are expecting only Babbage and Conway"

shelleyBasedEraFromRecentEra
:: RecentEra era
-> CardanoApi.ShelleyBasedEra (CardanoApiEra era)
shelleyBasedEraFromRecentEra = \case
RecentEraConway -> CardanoApi.ShelleyBasedEraConway
RecentEraBabbage -> CardanoApi.ShelleyBasedEraBabbage

-- Similar to 'CardanoApi.cardanoEra', but with an 'IsRecentEra era' constraint
-- instead of 'CardanoApi.IsCardanoEra'.
cardanoEra
:: forall era. IsRecentEra era
=> CardanoApi.CardanoEra (CardanoApiEra era)
cardanoEra = cardanoEraFromRecentEra $ recentEra @era

-- | For convenience working with 'IsRecentEra'.
--
-- Similar to 'CardanoApi.shelleyBasedEra, but with a 'IsRecentEra era'
-- constraint instead of 'CardanoApi.IsShelleyBasedEra'.
shelleyBasedEra
:: forall era. IsRecentEra era
=> CardanoApi.ShelleyBasedEra (CardanoApiEra era)
shelleyBasedEra = shelleyBasedEraFromRecentEra $ recentEra @era

data MaybeInRecentEra (thing :: Type -> Type)
= InNonRecentEraByron
| InNonRecentEraShelley
| InNonRecentEraAllegra
| InNonRecentEraMary
| InNonRecentEraAlonzo
| InRecentEraBabbage (thing BabbageEra)
| InRecentEraConway (thing ConwayEra)
| InRecentEraBabbage (thing Babbage)
| InRecentEraConway (thing Conway)

deriving instance (Eq (a BabbageEra), (Eq (a ConwayEra)))
deriving instance (Eq (a Babbage), (Eq (a Conway)))
=> Eq (MaybeInRecentEra a)
deriving instance (Show (a BabbageEra), (Show (a ConwayEra)))
deriving instance (Show (a Babbage), (Show (a Conway)))
=> Show (MaybeInRecentEra a)

-- | An existential type like 'AnyCardanoEra', but for 'RecentEra'.
Expand Down Expand Up @@ -297,10 +227,37 @@ instance Eq AnyRecentEra where
allRecentEras :: Set AnyRecentEra
allRecentEras = Set.fromList [minBound .. maxBound]

--------------------------------------------------------------------------------
-- Cardano.Api compatibility
--------------------------------------------------------------------------------
-- | Type family for converting to "Cardano.Api" eras.
type family CardanoApiEra era = cardanoApiEra | cardanoApiEra -> era
type instance CardanoApiEra Babbage = CardanoApi.BabbageEra
type instance CardanoApiEra Conway = CardanoApi.ConwayEra

-- | Convert to a 'CardanoEra'.
cardanoEraFromRecentEra
:: RecentEra era
-> CardanoApi.CardanoEra (CardanoApiEra era)
cardanoEraFromRecentEra = \case
RecentEraConway -> CardanoApi.ConwayEra
RecentEraBabbage -> CardanoApi.BabbageEra

-- | Convert to a 'ShelleyBasedEra'.
-- At this time, every 'RecentEra' is Shelley-based.
shelleyBasedEraFromRecentEra
:: RecentEra era
-> CardanoApi.ShelleyBasedEra (CardanoApiEra era)
shelleyBasedEraFromRecentEra = \case
RecentEraConway -> CardanoApi.ShelleyBasedEraConway
RecentEraBabbage -> CardanoApi.ShelleyBasedEraBabbage

-- | Currently needed for 'Enum' instance.
toAnyCardanoEra :: AnyRecentEra -> CardanoApi.AnyCardanoEra
toAnyCardanoEra (AnyRecentEra era) =
CardanoApi.AnyCardanoEra (fromRecentEra era)
CardanoApi.AnyCardanoEra (cardanoEraFromRecentEra era)

-- | Currently needed for 'Enum' instance.
fromAnyCardanoEra
:: CardanoApi.AnyCardanoEra
-> Maybe AnyRecentEra
Expand Down
31 changes: 18 additions & 13 deletions lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,14 @@ import GHC.Stack
( HasCallStack
)
import Internal.Cardano.Write.Eras
( BabbageEra
( Babbage
, CardanoApiEra
, ConwayEra
, Conway
, IsRecentEra (..)
, LatestLedgerEra
, MaybeInRecentEra (..)
, RecentEra (..)
, shelleyBasedEra
, shelleyBasedEraFromRecentEra
)
import Numeric.Natural
( Natural
Expand Down Expand Up @@ -288,7 +288,7 @@ unsafeMkTxIn hash ix = Ledger.mkTxInPartial

type TxOut era = Core.TxOut era

type TxOutInBabbage = Babbage.BabbageTxOut (Babbage.BabbageEra StandardCrypto)
type TxOutInBabbage = Babbage.BabbageTxOut Babbage

type Address = Ledger.Addr StandardCrypto

Expand Down Expand Up @@ -339,7 +339,7 @@ wrapTxOutInRecentEra out = case recentEra @era of
BabbageTxOut addr v d s = out
in
TxOutInRecentEra addr v d (strictMaybeToMaybe s)
RecentEraBabbage -> wrapTxOutInRecentEra @ConwayEra $ upgradeTxOut out
RecentEraBabbage -> wrapTxOutInRecentEra @Conway $ upgradeTxOut out

data ErrInvalidTxOutInEra
= InlinePlutusV3ScriptNotSupportedInBabbage
Expand All @@ -361,7 +361,7 @@ recentEraToConwayTxOut (TxOutInRecentEra addr val datum mscript) =

recentEraToBabbageTxOut
:: TxOutInRecentEra
-> Either ErrInvalidTxOutInEra (BabbageTxOut BabbageEra)
-> Either ErrInvalidTxOutInEra (BabbageTxOut Babbage)
recentEraToBabbageTxOut (TxOutInRecentEra addr val datum mscript) =
Babbage.BabbageTxOut addr val
(downgradeDatum datum)
Expand All @@ -376,17 +376,17 @@ recentEraToBabbageTxOut (TxOutInRecentEra addr val datum mscript) =
Alonzo.Datum (coerce binaryData)

downgradeScript
:: AlonzoScript ConwayEra
-> Either ErrInvalidTxOutInEra (AlonzoScript BabbageEra)
:: AlonzoScript Conway
-> Either ErrInvalidTxOutInEra (AlonzoScript Babbage)
downgradeScript = \case
TimelockScript timelockEra
-> pure $ Alonzo.TimelockScript (translateTimelock timelockEra)
PlutusScript s
-> PlutusScript <$> downgradePlutusScript s

downgradePlutusScript
:: PlutusScript ConwayEra
-> Either ErrInvalidTxOutInEra (PlutusScript BabbageEra)
:: PlutusScript Conway
-> Either ErrInvalidTxOutInEra (PlutusScript Babbage)
downgradePlutusScript = \case
ConwayPlutusV1 s -> pure $ BabbagePlutusV1 s
ConwayPlutusV2 s -> pure $ BabbagePlutusV2 s
Expand Down Expand Up @@ -503,7 +503,8 @@ toCardanoApiTx
=> Core.Tx era
-> CardanoApi.Tx (CardanoApiEra era)
toCardanoApiTx =
CardanoApi.ShelleyTx (shelleyBasedEra @era)
CardanoApi.ShelleyTx
$ shelleyBasedEraFromRecentEra (recentEra :: RecentEra era)

toCardanoApiUTxO
:: forall era. IsRecentEra era
Expand All @@ -512,8 +513,10 @@ toCardanoApiUTxO
toCardanoApiUTxO =
CardanoApi.UTxO
. Map.mapKeys CardanoApi.fromShelleyTxIn
. Map.map (CardanoApi.fromShelleyTxOut (shelleyBasedEra @era))
. Map.map (CardanoApi.fromShelleyTxOut shelleyBasedEra)
. unUTxO
where
shelleyBasedEra = shelleyBasedEraFromRecentEra (recentEra :: RecentEra era)

fromCardanoApiUTxO
:: forall era. IsRecentEra era
Expand All @@ -523,8 +526,10 @@ fromCardanoApiUTxO =
Shelley.UTxO
. Map.mapKeys CardanoApi.toShelleyTxIn
. Map.map
(CardanoApi.toShelleyTxOut (shelleyBasedEra @era))
(CardanoApi.toShelleyTxOut shelleyBasedEra)
. CardanoApi.unUTxO
where
shelleyBasedEra = shelleyBasedEraFromRecentEra (recentEra :: RecentEra era)

--------------------------------------------------------------------------------
-- PParams
Expand Down
8 changes: 4 additions & 4 deletions lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import Data.Maybe
( fromMaybe
)
import Internal.Cardano.Write.Eras
( BabbageEra
, ConwayEra
( Babbage
, Conway
, IsRecentEra (..)
, RecentEra (..)
)
Expand Down Expand Up @@ -106,7 +106,7 @@ mockPParams = case recentEra @era of
unsafeWrap :: PParamsHKD Identity era -> PParams era
unsafeWrap = unsafeCoerce

conwayPParams :: ConwayPParams Identity ConwayEra
conwayPParams :: ConwayPParams Identity Conway
conwayPParams = upgradeConwayPParams upgrade babbagePParams
where
upgrade :: UpgradeConwayPParams Identity
Expand Down Expand Up @@ -140,7 +140,7 @@ mockPParams = case recentEra @era of
, ucppPlutusV3CostModel = conwayPlutusV3CostModel
}

babbagePParams :: BabbagePParams Identity BabbageEra
babbagePParams :: BabbagePParams Identity Babbage
babbagePParams = BabbagePParams
{ bppMinFeeA = 44
-- ^ The linear factor for the minimum fee calculation
Expand Down
Loading

0 comments on commit 0d2adc1

Please sign in to comment.