Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ledger presentation of multi-asset values directly. Lens to make this uniform over ShelleyBasedEra #360

Merged
merged 11 commits into from
Nov 10, 2023
3 changes: 2 additions & 1 deletion cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ library internal
Cardano.Api.Eon.AlonzoEraOnwards
Cardano.Api.Eon.BabbageEraOnwards
Cardano.Api.Eon.ByronEraOnly
Cardano.Api.Eon.ByronToAllegraEra
Cardano.Api.Eon.ByronToAlonzoEra
Cardano.Api.Eon.ConwayEraOnwards
Cardano.Api.Eon.MaryEraOnwards
Expand Down Expand Up @@ -170,6 +169,7 @@ library internal
, errors
, filepath
, formatting
, groups
Copy link
Collaborator Author

@newhoggy newhoggy Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We import groups because ledger implements Group for multi-asset values which we can use to negate the entire L.Value ledgerera.

, iproute
, memory
, microlens
Expand Down Expand Up @@ -223,6 +223,7 @@ library
-- exposed by cardano-api-ledger
Cardano.Api.Ledger

reexported-modules: Cardano.Api.Ledger.Lens
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New export so we can use eon enabled from the CLI and from tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just expose it?

Copy link
Collaborator Author

@newhoggy newhoggy Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code needs to live in the internal component because code in internal uses it. This doesn't apply to the other modules because those are never imported by code in internal.


build-depends: bytestring
, cardano-api:internal
Expand Down
53 changes: 31 additions & 22 deletions cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ import Cardano.Api.Byron (KeyWitness (ByronKeyWitness),
import Cardano.Api.Governance.Actions.VotingProcedure
import Cardano.Api.Script (scriptInEraToRefScript)
import Cardano.Api.Shelley
import qualified Cardano.Api.Ledger as L
import qualified Cardano.Api.Ledger.Lens as A

import qualified Cardano.Binary as CBOR
import qualified Cardano.Crypto.Hash as Crypto
Expand Down Expand Up @@ -340,9 +342,8 @@ genPolicyId =
]

genAssetId :: Gen AssetId
genAssetId = Gen.choice [ AssetId <$> genPolicyId <*> genAssetName
, return AdaAssetId
]
genAssetId =
AssetId <$> genPolicyId <*> genAssetName

genQuantity :: Range Integer -> Gen Quantity
genQuantity range = fromInteger <$> Gen.integral range
Expand All @@ -364,34 +365,42 @@ genUnsignedQuantity = genQuantity (Range.constant 0 2)
genPositiveQuantity :: Gen Quantity
genPositiveQuantity = genQuantity (Range.constant 1 2)

genValue :: Gen AssetId -> Gen Quantity -> Gen Value
genValue genAId genQuant =
valueFromList <$>
genValue :: MaryEraOnwards era -> Gen AssetId -> Gen Quantity -> Gen (L.Value (ShelleyLedgerEra era))
genValue w genAId genQuant =
toLedgerValue w . valueFromList <$>
Gen.list (Range.constant 0 10)
((,) <$> genAId <*> genQuant)

-- | Generate a 'Value' with any asset ID and a positive or negative quantity.
genValueDefault :: Gen Value
genValueDefault = genValue genAssetId genSignedNonZeroQuantity
genValueDefault :: MaryEraOnwards era -> Gen (L.Value (ShelleyLedgerEra era))
genValueDefault w = genValue w genAssetId genSignedNonZeroQuantity

-- | Generate a 'Value' suitable for minting, i.e. non-ADA asset ID and a
-- positive or negative quantity.
genValueForMinting :: Gen Value
genValueForMinting = genValue genAssetIdNoAda genSignedNonZeroQuantity
genValueForMinting :: MaryEraOnwards era -> Gen Value
genValueForMinting w =
fromLedgerValue sbe <$> genValue w genAssetIdNoAda genSignedNonZeroQuantity
where
sbe = maryEraOnwardsToShelleyBasedEra w
genAssetIdNoAda :: Gen AssetId
genAssetIdNoAda = AssetId <$> genPolicyId <*> genAssetName

