Skip to content

Commit

Permalink
Add round trip tests for correctly encoded plutus scripts (i.e non
Browse files Browse the repository at this point in the history
double encoded plutus scripts)
  • Loading branch information
Jimbo4350 committed Jan 3, 2025
1 parent 67eb9f6 commit 9a0fbb4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
38 changes: 36 additions & 2 deletions cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ module Test.Gen.Cardano.Api.Typed
, genScript
, genSimpleScript
, genPlutusScript
, genPlutusV1Script
, genPlutusV2Script
, genPlutusV3Script
, genScriptInAnyLang
, genScriptInEra
, genScriptHash
Expand Down Expand Up @@ -143,6 +146,7 @@ import Cardano.Api.Shelley
import qualified Cardano.Api.Shelley as ShelleyApi

import qualified Cardano.Binary as CBOR
import qualified Data.ByteString.Base16 as Base16
import qualified Cardano.Crypto.Hash as Crypto
import qualified Cardano.Crypto.Hash.Class as CRYPTO
import qualified Cardano.Crypto.Seed as Crypto
Expand Down Expand Up @@ -241,9 +245,39 @@ genSimpleScript =
]

genPlutusScript :: PlutusScriptVersion lang -> Gen (PlutusScript lang)
genPlutusScript _ =
genPlutusScript l =
case l of
PlutusScriptV1 -> do
PlutusScript _ s <- genPlutusV1Script
return s
PlutusScriptV2 -> do
PlutusScript _ s <- genPlutusV2Script
return s
PlutusScriptV3 -> do
PlutusScript _ s <- genPlutusV3Script
return s
-- -- We make no attempt to create a valid script
-- PlutusScriptSerialised . SBS.toShort <$> Gen.bytes (Range.linear 0 32)


genPlutusV1Script :: Gen (Script PlutusScriptV1)
genPlutusV1Script =
let v1AlwaysSucceedsPlutusScriptHex = "50584e010000332232222325335333573466e200052080897a0070061613005001375a00464600200244a66a666ae68cdc3a410112f40020080062240022646600600600266e0400520021220021220011"
v1AlwaysSucceedsBytes = Base16.decodeLenient v1AlwaysSucceedsPlutusScriptHex
in return . PlutusScript PlutusScriptV1 . PlutusScriptSerialised $ SBS.toShort v1AlwaysSucceedsBytes

genPlutusV2Script :: Gen (Script PlutusScriptV2)
genPlutusV2Script =
let v2AlwaysSucceedsPlutusScriptHex = "04795904760100003232323322323222253233553232323232323335355003220021325335300d00113233300122533333500221533333500221333573466e3c00800405004c8840508404c40488840508854ccccd400c840508854cd4ccd5cd19b8f004002016015133300722333573466e3c00800406005c00c004405484050404c884054854ccccd40088404c88405084ccd5cd19b87002001014013101222101415333335001210122210132101210122210132215333335003210142210152101410132215335333573466e1c0100080580544ccc01c88ccd5cd19b87002001018017003001101500230033500600c500c100e5002133357346466e1ccc0044488c88c008004c050894cd4004400c884cc018008c010005400ccc0044488c88c008004c050894cd4004400c884cc018008c010004c008d401402c8cd5400488cdc024004002900000700689919980091299999a80110a99999a80110999ab9a3371e004002026024442026420242022442026442a66666a00642026442a66a666ae68cdc780200100a80a099980391199ab9a3371e00400202e02c006002202842026202444202842a66666a0044202444202642666ae68cdc380100080980908089108098a99999a80090808910809108088808910809110a99999a8019080991080a108098809110a99a999ab9a3370e00800402a028266600e44666ae68cdc380100080b80b001800880a280198011a802805a80591a8009100089aa8009100089a8008030991191919299a999ab9a3370e9000001007807099910919800801801191919299a999ab9a3370e9000001009008889110010a99a999ab9a3370e9001001009008889110008a99a999ab9a3370e900200100900888911001898052481035054310035573c0046aae74004dd51aba10013005357426ae880044c01d241035054310035573c0046aae74004dd500098009191919299a999ab9a3370e900000100700688911110010a99a999ab9a3370e900100100700689909111118018031bad357420022a66a666ae68cdc3a400800401c01a264244444600a00c6eb8d5d08008a99a999ab9a3370e900300100700689991091111198008038031bad357420026600aeb4d5d09aba200115335333573466e1d200800200e00d1332212222233004007006375c6ae84004cc015d71aba1357440022600c9201035054310035573c0046aae74004dd5000919118011bac001300d2233335573e0024244004466442446600200800660086ae84008c00cd5d1001002919319ab9c001002100116120013300622122253350021533500110082210092215335003100922153353300700400213335300912001007003001100b12001300522533500110042215335533333500221007221333573466e3c008c8c8cc034894cd4004400c884cc018008c01000400cc02888004c0248800802001c8401c4018884ccd5cd19b87002332323223002001300d2253350011003221330060023004001300b22001300a220020010080071300400110051220021220012330017149101002330012233700004002900011918008009180111980100100081"
v2AlwaysSucceedsBytes = Base16.decodeLenient v2AlwaysSucceedsPlutusScriptHex
in return . PlutusScript PlutusScriptV2 . PlutusScriptSerialised $ SBS.toShort v2AlwaysSucceedsBytes

