Skip to content

Commit

Permalink
Preserve original CBOR bytes of ScriptData which has now been wrapp…
Browse files Browse the repository at this point in the history
…ed with

the `HashableScriptData` type. Prior to this commit we were constructing
transactions with `toAlonzoData` which created a ledger `Data`
value without using the original bytes. Although the content is the same
this can result in the re-encoded value differing slightly and therefore
resulting in differing script data hashes. See:
IntersectMBO/cardano-ledger#2943 for more
details.
  • Loading branch information
Jimbo4350 committed Feb 16, 2023
1 parent 7074c45 commit 96e8f7b
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 139 deletions.
3 changes: 2 additions & 1 deletion cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ library gen
, cardano-ledger-alonzo-test
, cardano-ledger-byron-test ^>= 1.4
, cardano-ledger-core ^>= 0.1
, cardano-ledger-shelley ^>= 0.1
, containers
, hedgehog
, cardano-ledger-shelley ^>= 0.1
, text

test-suite cardano-api-test
Expand All @@ -216,6 +216,7 @@ test-suite cardano-api-test
, cardano-crypto-class ^>= 2.0
, cardano-crypto-test ^>= 1.4
, cardano-crypto-tests ^>= 2.0
, cardano-ledger-alonzo ^>= 0.1
, cardano-ledger-core ^>= 0.1
, cardano-slotting ^>= 0.1
, containers
Expand Down
22 changes: 17 additions & 5 deletions cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module Test.Gen.Cardano.Api.Typed
, genUTxO

-- * Scripts
, genHashableScriptData
, genReferenceScript
, genScript
, genSimpleScript
Expand Down Expand Up @@ -109,13 +110,14 @@ import Cardano.Api hiding (txIns)
import qualified Cardano.Api as Api
import Cardano.Api.Byron (KeyWitness (ByronKeyWitness),
WitnessNetworkIdOrByronAddress (..))
import Cardano.Api.Shelley (Hash (ScriptDataHash), KESPeriod (KESPeriod),
import Cardano.Api.Shelley (Hash (..), KESPeriod (KESPeriod),
OperationalCertificateIssueCounter (OperationalCertificateIssueCounter),
PlutusScript (PlutusScriptSerialised), ProtocolParameters (ProtocolParameters),
ReferenceScript (..), ReferenceTxInsScriptsInlineDatumsSupportedInEra (..),
StakeCredential (StakeCredentialByKey), StakePoolKey,
refInsScriptsAndInlineDatsSupportedInEra)


import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Short as SBS
Expand Down Expand Up @@ -220,6 +222,16 @@ genPlutusScript _ =
-- We make no attempt to create a valid script
PlutusScriptSerialised . SBS.toShort <$> Gen.bytes (Range.linear 0 32)


genHashableScriptData :: Gen HashableScriptData
genHashableScriptData = do
sd <- genScriptData
case deserialiseFromCBOR AsHashableScriptData $ serialiseToCBOR sd of
Left e -> error $ "genHashableScriptData: " <> show e
Right r -> return r


{-# DEPRECATED genScriptData "Use genHashableScriptData" #-}
genScriptData :: Gen ScriptData
genScriptData =
Gen.recursive
Expand Down Expand Up @@ -891,13 +903,13 @@ genTxOutDatumHashTxContext era = case era of
AlonzoEra -> Gen.choice
[ pure TxOutDatumNone
, TxOutDatumHash ScriptDataInAlonzoEra <$> genHashScriptData
, TxOutDatumInTx ScriptDataInAlonzoEra <$> genScriptData
, TxOutDatumInTx ScriptDataInAlonzoEra <$> genHashableScriptData
]
BabbageEra -> Gen.choice
[ pure TxOutDatumNone
, TxOutDatumHash ScriptDataInBabbageEra <$> genHashScriptData
, TxOutDatumInTx ScriptDataInBabbageEra <$> genScriptData
, TxOutDatumInline ReferenceTxInsScriptsInlineDatumsInBabbageEra <$> genScriptData
, TxOutDatumInTx ScriptDataInBabbageEra <$> genHashableScriptData
, TxOutDatumInline ReferenceTxInsScriptsInlineDatumsInBabbageEra <$> genHashableScriptData
]

genTxOutDatumHashUTxOContext :: CardanoEra era -> Gen (TxOutDatum CtxUTxO era)
Expand All @@ -913,7 +925,7 @@ genTxOutDatumHashUTxOContext era = case era of
BabbageEra -> Gen.choice
[ pure TxOutDatumNone
, TxOutDatumHash ScriptDataInBabbageEra <$> genHashScriptData
, TxOutDatumInline ReferenceTxInsScriptsInlineDatumsInBabbageEra <$> genScriptData
, TxOutDatumInline ReferenceTxInsScriptsInlineDatumsInBabbageEra <$> genHashableScriptData
]

mkDummyHash :: forall h a. CRYPTO.HashAlgorithm h => Int -> CRYPTO.Hash h a
Expand Down
6 changes: 6 additions & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ module Cardano.Api (
examplePlutusScriptAlwaysFails,

-- ** Script data
HashableScriptData,
hashScriptDataBytes,
getOriginalScriptDataBytes,
getScriptData,
ScriptData(..),
hashScriptData,

Expand All @@ -412,6 +416,8 @@ module Cardano.Api (
scriptDataToJson,
ScriptDataJsonError (..),
ScriptDataJsonSchemaError (..),
ScriptDataJsonBytesError,
scriptDataJsonToHashable,

-- ** Script execution units
ExecutionUnits(..),
Expand Down
10 changes: 5 additions & 5 deletions cardano-api/src/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -792,13 +792,13 @@ instance Eq (ScriptWitness witctx era) where

(==) _ _ = False

type ScriptRedeemer = ScriptData
type ScriptRedeemer = HashableScriptData

data ScriptDatum witctx where
ScriptDatumForTxIn :: ScriptData -> ScriptDatum WitCtxTxIn
InlineScriptDatum :: ScriptDatum WitCtxTxIn
NoScriptDatumForMint :: ScriptDatum WitCtxMint
NoScriptDatumForStake :: ScriptDatum WitCtxStake
ScriptDatumForTxIn :: HashableScriptData -> ScriptDatum WitCtxTxIn
InlineScriptDatum :: ScriptDatum WitCtxTxIn
NoScriptDatumForMint :: ScriptDatum WitCtxMint
NoScriptDatumForStake :: ScriptDatum WitCtxStake

deriving instance Eq (ScriptDatum witctx)
deriving instance Show (ScriptDatum witctx)
Expand Down
Loading

0 comments on commit 96e8f7b

Please sign in to comment.