-- | Generate a 'Value' suitable for usage in a transaction output, i.e. any
-- asset ID and a positive quantity.
genValueForTxOut :: Gen Value
genValueForTxOut = do
-- Generate a potentially empty list with multi assets
val <- genValue genAssetId genPositiveQuantity
genValueForTxOut :: ShelleyBasedEra era -> Gen (L.Value (ShelleyLedgerEra era))
genValueForTxOut sbe = do
-- Generate at least one positive ADA, without it Value in TxOut makes no sense
-- and will fail deserialization starting with ConwayEra
ada <- (,) AdaAssetId <$> genPositiveQuantity
pure $ valueFromList (ada : valueToList val)
ada <- A.mkAdaValue sbe . L.Coin <$> Gen.integral (Range.constant 1 2)

-- Generate a potentially empty list with multi assets
caseShelleyToAllegraOrMaryEraOnwards
(const (pure ada))
(\w -> do
v <- genValue w genAssetId genPositiveQuantity
pure $ ada <> v
)
sbe


-- Note that we expect to sometimes generate duplicate policy id keys since we
Expand Down Expand Up @@ -497,9 +506,9 @@ genTxIndex = TxIx . fromIntegral <$> Gen.word16 Range.constantBounded

genTxOutValue :: CardanoEra era -> Gen (TxOutValue era)
genTxOutValue =
caseByronToAllegraOrMaryEraOnwards
(\w -> TxOutAdaOnly w <$> genPositiveLovelace)
(\w -> TxOutValue w <$> genValueForTxOut)
caseByronOrShelleyBasedEra
(\w -> TxOutValueByron w <$> genPositiveLovelace)
(\sbe -> TxOutValueShelleyBased sbe <$> genValueForTxOut sbe)

genTxOutTxContext :: CardanoEra era -> Gen (TxOut CtxTx era)
genTxOutTxContext era =
Expand Down Expand Up @@ -617,12 +626,12 @@ genTxUpdateProposal sbe =

genTxMintValue :: CardanoEra era -> Gen (TxMintValue BuildTx era)
genTxMintValue =
caseByronToAllegraOrMaryEraOnwards
(const (pure TxMintNone))
inEonForEra
(pure TxMintNone)
(\supported ->
Gen.choice
[ pure TxMintNone
, TxMintValue supported <$> genValueForMinting <*> return (BuildTxWith mempty)
, TxMintValue supported <$> genValueForMinting supported <*> return (BuildTxWith mempty)
]
)

Expand Down
1 change: 1 addition & 0 deletions cardano-api/internal/Cardano/Api/Eon/AllegraEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type AllegraEraOnwardsConstraints era =
, L.EraPParams (ShelleyLedgerEra era)
, L.EraTx (ShelleyLedgerEra era)
, L.EraTxBody (ShelleyLedgerEra era)
, L.EraTxOut (ShelleyLedgerEra era)
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
, L.AllegraEraTxBody (ShelleyLedgerEra era)
, L.ShelleyEraTxBody (ShelleyLedgerEra era)
Expand Down
1 change: 1 addition & 0 deletions cardano-api/internal/Cardano/Api/Eon/AlonzoEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type AlonzoEraOnwardsConstraints era =
, L.EraPParams (ShelleyLedgerEra era)
, L.EraTx (ShelleyLedgerEra era)
, L.EraTxBody (ShelleyLedgerEra era)
, L.EraTxOut (ShelleyLedgerEra era)
, L.EraUTxO (ShelleyLedgerEra era)
, L.ExtendedUTxO (ShelleyLedgerEra era)
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
Expand Down
1 change: 1 addition & 0 deletions cardano-api/internal/Cardano/Api/Eon/BabbageEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type BabbageEraOnwardsConstraints era =
, L.EraPParams (ShelleyLedgerEra era)
, L.EraTx (ShelleyLedgerEra era)
, L.EraTxBody (ShelleyLedgerEra era)
, L.EraTxOut (ShelleyLedgerEra era)
, L.EraUTxO (ShelleyLedgerEra era)
, L.ExtendedUTxO (ShelleyLedgerEra era)
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
Expand Down
54 changes: 0 additions & 54 deletions cardano-api/internal/Cardano/Api/Eon/ByronToAllegraEra.hs

This file was deleted.

