Skip to content

Commit

Permalink
Fix datum conversion in 'fromShelleyTxOut' when using 'ShelleyBasedEr…
Browse files Browse the repository at this point in the history
…aAlonzo' as input
  • Loading branch information
koslambrou committed Aug 10, 2024
1 parent 1fb6947 commit 9025ae3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 14 additions & 1 deletion cardano-api/internal/Cardano/Api/Tx/Body.hs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,13 @@ fromShelleyTxOut sbe ledgerTxOut = shelleyBasedEraConstraints sbe $ do
ShelleyBasedEraMary ->
TxOut addressInEra txOutValue TxOutDatumNone ReferenceScriptNone
ShelleyBasedEraAlonzo ->
TxOut addressInEra txOutValue TxOutDatumNone ReferenceScriptNone
TxOut
addressInEra
txOutValue
(fromAlonzoTxOutDatumHash AlonzoEraOnwardsAlonzo datumHashM)
ReferenceScriptNone
where
datumHashM = ledgerTxOut ^. L.dataHashTxOutL
ShelleyBasedEraBabbage ->
TxOut
addressInEra
Expand Down Expand Up @@ -786,6 +792,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
7 changes: 7 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 @@ -63,6 +63,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 +78,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 9025ae3

Please sign in to comment.