Skip to content

Commit 585a8f9

Browse files
committed
Add test for estimating key witness count with simple script
1 parent 7f94db2 commit 585a8f9

File tree

2 files changed

+52
-4
lines changed
  • cardano-api

2 files changed

+52
-4
lines changed

cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ module Test.Gen.Cardano.Api.Typed
132132
, genProposals
133133
, genProposal
134134
, genVotingProcedures
135+
, genSimpleScriptWithoutEmptyAnys
135136
)
136137
where
137138

@@ -233,8 +234,14 @@ genScript SimpleScriptLanguage =
233234
genScript (PlutusScriptLanguage lang) =
234235
PlutusScript lang <$> genPlutusScript lang
235236

237+
genSimpleScriptWithoutEmptyAnys :: Gen SimpleScript
238+
genSimpleScriptWithoutEmptyAnys = genRandomSimpleScript False
239+
236240
genSimpleScript :: Gen SimpleScript
237-
genSimpleScript =
241+
genSimpleScript = genRandomSimpleScript True
242+
243+
genRandomSimpleScript :: Bool -> Gen SimpleScript
244+
genRandomSimpleScript hasEmptyAnys=
238245
genTerm
239246
where
240247
genTerm = Gen.recursive Gen.choice nonRecursive recursive
@@ -249,7 +256,7 @@ genSimpleScript =
249256
-- Recursive generators
250257
recursive =
251258
[ RequireAllOf <$> Gen.list (Range.linear 0 10) genTerm
252-
, RequireAnyOf <$> Gen.list (Range.linear 0 10) genTerm
259+
, RequireAnyOf <$> Gen.list (Range.linear (if hasEmptyAnys then 0 else 1) 10) genTerm
253260
, do
254261
ts <- Gen.list (Range.linear 0 10) genTerm
255262
m <- Gen.integral (Range.constant 0 (length ts))

cardano-api/test/cardano-api-test/Test/Cardano/Api/TxBody.hs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE OverloadedLists #-}
55
{-# LANGUAGE RankNTypes #-}
66
{-# LANGUAGE ScopedTypeVariables #-}
7+
{-# LANGUAGE TupleSections #-}
78

89
{- HLINT ignore "Use camelCase" -}
910

@@ -18,15 +19,24 @@ import Cardano.Api.Ledger qualified as L
1819
import Cardano.Api.Shelley (ShelleyLedgerEra)
1920

2021
import Data.Maybe (isJust)
21-
import Data.Type.Equality (TestEquality (testEquality))
22+
import Data.Set (Set)
23+
import Data.Set qualified as Set
24+
import Data.Type.Equality
25+
( TestEquality (testEquality)
26+
)
2227
import GHC.Exts (IsList (..))
2328

2429
import Test.Gen.Cardano.Api.Typed
2530

2631
import Test.Cardano.Api.Orphans ()
2732

28-
import Hedgehog (MonadTest, Property, (===))
33+
import Hedgehog
34+
( MonadTest
35+
, Property
36+
, (===)
37+
)
2938
import Hedgehog qualified as H
39+
import Hedgehog.Gen (shuffle)
3040
import Test.Tasty (TestTree, testGroup)
3141
import Test.Tasty.Hedgehog (testProperty)
3242

@@ -108,6 +118,34 @@ prop_roundtrip_txbodycontent_conway_fields = H.property $ do
108118
getProposalProcedures TxProposalProceduresNone = Nothing
109119
getProposalProcedures (TxProposalProcedures pp) = Just $ fst <$> toList pp
110120

121+
prop_simple_script_witness_count :: Property
122+
prop_simple_script_witness_count = H.property $ do
123+
let sbe = ShelleyBasedEraConway
124+
(_, contentWithoutScript) <- H.forAll $ genValidTxBody sbe
125+
script <- H.forAll genSimpleScriptWithoutEmptyAnys
126+
newTxIn <-
127+
H.forAll $
128+
(,BuildTxWith
129+
( ScriptWitness
130+
ScriptWitnessForSpending
131+
(SimpleScriptWitness SimpleScriptInConway (SScript script))
132+
))
133+
<$> genTxIn
134+
witList <- H.forAll $ satisfyScript script
135+
let witCount = fromIntegral $ Set.size witList
136+
H.diff
137+
(estimateTransactionKeyWitnessCount contentWithoutScript + witCount)
138+
(<=)
139+
(estimateTransactionKeyWitnessCount (addTxIn newTxIn contentWithoutScript))
140+
where
141+
satisfyScript :: SimpleScript -> H.Gen (Set (Hash PaymentKey))
142+
satisfyScript (RequireSignature paymentKeyHash) = return $ Set.singleton paymentKeyHash
143+
satisfyScript (RequireTimeBefore _) = return mempty
144+
satisfyScript (RequireTimeAfter _) = return mempty
145+
satisfyScript (RequireAllOf simpleScripts) = Set.unions <$> traverse satisfyScript simpleScripts
146+
satisfyScript (RequireMOf n simpleScripts) = shuffle simpleScripts >>= satisfyScript . RequireAllOf . take n
147+
satisfyScript (RequireAnyOf simpleScripts) = satisfyScript (RequireMOf 1 simpleScripts)
148+
111149
tests :: TestTree
112150
tests =
113151
testGroup
@@ -119,4 +157,7 @@ tests =
119157
, testProperty
120158
"roundtrip txbodycontent new conway fields"
121159
prop_roundtrip_txbodycontent_conway_fields
160+
, testProperty
161+
"simple script witness count"
162+
prop_simple_script_witness_count
122163
]

0 commit comments

Comments
 (0)