Skip to content

Commit

Permalink
Recalculate aux data hash on conversion and test it
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
v0d1ch committed May 8, 2024
1 parent e20a5cf commit 09ce38b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions hydra-cardano-api/src/Hydra/Cardano/Api/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ toLedgerTx = \case
& datsTxWitsL .~ datums
& rdmrsTxWitsL .~ redeemers
in mkBasicTx
-- TODO: Test that aux data hash is correctly updated in conversions
(body & auxDataHashTxBodyL .~ maybe SNothing (SJust . hashTxAuxData) auxData)
& isValidTxL .~ toLedgerScriptValidity validity
& auxDataTxL .~ maybeToStrictMaybe auxData
Expand All @@ -237,12 +236,13 @@ toLedgerTx = \case
fromLedgerTx :: Ledger.Tx (ShelleyLedgerEra Era) -> Tx Era
fromLedgerTx ledgerTx =
Tx
(ShelleyTxBody shelleyBasedEra body scripts scriptsData (strictMaybeToMaybe auxData) validity)
(ShelleyTxBody shelleyBasedEra body' scripts scriptsData (strictMaybeToMaybe auxData) validity)
(fromLedgerTxWitness wits)
where
-- XXX: The suggested way (by the ledger team) forward is to use lenses to
-- introspect ledger transactions.
Ledger.AlonzoTx body wits isValid auxData = ledgerTx
body' = body & auxDataHashTxBodyL .~ (hashTxAuxData <$> auxData)

scripts =
Map.elems $ Ledger.txscripts' wits
Expand Down
10 changes: 9 additions & 1 deletion hydra-node/test/Hydra/Ledger/CardanoSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import Hydra.Prelude
import Test.Hydra.Prelude

import Cardano.Binary (decodeFull, serialize')
import Cardano.Ledger.Api (ensureMinCoinTxOut)
import Cardano.Ledger.Api (auxDataHashTxBodyL, bodyTxL, ensureMinCoinTxOut)
import Cardano.Ledger.BaseTypes (StrictMaybe (..))
import Cardano.Ledger.Core (PParams ())
import Cardano.Ledger.Credential (Credential (..))
import Control.Lens ((.~))
import Data.Aeson (eitherDecode, encode)
import Data.Aeson qualified as Aeson
import Data.Aeson.Lens (key)
Expand Down Expand Up @@ -71,6 +73,8 @@ spec =

prop "Roundtrip to and from Ledger" roundtripLedger

prop "Roundtrip tx metadata" roundtripTxMetadata

prop "Roundtrip CBOR encoding" $ roundtripCBOR @Tx

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

roundtripTxMetadata :: Tx -> Property
roundtripTxMetadata tx =
fromLedgerTx (toLedgerTx tx & bodyTxL . auxDataHashTxBodyL .~ SNothing) === tx

roundtripCBOR :: (Eq a, Show a, ToCBOR a, FromCBOR a) => a -> Property
roundtripCBOR a =
let encoded = serialize' a
Expand Down

0 comments on commit 09ce38b

Please sign in to comment.