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

Fix datum conversion in 'fromShelleyTxOut' when using 'ShelleyBasedEraAlonzo' as input #613

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions cardano-api/internal/Cardano/Api/Tx/Body.hs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,13 @@ fromShelleyTxOut sbe ledgerTxOut = shelleyBasedEraConstraints sbe $ do
ShelleyBasedEraMary ->
TxOut addressInEra txOutValue TxOutDatumNone ReferenceScriptNone
ShelleyBasedEraAlonzo ->
TxOut addressInEra txOutValue TxOutDatumNone ReferenceScriptNone
TxOut
addressInEra
txOutValue
(fromAlonzoTxOutDatumHash AlonzoEraOnwardsAlonzo mDatumHash)
ReferenceScriptNone
where
mDatumHash = ledgerTxOut ^. L.dataHashTxOutL
ShelleyBasedEraBabbage ->
TxOut
addressInEra
Expand Down Expand Up @@ -791,6 +797,13 @@ toAlonzoTxOutDatumHash (TxOutDatumHash _ (ScriptDataHash dh)) = SJust dh
toAlonzoTxOutDatumHash (TxOutDatumInline{}) = SNothing
toAlonzoTxOutDatumHash (TxOutDatumInTx' _ (ScriptDataHash dh) _) = SJust dh

fromAlonzoTxOutDatumHash
:: AlonzoEraOnwards era
-> StrictMaybe (Plutus.DataHash StandardCrypto)
-> TxOutDatum ctx era
fromAlonzoTxOutDatumHash _ SNothing = TxOutDatumNone
fromAlonzoTxOutDatumHash w (SJust hash) = TxOutDatumHash w $ ScriptDataHash hash

toBabbageTxOutDatum
:: (L.Era (ShelleyLedgerEra era), Ledger.EraCrypto (ShelleyLedgerEra era) ~ StandardCrypto)
=> TxOutDatum ctx era
Expand Down Expand Up @@ -2009,7 +2022,7 @@ fromAlonzoTxOut w txOut =
TxOut
(fromShelleyAddr shelleyBasedEra (txOut ^. L.addrTxOutL))
(TxOutValueShelleyBased sbe (txOut ^. L.valueTxOutL))
TxOutDatumNone
(fromAlonzoTxOutDatumHash w (txOut ^. L.dataHashTxOutL))
ReferenceScriptNone
where
sbe = alonzoEraOnwardsToShelleyBasedEra w
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Cardano.Ledger.Api.Tx.Address
import Cardano.Ledger.Crypto
import Cardano.Ledger.SafeHash

import Control.Monad
import Control.Monad.Identity

import Test.Gen.Cardano.Api.Typed
Expand Down Expand Up @@ -63,6 +64,12 @@ prop_roundtrip_scriptdata_plutusdata = H.property $ do
sd <- getScriptData <$> forAll genHashableScriptData
H.tripping sd toPlutusData (Identity . fromPlutusData)

prop_roundtrip_ledger_txout :: Property
prop_roundtrip_ledger_txout = H.property $ do
forM_ [minBound .. maxBound] $ \(AnyShelleyBasedEra era) -> do
txOut <- forAll $ genTxOutUTxOContext era
txOut H.=== fromShelleyTxOut era (toShelleyTxOut era txOut)

-- -----------------------------------------------------------------------------

tests :: TestTree
Expand All @@ -72,4 +79,5 @@ tests =
[ testProperty "roundtrip Address CBOR" prop_roundtrip_Address_CBOR
, testProperty "roundtrip ScriptData" prop_roundtrip_scriptdata_plutusdata
, testProperty "script data bytes preserved" prop_original_scriptdata_bytes_preserved
, testProperty "roundtrip Ledger TxOut" prop_roundtrip_ledger_txout
]
Loading