-
Notifications
You must be signed in to change notification settings - Fork 155
/
Instances.hs
1304 lines (1105 loc) · 45.1 KB
/
Instances.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 AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
-- RecordWildCards cause name shadowing warnings in ghc-8.10.
#if __GLASGOW_HASKELL__ < 900
{-# OPTIONS_GHC -Wno-name-shadowing #-}
#endif
-- | This module provides the necessary instances of `HasSpec`
-- and `HasSimpleRep` to write specs for the environments,
-- states, and signals in the conway STS rules.
module Test.Cardano.Ledger.Constrained.Conway.Instances (
ConwayFn,
StringFn,
ProposalTree,
onJust',
onSized,
cKeyHashObj,
cScriptHashObj,
cSNothing_,
cSJust_,
succV_,
strLen_,
pProcGovAction_,
IsConwayUniv,
gasId_,
gasCommitteeVotes_,
gasDRepVotes_,
gasProposalProcedure_,
) where
import Cardano.Chain.Common (
AddrAttributes (..),
AddrType (..),
Address (..),
Address',
Attributes (..),
NetworkMagic (..),
UnparsedFields (..),
)
import Cardano.Crypto.Hash hiding (Blake2b_224)
import Cardano.Crypto.Hashing (AbstractHash, abstractHashFromBytes)
import Cardano.Ledger.Address
import Cardano.Ledger.Allegra.Scripts
import Cardano.Ledger.Alonzo.PParams
import Cardano.Ledger.Alonzo.Scripts (AlonzoScript (..))
import Cardano.Ledger.Alonzo.Tx
import Cardano.Ledger.Alonzo.TxAuxData (AlonzoTxAuxData (..), AuxiliaryDataHash)
import Cardano.Ledger.Alonzo.TxOut
import Cardano.Ledger.Alonzo.TxWits
import Cardano.Ledger.Babbage.TxBody (BabbageTxOut (..))
import Cardano.Ledger.BaseTypes hiding (inject)
import Cardano.Ledger.Binary (Sized (..))
import Cardano.Ledger.CertState
import Cardano.Ledger.Coin
import Cardano.Ledger.Compactible
import Cardano.Ledger.Conway (ConwayEra)
import Cardano.Ledger.Conway.Core
import Cardano.Ledger.Conway.Governance
import Cardano.Ledger.Conway.PParams
import Cardano.Ledger.Conway.Rules
import Cardano.Ledger.Conway.Scripts ()
import Cardano.Ledger.Conway.TxBody
import Cardano.Ledger.Conway.TxCert
import Cardano.Ledger.Credential
import Cardano.Ledger.Crypto (Crypto, StandardCrypto)
import Cardano.Ledger.EpochBoundary
import Cardano.Ledger.HKD
import Cardano.Ledger.Keys (
BootstrapWitness,
GenDelegPair (..),
GenDelegs (..),
KeyHash,
KeyRole (..),
WitVKey,
)
import Cardano.Ledger.Mary.Value (AssetName (..), MaryValue (..), MultiAsset (..), PolicyID (..))
import Cardano.Ledger.MemoBytes
import Cardano.Ledger.Plutus.CostModels
import Cardano.Ledger.Plutus.Data
import Cardano.Ledger.Plutus.ExUnits
import Cardano.Ledger.Plutus.Language
import Cardano.Ledger.PoolDistr
import Cardano.Ledger.PoolParams
import Cardano.Ledger.SafeHash
import Cardano.Ledger.Shelley.LedgerState hiding (ptrMap)
import Cardano.Ledger.Shelley.PoolRank
import Cardano.Ledger.Shelley.Rules
import Cardano.Ledger.Shelley.TxAuxData (Metadatum)
import Cardano.Ledger.TxIn (TxId (..), TxIn (..))
import Cardano.Ledger.UMap
import Cardano.Ledger.UTxO
import Cardano.Ledger.Val (Val)
import Constrained hiding (Value)
import Constrained qualified as C
import Control.Monad.Trans.Fail.String
import Crypto.Hash (Blake2b_224)
import Data.ByteString qualified as BS
import Data.ByteString.Short (ShortByteString)
import Data.ByteString.Short qualified as SBS
import Data.Coerce
import Data.Foldable
import Data.Kind
import Data.Map (Map)
import Data.Map.Strict qualified as Map
import Data.Maybe
import Data.OMap.Strict qualified as OMap
import Data.OSet.Strict qualified as SOS
import Data.Sequence (Seq)
import Data.Sequence qualified as Seq
import Data.Sequence.Strict (StrictSeq)
import Data.Sequence.Strict qualified as StrictSeq
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Tree
import Data.Typeable
import Data.VMap (VMap)
import Data.VMap qualified as VMap
import Data.Word
import Numeric.Natural (Natural)
import PlutusLedgerApi.V1 qualified as PV1
import System.Random
import Test.Cardano.Ledger.Allegra.Arbitrary ()
import Test.Cardano.Ledger.Alonzo.Arbitrary ()
import Test.Cardano.Ledger.Core.Utils
import Test.QuickCheck hiding (Args, Fun, forAll)
type ConwayUnivFns = StringFn : TreeFn : BaseFns
type ConwayFn = Fix (OneofL ConwayUnivFns)
type IsConwayUniv fn =
( BaseUniverse fn
, Member (StringFn fn) fn
, Member (MapFn fn) fn
, Member (FunFn fn) fn
, Member (TreeFn fn) fn
)
-- TxBody HasSpec instance ------------------------------------------------
-- NOTE: this is a representation of the `ConwayTxBody` type. You can't
-- simply use the generics to derive the `SimpleRep` for `ConwayTxBody`
-- because the type is memoized. So instead we say that the representation
-- is the same as what you would get from using the `ConwayTxBody` pattern.
type ConwayTxBodyTypes c =
'[ Set (TxIn (EraCrypto (ConwayEra c)))
, Set (TxIn (EraCrypto (ConwayEra c)))
, Set (TxIn (EraCrypto (ConwayEra c)))
, StrictSeq (Sized (TxOut (ConwayEra c)))
, StrictMaybe (Sized (TxOut (ConwayEra c)))
, StrictMaybe Coin
, SOS.OSet (ConwayTxCert (ConwayEra c))
, Withdrawals (EraCrypto (ConwayEra c))
, Coin
, ValidityInterval
, Set (KeyHash 'Witness (EraCrypto (ConwayEra c)))
, MultiAsset (EraCrypto (ConwayEra c))
, StrictMaybe (ScriptIntegrityHash (EraCrypto (ConwayEra c)))
, StrictMaybe (AuxiliaryDataHash (EraCrypto (ConwayEra c)))
, StrictMaybe Network
, VotingProcedures (ConwayEra c)
, SOS.OSet (ProposalProcedure (ConwayEra c))
, StrictMaybe Coin
, Coin
]
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (ConwayTxBody (ConwayEra c))
instance Crypto c => HasSimpleRep (ConwayTxBody (ConwayEra c)) where
type SimpleRep (ConwayTxBody (ConwayEra c)) = SOP '["ConwayTxBody" ::: ConwayTxBodyTypes c]
toSimpleRep ConwayTxBody {..} =
inject @"ConwayTxBody" @'["ConwayTxBody" ::: ConwayTxBodyTypes c]
ctbSpendInputs
ctbCollateralInputs
ctbReferenceInputs
ctbOutputs
ctbCollateralReturn
ctbTotalCollateral
ctbCerts
ctbWithdrawals
ctbTxfee
ctbVldt
ctbReqSignerHashes
ctbMint
ctbScriptIntegrityHash
ctbAdHash
ctbTxNetworkId
ctbVotingProcedures
ctbProposalProcedures
ctbCurrentTreasuryValue
ctbTreasuryDonation
fromSimpleRep rep =
algebra @'["ConwayTxBody" ::: ConwayTxBodyTypes c] rep ConwayTxBody
instance HasSimpleRep Coin where
type SimpleRep Coin = Word64
toSimpleRep (Coin i) = case integerToWord64 i of
Nothing -> error "The impossible happened in toSimpleRep for `Coin`"
Just w -> w
fromSimpleRep = word64ToCoin
instance IsConwayUniv fn => HasSpec fn Coin
instance IsConwayUniv fn => OrdLike fn Coin
instance IsConwayUniv fn => NumLike fn Coin
instance IsConwayUniv fn => Foldy fn Coin where
genList s s' = map fromSimpleRep <$> genList @fn @Word64 (toSimpleRepSpec s) (toSimpleRepSpec s')
theAddFn = addFn
theZero = Coin 0
-- TODO: This is hack to get around the need for `Num` in `NumLike`. We should possibly split
-- this up so that `NumLike` has its own addition etc. instead?
deriving via Integer instance Num Coin
instance HasSimpleRep DeltaCoin where
type SimpleRep DeltaCoin = Integer
fromSimpleRep = DeltaCoin
toSimpleRep (DeltaCoin c) = c
instance IsConwayUniv fn => HasSpec fn DeltaCoin
instance HasSimpleRep (GovProcedures era)
instance
( Era era
, EraPParams era
, IsConwayUniv fn
, HasSimpleRep (PParamsHKD StrictMaybe era)
, TypeSpec fn (SimpleRep (PParamsHKD StrictMaybe era)) ~ TypeSpec fn (PParamsHKD StrictMaybe era)
, HasSpec fn (SimpleRep (PParamsHKD StrictMaybe era))
) =>
HasSpec fn (GovProcedures era)
instance HasSimpleRep SlotNo
instance IsConwayUniv fn => OrdLike fn SlotNo
instance IsConwayUniv fn => HasSpec fn SlotNo
instance HasSimpleRep EpochNo
instance IsConwayUniv fn => OrdLike fn EpochNo
instance IsConwayUniv fn => HasSpec fn EpochNo
instance HasSimpleRep TxIx
instance IsConwayUniv fn => HasSpec fn TxIx
instance (IsConwayUniv fn, Crypto c, Typeable index) => HasSpec fn (SafeHash c index) where
type TypeSpec fn (SafeHash c index) = ()
emptySpec = ()
combineSpec _ _ = TrueSpec
genFromTypeSpec _ = pureGen arbitrary
shrinkWithTypeSpec _ = shrink
conformsTo _ _ = True
toPreds _ _ = toPred True
instance HasSimpleRep (TxId c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (TxId c)
instance HasSimpleRep (TxIn c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (TxIn c)
instance (IsConwayUniv fn, Typeable r, Crypto c) => HasSpec fn (KeyHash r c) where
type TypeSpec fn (KeyHash r c) = ()
emptySpec = ()
combineSpec _ _ = TrueSpec
genFromTypeSpec _ = pureGen arbitrary
shrinkWithTypeSpec _ = shrink
conformsTo _ _ = True
toPreds _ _ = toPred True
instance HasSimpleRep (StrictSeq a) where
type SimpleRep (StrictSeq a) = [a]
toSimpleRep = toList
fromSimpleRep = StrictSeq.fromList
instance (IsConwayUniv fn, HasSpec fn a) => HasSpec fn (StrictSeq a)
instance HasSimpleRep (Seq a) where
type SimpleRep (Seq a) = [a]
toSimpleRep = toList
fromSimpleRep = Seq.fromList
instance (IsConwayUniv fn, HasSpec fn a) => HasSpec fn (Seq a)
instance HasSimpleRep (StrictMaybe a)
instance HasSpec fn a => HasSpec fn (StrictMaybe a)
cSNothing_ :: HasSpec fn a => Term fn (StrictMaybe a)
cSNothing_ = con @"SNothing" (lit ())
cSJust_ :: HasSpec fn a => Term fn a -> Term fn (StrictMaybe a)
cSJust_ = con @"SJust"
instance HasSimpleRep (Sized a)
instance (IsConwayUniv fn, HasSpec fn a) => HasSpec fn (Sized a)
instance HasSimpleRep Addr28Extra
instance IsConwayUniv fn => HasSpec fn Addr28Extra
instance HasSimpleRep DataHash32
instance IsConwayUniv fn => HasSpec fn DataHash32
type BabbageTxOutTypes era =
'[ Addr (EraCrypto era)
, Value era
, Datum era
, StrictMaybe (Script era)
]
instance (Era era, Val (Value era)) => HasSimpleRep (BabbageTxOut era) where
type SimpleRep (BabbageTxOut era) = SOP '["BabbageTxOut" ::: BabbageTxOutTypes era]
toSimpleRep (BabbageTxOut addr val dat msc) =
inject @"BabbageTxOut" @'["BabbageTxOut" ::: BabbageTxOutTypes era]
addr
val
dat
msc
fromSimpleRep rep =
algebra @'["BabbageTxOut" ::: BabbageTxOutTypes era] rep BabbageTxOut
instance
( IsConwayUniv fn
, HasSpec fn (Value era)
, Era era
, HasSpec fn (Data era)
, Val (Value era)
, Crypto (EraCrypto era)
, HasSpec fn (Script era)
) =>
HasSpec fn (BabbageTxOut era)
instance (Typeable a, Show a, Compactible a) => HasSimpleRep (CompactForm a) where
type SimpleRep (CompactForm a) = a
toSimpleRep = fromCompact
fromSimpleRep rep = case toCompact rep of
Nothing -> error $ "toCompact @" ++ show (typeOf rep) ++ " " ++ show rep
Just c -> c
instance (IsConwayUniv fn, Compactible a, HasSpec fn a) => HasSpec fn (CompactForm a)
instance HasSimpleRep (MaryValue c) where
type SimpleRep (MaryValue c) = SOP '["MaryValue" ::: '[Coin]]
toSimpleRep (MaryValue c _) = c
fromSimpleRep c = MaryValue c mempty
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (MaryValue c)
instance HasSimpleRep PV1.Data
instance IsConwayUniv fn => HasSpec fn PV1.Data where
type TypeSpec fn PV1.Data = ()
emptySpec = ()
combineSpec _ _ = TrueSpec
genFromTypeSpec _ = pureGen arbitrary
shrinkWithTypeSpec _ = shrink
conformsTo _ _ = True
toPreds _ _ = toPred True
instance Era era => HasSimpleRep (Data era) where
type SimpleRep (Data era) = PV1.Data
toSimpleRep = getPlutusData
fromSimpleRep = mkMemoized . PlutusData
instance (IsConwayUniv fn, Era era) => HasSpec fn (Data era)
instance Era era => HasSimpleRep (BinaryData era) where
type SimpleRep (BinaryData era) = Data era
toSimpleRep = binaryDataToData
fromSimpleRep = dataToBinaryData
instance
(IsConwayUniv fn, Era era, Crypto (EraCrypto era), HasSpec fn (Data era)) =>
HasSpec fn (BinaryData era)
instance HasSimpleRep (Datum era)
instance (IsConwayUniv fn, Era era, HasSpec fn (Data era), Crypto (EraCrypto era)) => HasSpec fn (Datum era)
-- TODO: here we are cheating to get out of having to deal with Plutus scripts
instance HasSimpleRep (AlonzoScript era) where
type SimpleRep (AlonzoScript era) = Timelock era
toSimpleRep (TimelockScript tl) = tl
toSimpleRep (PlutusScript _) = error "toSimpleRep for AlonzoScript on a PlutusScript"
fromSimpleRep = TimelockScript
instance (IsConwayUniv fn, EraScript era, Script era ~ AlonzoScript era) => HasSpec fn (AlonzoScript era)
{-
NOTE:
You might think that you could do something like this for `Timelock`.
However, when you do that some questions arise:
(1) How are you going to write constraints over recursive types
that don't blow up to infinity?
(2) How are you going to generate recursive values?
(2) you could imagine solving with some tricks for controlling how we generate
Sum and Prod things (with some global index of sizes: `TypeRep -> Int`). Potentially
you could solve this by having size constraints in the language. There the question is
how you design those constraints - their semantics could be `const True` while still
changing the `Specification` - thus giving you the ability to provide a generation time hint!
Solving (1) is more tricky however. The best guess I have is that you would need
to push any constraint you have into functions `MyConstraint :: MyUniv fn '[Timelock era] Bool`
and implement everything "offline". This is highly non-satisfactory - but it's hard to see
how else you would do it.
type TimelockTypes era =
'[ -- RequireSignature
'[KeyHash 'Witness (EraCrypto era)]
-- RequireAllOf
, '[StrictSeq (Timelock era)]
-- RequireAnyOf
, '[StrictSeq (Timelock era)]
-- RequireMOf
, '[Int, StrictSeq (Timelock era)]
-- RequireTimeExpire
, '[SlotNo]
-- RequireTimeStart
, '[SlotNo]
]
instance Era era => HasSimpleRep (Timelock era) where
type SimpleRep (Timelock era) = SOP (TimelockTypes era)
toSimpleRep (RequireSignature h) = inject @0 @(TimelockTypes era) h
toSimpleRep (RequireAllOf ts) = inject @1 @(TimelockTypes era) ts
toSimpleRep (RequireAnyOf ts) = inject @2 @(TimelockTypes era) ts
toSimpleRep (RequireMOf m ts) = inject @3 @(TimelockTypes era) m ts
toSimpleRep (RequireTimeExpire s) = inject @4 @(TimelockTypes era) s
toSimpleRep (RequireTimeStart s) = inject @5 @(TimelockTypes era) s
fromSimpleRep rep =
algebra @(TimelockTypes era) rep
RequireSignature
RequireAllOf
RequireAnyOf
RequireMOf
RequireTimeExpire
RequireTimeStart
-}
instance (IsConwayUniv fn, Crypto (EraCrypto era), Era era) => HasSpec fn (Timelock era) where
type TypeSpec fn (Timelock era) = ()
emptySpec = ()
combineSpec _ _ = TrueSpec
genFromTypeSpec _ = pureGen arbitrary
shrinkWithTypeSpec _ = shrink
conformsTo _ _ = True
toPreds _ _ = toPred True
instance Crypto c => HasSimpleRep (CompactAddr c) where
type SimpleRep (CompactAddr c) = SimpleRep (Addr c)
toSimpleRep = toSimpleRep . decompactAddr
fromSimpleRep = compactAddr . fromSimpleRep
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (CompactAddr c)
instance HasSimpleRep (Addr c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (Addr c)
instance HasSimpleRep (BootstrapAddress c) where
type
TheSop (BootstrapAddress c) =
'[ "BootstrapAddress"
::: '[ AbstractHash Blake2b_224 Address'
, NetworkMagic
, AddrType
]
]
toSimpleRep (BootstrapAddress (Address root (Attributes (AddrAttributes _ magic) _) typ)) =
inject @"BootstrapAddress" @(TheSop (BootstrapAddress c))
root
magic
typ
fromSimpleRep rep =
algebra @(TheSop (BootstrapAddress c)) rep $
\root magic typ ->
BootstrapAddress
(Address root (Attributes (AddrAttributes Nothing magic) (UnparsedFields mempty)) typ)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (BootstrapAddress c)
instance HasSimpleRep NetworkMagic
instance IsConwayUniv fn => HasSpec fn NetworkMagic
instance HasSimpleRep AddrType
instance IsConwayUniv fn => HasSpec fn AddrType
instance (IsConwayUniv fn, Typeable b) => HasSpec fn (AbstractHash Blake2b_224 b) where
type TypeSpec fn (AbstractHash Blake2b_224 b) = ()
emptySpec = ()
combineSpec _ _ = TrueSpec
genFromTypeSpec _ = do
bytes <- pureGen $ vectorOf 28 arbitrary
pure $ fromJust $ abstractHashFromBytes (BS.pack bytes)
shrinkWithTypeSpec _ _ = []
conformsTo _ _ = True
toPreds _ _ = toPred True
instance HasSimpleRep (StakeReference c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (StakeReference c)
instance HasSimpleRep Ptr
instance IsConwayUniv fn => HasSpec fn Ptr
instance HasSimpleRep CertIx where
type SimpleRep CertIx = Word16
toSimpleRep (CertIx w) = fromIntegral w
fromSimpleRep = mkCertIx
instance IsConwayUniv fn => HasSpec fn CertIx
instance HasSimpleRep (Credential r c)
instance (IsConwayUniv fn, Typeable r, Crypto c) => HasSpec fn (Credential r c)
cKeyHashObj ::
(IsConwayUniv fn, Typeable r, Crypto c) => Term fn (KeyHash r c) -> Term fn (Credential r c)
cKeyHashObj = con @"KeyHashObj"
cScriptHashObj ::
(IsConwayUniv fn, Typeable r, Crypto c) => Term fn (ScriptHash c) -> Term fn (Credential r c)
cScriptHashObj = con @"ScriptHashObj"
instance HasSimpleRep (ScriptHash c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (ScriptHash c)
instance (IsConwayUniv fn, HashAlgorithm a, Typeable b) => HasSpec fn (Hash a b) where
type TypeSpec fn (Hash a b) = ()
emptySpec = ()
combineSpec _ _ = TrueSpec
genFromTypeSpec _ = pureGen arbitrary
shrinkWithTypeSpec _ = shrink
conformsTo _ _ = True
toPreds _ _ = toPred True
instance HasSimpleRep (ConwayTxCert era)
instance (IsConwayUniv fn, Era era) => HasSpec fn (ConwayTxCert era)
instance HasSimpleRep (ConwayDelegCert c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (ConwayDelegCert c)
instance HasSimpleRep (PoolCert c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (PoolCert c)
instance HasSimpleRep (PoolParams c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (PoolParams c)
instance HasSimpleRep PoolMetadata
instance IsConwayUniv fn => HasSpec fn PoolMetadata
instance IsConwayUniv fn => HasSpec fn StakePoolRelay where
type TypeSpec fn StakePoolRelay = ()
emptySpec = ()
combineSpec _ _ = TrueSpec
genFromTypeSpec _ = pureGen arbitrary
shrinkWithTypeSpec _ = shrink
conformsTo _ _ = True
toPreds _ _ = toPred True
instance HasSimpleRep Port
instance IsConwayUniv fn => HasSpec fn Port
-- TODO: once you start adding interesting functions on these to the
-- function universe this instance has to become more complicated
instance IsConwayUniv fn => HasSpec fn UnitInterval where
type TypeSpec fn UnitInterval = ()
emptySpec = ()
combineSpec _ _ = TrueSpec
genFromTypeSpec _ = pureGen arbitrary
shrinkWithTypeSpec _ = shrink
conformsTo _ _ = True
toPreds _ _ = toPred True
-- TODO: once you start adding interesting functions on these to the
-- function universe this instance has to become more complicated
instance IsConwayUniv fn => HasSpec fn NonNegativeInterval where
type TypeSpec fn NonNegativeInterval = ()
emptySpec = ()
combineSpec _ _ = TrueSpec
genFromTypeSpec _ = pureGen arbitrary
shrinkWithTypeSpec _ = shrink
conformsTo _ _ = True
toPreds _ _ = toPred True
instance HasSimpleRep (ConwayGovCert c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (ConwayGovCert c)
instance HasSimpleRep (Anchor c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (Anchor c)
instance HasSimpleRep Url
instance IsConwayUniv fn => HasSpec fn Url
instance IsConwayUniv fn => HasSpec fn Text where
type TypeSpec fn Text = ()
emptySpec = ()
combineSpec _ _ = TrueSpec
genFromTypeSpec _ = pureGen arbitrary
shrinkWithTypeSpec _ = shrink
conformsTo _ _ = True
toPreds _ _ = toPred True
data StringSpec fn = StringSpec {strSpecLen :: Specification fn Int}
deriving instance IsConwayUniv fn => Show (StringSpec fn)
instance HasSpec fn Int => Semigroup (StringSpec fn) where
StringSpec len <> StringSpec len' = StringSpec (len <> len')
instance HasSpec fn Int => Monoid (StringSpec fn) where
mempty = StringSpec TrueSpec
instance IsConwayUniv fn => HasSpec fn ByteString where
type TypeSpec fn ByteString = StringSpec fn
emptySpec = mempty
combineSpec s s' = typeSpec $ s <> s'
genFromTypeSpec (StringSpec ls) = do
len <- genFromSpec ls
BS.pack <$> vectorOfT len (pureGen arbitrary)
shrinkWithTypeSpec _ = shrink
conformsTo bs (StringSpec ls) = BS.length bs `conformsToSpec` ls
toPreds str (StringSpec len) = satisfies (strLen_ str) len
instance IsConwayUniv fn => HasSpec fn ShortByteString where
type TypeSpec fn ShortByteString = StringSpec fn
emptySpec = mempty
combineSpec s s' = typeSpec $ s <> s'
genFromTypeSpec (StringSpec ls) = do
len <- genFromSpec ls
SBS.pack <$> vectorOfT len (pureGen arbitrary)
shrinkWithTypeSpec _ = shrink
conformsTo bs (StringSpec ls) = SBS.length bs `conformsToSpec` ls
toPreds str (StringSpec len) = satisfies (strLen_ str) len
instance StringLike ByteString where
lengthSpec = StringSpec
getLengthSpec (StringSpec len) = len
getLength = BS.length
instance StringLike ShortByteString where
lengthSpec = StringSpec
getLengthSpec (StringSpec len) = len
getLength = SBS.length
data StringFn (fn :: [Type] -> Type -> Type) as b where
LengthFn :: StringLike s => StringFn fn '[s] Int
deriving instance IsConwayUniv fn => Show (StringFn fn as b)
deriving instance IsConwayUniv fn => Eq (StringFn fn as b)
strLen_ ::
forall fn s.
(Member (StringFn fn) fn, StringLike s, HasSpec fn s) =>
Term fn s ->
Term fn Int
strLen_ = app (injectFn $ LengthFn @_ @fn)
instance FunctionLike (StringFn fn) where
sem LengthFn = getLength
instance IsConwayUniv fn => Functions (StringFn fn) fn where
propagateSpecFun _ _ TrueSpec = TrueSpec
propagateSpecFun _ _ (ErrorSpec err) = ErrorSpec err
propagateSpecFun fn ctx spec = case fn of
_
| SuspendedSpec {} <- spec
, ListCtx pre HOLE suf <- ctx ->
constrained $ \x' ->
let args =
appendList
(mapList (\(C.Value a) -> lit a) pre)
(x' :> mapList (\(C.Value a) -> lit a) suf)
in uncurryList (app @fn $ injectFn fn) args `satisfies` spec
LengthFn ->
-- No TypeAbstractions in ghc-8.10
case fn of
(_ :: StringFn fn '[s] Int)
| NilCtx HOLE <- ctx -> typeSpec $ lengthSpec @s spec
mapTypeSpec f@LengthFn ss =
-- No TypeAbstractions in ghc-8.10
case f of
(_ :: StringFn fn '[s] Int) -> getLengthSpec @s ss
class StringLike s where
lengthSpec :: IsConwayUniv fn => Specification fn Int -> TypeSpec fn s
getLengthSpec :: TypeSpec fn s -> Specification fn Int
getLength :: s -> Int
instance HasSimpleRep (Delegatee c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (Delegatee c)
instance HasSimpleRep (DRep c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (DRep c)
instance HasSimpleRep (Withdrawals c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (Withdrawals c)
instance HasSimpleRep (RewardAccount c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (RewardAccount c)
instance HasSimpleRep Network
instance IsConwayUniv fn => HasSpec fn Network
instance HasSimpleRep (MultiAsset c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (MultiAsset c) where
emptySpec = flip (MapSpec mempty [] mempty) NoFold $
constrained' $ \_ innerMap ->
forAll innerMap $ \kv' ->
lit 0 <=. snd_ kv'
instance HasSimpleRep AssetName where
type SimpleRep AssetName = ShortByteString
toSimpleRep (AssetName sbs) = sbs
fromSimpleRep sbs = AssetName sbs
instance IsConwayUniv fn => HasSpec fn AssetName
instance HasSimpleRep (PolicyID c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (PolicyID c)
instance HasSimpleRep (AuxiliaryDataHash c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (AuxiliaryDataHash c)
instance HasSimpleRep (VotingProcedures era)
instance (IsConwayUniv fn, Typeable era, Crypto (EraCrypto era)) => HasSpec fn (VotingProcedures era)
instance HasSimpleRep (VotingProcedure era)
instance (IsConwayUniv fn, Typeable era, Crypto (EraCrypto era)) => HasSpec fn (VotingProcedure era)
instance HasSimpleRep Vote
instance IsConwayUniv fn => HasSpec fn Vote
instance HasSimpleRep (GovActionId c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (GovActionId c)
instance HasSimpleRep GovActionIx
instance IsConwayUniv fn => HasSpec fn GovActionIx
instance HasSimpleRep ProtVer
instance IsConwayUniv fn => HasSpec fn ProtVer
-- We do this like this to get the right bounds for `VersionRep`
-- while ensuring that we don't have to add instances for e.g. `Num`
-- to version.
newtype VersionRep = VersionRep Word8
deriving (Show, Eq, Ord, Num, Random, Arbitrary) via Word8
instance BaseUniverse fn => HasSpec fn VersionRep where
type TypeSpec fn VersionRep = NumSpec VersionRep
emptySpec = emptyNumSpec
combineSpec = combineNumSpec
genFromTypeSpec = genFromNumSpec
shrinkWithTypeSpec = shrinkWithNumSpec
conformsTo = conformsToNumSpec
toPreds = toPredsNumSpec
instance Bounded VersionRep where
minBound = VersionRep $ getVersion minBound
maxBound = VersionRep $ getVersion maxBound
instance MaybeBounded VersionRep
instance HasSimpleRep Version where
type SimpleRep Version = VersionRep
fromSimpleRep (VersionRep rep) = case runFail $ mkVersion rep of
Left err ->
error $
unlines
[ "fromSimpleRep @Version:"
, show rep
, err
]
Right a -> a
toSimpleRep = VersionRep . getVersion
instance BaseUniverse fn => HasSpec fn Version
instance BaseUniverse fn => OrdLike fn Version
succV_ :: BaseUniverse fn => Term fn Version -> Term fn Version
succV_ = fromGeneric_ . (+ 1) . toGeneric_
instance HasSimpleRep (GovPurposeId p era)
instance (Typeable p, IsConwayUniv fn, Era era) => HasSpec fn (GovPurposeId p era)
instance HasSimpleRep (GovAction era)
instance
( IsConwayUniv fn
, EraPParams era
, HasSimpleRep (PParamsHKD StrictMaybe era)
, TypeSpec fn (SimpleRep (PParamsHKD StrictMaybe era)) ~ TypeSpec fn (PParamsHKD StrictMaybe era)
, HasSpec fn (SimpleRep (PParamsHKD StrictMaybe era))
) =>
HasSpec fn (GovAction era)
instance HasSimpleRep (Constitution era)
instance (IsConwayUniv fn, EraPParams era) => HasSpec fn (Constitution era)
instance HasSimpleRep (PParamsHKD StrictMaybe era) => HasSimpleRep (PParamsUpdate era) where
type SimpleRep (PParamsUpdate era) = SimpleRep (PParamsHKD StrictMaybe era)
toSimpleRep (PParamsUpdate hkd) = toSimpleRep hkd
fromSimpleRep = PParamsUpdate . fromSimpleRep
instance
( IsConwayUniv fn
, EraPParams era
, HasSimpleRep (PParamsHKD StrictMaybe era)
, TypeSpec fn (SimpleRep (PParamsHKD StrictMaybe era)) ~ TypeSpec fn (PParamsHKD StrictMaybe era)
, HasSpec fn (SimpleRep (PParamsHKD StrictMaybe era))
, Show (TypeSpec fn (PParamsHKD StrictMaybe era))
) =>
HasSpec fn (PParamsUpdate era)
instance HasSimpleRep EpochInterval
instance IsConwayUniv fn => OrdLike fn EpochInterval
instance IsConwayUniv fn => HasSpec fn EpochInterval
instance HasSimpleRep (ConwayPParams StrictMaybe c)
instance
( IsConwayUniv fn
, Typeable c
) =>
HasSpec fn (ConwayPParams StrictMaybe c)
instance HasSimpleRep (ConwayPParams Identity era)
instance (IsConwayUniv fn, Era era) => HasSpec fn (ConwayPParams Identity era)
instance HasSimpleRep (PParams (ConwayEra StandardCrypto))
instance IsConwayUniv fn => HasSpec fn (PParams (ConwayEra StandardCrypto))
instance HasSimpleRep ExUnits where
type SimpleRep ExUnits = SimpleRep (Natural, Natural)
fromSimpleRep = uncurry ExUnits . fromSimpleRep
toSimpleRep (ExUnits a b) = toSimpleRep (a, b)
instance IsConwayUniv fn => HasSpec fn ExUnits
instance HasSimpleRep OrdExUnits where
type SimpleRep OrdExUnits = SimpleRep ExUnits
fromSimpleRep = OrdExUnits . fromSimpleRep
toSimpleRep = toSimpleRep . unOrdExUnits
instance IsConwayUniv fn => HasSpec fn OrdExUnits
instance HasSimpleRep CoinPerByte where
-- TODO: consider `SimpleRep Coin` instead if this is annoying
type SimpleRep CoinPerByte = Coin
fromSimpleRep = CoinPerByte
toSimpleRep = unCoinPerByte
instance IsConwayUniv fn => HasSpec fn CoinPerByte
instance HasSimpleRep CostModels
instance IsConwayUniv fn => HasSpec fn CostModels where
emptySpec =
Cartesian
(constrained $ \m -> size_ (dom_ m) <=. 3)
(constrained $ \p -> size_ (dom_ (fst_ $ fromGeneric_ p)) <=. 3)
instance HasSimpleRep PoolVotingThresholds
instance IsConwayUniv fn => HasSpec fn PoolVotingThresholds
instance HasSimpleRep DRepVotingThresholds
instance IsConwayUniv fn => HasSpec fn DRepVotingThresholds
instance HasSimpleRep CostModelError
instance IsConwayUniv fn => HasSpec fn CostModelError
instance HasSimpleRep CostModelApplyError
instance IsConwayUniv fn => HasSpec fn CostModelApplyError
instance IsConwayUniv fn => HasSpec fn Char where
type TypeSpec fn Char = ()
emptySpec = ()
combineSpec _ _ = TrueSpec
genFromTypeSpec _ = pureGen arbitrary
shrinkWithTypeSpec _ = shrink
conformsTo _ _ = True
toPreds _ _ = toPred True
instance IsConwayUniv fn => HasSpec fn CostModel where
type TypeSpec fn CostModel = ()
emptySpec = ()
combineSpec _ _ = TrueSpec
genFromTypeSpec _ = pureGen arbitrary
shrinkWithTypeSpec _ = shrink
conformsTo _ _ = True
toPreds _ _ = toPred True
instance HasSimpleRep Language
instance IsConwayUniv fn => HasSpec fn Language
instance HasSimpleRep Prices
instance IsConwayUniv fn => HasSpec fn Prices
instance HasSimpleRep (NoUpdate a)
instance (IsConwayUniv fn, Typeable a) => HasSpec fn (NoUpdate a)
instance HasSimpleRep (THKD tag StrictMaybe a) where
type SimpleRep (THKD tag StrictMaybe a) = SimpleRep (StrictMaybe a)
fromSimpleRep = THKD . fromSimpleRep
toSimpleRep (THKD sm) = toSimpleRep sm
instance (IsConwayUniv fn, Typeable tag, HasSpec fn a) => HasSpec fn (THKD tag StrictMaybe a)
instance HasSimpleRep (THKD tag Identity a) where
type SimpleRep (THKD tag Identity a) = a
fromSimpleRep = THKD
toSimpleRep (THKD a) = a
instance (IsConwayUniv fn, Typeable tag, HasSpec fn a) => HasSpec fn (THKD tag Identity a)
instance HasSimpleRep GovActionPurpose
instance IsConwayUniv fn => HasSpec fn GovActionPurpose
instance HasSimpleRep (Voter c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (Voter c)
-- TODO: this might be a problem considering duplicates in the list! This
-- type might require having its own `HasSpec` at some point
instance Ord a => HasSimpleRep (SOS.OSet a) where
type SimpleRep (SOS.OSet a) = [a]
fromSimpleRep = SOS.fromStrictSeq . StrictSeq.fromList
toSimpleRep = toList . SOS.toStrictSeq
instance (IsConwayUniv fn, Ord a, HasSpec fn a) => HasSpec fn (SOS.OSet a)
instance Ord a => Forallable (SOS.OSet a) a
instance HasSimpleRep (ProposalProcedure era)
instance
( IsConwayUniv fn
, EraPParams era
, HasSimpleRep (PParamsHKD StrictMaybe era)
, TypeSpec fn (SimpleRep (PParamsHKD StrictMaybe era)) ~ TypeSpec fn (PParamsHKD StrictMaybe era)
, HasSpec fn (SimpleRep (PParamsHKD StrictMaybe era))
) =>
HasSpec fn (ProposalProcedure era)
pProcGovAction_ ::
IsConwayUniv fn =>
Term fn (ProposalProcedure (ConwayEra StandardCrypto)) ->
Term fn (GovAction (ConwayEra StandardCrypto))
pProcGovAction_ = sel @2
instance HasSimpleRep ValidityInterval
instance IsConwayUniv fn => HasSpec fn ValidityInterval
instance HasSimpleRep (DRepState c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (DRepState c)
instance HasSimpleRep (CommitteeAuthorization c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (CommitteeAuthorization c)
instance HasSimpleRep (CommitteeState era)
instance (IsConwayUniv fn, Era era) => HasSpec fn (CommitteeState era)
instance HasSimpleRep (VState era)
instance (IsConwayUniv fn, Era era) => HasSpec fn (VState era)
instance HasSimpleRep (PState era)
instance (IsConwayUniv fn, Era era) => HasSpec fn (PState era)
instance HasSimpleRep (DState era)
instance (IsConwayUniv fn, Era era) => HasSpec fn (DState era)
instance HasSimpleRep (FutureGenDeleg c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (FutureGenDeleg c)
instance HasSimpleRep (GenDelegPair c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (GenDelegPair c)
instance HasSimpleRep (GenDelegs c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (GenDelegs c)
instance HasSimpleRep (InstantaneousRewards c)
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (InstantaneousRewards c)
type UMapTypes c =
'[ Map (Credential 'Staking c) RDPair
, Map Ptr (Credential 'Staking c)
, Map (Credential 'Staking c) (KeyHash 'StakePool c)
, Map (Credential 'Staking c) (DRep c)
]
instance Crypto c => HasSimpleRep (UMap c) where
type SimpleRep (UMap c) = SOP '["UMap" ::: UMapTypes c]
toSimpleRep um = inject @"UMap" @'["UMap" ::: UMapTypes c] (rdPairMap um) (ptrMap um) (sPoolMap um) (dRepMap um)
fromSimpleRep rep = algebra @'["UMap" ::: UMapTypes c] rep unify
instance (IsConwayUniv fn, Crypto c) => HasSpec fn (UMap c)
instance HasSimpleRep RDPair where
type SimpleRep RDPair = SOP '["RDPair" ::: '[Coin, Coin]]
toSimpleRep (RDPair rew dep) = inject @"RDPair" @'["RDPair" ::: '[Coin, Coin]] (toSimpleRep rew) (toSimpleRep dep)
fromSimpleRep rep =
algebra @'["RDPair" ::: '[Coin, Coin]]
rep
(\rew dep -> RDPair (fromSimpleRep rew) (fromSimpleRep dep))
instance IsConwayUniv fn => HasSpec fn RDPair
instance HasSimpleRep (CertState era)
instance (IsConwayUniv fn, Era era) => HasSpec fn (CertState era)
instance HasSimpleRep (GovRelation StrictMaybe era)
instance (IsConwayUniv fn, Era era) => HasSpec fn (GovRelation StrictMaybe era)
instance HasSimpleRep (GovEnv (ConwayEra StandardCrypto))
instance IsConwayUniv fn => HasSpec fn (GovEnv (ConwayEra StandardCrypto))
instance HasSimpleRep (GovActionState (ConwayEra StandardCrypto))
instance IsConwayUniv fn => HasSpec fn (GovActionState (ConwayEra StandardCrypto))
gasId_ ::
IsConwayUniv fn =>
Term fn (GovActionState (ConwayEra StandardCrypto)) ->
Term fn (GovActionId StandardCrypto)