genPlutusV3Script :: Gen (Script PlutusScriptV3)
genPlutusV3Script =
-- We make no attempt to create a valid script
PlutusScriptSerialised . SBS.toShort <$> Gen.bytes (Range.linear 0 32)
let v3DoubleEncodedAlwaysSucceedsPlutusScriptHex = "0b2c590b29010100323232323232323232232498c8c8c954ccd5cd19b874800000844c8c8c8c8c8c8c8ca002646464aa666ae68cdc3a4000004226464646464646464646464646464646466666666666646664664664444444444444445001010807c03a01b00c805c02a013008803c01a00b004801c00a00230013574202860026ae8404cc0908c8c8c954ccd5cd19b87480000084600260406ae84006600a6ae84d5d1000844c0b52401035054310035573c0046aae74004dd5000998120009aba1011232323255333573466e1d20000021132328009919192a999ab9a3370e900000108c004c08cd5d0800ccc0848c8c8c954ccd5cd19b874800000846002604e6ae8400422aa666ae68cdc3a40040042265003375a6ae8400a6eb4d5d0800cdd69aba1357440023574400222606a9201035054310035573c0046aae74004dd50009aba135744002113031491035054310035573c0046aae74004dd51aba100398039aba10029919192a999ab9a3370e900000108c0004554ccd5cd19b87480080084600a6eb8d5d080084554ccd5cd19b8748010008460066ae840042260629201035054310035573c0046aae74004dd51aba10019980f3ae357426ae880046ae88004d5d1000889816249035054310035573c0046aae74004dd50009bad3574201e60026ae84038c004c005d69981100b1aba100c33301501975a6ae8402cc8c8c954ccd5cd19b874800000846002646464aa666ae68cdc3a4000004230013302b75a6ae8400660546ae84d5d1000844c0b5241035054310035573c0046aae74004dd51aba10019919192a999ab9a3370e900000108c004cc0add69aba100198151aba13574400211302d4901035054310035573c0046aae74004dd51aba13574400211302a4901035054310035573c0046aae74004dd51aba100a3302275c6ae84024ccc0548c8c8c954ccd5cd19b8748000008460066eb8d5d080084554ccd5cd19b874800800846012603c6ae8400422aa666ae68cdc3a400800423007301d357420021155333573466e1d2006002118009bad35742003301a357426ae8800422aa666ae68cdc3a40100042300b301c357420021155333573466e1d200a002118029bad357420033018357426ae880042260569201035054310035573c0046aae74004dd50008119aba1008330010233574200e6eb8d5d080319980a80c1980a81311919192a999ab9a3370e900000108c0084554ccd5cd19b87480080084600822aa666ae68cdc3a40080042300011302b491035054310035573c0046aae74004dd50009aba1005330220143574200860046ae8400cc008d5d09aba2003301475c602aeb4d5d10009aba2001357440026ae88004d5d10009aba2001357440026ae88004d5d10009aba2001357440026ae88004d5d10009aba20011130174901035054310035573c0046aae74004dd51aba10099aba10089919192a999ab9a3370e900000108c00cdd71aba100108aa999ab9a3370e900100108c024c028d5d0800ccc01c04cd5d09aba200108aa999ab9a3370e900200108c01cc024d5d080084554ccd5cd19b8748018008460026eb4d5d0800cc018d5d09aba200108aa999ab9a3370e900400108c02cc020d5d080084554ccd5cd19b87480280084600a6eb4d5d0800cc010d5d09aba200108980ba481035054310035573c0046aae74004dd51aba135744010232323255333573466e1d200000211328009bad35742005300a3574200332323255333573466e1d200000211328049980600d9aba10029aba1001998063ae357426ae880046ae880044554ccd5cd19b874800800846002660160346ae84006646464aa666ae68cdc3a400000423001375a6ae840066eb4d5d09aba200108980f2481035054310035573c0046aae74004dd51aba1357440021155333573466e1d200400211805999804806bad357420033300b75c6ae84d5d100084554ccd5cd19b87480180084600e660160346ae8400422aa666ae68cdc3a401000422646500d3300d01c357420073301800f3574200533300b00f75a6ae840072646464aa666ae68cdc3a400000423001375a6ae840066eb4d5d09aba20010898102481035054310035573c0046aae74004dd51aba13574400322330180020010d5d10009aba20011155333573466e1d200a002118029980580d1aba10019919192a999ab9a3370e900000108998073ae3574200222603c9201035054310035573c0046aae74004dd51aba1357440021155333573466e1d200c0021180108980da481035054310035573c0046aae74004dd51aba1357440023574400222602e9201035054310035573c0046aae74004dd50009119118011bab00130152233335573e0025000232801c004c018d55ce800cc014d55cf000a60086ae8800c6ae8400a0004646464aa666ae68cdc3a40000042300d3007357420033300575a6ae84d5d100084554ccd5cd19b874800800846026600e6ae840066600aeb4d5d09aba200108a992999ab9a3370e900200188c00cc020d5d08014c004d5d09aba200208aa999ab9a3370e90030018899402cc024d5d0801cc008d5d0800cdd69aba1357440023574400422aa666ae68cdc3a401000623009300835742005375a6ae84d5d100104554ccd5cd19b874802800c4602a60106ae8400822aa666ae68cdc3a401800623011300835742005375a6ae84d5d100104554ccd5cd19b874803800c4600a6eb8d5d08014dd71aba1357440041155333573466e1d2010003118039bae35742005375a6ae84d5d100104554ccd5cd19b874804800c4600260106ae8400a60106ae84d5d100104554ccd5cd19b874805000c4601e60106ae8400822602c9210350543100232323255333573466e1d2000002118009bae35742002115325333573466e1d20020031180298009aba100208aa999ab9a3370e900200188c00cdd71aba100298009aba13574400411301a49010350543100232323255333573466e1d20000021180098079aba100108aa999ab9a3370e900100108c0084554ccd5cd19b87480100084600822603a9201035054310035573c0046aae74004dd50009aab9e00235573a0026ea8004d55cf0011aab9d001375400244646464aa666ae68cdc3a4004004230021155333573466e1d20000021180098029aba100108980aa49035054310035573c0046aae74004dd500091919192a999ab9a3370e900000108c004c014d5d080084554ccd5cd19b874800800846006600a6ae8400422aa666ae68cdc3a400800423005375c6ae840042260269201035054310035573c0046aae74004dd500091919192a999ab9a3370e900000108c004dd71aba100108aa999ab9a3370e900100108c00cdd71aba1001089809249035054310035573c0046aae74004dd500091919192a999ab9a3370e900000108c004dd71aba10019bad357426ae880042260229201035054310035573c0046aae74004dd50009aba200111300c4901035054310035573c0046aae74004dd500098041112a999ab9a3370e9000000889805248103505433001155333573466e200052000113300333702900000119b814800000444ca00266e1000c00666e1000800466008004002600e444aa666ae68cdc3a400000222004226600600266e180080048c88c008dd60009803911999aab9f00128001400cc010d5d08014c00cd5d1001200040024646464aa666ae68cdc3a4000004230021155333573466e1d200200211800089803a481035054310035573c0046aae74004dd5000911919192a999ab9a3370e900000108c0084554ccd5cd19b874800800846002600a6ae8400422aa666ae68cdc3a400800423004113007491035054310035573c0046aae74004dd500091919192a999ab9a3370e900000108c004dd71aba10019bad357426ae8800422600a9201035054310035573c0046aae74004dd5000919319ab9c0018001191800800918011198010010009"
v3DoubleEncodedBytes = Base16.decodeLenient v3DoubleEncodedAlwaysSucceedsPlutusScriptHex
in return . PlutusScript PlutusScriptV3 . PlutusScriptSerialised $ SBS.toShort v3DoubleEncodedBytes

