Skip to content

Commit

Permalink
Merge pull request #613 from IntersectMBO/koslambrou/fix-fromShelleyT…
Browse files Browse the repository at this point in the history
…xOut

Fix datum conversion in 'fromShelleyTxOut' when using 'ShelleyBasedEraAlonzo' as input
  • Loading branch information
carbolymer committed Aug 20, 2024
2 parents b20f3be + 37d66d1 commit fe2ae13
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
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
8 changes: 8 additions & 0 deletions cardano-api/test/cardano-api-test/Test/Cardano/Api/Ledger.hs
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
]

0 comments on commit fe2ae13

Please sign in to comment.