Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script data serialisation #5002

Merged
merged 4 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,21 @@ genScriptData =
, ScriptDataBytes <$> genByteString
]
-- The Gen.recursive combinator calls these with the size halved
[ ScriptDataConstructor <$> genInteger
[ ScriptDataConstructor <$> genConstructorInteger
<*> genScriptDataList
, ScriptDataList <$> genScriptDataList
, ScriptDataMap <$> genScriptDataMap
]
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
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/ScriptData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 AsHashableScriptData genHashableScriptData

prop_roundtrip_UpdateProposal_CBOR :: Property
prop_roundtrip_UpdateProposal_CBOR =
roundtrip_CBOR AsUpdateProposal genUpdateProposal
Expand Down Expand Up @@ -216,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
Expand Down
7 changes: 7 additions & 0 deletions cardano-api/test/Test/Cardano/Api/Typed/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ prop_roundtrip_ScriptData =
sData <- H.forAll genHashableScriptData
sData === fromAlonzoData (toAlonzoData sData)

prop_roundtrip_HashableScriptData_JSON :: Property
prop_roundtrip_HashableScriptData_JSON =
H.property $ do
sData <- H.forAll genHashableScriptData
H.tripping sData scriptDataToJsonDetailedSchema scriptDataFromJsonDetailedSchema

-- -----------------------------------------------------------------------------

tests :: TestTree
Expand All @@ -128,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
]