genScriptDataSchema :: Gen ScriptDataJsonSchema
genScriptDataSchema = Gen.element [ScriptDataJsonNoSchema, ScriptDataJsonDetailedSchema]
Expand Down
32 changes: 32 additions & 0 deletions cardano-api/test/cardano-api-test/Test/Cardano/Api/CBOR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,26 @@ prop_roundtrip_script_PlutusScriptV2_CBOR = H.property $ do
x <- H.forAll $ genScript (PlutusScriptLanguage PlutusScriptV2)
H.trippingCbor (AsScript AsPlutusScriptV2) x

prop_roundtrip_script_PlutusScriptV3_CBOR :: Property
prop_roundtrip_script_PlutusScriptV3_CBOR = H.property $ do
x <- H.forAll genPlutusV3Script
H.trippingCbor (AsScript AsPlutusScriptV3) x

prop_roundtrip_plutus_script_PlutusScriptV1_CBOR :: Property
prop_roundtrip_plutus_script_PlutusScriptV1_CBOR = H.property $ do
PlutusScript _ x <- H.forAll genPlutusV1Script
H.trippingCbor (AsPlutusScript AsPlutusScriptV1) x

prop_roundtrip_plutus_script_PlutusScriptV2_CBOR :: Property
prop_roundtrip_plutus_script_PlutusScriptV2_CBOR = H.property $ do
PlutusScript _ x <- H.forAll genPlutusV2Script
H.trippingCbor (AsPlutusScript AsPlutusScriptV2) x