1 change: 1 addition & 0 deletions cardano-api/internal/Cardano/Api/Eon/ConwayEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type ConwayEraOnwardsConstraints era =
, L.EraPParams (ShelleyLedgerEra era)
, L.EraTx (ShelleyLedgerEra era)
, L.EraTxBody (ShelleyLedgerEra era)
, L.EraTxOut (ShelleyLedgerEra era)
, L.EraUTxO (ShelleyLedgerEra era)
, L.ExtendedUTxO (ShelleyLedgerEra era)
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
Expand Down
1 change: 1 addition & 0 deletions cardano-api/internal/Cardano/Api/Eon/MaryEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type MaryEraOnwardsConstraints era =
, L.EraPParams (ShelleyLedgerEra era)
, L.EraTx (ShelleyLedgerEra era)
, L.EraTxBody (ShelleyLedgerEra era)
, L.EraTxOut (ShelleyLedgerEra era)
, L.EraUTxO (ShelleyLedgerEra era)
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
, L.MaryEraTxBody (ShelleyLedgerEra era)
Expand Down
3 changes: 3 additions & 0 deletions cardano-api/internal/Cardano/Api/Eon/ShelleyBasedEra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import qualified Cardano.Ledger.BaseTypes as L
import Cardano.Ledger.Binary (FromCBOR)
import qualified Cardano.Ledger.Core as L
import qualified Cardano.Ledger.SafeHash as L
import qualified Cardano.Ledger.UTxO as L
import qualified Ouroboros.Consensus.Protocol.Abstract as Consensus
import qualified Ouroboros.Consensus.Protocol.Praos.Common as Consensus
import Ouroboros.Consensus.Shelley.Eras as Consensus (StandardAllegra, StandardAlonzo,
Expand Down Expand Up @@ -204,6 +205,8 @@ type ShelleyBasedEraConstraints era =
, L.EraPParams (ShelleyLedgerEra era)
, L.EraTx (ShelleyLedgerEra era)
, L.EraTxBody (ShelleyLedgerEra era)
, L.EraTxOut (ShelleyLedgerEra era)
, L.EraUTxO (ShelleyLedgerEra era)
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
, L.ShelleyEraTxBody (ShelleyLedgerEra era)
, L.ShelleyEraTxCert (ShelleyLedgerEra era)
Expand Down
1 change: 1 addition & 0 deletions cardano-api/internal/Cardano/Api/Eon/ShelleyEraOnly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type ShelleyEraOnlyConstraints era =
, L.EraPParams (ShelleyLedgerEra era)
, L.EraTx (ShelleyLedgerEra era)
, L.EraTxBody (ShelleyLedgerEra era)
, L.EraTxOut (ShelleyLedgerEra era)
, L.ExactEra L.ShelleyEra (ShelleyLedgerEra era)
, L.ExactEra L.ShelleyEra (ShelleyLedgerEra era)
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type ShelleyToAllegraEraConstraints era =
, L.EraPParams (ShelleyLedgerEra era)
, L.EraTx (ShelleyLedgerEra era)
, L.EraTxBody (ShelleyLedgerEra era)
, L.EraTxOut (ShelleyLedgerEra era)
, L.EraUTxO (ShelleyLedgerEra era)
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
, L.ProtVerAtMost (ShelleyLedgerEra era) 4
Expand Down
1 change: 1 addition & 0 deletions cardano-api/internal/Cardano/Api/Eon/ShelleyToAlonzoEra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type ShelleyToAlonzoEraConstraints era =
, L.EraPParams (ShelleyLedgerEra era)
, L.EraTx (ShelleyLedgerEra era)
, L.EraTxBody (ShelleyLedgerEra era)
, L.EraTxOut (ShelleyLedgerEra era)
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
, L.ProtVerAtMost (ShelleyLedgerEra era) 6
, L.ProtVerAtMost (ShelleyLedgerEra era) 8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type ShelleyToBabbageEraConstraints era =
, L.EraPParams (ShelleyLedgerEra era)
, L.EraTx (ShelleyLedgerEra era)
, L.EraTxBody (ShelleyLedgerEra era)
, L.EraTxOut (ShelleyLedgerEra era)
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
, L.ProtVerAtMost (ShelleyLedgerEra era) 8
, L.ShelleyEraTxBody (ShelleyLedgerEra era)
Expand Down
1 change: 1 addition & 0 deletions cardano-api/internal/Cardano/Api/Eon/ShelleyToMaryEra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type ShelleyToMaryEraConstraints era =
, L.EraPParams (ShelleyLedgerEra era)
, L.EraTx (ShelleyLedgerEra era)
, L.EraTxBody (ShelleyLedgerEra era)
, L.EraTxOut (ShelleyLedgerEra era)
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
, L.ProtVerAtMost (ShelleyLedgerEra era) 4
, L.ProtVerAtMost (ShelleyLedgerEra era) 6
Expand Down
1 change: 0 additions & 1 deletion cardano-api/internal/Cardano/Api/Eras.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ module Cardano.Api.Eras
, caseByronOrShelleyBasedEra

-- ** Case on ShelleyBasedEra
, caseByronToAllegraOrMaryEraOnwards
, caseByronToAlonzoOrBabbageEraOnwards
, caseShelleyToAllegraOrMaryEraOnwards
, caseShelleyToMaryOrAlonzoEraOnwards
Expand Down
24 changes: 0 additions & 24 deletions cardano-api/internal/Cardano/Api/Eras/Case.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
module Cardano.Api.Eras.Case
( -- Case on CardanoEra
caseByronOrShelleyBasedEra
, caseByronToAllegraOrMaryEraOnwards
, caseByronToAlonzoOrBabbageEraOnwards

-- Case on ShelleyBasedEra
Expand All @@ -22,7 +21,6 @@ module Cardano.Api.Eras.Case
, disjointByronEraOnlyAndShelleyBasedEra

-- Conversions
, shelleyToAllegraEraToByronToAllegraEra
, shelleyToAlonzoEraToShelleyToBabbageEra
, alonzoEraOnwardsToMaryEraOnwards
, babbageEraOnwardsToMaryEraOnwards
Expand All @@ -33,7 +31,6 @@ import Cardano.Api.Eon.AllegraEraOnwards
import Cardano.Api.Eon.AlonzoEraOnwards
import Cardano.Api.Eon.BabbageEraOnwards
import Cardano.Api.Eon.ByronEraOnly
import Cardano.Api.Eon.ByronToAllegraEra
import Cardano.Api.Eon.ByronToAlonzoEra
import Cardano.Api.Eon.ConwayEraOnwards
import Cardano.Api.Eon.MaryEraOnwards
Expand All @@ -60,22 +57,6 @@ caseByronOrShelleyBasedEra l r = \case
BabbageEra -> r ShelleyBasedEraBabbage
ConwayEra -> r ShelleyBasedEraConway

-- | @caseByronToAllegraOrMaryEraOnwards f g era@ applies @f@ to byron, shelley, and allegra;
-- and @g@ to mary and later eras.
caseByronToAllegraOrMaryEraOnwards :: ()
=> (ByronToAllegraEraConstraints era => ByronToAllegraEra era -> a)
-> (MaryEraOnwardsConstraints era => MaryEraOnwards era -> a)
-> CardanoEra era
-> a
caseByronToAllegraOrMaryEraOnwards l r = \case
ByronEra -> l ByronToAllegraEraByron
ShelleyEra -> l ByronToAllegraEraShelley
AllegraEra -> l ByronToAllegraEraAllegra
MaryEra -> r MaryEraOnwardsMary
AlonzoEra -> r MaryEraOnwardsAlonzo
BabbageEra -> r MaryEraOnwardsBabbage
ConwayEra -> r MaryEraOnwardsConway
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deletes a case function because we don't need it anymore.


-- | @caseByronToAlonzoOrBabbageEraOnwards f g era@ applies @f@ to byron, shelley, allegra, mary, and alonzo;
-- and @g@ to babbage and later eras.
caseByronToAlonzoOrBabbageEraOnwards :: ()
Expand Down Expand Up @@ -174,11 +155,6 @@ noByronEraInShelleyBasedEra = flip disjointByronEraOnlyAndShelleyBasedEra
disjointByronEraOnlyAndShelleyBasedEra :: ByronEraOnly era -> ShelleyBasedEra era -> a
disjointByronEraOnlyAndShelleyBasedEra ByronEraOnlyByron sbe = case sbe of {}

shelleyToAllegraEraToByronToAllegraEra :: ShelleyToAllegraEra era -> ByronToAllegraEra era
shelleyToAllegraEraToByronToAllegraEra = \case
ShelleyToAllegraEraShelley -> ByronToAllegraEraShelley
ShelleyToAllegraEraAllegra -> ByronToAllegraEraAllegra

shelleyToAlonzoEraToShelleyToBabbageEra :: ()
=> ShelleyToAlonzoEra era
-> ShelleyToBabbageEra era
Expand Down
Loading