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
1819import Cardano.Api.Shelley (ShelleyLedgerEra )
1920
2021import 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+ )
2227import GHC.Exts (IsList (.. ))
2328
2429import Test.Gen.Cardano.Api.Typed
2530
2631import Test.Cardano.Api.Orphans ()
2732
28- import Hedgehog (MonadTest , Property , (===) )
33+ import Hedgehog
34+ ( MonadTest
35+ , Property
36+ , (===)
37+ )
2938import Hedgehog qualified as H
39+ import Hedgehog.Gen (shuffle )
3040import Test.Tasty (TestTree , testGroup )
3141import 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+
111149tests :: TestTree
112150tests =
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