prop_roundtrip_plutus_script_PlutusScriptV3_CBOR :: Property
prop_roundtrip_plutus_script_PlutusScriptV3_CBOR = H.property $ do
PlutusScript _ x <- H.forAll genPlutusV3Script
H.trippingCbor (AsPlutusScript AsPlutusScriptV3) x

prop_roundtrip_ScriptData_CBOR :: Property
prop_roundtrip_ScriptData_CBOR = H.property $ do
x <- H.forAll genHashableScriptData
Expand Down Expand Up @@ -307,6 +327,18 @@ tests =
, testProperty
"roundtrip script PlutusScriptV2 CBOR"
prop_roundtrip_script_PlutusScriptV2_CBOR
, testProperty
"roundtrip script PlutusScriptV3 CBOR"
prop_roundtrip_script_PlutusScriptV3_CBOR
, testProperty
"roundtrip plutus script PlutusScriptV1 CBOR"
prop_roundtrip_plutus_script_PlutusScriptV1_CBOR
, testProperty
"roundtrip plutus script PlutusScriptV2 CBOR"
prop_roundtrip_plutus_script_PlutusScriptV2_CBOR
, testProperty
"roundtrip plutus script PlutusScriptV3 CBOR"
prop_roundtrip_plutus_script_PlutusScriptV3_CBOR
, testProperty
"cddlTypeToEra for Tx types"
prop_Tx_cddlTypeToEra
Expand Down

0 comments on commit 9a0fbb4

Please sign in to comment.