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 17, 2023
1 parent daeae61 commit 26008d1
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 140 deletions.
5 changes: 4 additions & 1 deletion cardano-api/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

- **Breaking change** - `deserialiseFromRawBytes` method of the `SerialiseAsRawBytes` type class to return `Either` instead of `Maybe`. Deprecate `eitherDeserialiseFromRawBytes`. Use `deserialiseFromRawBytes` instead.

- The `cardano-cli governance create-update-proposal` command to reject empty cost model.
- The `cardano-cli governance create-update-proposal` command to reject empty cost model ([PR4885](https://github.com/input-output-hk/cardano-node/pull/4885))

- **Breaking change** - Preserve ScriptData bytes with HashableScriptData ([PR4886](https://github.com/input-output-hk/cardano-node/pull/4886))


### Bugs

Expand Down
4 changes: 3 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,11 +216,13 @@ 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
, hedgehog
, hedgehog-extras
, mtl
, ouroboros-consensus
, ouroboros-consensus-shelley
, QuickCheck
Expand Down
25 changes: 20 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 All @@ -35,6 +36,7 @@ module Test.Gen.Cardano.Api.Typed
, genScriptInEra
, genScriptHash
, genScriptData
, genScriptDataSchema
, genScriptValidity

, genAssetName
Expand Down Expand Up @@ -109,13 +111,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 +223,18 @@ genPlutusScript _ =
-- We make no attempt to create a valid script
PlutusScriptSerialised . SBS.toShort <$> Gen.bytes (Range.linear 0 32)

genScriptDataSchema :: Gen ScriptDataJsonSchema
genScriptDataSchema = Gen.element [ScriptDataJsonNoSchema, ScriptDataJsonDetailedSchema]

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 +906,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 +928,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 26008d1

Please sign in to comment.