Skip to content

Commit 8c7c2a3

Browse files
v0d1chch1bo
authored andcommitted
Recalculate aux data hash on conversion and test it
On each conversion we need to recalculate the aux data hash since cardano-api does not provide a way to set the data hash and on top of it it seems that it removes it completely. Add a roundtrip test to assert the metadata is correctly converted.
1 parent 141ed84 commit 8c7c2a3

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

hydra-cardano-api/src/Hydra/Cardano/Api/Tx.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ toLedgerTx = \case
227227
& datsTxWitsL .~ datums
228228
& rdmrsTxWitsL .~ redeemers
229229
in mkBasicTx
230-
-- TODO: Test that aux data hash is correctly updated in conversions
231230
(body & auxDataHashTxBodyL .~ maybe SNothing (SJust . hashTxAuxData) auxData)
232231
& isValidTxL .~ toLedgerScriptValidity validity
233232
& auxDataTxL .~ maybeToStrictMaybe auxData
@@ -237,12 +236,13 @@ toLedgerTx = \case
237236
fromLedgerTx :: Ledger.Tx (ShelleyLedgerEra Era) -> Tx Era
238237
fromLedgerTx ledgerTx =
239238
Tx
240-
(ShelleyTxBody shelleyBasedEra body scripts scriptsData (strictMaybeToMaybe auxData) validity)
239+
(ShelleyTxBody shelleyBasedEra body' scripts scriptsData (strictMaybeToMaybe auxData) validity)
241240
(fromLedgerTxWitness wits)
242241
where
243242
-- XXX: The suggested way (by the ledger team) forward is to use lenses to
244243
-- introspect ledger transactions.
245244
Ledger.AlonzoTx body wits isValid auxData = ledgerTx
245+
body' = body & auxDataHashTxBodyL .~ (hashTxAuxData <$> auxData)
246246

247247
scripts =
248248
Map.elems $ Ledger.txscripts' wits

hydra-node/test/Hydra/Ledger/CardanoSpec.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import Hydra.Prelude
88
import Test.Hydra.Prelude
99

1010
import Cardano.Binary (decodeFull, serialize')
11-
import Cardano.Ledger.Api (ensureMinCoinTxOut)
11+
import Cardano.Ledger.Api (auxDataHashTxBodyL, bodyTxL, ensureMinCoinTxOut)
12+
import Cardano.Ledger.BaseTypes (StrictMaybe (..))
1213
import Cardano.Ledger.Core (PParams ())
1314
import Cardano.Ledger.Credential (Credential (..))
15+
import Control.Lens ((.~))
1416
import Data.Aeson (eitherDecode, encode)
1517
import Data.Aeson qualified as Aeson
1618
import Data.Aeson.Lens (key)
@@ -71,6 +73,8 @@ spec =
7173

7274
prop "Roundtrip to and from Ledger" roundtripLedger
7375

76+
prop "Roundtrip tx metadata" roundtripTxMetadata
77+
7478
prop "Roundtrip CBOR encoding" $ roundtripCBOR @Tx
7579

7680
prop "JSON encoding of Tx according to schema" $
@@ -152,6 +156,10 @@ roundtripLedger :: Tx -> Property
152156
roundtripLedger tx =
153157
fromLedgerTx (toLedgerTx tx) === tx
154158

159+
roundtripTxMetadata :: Tx -> Property
160+
roundtripTxMetadata tx =
161+
fromLedgerTx (toLedgerTx tx & bodyTxL . auxDataHashTxBodyL .~ SNothing) === tx
162+
155163
roundtripCBOR :: (Eq a, Show a, ToCBOR a, FromCBOR a) => a -> Property
156164
roundtripCBOR a =
157165
let encoded = serialize' a

0 commit comments

Comments
 (0)