From d29d786fecae389af55d59821e62d6f0bff7b083 Mon Sep 17 00:00:00 2001 From: Richard Marko Date: Thu, 12 May 2022 18:10:40 +0200 Subject: [PATCH 1/4] Add JSON roundtrip property for ScriptData using detailed schema. --- cardano-api/test/Test/Cardano/Api/Typed/Script.hs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cardano-api/test/Test/Cardano/Api/Typed/Script.hs b/cardano-api/test/Test/Cardano/Api/Typed/Script.hs index 8c69cc8040b..4d640117811 100644 --- a/cardano-api/test/Test/Cardano/Api/Typed/Script.hs +++ b/cardano-api/test/Test/Cardano/Api/Typed/Script.hs @@ -116,6 +116,12 @@ prop_roundtrip_ScriptData = sData <- H.forAll genHashableScriptData sData === fromAlonzoData (toAlonzoData sData) +prop_roundtrip_ScriptData_JSON :: Property +prop_roundtrip_ScriptData_JSON = + H.property $ do + sData <- H.forAll genScriptData + H.tripping sData scriptDataToJsonDetailedSchema scriptDataFromJsonDetailedSchema + -- ----------------------------------------------------------------------------- tests :: TestTree From 0e57ee2710682172218aff0c54dacd3006ab0e08 Mon Sep 17 00:00:00 2001 From: Richard Marko Date: Thu, 12 May 2022 18:10:55 +0200 Subject: [PATCH 2/4] Add CBOR roundtrip property for ScriptData --- cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs b/cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs index 92a0690de15..760f65616e3 100644 --- a/cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs +++ b/cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs @@ -147,6 +147,10 @@ prop_roundtrip_script_PlutusScriptV2_CBOR = roundtrip_CBOR (AsScript AsPlutusScriptV2) (genScript (PlutusScriptLanguage PlutusScriptV2)) +prop_roundtrip_ScriptData_CBOR :: Property +prop_roundtrip_ScriptData_CBOR = + roundtrip_CBOR AsScriptData genScriptData + prop_roundtrip_UpdateProposal_CBOR :: Property prop_roundtrip_UpdateProposal_CBOR = roundtrip_CBOR AsUpdateProposal genUpdateProposal From ef046c177c4c869331abd7f8ba00263318ee7c50 Mon Sep 17 00:00:00 2001 From: Richard Marko Date: Thu, 12 May 2022 18:12:05 +0200 Subject: [PATCH 3/4] Improve genScriptData to generate larger range of numbers for `ScriptDataNumber` constructor including numbers outside `maxBound @Word64`. --- cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs b/cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs index ad7e4a4e525..b54135d4a51 100644 --- a/cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs +++ b/cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs @@ -238,7 +238,7 @@ genScriptData = , ScriptDataBytes <$> genByteString ] -- The Gen.recursive combinator calls these with the size halved - [ ScriptDataConstructor <$> genInteger + [ ScriptDataConstructor <$> genConstructorInteger <*> genScriptDataList , ScriptDataList <$> genScriptDataList , ScriptDataMap <$> genScriptDataMap @@ -246,6 +246,13 @@ genScriptData = where genInteger :: Gen Integer genInteger = Gen.integral + (Range.linear + (-fromIntegral (maxBound :: Word64) :: Integer) + (2 * fromIntegral (maxBound :: Word64) :: Integer)) + + + genConstructorInteger :: Gen Integer + genConstructorInteger = Gen.integral (Range.linear 0 -- TODO: Alonzo should be -> (-fromIntegral (maxBound :: Word64) :: Integer) -- Wrapping bug needs to be fixed in Plutus library From 9b8e749e62f1398cde86afb64cd1b10190641827 Mon Sep 17 00:00:00 2001 From: John Ky Date: Sun, 19 Mar 2023 17:54:39 +1100 Subject: [PATCH 4/4] Switch to HashableScriptData --- cardano-api/src/Cardano/Api/ScriptData.hs | 2 +- cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs | 3 ++- cardano-api/test/Test/Cardano/Api/Typed/Script.hs | 7 ++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cardano-api/src/Cardano/Api/ScriptData.hs b/cardano-api/src/Cardano/Api/ScriptData.hs index 5d0e90fa446..6100d2fed83 100644 --- a/cardano-api/src/Cardano/Api/ScriptData.hs +++ b/cardano-api/src/Cardano/Api/ScriptData.hs @@ -270,7 +270,7 @@ validateScriptData d = -- newtype ScriptDataRangeError = - -- | The number is outside the maximum range of @-2^64-1 .. 2^64-1@. + -- | The constructor number is outside the maximum range of @-2^64-1 .. 2^64-1@. -- ScriptDataConstructorOutOfRange Integer deriving (Eq, Show) diff --git a/cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs b/cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs index 760f65616e3..5ae41145950 100644 --- a/cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs +++ b/cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs @@ -149,7 +149,7 @@ prop_roundtrip_script_PlutusScriptV2_CBOR = prop_roundtrip_ScriptData_CBOR :: Property prop_roundtrip_ScriptData_CBOR = - roundtrip_CBOR AsScriptData genScriptData + roundtrip_CBOR AsHashableScriptData genHashableScriptData prop_roundtrip_UpdateProposal_CBOR :: Property prop_roundtrip_UpdateProposal_CBOR = @@ -220,6 +220,7 @@ tests = testGroup "Test.Cardano.Api.Typed.CBOR" , testPropertyNamed "roundtrip script PlutusScriptV1 CBOR" "roundtrip script PlutusScriptV1 CBOR" prop_roundtrip_script_PlutusScriptV1_CBOR , testPropertyNamed "roundtrip script PlutusScriptV2 CBOR" "roundtrip script PlutusScriptV2 CBOR" prop_roundtrip_script_PlutusScriptV2_CBOR , testPropertyNamed "roundtrip UpdateProposal CBOR" "roundtrip UpdateProposal CBOR" prop_roundtrip_UpdateProposal_CBOR + , testPropertyNamed "roundtrip ScriptData CBOR" "roundtrip ScriptData CBOR" prop_roundtrip_ScriptData_CBOR , testGroup "roundtrip txbody CBOR" test_roundtrip_txbody_CBOR , testGroup "roundtrip tx CBOR" test_roundtrip_tx_CBOR , testGroup "roundtrip Tx Cddl" test_roundtrip_Tx_Cddl diff --git a/cardano-api/test/Test/Cardano/Api/Typed/Script.hs b/cardano-api/test/Test/Cardano/Api/Typed/Script.hs index 4d640117811..dd2a5af02f0 100644 --- a/cardano-api/test/Test/Cardano/Api/Typed/Script.hs +++ b/cardano-api/test/Test/Cardano/Api/Typed/Script.hs @@ -116,10 +116,10 @@ prop_roundtrip_ScriptData = sData <- H.forAll genHashableScriptData sData === fromAlonzoData (toAlonzoData sData) -prop_roundtrip_ScriptData_JSON :: Property -prop_roundtrip_ScriptData_JSON = +prop_roundtrip_HashableScriptData_JSON :: Property +prop_roundtrip_HashableScriptData_JSON = H.property $ do - sData <- H.forAll genScriptData + sData <- H.forAll genHashableScriptData H.tripping sData scriptDataToJsonDetailedSchema scriptDataFromJsonDetailedSchema -- ----------------------------------------------------------------------------- @@ -134,4 +134,5 @@ tests = testGroup "Test.Cardano.Api.Typed.Script" , testPropertyNamed "golden SimpleScriptV2 MofN" "golden SimpleScriptV2 MofN" prop_golden_SimpleScriptV2_MofN , testPropertyNamed "roundtrip SimpleScript JSON" "roundtrip SimpleScript JSON" prop_roundtrip_SimpleScript_JSON , testPropertyNamed "roundtrip ScriptData" "roundtrip ScriptData" prop_roundtrip_ScriptData + , testPropertyNamed "roundtrip HashableScriptData" "roundtrip HashableScriptData" prop_roundtrip_HashableScriptData_JSON ]