-
Notifications
You must be signed in to change notification settings - Fork 212
/
GenSpec.hs
1745 lines (1588 loc) · 62.6 KB
/
GenSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_GHC -fno-warn-overlapping-patterns #-}
module Cardano.Api.GenSpec (spec) where
import Prelude
import Cardano.Api
( AddressInEra (..)
, AddressTypeInEra (..)
, AnyCardanoEra (..)
, AnyPlutusScriptVersion
, AnyScriptLanguage (..)
, AssetId (..)
, AssetName (..)
, BuildTx
, BuildTxWith (..)
, ByronEra
, CardanoEra (..)
, CardanoEraStyle (..)
, CostModel (..)
, EpochNo (..)
, ExecutionUnitPrices (..)
, ExecutionUnits (..)
, KeyWitnessInCtx (..)
, Lovelace
, MIRTarget (..)
, NetworkId (..)
, NetworkMagic (..)
, PaymentCredential (..)
, Quantity (..)
, ScriptValidity (..)
, ShelleyWitnessSigningKey (..)
, SimpleScript (..)
, SlotNo (..)
, StakeAddressReference (..)
, TxAuxScripts (..)
, TxCertificates (..)
, TxExtraKeyWitnesses (..)
, TxFee (..)
, TxIn (..)
, TxInsCollateral (..)
, TxIx (..)
, TxMetadata (..)
, TxMetadataInEra (..)
, TxMetadataValue (..)
, TxMintValue (..)
, TxOut (..)
, TxOutDatum (..)
, TxOutValue (..)
, TxScriptValidity (..)
, TxUpdateProposal (..)
, TxValidityLowerBound (..)
, TxValidityUpperBound (..)
, TxWithdrawals (..)
, UpdateProposal (..)
, Value
, WitCtxStake
, Witness (..)
, auxScriptsSupportedInEra
, cardanoEraStyle
, certificatesSupportedInEra
, collateralSupportedInEra
, extraKeyWitnessesSupportedInEra
, multiAssetSupportedInEra
, scriptDataSupportedInEra
, scriptLanguageSupportedInEra
, txFeesExplicitInEra
, txMetadataSupportedInEra
, txScriptValiditySupportedInCardanoEra
, updateProposalSupportedInEra
, validityLowerBoundSupportedInEra
, validityUpperBoundSupportedInEra
, valueToList
, withdrawalsSupportedInEra
)
import Cardano.Api.Byron
( KeyWitness (ByronKeyWitness), WitnessNetworkIdOrByronAddress (..) )
import Cardano.Api.Gen
( genAddressInEra
, genAlphaNum
, genAssetName
, genByronKeyWitness
, genCertIx
, genCostModel
, genCostModels
, genEpochNo
, genExecutionUnitPrices
, genExecutionUnits
, genExtraKeyWitnesses
, genLovelace
, genMIRPot
, genMIRTarget
, genNat
, genNetworkId
, genNetworkMagic
, genPaymentCredential
, genPtr
, genRational
, genRationalInt64
, genScriptValidity
, genShelleyWitnessSigningKey
, genSignedQuantity
, genSimpleScript
, genSlotNo
, genSlotNo32
, genStakeAddressReference
, genStakeCredential
, genTxAuxScripts
, genTxBody
, genTxCertificate
, genTxCertificates
, genTxFee
, genTxIn
, genTxIndex
, genTxInsCollateral
, genTxIx
, genTxMetadata
, genTxMetadataInEra
, genTxMetadataValue
, genTxMintValue
, genTxOut
, genTxOutDatum
, genTxOutValue
, genTxScriptValidity
, genTxValidityLowerBound
, genTxValidityRange
, genTxValidityUpperBound
, genTxWithdrawals
, genUnsignedQuantity
, genUpdateProposal
, genValueForMinting
, genValueForTxOut
, genWitnessNetworkIdOrByronAddress
, genWitnessStake
, genWitnesses
)
import Cardano.Api.Shelley
( Certificate (..)
, ReferenceScript (..)
, StakeCredential (..)
, refInsScriptsAndInlineDatsSupportedInEra
)
import Cardano.Chain.UTxO
( TxInWitness (..) )
import Cardano.Ledger.Credential.Safe
( Ptr, SlotNo32, safePtr, safeUnwrapPtr )
import Cardano.Ledger.Shelley.API
( MIRPot (..) )
import Data.Char
( isAlphaNum, isDigit, isLower, isUpper )
import Data.Foldable
( traverse_ )
import Data.Function
( (&) )
import Data.Int
( Int32 )
import Data.List
( (\\) )
import Data.Map.Strict
( Map )
import Data.Ratio
( denominator, numerator )
import Data.Word
( Word32, Word64, Word8 )
import Numeric.Natural
( Natural )
import Test.Hspec
( Spec, describe, it )
import Test.QuickCheck
( Arbitrary
, Property
, arbitrary
, checkCoverage
, conjoin
, counterexample
, cover
, forAll
, label
, property
, (===)
)
import Test.QuickCheck.Instances.ByteString
()
import qualified Cardano.Ledger.Alonzo.Scripts as Ledger
import qualified Cardano.Ledger.BaseTypes as Ledger
( CertIx (..), TxIx (..) )
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as B8
import qualified Data.Map.Strict as Map
import qualified Test.Cardano.Ledger.Alonzo.PlutusScripts as Plutus
spec :: Spec
spec =
describe "Cardano.Api.Gen" $
describe "Generator coverage" $ do
it "genTxIx" $
property genTxIxCoverage
it "genTxIn" $
property genTxInCoverage
describe "genTxInsCollateral" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genTxInsCollateral era)
$ genTxInCollateralCoverage era
it "genSlotNo" $
property genSlotNoCoverage
it "genLovelace" $
property genLovelaceCoverage
describe "genTxFee" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genTxFee era)
$ genTxFeeCoverage era
it "genTtl" $
property genTtlCoverage
describe "genTxValidityLowerBound" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genTxValidityLowerBound era)
$ genTxValidityLowerBoundCoverage era
describe "genTxValidityUpperBound" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genTxValidityUpperBound era)
$ genTxValidityUpperBoundCoverage era
describe "genTxValidityRangeBound" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genTxValidityRange era)
$ genTxValidityRangeCoverage era
it "genScriptValidity" $
property genScriptValidityCoverage
describe "genTxScriptValidity" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genTxScriptValidity era)
$ genTxScriptValidityCoverage era
describe "genExtraKeyWitnesses" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genExtraKeyWitnesses era)
$ genExtraKeyWitnessesCoverage era
it "genSimpleScriptV1" $
property genSimpleScriptCoverageV1
it "genSimpleScript" $
property genSimpleScriptCoverageV2
it "genAssetName" $
property genAssetNameCoverage
it "genAlphaNum" $
property genAlphaNumCoverage
it "genValueForMinting" $
property
$ forAll genValueForMinting genValueForMintingCoverage
it "genSignedQuantity" $
property genSignedQuantityCoverage
describe "genTxMintValue" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genTxMintValue era)
$ genTxMintValueCoverage era
it "genNetworkMagic" $
property genNetworkMagicCoverage
it "genNetworkId" $
property genNetworkIdCoverage
it "genStakeCredential" $
property genStakeCredentialCoverage
-- it "genScriptDataCoverage" $
-- property genScriptDataCoverage
it "genExecutionUnits" $
property genExecutionUnitsCoverage
describe "genWitnessStake" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genWitnessStake era)
$ genWitnessStakeCoverage era
describe "genTxWithdrawals" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genTxWithdrawals era)
$ genTxWithdrawalsCoverage era
describe "genTxAuxScripts" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genTxAuxScripts era)
$ genTxAuxScriptsCoverage era
it "genTxMetadataValue" $
property genTxMetadataValueCoverage
it "genTxMetadata" $
property genTxMetadataCoverage
describe "genTxMetadataInEra" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genTxMetadataInEra era)
$ genTxMetadataInEraCoverage era
it "genTxIx" $
-- NOTE: can't use Arbitrary here because Ix is a type synonym
property (forAll genTxIx genTxIxCoverage')
it "genPtrCoverage" $
property genPtrCoverage
it "prop_safePtr_safeUnwrapPtr" $
property prop_safePtr_safeUnwrapPtr
it "genStakeAddressReference" $
property genStakeAddressReferenceCoverage
it "genPaymentCredential" $
property genPaymentCredentialCoverage
describe "genAddressInEra" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property $ forAll
(genAddressInEra era)
(genAddressInEraCoverage era)
it "genUnsignedQuantity" $
property $
forAll genUnsignedQuantity genUnsignedQuantityCoverage
it "genValueForTxOutCoverage" $
property $ forAll genValueForTxOut genValueForTxOutCoverage
describe "genTxOutDatum" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property $ forAll
(genTxOutDatum era)
(genTxOutDatumCoverage era)
describe "genTxOutValue" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property $ forAll
(genTxOutValue era)
(genTxOutValueCoverage era)
describe "genTxOut" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property $ forAll
(genTxOut era)
(genTxOutCoverage era)
it "genWitnessNetworkIdOrByronAddress" $
property genWitnessNetworkIdOrByronAddressCoverage
it "genByronKeyWitness" $
property $ forAll genByronKeyWitness genByronKeyWitnessCoverage
it "genShelleyWitnessSigningKey" $
property genShelleyWitnessSigningKeyCoverage
describe "genWitnesses" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property
$ forAll (genTxBody era)
$ \body -> forAll (genWitnesses era body)
$ genWitnessesCoverage era
-- describe "genTxBodyContent" $ do
-- it "ByronEra" $
-- property
-- $ forAll (genTxBodyContent ByronEra)
-- $ genTxBodyContentCoverage ByronEra
-- it "ShelleyEra" $
-- property
-- $ forAll (genTxBodyContent ShelleyEra)
-- $ genTxBodyContentCoverage ShelleyEra
-- it "AllegraEra" $
-- property
-- $ forAll (genTxBodyContent AllegraEra)
-- $ genTxBodyContentCoverage AllegraEra
-- it "MaryEra" $
-- property
-- $ forAll (genTxBodyContent MaryEra)
-- $ genTxBodyContentCoverage MaryEra
-- it "AlonzoEra" $
-- property
-- $ forAll (genTxBodyContent AlonzoEra)
-- $ genTxBodyContentCoverage AlonzoEra
-- describe "genTx" $ do
-- it "ByronEra" $
-- property
-- $ forAll (genTx ByronEra)
-- $ genTxCoverage ByronEra
-- it "ShelleyEra" $
-- property
-- $ forAll (genTx ShelleyEra)
-- $ genTxCoverage ShelleyEra
-- it "AllegraEra" $
-- property
-- $ forAll (genTx AllegraEra)
-- $ genTxCoverage AllegraEra
-- it "MaryEra" $
-- property
-- $ forAll (genTx MaryEra)
-- $ genTxCoverage MaryEra
-- it "AlonzoEra" $
-- property
-- $ forAll (genTx AlonzoEra)
-- $ genTxCoverage AlonzoEra
it "genNat" $
property genNatCoverage
it "genRationalCoverage" $
property $ forAll genRational genRationalCoverage
it "genRationalInt64Coverage" $
property $ forAll genRationalInt64 genRationalInt64Coverage
it "genEpochNo" $
property genEpochNoCoverage
it "genCostModel" $
property genCostModelCoverage
it "genCostModels" $
property $ forAll genCostModels genCostModelsCoverage
it "genExecutionUnitPrices" $
property genExecutionUnitPricesCoverage
-- it "genProtocolParameters" $
-- property genProtocolParametersCoverage
it "genMIRPot" $
property genMIRPotCoverage
it "genMIRTarget" $
property genMIRTargetCoverage
it "genTxCertificate" $
property genTxCertificateCoverage
describe "genTxCertificates" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property $ forAll
(genTxCertificates era)
(genTxCertificatesCoverage era)
describe "genUpdateProposal" $
forAllEras $ \(AnyCardanoEra era) ->
it (show era) $
property $ forAll
(genUpdateProposal era)
(genUpdateProposalCoverage era)
genTxIxCoverage :: TxIx -> Property
genTxIxCoverage (TxIx ix) = unsignedCoverage (6 * maxBound @Word8) "txIx" ix
instance Arbitrary TxIx where
arbitrary = genTxIndex
genTxIxCoverage' :: Ledger.TxIx -> Property
genTxIxCoverage' (Ledger.TxIx ix) =
unsignedCoverage (maxBound @Word32 - 1024) "txIx" ix
instance Arbitrary Ledger.TxIx where
arbitrary = genTxIx
genCertIxCoverage :: Ledger.CertIx -> Property
genCertIxCoverage (Ledger.CertIx ix) =
unsignedCoverage (maxBound @Word32 - 1024) "certIx" ix
instance Arbitrary Ledger.CertIx where
arbitrary = genCertIx
genTxInCoverage :: TxIn -> Property
genTxInCoverage (TxIn _id ix) =
-- We don't provide any coverage for genShelleyHash, and so we don't provide
-- any coverage for txId either (as txId consists of a shelleyHash).
genTxIxCoverage ix
instance Arbitrary TxIn where
arbitrary = genTxIn
genTxInCollateralCoverage :: CardanoEra era -> TxInsCollateral era -> Property
genTxInCollateralCoverage era collateral =
case collateralSupportedInEra era of
Nothing ->
collateral == TxInsCollateralNone
& label ("collateral is never generated in " <> show era)
& counterexample ("collateral was generated in " <> show era)
Just _ ->
checkCoverage
$ cover 10 (hasNoCollateral collateral)
"no collateral"
$ cover 10 (hasSomeCollateral collateral)
"some collateral"
$ cover 2 (collateralLength collateral == Just 0)
"list of zero collateral"
$ cover 10 (collateralLength collateral > Just 0)
"list of more than zero collateral"
$ cover 10 (collateralLength collateral > Just 3)
"list of more than three collateral"
True
where
hasNoCollateral = (== TxInsCollateralNone)
hasSomeCollateral = \case
TxInsCollateralNone -> False
TxInsCollateral _ _ -> True
collateralLength = \case
TxInsCollateralNone -> Nothing
TxInsCollateral _ cs -> Just $ length cs
genSlotNoCoverage :: SlotNo -> Property
genSlotNoCoverage = unsignedCoverage (maxBound @Word64 - 1000) "slot number"
genSlotNo32Coverage :: SlotNo32 -> Property
genSlotNo32Coverage =
unsignedCoverage (maxBound @Word32 - 10000) "slot number (32-bit)"
instance Arbitrary SlotNo where
arbitrary = genSlotNo
genLovelaceCoverage :: Lovelace -> Property
genLovelaceCoverage = unsignedCoverage (maxBound @Word32) "lovelace"
instance Arbitrary Lovelace where
arbitrary = genLovelace
genTxFeeCoverage :: CardanoEra era -> TxFee era -> Property
genTxFeeCoverage era fee =
case txFeesExplicitInEra era of
Left implicit ->
fee == TxFeeImplicit implicit
& label ("fee in " <> show era <> " is always implicit")
& counterexample ( "fee in era "
<> show era
<> " wasn't implicit but should be"
)
Right _ ->
case fee of
TxFeeImplicit _ ->
error "fees are explicit in era but received implicit fee"
TxFeeExplicit _ l ->
genLovelaceCoverage l
genTtlCoverage :: SlotNo -> Property
genTtlCoverage = genSlotNoCoverage
genTxValidityLowerBoundCoverage
:: CardanoEra era -> TxValidityLowerBound era -> Property
genTxValidityLowerBoundCoverage era validFrom =
case validityLowerBoundSupportedInEra era of
Nothing ->
validFrom == TxValidityNoLowerBound
& label ("validity lower bound not supported in " <> show era)
& counterexample ( "validity lower bound shouldn't be supported in "
<> show era
)
Just _ ->
case validFrom of
TxValidityNoLowerBound ->
False
& counterexample ( "validity lower bound supported in "
<> show era
<> ", should have lower bound")
TxValidityLowerBound _ ttl ->
genTtlCoverage ttl
genTxValidityUpperBoundCoverage
:: CardanoEra era -> TxValidityUpperBound era -> Property
genTxValidityUpperBoundCoverage era validFrom =
case validityUpperBoundSupportedInEra era of
Nothing ->
case validFrom of
(TxValidityNoUpperBound _) ->
True
& label ( "validity upper bound not supported in "
<> show era
)
& counterexample
( "validity upper bound shouldn't be supported in "
<> show era
)
(TxValidityUpperBound _ _) ->
error ( "validity upper bound not supported in "
<> show era
<> ", no upper bound should be generated."
)
Just _ ->
case validFrom of
TxValidityNoUpperBound _ ->
False
& counterexample ( "validity upper bound supported in "
<> show era
<> ", should have upper bound")
TxValidityUpperBound _ ttl ->
genTtlCoverage ttl
genTxValidityRangeCoverage
:: CardanoEra era
-> (TxValidityLowerBound era, TxValidityUpperBound era)
-> Property
genTxValidityRangeCoverage era (lower, upper) = checkCoverage $ conjoin
[ genTxValidityLowerBoundCoverage era lower
, genTxValidityUpperBoundCoverage era upper
]
genScriptValidityCoverage :: ScriptValidity -> Property
genScriptValidityCoverage scriptValidity = checkCoverage
$ cover 40 (scriptValidity == ScriptInvalid)
"script is invalid"
$ cover 40 (scriptValidity == ScriptValid)
"script is valid"
True
instance Arbitrary ScriptValidity where
arbitrary = genScriptValidity
genTxScriptValidityCoverage :: CardanoEra era -> TxScriptValidity era -> Property
genTxScriptValidityCoverage era scriptValidity =
case txScriptValiditySupportedInCardanoEra era of
Nothing ->
scriptValidity == TxScriptValidityNone
& label "script validity is always none in eras it is not supported"
& counterexample
("script validity was generated in unsupported " <> show era)
Just _ ->
case scriptValidity of
TxScriptValidityNone ->
False
& counterexample
(show era <> "era should have script validity")
TxScriptValidity _ validity ->
genScriptValidityCoverage validity
genExtraKeyWitnessesCoverage
:: CardanoEra era -> TxExtraKeyWitnesses era -> Property
genExtraKeyWitnessesCoverage era ws =
case extraKeyWitnessesSupportedInEra era of
Nothing ->
label "extra key witnesses are not generated in unsupported eras"
(noWitnesses ws)
& counterexample ( "key witnesses were generated in unsupported "
<> show era
)
Just _ -> checkCoverage
$ cover 10 (noWitnesses ws) "no witnesses"
$ cover 10 (witnesses ws) "witnesses"
$ case ws of
TxExtraKeyWitnessesNone -> property True
(TxExtraKeyWitnesses _ wits) -> checkCoverage
$ cover 1 (null wits) "empty witneses"
$ cover 30 (not (null wits)) "some witnesses"
$ cover 10 (length wits > 3) "> 3 witnesses" True
where
noWitnesses = (== TxExtraKeyWitnessesNone)
witnesses = \case
TxExtraKeyWitnessesNone -> False
TxExtraKeyWitnesses _ _ -> True
genSimpleScriptCoverageV1 :: Property
genSimpleScriptCoverageV1 =
forAll genSimpleScript $ \script ->
checkCoverage
$ cover 10 (requiresSignature script)
"script has \"require signature\""
$ cover 10 (requiresAllOf script)
"script has \"require all of\""
$ cover 10 (requiresAnyOf script)
"script has \"require any of\""
$ cover 10 (requiresMOf script)
"script has \"require M of\""
$ cover 10 (hasOnlyNonRecursiveScriptPrimitive script)
"script has only non-recursive script primitives (sig, timeBefore, timeAfter)"
$ cover 10 (not (hasOnlyNonRecursiveScriptPrimitive script))
"script has recursive script primitives (allOf, anyOf, mOf)"
True
genSimpleScriptCoverageV2 :: Property
genSimpleScriptCoverageV2 =
forAll genSimpleScript $ \script ->
checkCoverage
$ cover 10 (requiresSignature script)
"script has \"require signature\""
$ cover 10 (requiresAllOf script)
"script has \"require all of\""
$ cover 10 (requiresAnyOf script)
"script has \"require any of\""
$ cover 10 (requiresMOf script)
"script has \"require M of\""
$ cover 10 (requiresTimeBefore script)
"script has \"time before\""
$ cover 10 (requiresTimeAfter script)
"script has \"time after\""
$ cover 10 (hasOnlyNonRecursiveScriptPrimitive script)
"script has only non-recursive script primitives (sig, timeBefore, timeAfter)"
$ cover 10 (not (hasOnlyNonRecursiveScriptPrimitive script))
"script has recursive script primitives (allOf, anyOf, mOf)"
True
hasOnlyNonRecursiveScriptPrimitive :: SimpleScript -> Bool
hasOnlyNonRecursiveScriptPrimitive = \case
(RequireSignature _) -> True
(RequireTimeBefore _) -> True
(RequireTimeAfter _) -> True
(RequireAllOf _) -> False
(RequireAnyOf _) -> False
(RequireMOf _ _) -> False
-- Returns true if any part of the script might require a signature.
requiresSignature :: SimpleScript -> Bool
requiresSignature = \case
(RequireSignature _) -> True
(RequireTimeBefore _) -> False
(RequireTimeAfter _) -> False
(RequireAllOf ss) -> any requiresSignature ss
(RequireAnyOf ss) -> any requiresSignature ss
(RequireMOf _ ss) -> any requiresSignature ss
-- Returns true if any part of the script requires "time before".
requiresTimeBefore :: SimpleScript -> Bool
requiresTimeBefore = \case
(RequireSignature _) -> False
(RequireTimeBefore _) -> True
(RequireTimeAfter _) -> False
(RequireAllOf ss) -> any requiresTimeBefore ss
(RequireAnyOf ss) -> any requiresTimeBefore ss
(RequireMOf _ ss) -> any requiresTimeBefore ss
-- Returns true if any part of the script requires "time after".
requiresTimeAfter :: SimpleScript -> Bool
requiresTimeAfter = \case
(RequireSignature _) -> False
(RequireTimeBefore _) -> False
(RequireTimeAfter _) -> True
(RequireAllOf ss) -> any requiresTimeBefore ss
(RequireAnyOf ss) -> any requiresTimeBefore ss
(RequireMOf _ ss) -> any requiresTimeBefore ss
requiresAllOf :: SimpleScript -> Bool
requiresAllOf = \case
(RequireSignature _) -> False
(RequireTimeBefore _) -> False
(RequireTimeAfter _) -> False
(RequireAllOf _) -> True
(RequireAnyOf ss) -> any requiresAllOf ss
(RequireMOf _ ss) -> any requiresAllOf ss
requiresAnyOf :: SimpleScript -> Bool
requiresAnyOf = \case
(RequireSignature _) -> False
(RequireTimeBefore _) -> False
(RequireTimeAfter _) -> False
(RequireAllOf ss) -> any requiresAnyOf ss
(RequireAnyOf _) -> True
(RequireMOf _ ss) -> any requiresAnyOf ss
requiresMOf :: SimpleScript -> Bool
requiresMOf = \case
(RequireSignature _) -> False
(RequireTimeBefore _) -> False
(RequireTimeAfter _) -> False
(RequireAllOf ss) -> any requiresMOf ss
(RequireAnyOf ss) -> any requiresMOf ss
(RequireMOf _ _) -> True
genAssetNameCoverage :: AssetName -> Property
genAssetNameCoverage n = checkCoverage
$ cover 1 (assetNameLen == 0)
"asset name is empty"
$ cover 0.1 (assetNameLen == shortLength)
"asset name is short"
$ cover 5 (assetNameLen == longLength)
"asset name is long"
$ cover 5 (assetNameLen > shortLength && assetNameLen < longLength)
"asset name is between short and long"
$ label "is alphanumeric" (all isAlphaNum assetNameStr)
& counterexample "character wasn't alphabetic or numeric"
where
assetNameStr = (\(AssetName n') -> B8.unpack n') n
assetNameLen = (\(AssetName n') -> BS.length n') n
shortLength = 1
longLength = 32
instance Arbitrary AssetName where
arbitrary = genAssetName
genAlphaNumCoverage :: Property
genAlphaNumCoverage = forAll genAlphaNum $ \c ->
checkCoverage
$ cover 10 (isDigit c)
"character is digit"
$ cover 10 (isUpper c)
"character is upper-case alphabetic"
$ cover 10 (isLower c)
"character is lower-case alphabetic"
$ label "character is alphabetic or numeric" (isAlphaNum c)
& counterexample "character wasn't alphabetic or numeric"
genValueForMintingCoverage :: Value -> Property
genValueForMintingCoverage val = checkCoverage
$ cover 10 (hasMintingValue val)
"minting assets"
$ cover 10 (hasBurningValue val)
"burning assets"
$ conjoin
[ label "no empty mint/burn" (not $ hasZeroValue val)
& counterexample "shouldn't generate a zero mint/burn value"
, label "is never ADA value (can't mint ADA)" (hasNoAdaValue val)
& counterexample "generated ADA mint (you can't mint ADA!)"
]
where
hasNoAdaValue = all ((/= AdaAssetId) . fst) . valueToList
hasMintingValue = any ((> 0) . snd) . valueToList
hasBurningValue = any ((< 0) . snd) . valueToList
hasZeroValue = any ((== 0) . snd) . valueToList
genSignedQuantityCoverage :: Quantity -> Property
genSignedQuantityCoverage = signedCoverage "quantity"
instance Arbitrary Quantity where
arbitrary = genSignedQuantity
genTxMintValueCoverage :: CardanoEra era -> TxMintValue build era -> Property
genTxMintValueCoverage era val =
case multiAssetSupportedInEra era of
Left _ ->
label "mint values are not generated in unsupported eras"
(val == TxMintNone)
& counterexample ( "a mint value was generated in an unsupported "
<> show era
)
Right _ ->
checkCoverage
$ cover 40 (noMint val)
"no mint"
$ cover 40 (someMint val)
"mint"
True
where
noMint = (== TxMintNone)
someMint = \case
TxMintNone -> False
TxMintValue {} -> True
genNetworkMagicCoverage :: NetworkMagic -> Property
genNetworkMagicCoverage (NetworkMagic n) =
unsignedCoverage (maxBound :: Int32) "network magic" n
instance Arbitrary NetworkMagic where
arbitrary = genNetworkMagic
genNetworkIdCoverage :: NetworkId -> Property
genNetworkIdCoverage n = checkCoverage
$ cover 10 (isMainnet n)
"network is mainnet"
$ cover 1 (isTestnet n)
"network is testnet"
True
where
isMainnet = (== Mainnet)
isTestnet = \case
Mainnet -> False
Testnet _ -> True
instance Arbitrary NetworkId where
arbitrary = genNetworkId
genStakeCredentialCoverage :: StakeCredential -> Property
genStakeCredentialCoverage sc = checkCoverage
$ cover 10 (isByKey sc)
"stake credential built from key"
$ cover 10 (isByScript sc)
"stake credential built from script"
True
where
isByKey = \case
StakeCredentialByKey _ -> True
StakeCredentialByScript _ -> False
isByScript = \case
StakeCredentialByKey _ -> False
StakeCredentialByScript _ -> True
instance Arbitrary StakeCredential where
arbitrary = genStakeCredential
-- genScriptDataNumberCoverage :: Integer -> Property
-- genScriptDataNumberCoverage n = checkCoverage
-- $ cover 1 (n == 0)
-- "number is equal to 0"
-- $ cover 2 (n >= veryLargeNumber)
-- "number is very large"
-- $ cover 2 (n <= verySmallNumber)
-- "number is very small"
-- $ cover 10 (n > verySmallNumber && n < veryLargeNumber)
-- "number is between very small and very large"
-- True
-- where
-- verySmallNumber = fromIntegral (minBound :: Int32)
-- veryLargeNumber = fromIntegral (maxBound :: Int32)
-- genScriptDataBytesCoverage :: ByteString -> Property
-- genScriptDataBytesCoverage bs = checkCoverage
-- $ cover 1 (BS.length bs == 0)
-- "no bytes"
-- $ cover 10 (BS.length bs > 0)
-- "some bytes"
-- $ cover 2 (BS.length bs > 32)
-- "lots of bytes"
-- True
-- genScriptDataListCoverage :: [ScriptData] -> Property
-- genScriptDataListCoverage ss = checkCoverage
-- $ cover 1 (null ss)
-- "no scripts in list"
-- $ cover 10 (not $ null ss)
-- "some scripts in list"
-- $ cover 10 (length ss > 32)
-- "lots of scripts in list"
-- $ conjoin
-- $ fmap genScriptDataCoverage ss
-- genScriptDataMapCoverage :: [(ScriptData, ScriptData)] -> Property
-- genScriptDataMapCoverage ss = checkCoverage
-- $ cover 1 (null ss)
-- "no scripts in map"
-- $ cover 10 (not $ null ss)
-- "some scripts in map"
-- $ cover 10 (length ss > 32)
-- "lots of scripts in map"
-- $ conjoin
-- $ fmap (\(k, v) ->
-- conjoin [genScriptDataCoverage k, genScriptDataCoverage v]
-- ) ss
-- genScriptDataConstructorCoverage :: (Integer, [ScriptData]) -> Property
-- genScriptDataConstructorCoverage (ix, ss) = checkCoverage
-- $ cover 1 (null ss)
-- "no scripts in constr"
-- $ cover 10 (not $ null ss)
-- "some scripts in constr"
-- $ cover 10 (length ss > 3)
-- "lots of scripts in constr"
-- $ cover 1 (ix == 0)
-- "ix == 0"
-- $ cover 10 (ix > 0)
-- "ix > 0"
-- $ cover 10 (ix > 3)
-- "ix > 3"
-- True
-- genScriptDataCoverage :: ScriptData -> Property
-- genScriptDataCoverage dat = checkCoverage $ conjoin
-- [ checkCoverage
-- $ case dat of
-- ScriptDataNumber _ -> cover 5 True "is script data number"
-- ScriptDataBytes _ -> cover 5 True "is script data bytes"
-- ScriptDataList _ -> cover 5 True "is script data list"
-- ScriptDataMap _ -> cover 5 True "is script data map"
-- ScriptDataConstructor _ _ -> cover 5 True "is script data constructor"
-- $ True
-- , case dat of
-- ScriptDataNumber n ->
-- genScriptDataNumberCoverage n
-- ScriptDataBytes bs ->
-- genScriptDataBytesCoverage bs
-- ScriptDataList ss ->
-- genScriptDataListCoverage ss
-- ScriptDataMap ss ->
-- genScriptDataMapCoverage ss
-- ScriptDataConstructor n ss ->
-- genScriptDataConstructorCoverage (n, ss)
-- ]
-- instance Arbitrary ScriptData where
-- arbitrary = genScriptData
genExecutionUnitsCoverage :: ExecutionUnits -> Property
genExecutionUnitsCoverage (ExecutionUnits steps mem) = checkCoverage $ conjoin
[ unsignedCoverage (maxBound @Word32) "execution steps" steps
, unsignedCoverage (maxBound @Word32) "execution mem" mem
]
instance Arbitrary ExecutionUnits where
arbitrary = genExecutionUnits
genWitnessStakeCoverage
:: CardanoEra era
-> Witness WitCtxStake era
-> Property
genWitnessStakeCoverage era wit =
let
supportedLangs = [ ()
| AnyScriptLanguage lang <- [minBound..maxBound]
, Just _langInEra <-