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
56 changes: 4 additions & 52 deletions cardano-api/internal/Cardano/Api/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,6 @@ toShelleyTxOut sbe = \case -- jky simplify
L.mkBasicTxOut (toShelleyAddr addr) value
ShelleyBasedEraAlonzo ->
L.mkBasicTxOut (toShelleyAddr addr) value
& L.dataHashTxOutL .~ toAlonzoTxOutDataHash txoutdata
Jimbo4350 marked this conversation as resolved.
Show resolved Hide resolved
ShelleyBasedEraBabbage ->
L.mkBasicTxOut (toShelleyAddr addr) value
& L.datumTxOutL .~ toBabbageTxOutDatum txoutdata
Expand Down Expand Up @@ -742,12 +741,7 @@ fromShelleyTxOut sbe ledgerTxOut = shelleyBasedEraConstraints sbe $ do
TxOut addressInEra txOutValue TxOutDatumNone ReferenceScriptNone

ShelleyBasedEraAlonzo ->
TxOut addressInEra
txOutValue
(fromAlonzoTxOutDataHash AlonzoEraOnwardsAlonzo datahash)
ReferenceScriptNone
where
datahash = ledgerTxOut ^. L.dataHashTxOutL
TxOut addressInEra txOutValue TxOutDatumNone ReferenceScriptNone

ShelleyBasedEraBabbage ->
TxOut addressInEra
Expand Down Expand Up @@ -779,25 +773,6 @@ fromShelleyTxOut sbe ledgerTxOut = shelleyBasedEraConstraints sbe $ do
datum = ledgerTxOut ^. L.datumTxOutL
mRefScript = ledgerTxOut ^. L.referenceScriptTxOutL


-- TODO: If ledger creates an open type family for datums
-- we can consolidate this function with the Babbage version
toAlonzoTxOutDataHash
:: TxOutDatum CtxUTxO AlonzoEra
-> StrictMaybe (L.DataHash StandardCrypto)
toAlonzoTxOutDataHash TxOutDatumNone = SNothing
toAlonzoTxOutDataHash (TxOutDatumHash _ (ScriptDataHash dh)) = SJust dh
toAlonzoTxOutDataHash (TxOutDatumInline inlineDatumSupp _sd) =
case inlineDatumSupp :: BabbageEraOnwards AlonzoEra of {}

fromAlonzoTxOutDataHash :: AlonzoEraOnwards era
-> StrictMaybe (L.DataHash StandardCrypto)
-> TxOutDatum ctx era
fromAlonzoTxOutDataHash _ SNothing = TxOutDatumNone
fromAlonzoTxOutDataHash s (SJust dh) = TxOutDatumHash s (ScriptDataHash dh)
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.

A bunch of alonzo specific code disappears because we remove the feature that only worked in Alonzo and choose to no longer support.

Copy link
Contributor

@Jimbo4350 Jimbo4350 Nov 9, 2023

Choose a reason for hiding this comment

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

Datum hashes weren't deprecated. See in ledger: instance Crypto c => AlonzoEraTxOut (BabbageEra c) where. This is removing the possibility for users to specify datum hashes instead of inline datums!


-- TODO: If ledger creates an open type family for datums
-- we can consolidate this function with the Alonzo version
toBabbageTxOutDatum
:: (L.Era (ShelleyLedgerEra era), Ledger.EraCrypto (ShelleyLedgerEra era) ~ StandardCrypto)
=> TxOutDatum CtxUTxO era -> Babbage.Datum (ShelleyLedgerEra era)
Expand Down Expand Up @@ -2174,10 +2149,8 @@ fromLedgerTxOuts sbe body scriptdata =
ShelleyBasedEraAlonzo ->
[ fromAlonzoTxOut
AlonzoEraOnwardsAlonzo
txdatums
txout
| let txdatums = selectTxDatums scriptdata
, txout <- toList (body ^. L.outputsTxBodyL) ]
| txout <- toList (body ^. L.outputsTxBodyL) ]

ShelleyBasedEraBabbage ->
[ fromBabbageTxOut
Expand All @@ -2202,30 +2175,18 @@ fromLedgerTxOuts sbe body scriptdata =

fromAlonzoTxOut :: ()
=> AlonzoEraOnwards era
-> Map (L.DataHash StandardCrypto) (L.Data ledgerera)
-> L.TxOut (ShelleyLedgerEra era)
-> TxOut CtxTx era
fromAlonzoTxOut w txdatums txOut =
fromAlonzoTxOut w txOut =
alonzoEraOnwardsConstraints w $
TxOut
(fromShelleyAddr shelleyBasedEra (txOut ^. L.addrTxOutL))
(TxOutValueShelleyBased sbe (txOut ^. L.valueTxOutL))
(fromAlonzoTxOutDatum w txdatums (txOut ^. L.dataHashTxOutL))
TxOutDatumNone
ReferenceScriptNone
where
sbe = alonzoEraOnwardsToShelleyBasedEra w

fromAlonzoTxOutDatum :: ()
=> AlonzoEraOnwards era
-> Map (L.DataHash StandardCrypto) (L.Data ledgerera)
-> StrictMaybe (L.DataHash StandardCrypto)
-> TxOutDatum CtxTx era
fromAlonzoTxOutDatum w txdatums = \case
SNothing -> TxOutDatumNone
SJust dh
| Just d <- Map.lookup dh txdatums -> TxOutDatumInTx' w (ScriptDataHash dh) (fromAlonzoData d)
| otherwise -> TxOutDatumHash w (ScriptDataHash dh)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A bunch of alonzo specific code disappears because we remove the feature that only worked in Alonzo and choose to no longer support.


fromBabbageTxOut :: forall era. ()
=> BabbageEraOnwards era
-> Map (L.DataHash StandardCrypto) (L.Data (ShelleyLedgerEra era))
Expand Down Expand Up @@ -3164,7 +3125,6 @@ toShelleyTxOutAny sbe = \case

ShelleyBasedEraAlonzo ->
L.mkBasicTxOut (toShelleyAddr addr) value
& L.dataHashTxOutL .~ toAlonzoTxOutDataHash' txoutdata

ShelleyBasedEraBabbage ->
L.mkBasicTxOut (toShelleyAddr addr) value
Expand All @@ -3178,14 +3138,6 @@ toShelleyTxOutAny sbe = \case
where
cEra = shelleyBasedToCardanoEra sbe
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Opportunity to simplify because L.mkBasicTxOut (toShelleyAddr addr) value is repeated. This wasn't the case previously.


toAlonzoTxOutDataHash' :: TxOutDatum ctx AlonzoEra
-> StrictMaybe (L.DataHash StandardCrypto)
toAlonzoTxOutDataHash' TxOutDatumNone = SNothing
toAlonzoTxOutDataHash' (TxOutDatumHash _ (ScriptDataHash dh)) = SJust dh
toAlonzoTxOutDataHash' (TxOutDatumInTx' _ (ScriptDataHash dh) _) = SJust dh
toAlonzoTxOutDataHash' (TxOutDatumInline inlineDatumSupp _sd) =
case inlineDatumSupp :: BabbageEraOnwards AlonzoEra of {}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The code above gets the hash and the code below also gets the hash as a Datum.

-- TODO: Consolidate with alonzo function and rename
toBabbageTxOutDatum'
:: (L.Era (ShelleyLedgerEra era), Ledger.EraCrypto (ShelleyLedgerEra era) ~ StandardCrypto)
Expand Down
Loading