@@ -34,9 +34,19 @@ module Cardano.Ledger.Dijkstra.HuddleSpec (
3434
3535import Cardano.Ledger.Conway.HuddleSpec hiding ()
3636import Cardano.Ledger.Dijkstra (DijkstraEra )
37+ import Cardano.Ledger.Huddle.Gen (genArrayTerm )
38+ import Codec.CBOR.Cuddle.CBOR.Gen (generateFromName )
39+ import Codec.CBOR.Cuddle.CDDL.CBORGenerator (CBORGen , WrappedTerm (.. ), liftAntiGen , withAntiGen )
40+ import Codec.CBOR.Term (Term (.. ))
41+ import Control.Monad (zipWithM )
3742import Data.Proxy (Proxy (.. ))
3843import Data.Text ()
39- import Data.Word (Word64 )
44+ import Data.Text qualified as T
45+ import Data.Word (Word16 , Word64 )
46+ import Test.AntiGen (withAnnotation , (|!) )
47+ import Test.QuickCheck (choose , shuffle )
48+ import Test.QuickCheck qualified as QC
49+ import Test.QuickCheck.GenT (liftGen )
4050import Text.Heredoc
4151import Prelude hiding ((/) )
4252
@@ -854,27 +864,62 @@ instance HuddleRule "header" DijkstraEra where
854864
855865instance HuddleRule " block" DijkstraEra where
856866 huddleRuleNamed pname p =
867+ pname
868+ =.= arr
869+ [ a $ huddleRule @ " header" p
870+ , a $ huddleRule @ " block_body" p
871+ ]
872+
873+ instance HuddleRule " peras_certificate" DijkstraEra where
874+ huddleRuleNamed pname _era = pname =.= VBytes / VNil
875+
876+ instance HuddleRule " invalid_transactions" DijkstraEra where
877+ huddleRuleNamed pname era = pname =.= huddleRule1 @ " nonempty_set" era (huddleRule @ " transaction_index" era)
878+
879+ instance HuddleRule " block_body" DijkstraEra where
880+ huddleRuleNamed pname era =
857881 comment
858- [str |Valid blocks must also satisfy the following two constraints:
859- | 1) the length of transaction_bodies and transaction_witness_sets must be
860- | the same
861- | 2) every transaction_index must be strictly smaller than the length of
862- | transaction_bodies
882+ [str |Note that every transaction_index must be strictly smaller than the length of transaction_bodies
863883 |]
884+ $ withCBORGen blockBodyGen
864885 $ pname
865886 =.= arr
866- [ a $ huddleRule @ " header" p
867- , " transaction_bodies" ==> arr [0 <+ a (huddleRule @ " transaction_body" p)]
868- , " transaction_witness_sets" ==> arr [0 <+ a (huddleRule @ " transaction_witness_set" p)]
869- , " auxiliary_data_set"
870- ==> mp
871- [ 0
872- <+ asKey (huddleRule @ " transaction_index" p)
873- ==> huddleRule @ " auxiliary_data" p
874- ]
875- , " invalid_transactions" ==> arr [0 <+ a (huddleRule @ " transaction_index" p)]
887+ [ " invalid_transactions" ==> huddleRule @ " invalid_transactions" era / VNil
888+ , " transactions" ==> arr [0 <+ a (huddleRule @ " transaction" era)]
889+ , " peras_certificate" ==> huddleRule @ " peras_certificate" era
876890 ]
877891
892+ blockBodyGen :: CBORGen WrappedTerm
893+ blockBodyGen = do
894+ numTxs <- liftGen . QC. sized $ \ s -> choose (0 :: Int , s )
895+ txs <-
896+ mapM
897+ (\ i -> withAntiGen (withAnnotation (T. pack $ show i)) $ generateFromName " transaction" )
898+ [0 .. numTxs - 1 ]
899+ invalidIxIxs <-
900+ if numTxs == 0
901+ then pure []
902+ else do
903+ n <-
904+ liftAntiGen $
905+ choose (0 , numTxs) |! choose (numTxs + 1 , 2 * numTxs)
906+ txIndices <- liftGen $ shuffle [0 .. toInteger numTxs - 1 ]
907+ -- We need this so that a zapped `n` still produces indices
908+ txIndicesOverflow <- liftGen $ shuffle txIndices
909+ let
910+ txIndicesWithOverflow = take n $ txIndices <> txIndicesOverflow
911+ faultyIndex pos i =
912+ withAnnotation (T. pack $ show pos) $
913+ pure i
914+ |! choose (toInteger numTxs + 1 , toInteger $ maxBound @ Word16 )
915+ liftAntiGen $
916+ withAnnotation " invalid_transactions" $
917+ zipWithM faultyIndex [0 :: Int .. ] txIndicesWithOverflow
918+ invalidTxIxsTerm <- genArrayTerm $ TInteger . toInteger <$> invalidIxIxs
919+ txsTerm <- withAntiGen (withAnnotation " transactions" ) $ genArrayTerm txs
920+ perasCertTerm <- generateFromName " peras_certificate"
921+ S <$> liftGen (genArrayTerm [invalidTxIxsTerm, txsTerm, perasCertTerm])
922+
878923instance HuddleRule " auxiliary_scripts" DijkstraEra where
879924 huddleRuleNamed = auxiliaryScriptsRule
880925
0 commit comments